Developer Documentation

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
148 QString Core::getCurrentViewMode() {
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()) {
177 coreWidget_->moveToolBoxToTop(_name);
178 }
179 }
180
181 //-----------------------------------------------------------------------------
182
183 void Core::moveToolBoxToBottom(QString _name) {
184
185 if(OpenFlipper::Options::gui()) {
186 coreWidget_->moveToolBoxToBottom(_name);
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") {
223 coreWidget_->setToolBoxOrientationOnTheRight(false);
224 } else if(_side.toLower() == "right") {
225 coreWidget_->setToolBoxOrientationOnTheRight(true);
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
314 void Core::blockSceneGraphUpdates() {
315 OpenFlipper::Options::blockSceneGraphUpdates();
316 }
317
319 void Core::unblockSceneGraphUpdates() {
320 OpenFlipper::Options::unblockSceneGraphUpdates();
321 }
322
323 void Core::setView(QString view) {
324 coreWidget_->slotSetView(view);
325 }
326
327 void Core::setViewAndWindowGeometry(QString view) {
328 coreWidget_->slotSetViewAndWindowGeometry(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
Core::addViewModeToolbars
void addViewModeToolbars(QString _modeName, QString _toolbarList)
Scripting function to set toolbars in a view mode.
Definition: scripting.cc:200
CoreWidget::moveToolBoxToBottom
void moveToolBoxToBottom(QString _name)
Move a specific toolbox widget to the bottom of the side area.
Definition: viewMode.cc:457
Core::slotScriptInfo
void slotScriptInfo(QString _pluginName, QString _functionName)
Core scripting engine.
Definition: scripting.cc:70
CoreWidget::slotAddViewModeToolboxes
void slotAddViewModeToolboxes(QString _mode, QStringList _usedWidgets)
Add or change Toolboxes for a ViewMode (non-userdefined viewMode)
Definition: viewMode.cc:99
Core::createWidget
void createWidget(QString _objectName, QString _uiFilename, bool _show=true)
Create an script object from a ui file.
Definition: scripting.cc:94
Core::plugins
const std::vector< PluginInfo > plugins() const
List of all loaded plugins_.
Definition: Core.hh:1208
Core::setToolBoxSide
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
PluginFunctions::getObjectId
int getObjectId(const QString &_name)
Definition: PluginFunctions.cc:332
LOGERR
Definition: LoggingInterface.hh:107
CoreWidget::expandToolBoxWidget
void expandToolBoxWidget(QWidget *widget, bool expand)
Definition: picking.cc:487
Core::viewModes_
QVector< ViewMode * > viewModes_
List of available draw modes.
Definition: Core.hh:1562
Core::unblockSceneGraphUpdates
void unblockSceneGraphUpdates()
Unblock the scenegraph updates.
Definition: scripting.cc:319
CoreWidget::slotAddViewModeContextMenus
void slotAddViewModeContextMenus(QString _mode, QStringList _usedToolbars)
Add or change Toolbars for a ViewMode (non-userdefined viewMode)
Definition: viewMode.cc:200
SideArea::setElementActive
void setElementActive(QString _name, bool _active)
set the active state of given element
Definition: SideArea.cc:218
Core::setToolBoxActive
void setToolBoxActive(QString _toolBoxName, bool _active)
Scripting function to activate or deactivate a toolbox.
Definition: scripting.cc:306
Core::moveToolBoxToBottom
void moveToolBoxToBottom(QString _name)
Move selected toolbox to bottom of side area.
Definition: scripting.cc:183
Core::getObjectId
int getObjectId(QString _filename)
Get object id from filename.
Definition: scripting.cc:133
CoreWidget::toolBox_
SideArea * toolBox_
Toolbox.
Definition: CoreWidget.hh:726
Core::setView
void setView(QString view)
Called when a plugin requests an update in the viewer.
Definition: scripting.cc:323
Core::slotGetAllAvailableFunctions
void slotGetAllAvailableFunctions(QStringList &_functions)
Core scripting engine.
Definition: scripting.cc:86
Core::scriptLog
void scriptLog(QString _message)
Logging signal for ScriptEngine.
CoreWidget::slotSetViewModeIcon
void slotSetViewModeIcon(QString _mode, QString _iconName)
Sets the Icon for a given View Mode (non-userdefined viewMode)
Definition: viewMode.cc:247
Core::scriptingFunctions_
QStringList scriptingFunctions_
List of all registered scripting functions.
Definition: Core.hh:1308
Core
Definition: Core.hh:139
Core::addToolbox
void addToolbox(QString _name, QWidget *_widget)
Add a Toolbox from a plugin or from scripting.
Definition: scripting.cc:256
Core::scriptEngine_
QScriptEngine scriptEngine_
Core scripting engine.
Definition: Core.hh:1302
printToFileFunction
QScriptValue printToFileFunction(QScriptContext *context, QScriptEngine *engine)
Special print function for sending output to a file.
Definition: scripting.cc:357
Core::addViewModeContextMenus
void addViewModeContextMenus(QString _modeName, QString _contextMenuList)
Scripting function to set context menus in a view mode.
Definition: scripting.cc:208
Core::setViewMode
void setViewMode(QString _viewMode)
Set the active ViewMode.
Definition: scripting.cc:140
Core::addViewModeToolboxes
void addViewModeToolboxes(QString _modeName, QString _toolboxList)
Scripting function to set toolboxes in a view mode.
Definition: scripting.cc:192
Core::executeScript
void executeScript(QString _script)
Core scripting engine.
Core::plugins_
std::vector< PluginInfo > plugins_
List of all loaded plugins_.
Definition: Core.hh:1208
Core::setViewModeIcon
void setViewModeIcon(QString _mode, QString _iconName)
Set the icon of a viewMode.
Definition: scripting.cc:154
Core::log
void log(Logtype _type, QString _message)
Logg with OUT,WARN or ERR as type.
CoreWidget::setViewMode
void setViewMode(QString _mode, bool _expandAll=false)
Set the view Mode to the given Mode.
Definition: viewMode.cc:316
CoreWidget::setToolBoxOrientationOnTheRight
void setToolBoxOrientationOnTheRight(bool _toolBoxRight)
Set orientation of tool box (either on the right or the left side of the screen)
Definition: CoreWidget.cc:853
Core::moveToolBoxToTop
void moveToolBoxToTop(QString _name)
Move selected toolbox to top of side area.
Definition: scripting.cc:174
Core::getToolbox
QWidget * getToolbox(QString _pluginName, QString _toolboxName)
Definition: scripting.cc:233
Core::blockSceneGraphUpdates
void blockSceneGraphUpdates()
Block the scenegraph updates.
Definition: scripting.cc:314
CoreWidget::moveToolBoxToTop
void moveToolBoxToTop(QString _name)
Move a specific toolbox widget to the top of the side area.
Definition: viewMode.cc:452
Core::executeFileScript
void executeFileScript(QString _filename)
Core scripting engine.
CoreWidget::slotSetViewAndWindowGeometry
void slotSetViewAndWindowGeometry(QString view)
Set the supplied serialized view.
Definition: viewerControl.cc:949
Core::slotExecuteScript
void slotExecuteScript(QString _script)
Definition: scripting.cc:74
Core::coreWidget_
CoreWidget * coreWidget_
The main applications widget ( only created in gui mode )
Definition: Core.hh:1553
Core::activateToolbox
void activateToolbox(QString _pluginName, QString _toolboxName, bool activate)
Gets called by examiner widget when mouse is moved in picking mode.
Definition: scripting.cc:251
Core::slotScriptError
void slotScriptError(const QScriptValue &error)
Core scripting engine.
Definition: scripting.cc:335
Core.hh
helpFunction
QScriptValue helpFunction(QScriptContext *context, QScriptEngine *engine)
Function to print help about scripting functions.
Definition: scripting.cc:383
CoreWidget::slotSetView
void slotSetView(QString view)
Set the supplied serialized view.
Definition: viewerControl.cc:945
Core::setViewAndWindowGeometry
void setViewAndWindowGeometry(QString view)
Called when a plugin requests an update in the viewer.
Definition: scripting.cc:327
myPrintFunction
QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
Special print function for core logger.
Definition: scripting.cc:339
Core::addViewModeIcon
void addViewModeIcon(QString _modeName, QString _iconName)
Scripting function to set an icon for a view mode.
Definition: scripting.cc:214
Core::scriptInfo
void scriptInfo(QString _pluginName, QString _functionName)
Core scripting engine.
Core::slotGetScriptingEngine
void slotGetScriptingEngine(QScriptEngine *&_engine)
Core scripting engine.
Definition: scripting.cc:82
Core::getCurrentViewMode
QString getCurrentViewMode()
Get current view mode.
Definition: scripting.cc:148
CoreWidget::slotAddViewModeToolbars
void slotAddViewModeToolbars(QString _mode, QStringList _usedToolbars)
Add or change Toolbars for a ViewMode (non-userdefined viewMode)
Definition: viewMode.cc:146
Core::slotExecuteFileScript
void slotExecuteFileScript(QString _filename)
Definition: scripting.cc:78
Core::scriptLogFunction
void scriptLogFunction(QString _output)
stream for logging to file
Definition: scripting.cc:90