Developer Documentation
scripting.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS Core - IMPLEMENTATION
55 //
56 //=============================================================================
57 
58 
59 //== INCLUDES =================================================================
60 
61 // -------------------- mview
62 #include "Core.hh"
63 #include <QUiLoader>
64 
65 
66 //== IMPLEMENTATION ==========================================================
67 
68 
69 
70 void Core::slotScriptInfo( QString _pluginName , QString _functionName ) {
71  emit scriptInfo( _pluginName , _functionName );
72 }
73 
74 void Core::slotExecuteScript( QString _script ) {
75  emit executeScript( _script );
76 }
77 
78 void Core::slotExecuteFileScript( QString _filename ) {
79  emit executeFileScript( _filename );
80 }
81 
82 void Core::slotGetScriptingEngine( QScriptEngine*& _engine ) {
83  _engine = &scriptEngine_;
84 }
85 
86 void Core::slotGetAllAvailableFunctions( QStringList& _functions ) {
87  _functions = scriptingFunctions_;
88 }
89 
90 void Core::scriptLogFunction( QString _output) {
91  emit scriptLog(_output);
92 }
93 
94 void Core::createWidget(QString _objectName, QString _uiFilename, bool _show) {
95  if ( OpenFlipper::Options::gui()) {
96  QUiLoader loader;
97 
98  QFile uiFile(_uiFilename);
99 
100  if ( !uiFile.exists() ) {
101  emit log(LOGERR,tr("File does not exist : ") + _uiFilename );
102  return;
103  }
104 
105  uiFile.open(QIODevice::ReadOnly);
106  QWidget *ui = loader.load(&uiFile, coreWidget_);
107  uiFile.close();
108 
109  if ( ui == 0 ) {
110  emit log(LOGERR,tr("Unable to create QWidget from ui file for ") + _objectName );
111  return;
112  }
113 
114  QScriptValue scriptUi = scriptEngine_.newQObject(ui, QScriptEngine::ScriptOwnership);
115 
116  if ( !scriptUi.isValid() ) {
117  emit log(LOGERR,tr("Unable to generate script interface for ") + _objectName );
118  return;
119  }
120 
121  scriptEngine_.globalObject().setProperty(_objectName, scriptUi);
122 
123 
124  if(_show) ui->show();
125  } else {
126  emit log(LOGERR,tr("Error! Script tried to create Widget in ui less batc mode! Creation Aborted!"));
127  }
128 
129 }
130 
131 //-----------------------------------------------------------------------------
132 
133 int Core::getObjectId( const QString _name ) {
134 
135  return PluginFunctions::getObjectId(_name);
136 }
137 
138 //-----------------------------------------------------------------------------
139 
140 void Core::setViewMode(QString _viewMode){
141 
142  if ( OpenFlipper::Options::gui() )
143  coreWidget_->setViewMode( _viewMode );
144 }
145 
146 //-----------------------------------------------------------------------------
147 
149  return OpenFlipper::Options::currentViewMode();
150 }
151 
152 //-----------------------------------------------------------------------------
153 
154 void Core::setViewModeIcon(QString _mode, QString _iconName){
155 
156  if ( OpenFlipper::Options::gui() ){
157 
158  QFile file(_iconName);
159  QFileInfo fileInfo(file);
160 
161  if ( ! file.exists() ){
162  emit log(LOGERR, tr("Icon not found (%1)").arg(_iconName) );
163  return;
164  }
165 
166  file.copy(OpenFlipper::Options::configDirStr() + QDir::separator() + "Icons" + QDir::separator() + "viewMode_" + fileInfo.fileName() );
167 
168  coreWidget_->slotSetViewModeIcon( _mode, "viewMode_" + fileInfo.fileName() );
169  }
170 }
171 
172 //-----------------------------------------------------------------------------
173 
174 void Core::moveToolBoxToTop(QString _name) {
175 
176  if(OpenFlipper::Options::gui()) {
178  }
179 }
180 
181 //-----------------------------------------------------------------------------
182 
183 void Core::moveToolBoxToBottom(QString _name) {
184 
185  if(OpenFlipper::Options::gui()) {
187  }
188 }
189 
190 //-----------------------------------------------------------------------------
191 
192 void Core::addViewModeToolboxes(QString _modeName, QString _toolboxList) {
193 
194  QStringList list = _toolboxList.split(";");
195  coreWidget_->slotAddViewModeToolboxes(_modeName,list);
196 }
197 
198 //-----------------------------------------------------------------------------
199 
200 void Core::addViewModeToolbars(QString _modeName, QString _toolbarList) {
201 
202  QStringList list = _toolbarList.split(";");
203  coreWidget_->slotAddViewModeToolbars(_modeName,list);
204 }
205 
206 //-----------------------------------------------------------------------------
207 
208 void Core::addViewModeContextMenus(QString _modeName, QString _contextMenuList) {
209 
210  QStringList list = _contextMenuList.split(";");
211  coreWidget_->slotAddViewModeContextMenus(_modeName,list);
212 }
213 
214 void Core::addViewModeIcon(QString _modeName, QString _iconName) {
215  coreWidget_->slotSetViewModeIcon(_modeName,true,_iconName);
216 }
217 
218 //-----------------------------------------------------------------------------
219 
220 void Core::setToolBoxSide(QString _side) {
221 
222  if(_side.toLower() == "left") {
224  } else if(_side.toLower() == "right") {
226  } else {
227  emit log(LOGERR, QString("Could not display toolboxes on side '%1'. Use either 'left' or 'right' as string!").arg(_side));
228  }
229 }
230 
231 //-----------------------------------------------------------------------------
232 
233 QWidget *Core::getToolbox(QString _pluginName, QString _toolboxName) {
234  std::vector<PluginInfo>::const_iterator pluginIt = plugins_.end();
235  for (std::vector<PluginInfo>::const_iterator it = plugins_.begin(), it_end = plugins_.end(); it != it_end; ++it) {
236  if (it->name == _pluginName) {
237  pluginIt = it;
238  }
239  }
240  if (pluginIt == plugins_.end()) return 0;
241 
242  for (std::vector<std::pair<QString , QWidget*> >::const_iterator it = pluginIt->toolboxWidgets.begin(), it_end = pluginIt->toolboxWidgets.end();
243  it != it_end; ++it) {
244  if (it->first == _toolboxName)
245  return it->second;
246  }
247 
248  return 0;
249 }
250 
251 void Core::activateToolbox(QString _pluginName, QString _toolboxName, bool activate) {
252  QWidget *toolbox = Core::getToolbox(_pluginName, _toolboxName);
253  coreWidget_->expandToolBoxWidget(toolbox, activate);
254 }
255 
256 void Core::addToolbox(QString _name ,QWidget* _widget) {
257  addToolbox(_name, _widget, 0, 0);
258 }
259 
260 void Core::addToolbox(QString _name ,QWidget* _widget, QIcon* _icon) {
261  addToolbox(_name, _widget, _icon, 0);
262 }
263 
264 void Core::addToolbox(QString _name ,QWidget* _widget, QIcon* _icon,
265  QWidget *_headerAreaWidget) {
266  int id = -1;
267 
268  // Find the plugin which added this Toolbox
269  for ( uint i = 0 ; i < plugins_.size(); ++i ) {
270  if ( plugins_[i].plugin == sender() ) {
271  id = i;
272  break;
273  }
274  }
275 
276  // Find the scripting plugin because we assign this toolBox to it as we did not find the original sender
277  if ( id == -1 ) {
278  for ( uint i = 0 ; i < plugins_.size(); ++i ) {
279  if ( plugins_[i].name == "Scripting" ) {
280  id = i;
281  break;
282  }
283  }
284 
285 
286  if ( id == -1 ) {
287  std::cerr << "Unknown sender plugin when adding Toolbox!" << std::endl;
288  return;
289  }
290  }
291 
292  spinBoxEventFilter_.hookUpToWidgetTree(_widget);
293  plugins_[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
294  plugins_[id].toolboxIcons.push_back( _icon );
295  plugins_[id].headerAreaWidgets.push_back( std::pair< QString,QWidget* >( _name , _headerAreaWidget) );
296 
297  // add widget name to viewMode 'all'
298  if ( !viewModes_[0]->visibleToolboxes.contains(_name) ){
299  viewModes_[0]->visibleToolboxes << _name;
300  viewModes_[0]->visibleToolboxes.sort();
301  }
302 
303  setViewMode( OpenFlipper::Options::currentViewMode() );
304 }
305 
306 void Core::setToolBoxActive(QString _toolBoxName, bool _active)
307 {
308  if ( OpenFlipper::Options::gui() ){
309  coreWidget_->toolBox_->setElementActive(_toolBoxName,_active);
310  }
311 }
312 
315  OpenFlipper::Options::blockSceneGraphUpdates();
316 }
317 
320  OpenFlipper::Options::unblockSceneGraphUpdates();
321 }
322 
323 void Core::setView(QString view) {
324  coreWidget_->slotSetView(view);
325 }
326 
327 void Core::setViewAndWindowGeometry(QString view) {
329 }
330 
331 //=============================================================================
332 //== Script Special Functions =================================================
333 //=============================================================================
334 
335 void Core::slotScriptError(const QScriptValue &error) {
336  emit log(LOGERR, tr("Script error: ") + error.toString());
337 }
338 
339 QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
340 {
341  QString result;
342  for (int i = 0; i < context->argumentCount(); ++i) {
343  if (i > 0)
344  result.append(" ");
345  result.append(context->argument(i).toString());
346  }
347 
348  // Get the textedit for Output ( Set in Core.cc )
349  QScriptValue calleeData = context->callee().property("textedit");
350  Core *widget = qobject_cast<Core*>(calleeData.toQObject());
351 
352  widget->scriptLogFunction(result);
353 
354  return engine->undefinedValue();
355 }
356 
357 QScriptValue printToFileFunction(QScriptContext *context, QScriptEngine *engine)
358 {
359  if ( context->argumentCount() < 2 ) {
360  context->throwError( QScriptContext::SyntaxError, "Error! printToFileFunction needs at least two arguments, filename and what should be printed" );
361  return engine->undefinedValue();
362  }
363 
364  QString result;
365  for (int i = 1; i < context->argumentCount(); ++i) {
366  if (i > 1)
367  result.append(" ");
368  result.append(context->argument(i).toString());
369  }
370 
371  QFile file(context->argument(0).toString());
372 
373  file.open(QIODevice::Append);
374  QTextStream stream(&file);
375 
376  stream << result << "\n";
377 
378  file.close();
379 
380  return engine->undefinedValue();
381 }
382 
383 QScriptValue helpFunction(QScriptContext *context, QScriptEngine *engine)
384 {
385  if ( context->argumentCount() != 1 ) {
386  context->throwError( QScriptContext::SyntaxError, "Error! helpFunction needs one argument" );
387  return engine->undefinedValue();
388  }
389 
390  QString helpString = context->argument(0).toString();
391 
392  // Get the corewidget poiter ( Set in Core.cc )
393  QScriptValue calleeData = context->callee().property("core");
394  Core *core = qobject_cast<Core*>(calleeData.toQObject());
395 
396  const std::vector<PluginInfo> plugins = core->plugins();
397 
398  for (unsigned int i=0; i < plugins.size(); i++) {
399  if (plugins[i].rpcName == helpString) {
400  core->scriptLogFunction( "=======================================================\n" );
401  core->scriptLogFunction( "Found Plugin \"" + plugins[i].name + "\" \n" );
402  core->scriptLogFunction( "Description: " + plugins[i].description + " \n");
403  core->scriptLogFunction( "=======================================================\n" );
404  core->scriptLogFunction( "Scripting functions: \n");
405 
406  for ( int j = 0 ; j < plugins[i].rpcFunctions.size() ; ++j ) {
407  core->scriptLogFunction( plugins[i].rpcFunctions[j]+"\n");
408  }
409 
410  core->scriptLogFunction( "\n\n");
411  }
412  }
413 
414 
415 
416  return engine->undefinedValue();
417 }
418 
QScriptValue printToFileFunction(QScriptContext *context, QScriptEngine *engine)
Special print function for sending output to a file.
Definition: scripting.cc:357
QStringList scriptingFunctions_
List of all registered scripting functions.
Definition: Core.hh:1308
QScriptValue helpFunction(QScriptContext *context, QScriptEngine *engine)
Function to print help about scripting functions.
Definition: scripting.cc:383
void createWidget(QString _objectName, QString _uiFilename, bool _show=true)
Create an script object from a ui file.
Definition: scripting.cc:94
void setToolBoxSide(QString _side)
Scripting function to set the side of the main window on which the toolbox should be displayed...
Definition: scripting.cc:220
void setToolBoxOrientationOnTheRight(bool _toolBoxRight)
Set orientation of tool box (either on the right or the left side of the screen)
Definition: CoreWidget.cc:853
std::vector< PluginInfo > plugins_
List of all loaded plugins_.
Definition: Core.hh:1208
void blockSceneGraphUpdates()
Block the scenegraph updates.
Definition: scripting.cc:314
void addViewModeContextMenus(QString _modeName, QString _contextMenuList)
Scripting function to set context menus in a view mode.
Definition: scripting.cc:208
CoreWidget * coreWidget_
The main applications widget ( only created in gui mode )
Definition: Core.hh:1553
void setView(QString view)
Called when a plugin requests an update in the viewer.
Definition: scripting.cc:323
void moveToolBoxToBottom(QString _name)
Move a specific toolbox widget to the bottom of the side area.
Definition: viewMode.cc:463
void slotScriptInfo(QString _pluginName, QString _functionName)
Core scripting engine.
Definition: scripting.cc:70
QWidget * getToolbox(QString _pluginName, QString _toolboxName)
Definition: scripting.cc:233
QString getCurrentViewMode()
Get current view mode.
Definition: scripting.cc:148
void slotExecuteScript(QString _script)
Definition: scripting.cc:74
void unblockSceneGraphUpdates()
Unblock the scenegraph updates.
Definition: scripting.cc:319
void expandToolBoxWidget(QWidget *widget, bool expand)
Definition: picking.cc:487
int getObjectId(QString _filename)
Get object id from filename.
Definition: scripting.cc:133
void addViewModeIcon(QString _modeName, QString _iconName)
Scripting function to set an icon for a view mode.
Definition: scripting.cc:214
SideArea * toolBox_
Toolbox.
Definition: CoreWidget.hh:726
const std::vector< PluginInfo > plugins() const
List of all loaded plugins_.
Definition: Core.hh:1208
void setViewMode(QString _mode, bool _expandAll=false)
Set the view Mode to the given Mode.
Definition: viewMode.cc:322
void slotAddViewModeContextMenus(QString _mode, QStringList _usedToolbars)
Add or change Toolbars for a ViewMode (non-userdefined viewMode)
Definition: viewMode.cc:202
int getObjectId(const QString &_name)
void slotGetScriptingEngine(QScriptEngine *&_engine)
Core scripting engine.
Definition: scripting.cc:82
void setViewModeIcon(QString _mode, QString _iconName)
Set the icon of a viewMode.
Definition: scripting.cc:154
void addToolbox(QString _name, QWidget *_widget)
Add a Toolbox from a plugin or from scripting.
Definition: scripting.cc:256
QVector< ViewMode * > viewModes_
List of available draw modes.
Definition: Core.hh:1562
void addViewModeToolbars(QString _modeName, QString _toolbarList)
Scripting function to set toolbars in a view mode.
Definition: scripting.cc:200
void slotSetViewAndWindowGeometry(QString view)
Set the supplied serialized view.
Definition: Core.hh:139
void setViewAndWindowGeometry(QString view)
Called when a plugin requests an update in the viewer.
Definition: scripting.cc:327
void setToolBoxActive(QString _toolBoxName, bool _active)
Scripting function to activate or deactivate a toolbox.
Definition: scripting.cc:306
void activateToolbox(QString _pluginName, QString _toolboxName, bool activate)
Gets called by examiner widget when mouse is moved in picking mode.
Definition: scripting.cc:251
void slotSetView(QString view)
Set the supplied serialized view.
void slotAddViewModeToolboxes(QString _mode, QStringList _usedWidgets)
Add or change Toolboxes for a ViewMode (non-userdefined viewMode)
Definition: viewMode.cc:99
void executeFileScript(QString _filename)
Core scripting engine.
QScriptEngine scriptEngine_
Core scripting engine.
Definition: Core.hh:1302
void slotScriptError(const QScriptValue &error)
Core scripting engine.
Definition: scripting.cc:335
void executeScript(QString _script)
Core scripting engine.
void slotGetAllAvailableFunctions(QStringList &_functions)
Core scripting engine.
Definition: scripting.cc:86
void log(Logtype _type, QString _message)
Logg with OUT,WARN or ERR as type.
void moveToolBoxToTop(QString _name)
Move selected toolbox to top of side area.
Definition: scripting.cc:174
void addViewModeToolboxes(QString _modeName, QString _toolboxList)
Scripting function to set toolboxes in a view mode.
Definition: scripting.cc:192
void moveToolBoxToBottom(QString _name)
Move selected toolbox to bottom of side area.
Definition: scripting.cc:183
void slotSetViewModeIcon(QString _mode, QString _iconName)
Sets the Icon for a given View Mode (non-userdefined viewMode)
Definition: viewMode.cc:249
void slotAddViewModeToolbars(QString _mode, QStringList _usedToolbars)
Add or change Toolbars for a ViewMode (non-userdefined viewMode)
Definition: viewMode.cc:146
void scriptLogFunction(QString _output)
stream for logging to file
Definition: scripting.cc:90
void slotExecuteFileScript(QString _filename)
Definition: scripting.cc:78
void scriptInfo(QString _pluginName, QString _functionName)
Core scripting engine.
void moveToolBoxToTop(QString _name)
Move a specific toolbox widget to the top of the side area.
Definition: viewMode.cc:458
void setViewMode(QString _viewMode)
Set the active ViewMode.
Definition: scripting.cc:140
void scriptLog(QString _message)
Logging signal for ScriptEngine.
QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
Special print function for core logger.
Definition: scripting.cc:339
void setElementActive(QString _name, bool _active)
set the active state of given element
Definition: SideArea.cc:218