scripting.cc 14.5 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
34

35
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55



//=============================================================================
//
//  CLASS Core - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================

// -------------------- mview
#include "Core.hh"
56
#include <QUiLoader>
Jan Möbius's avatar
 
Jan Möbius committed
57

58
59

//== IMPLEMENTATION ==========================================================
Jan Möbius's avatar
 
Jan Möbius committed
60
61
62
63
64
65
66
67
68
69
70



void Core::slotScriptInfo( QString _pluginName , QString _functionName  ) {
  emit scriptInfo( _pluginName , _functionName );
}

void Core::slotExecuteScript( QString _script ) {
  emit executeScript( _script );
}

Jan Möbius's avatar
Jan Möbius committed
71
72
73
74
void Core::slotExecuteFileScript( QString _filename ) {
  emit executeFileScript( _filename );
}

Jan Möbius's avatar
 
Jan Möbius committed
75
void Core::slotGetScriptingEngine( QScriptEngine*& _engine  ) {
76
  _engine = &scriptEngine_;
Jan Möbius's avatar
 
Jan Möbius committed
77
78
79
80
81
82
83
}

void Core::slotGetAllAvailableFunctions( QStringList& _functions  ) {
  _functions = scriptingFunctions_;
}

void Core::scriptLogFunction( QString _output) {
84
85
86
87
   emit scriptLog(_output);
}

void Core::createWidget(QString _objectName, QString _uiFilename) {
88
89
  if ( OpenFlipper::Options::gui()) {
    QUiLoader loader;
90

91
    QFile uiFile(_uiFilename);
92

93
94
95
96
    if ( !uiFile.exists() ) {
      emit log(LOGERR,tr("File does not exist : ") + _uiFilename );
      return;
    }
97

98
    uiFile.open(QIODevice::ReadOnly);
99
    QWidget *ui = loader.load(&uiFile, coreWidget_);
100
    uiFile.close();
101

102
103
104
105
    if ( ui == 0 ) {
      emit log(LOGERR,tr("Unable to create QWidget from ui file for ") + _objectName );
      return;
    }
106

107
    QScriptValue scriptUi = scriptEngine_.newQObject(ui, QScriptEngine::ScriptOwnership);
108

109
110
111
112
    if ( !scriptUi.isValid() ) {
      emit log(LOGERR,tr("Unable to generate script interface for ") + _objectName );
      return;
    }
113

114
    scriptEngine_.globalObject().setProperty(_objectName, scriptUi);
115
116


117
118
119
120
    ui->show();
  } else {
    emit log(LOGERR,tr("Error! Script tried to create Widget in ui less batc mode! Creation Aborted!"));
  }
121

Jan Möbius's avatar
 
Jan Möbius committed
122
123
}

124
125
//-----------------------------------------------------------------------------

126
127
128
129
130
131
132
int Core::getObjectId( const QString _name ) {

	return PluginFunctions::getObjectId(_name);
}

//-----------------------------------------------------------------------------

133
134
135
136
137
138
139
140
void Core::setViewMode(QString _viewMode){

  if ( OpenFlipper::Options::gui() )
    coreWidget_->setViewMode( _viewMode );
}

//-----------------------------------------------------------------------------

141
142
143
144
145
146
QString Core::getCurrentViewMode() {
  return OpenFlipper::Options::currentViewMode();
}

//-----------------------------------------------------------------------------

Dirk Wilden's avatar
Dirk Wilden committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
void Core::setViewModeIcon(QString _mode, QString _iconName){

  if ( OpenFlipper::Options::gui() ){

    QFile file(_iconName);
    QFileInfo fileInfo(file);
  
    if ( ! file.exists() ){
      emit log(LOGERR, tr("Icon not found (%1)").arg(_iconName) );
      return;
    }

    file.copy(OpenFlipper::Options::configDirStr() + QDir::separator() + "Icons" + QDir::separator() + "viewMode_" + fileInfo.fileName() );

    coreWidget_->slotSetViewModeIcon( _mode, "viewMode_" + fileInfo.fileName() );
  }
}

//-----------------------------------------------------------------------------

167
void Core::moveToolBoxToTop(QString _name) {
168
169
    
  if(OpenFlipper::Options::gui()) {
170
    coreWidget_->moveToolBoxToTop(_name);
171
172
173
174
175
  }
}

//-----------------------------------------------------------------------------

176
void Core::moveToolBoxToBottom(QString _name) {
177
178
    
  if(OpenFlipper::Options::gui()) {
179
    coreWidget_->moveToolBoxToBottom(_name);
180
181
182
183
184
  }
}

//-----------------------------------------------------------------------------

185
void Core::addViewModeToolboxes(QString _modeName, QString _toolboxList) {
186
187

  QStringList list = _toolboxList.split(";");
Jan Möbius's avatar
Jan Möbius committed
188
  coreWidget_->slotAddViewModeToolboxes(_modeName,list);
189
190
191
192
}

//-----------------------------------------------------------------------------

193
194
195
196
197
198
199
200
void Core::addViewModeToolbars(QString _modeName, QString _toolbarList) {
  
  QStringList list = _toolbarList.split(";");
  coreWidget_->slotAddViewModeToolbars(_modeName,list);
}

//-----------------------------------------------------------------------------

Jan Möbius's avatar
Marlin:    
Jan Möbius committed
201
202
203
204
205
206
void Core::addViewModeContextMenus(QString _modeName, QString _contextMenuList) {
  
  QStringList list = _contextMenuList.split(";");
  coreWidget_->slotAddViewModeContextMenus(_modeName,list);
}

207
208
209
210
void Core::addViewModeIcon(QString _modeName, QString _iconName) {
  coreWidget_->slotSetViewModeIcon(_modeName,true,_iconName);
}

Jan Möbius's avatar
Marlin:    
Jan Möbius committed
211
212
//-----------------------------------------------------------------------------

213
214
215
216
217
218
219
220
221
222
223
224
225
void Core::setToolBoxSide(QString _side) {
  
    if(_side.toLower() == "left") {
        coreWidget_->setToolBoxOrientationOnTheRight(false);
    } else if(_side.toLower() == "right") {
        coreWidget_->setToolBoxOrientationOnTheRight(true);
    } else {
        emit log(LOGERR, QString("Could not display toolboxes on side '%1'. Use either 'left' or 'right' as string!").arg(_side));
    }
}

//-----------------------------------------------------------------------------

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
QWidget *Core::getToolbox(QString _pluginName, QString _toolboxName) {
    std::vector<PluginInfo>::const_iterator pluginIt = plugins_.end();
    for (std::vector<PluginInfo>::const_iterator it = plugins_.begin(), it_end = plugins_.end(); it != it_end; ++it) {
        if (it->name == _pluginName) {
            pluginIt = it;
        }
    }
    if (pluginIt == plugins_.end()) return 0;

    for (std::vector<std::pair<QString , QWidget*> >::const_iterator it = pluginIt->toolboxWidgets.begin(), it_end = pluginIt->toolboxWidgets.end();
            it != it_end; ++it) {
        if (it->first == _toolboxName)
            return it->second;
    }

    return 0;
}

Jan Möbius's avatar
Jan Möbius committed
244
245
246
247
248
void Core::activateToolbox(QString _pluginName, QString _toolboxName, bool activate) {
    QWidget *toolbox = Core::getToolbox(_pluginName, _toolboxName);
    coreWidget_->expandToolBoxWidget(toolbox, activate);
}

249
250
251
252
void Core::addToolbox(QString _name ,QWidget* _widget) {
  int id = -1;

  // Find the plugin which added this Toolbox
Jan Möbius's avatar
Jan Möbius committed
253
254
  for ( uint i = 0 ; i < plugins_.size(); ++i ) {
    if ( plugins_[i].plugin == sender() ) {
255
256
257
258
259
260
261
      id = i;
      break;
    }
  }

  // Find the scripting plugin because we assign this toolBox to it as we did not find the original sender
  if ( id == -1 ) {
Jan Möbius's avatar
Jan Möbius committed
262
263
    for ( uint i = 0 ; i < plugins_.size(); ++i ) {
      if ( plugins_[i].name == "Scripting" ) {
264
265
266
267
268
269
270
271
272
273
274
275
        id = i;
        break;
      }
    }


    if ( id == -1 ) {
      std::cerr << "Unknown sender plugin when adding Toolbox!" << std::endl;
      return;
    }
  }

276
  spinBoxEventFilter_.hookUpToWidgetTree(_widget);
Jan Möbius's avatar
Jan Möbius committed
277
278
  plugins_[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
  plugins_[id].toolboxIcons.push_back( 0 );
279
280

  // add widget name to viewMode 'all'
Jan Möbius's avatar
Jan Möbius committed
281
282
283
  if ( !viewModes_[0]->visibleToolboxes.contains(_name) ){
    viewModes_[0]->visibleToolboxes << _name;
    viewModes_[0]->visibleToolboxes.sort();
284
285
  }

Jan Möbius's avatar
Marlin:    
Jan Möbius committed
286
  setViewMode( OpenFlipper::Options::currentViewMode() );
287
288
}

Dirk Wilden's avatar
Dirk Wilden committed
289
290
291
292
293
294
//-----------------------------------------------------------------------------

void Core::addToolbox(QString _name ,QWidget* _widget, QIcon* _icon) {
  int id = -1;

  // Find the plugin which added this Toolbox
Jan Möbius's avatar
Jan Möbius committed
295
296
  for ( uint i = 0 ; i < plugins_.size(); ++i ) {
    if ( plugins_[i].plugin == sender() ) {
Dirk Wilden's avatar
Dirk Wilden committed
297
298
299
300
301
302
303
      id = i;
      break;
    }
  }

  // Find the scripting plugin because we assign this toolBox to it as we did not find the original sender
  if ( id == -1 ) {
Jan Möbius's avatar
Jan Möbius committed
304
305
    for ( uint i = 0 ; i < plugins_.size(); ++i ) {
      if ( plugins_[i].name == "Scripting" ) {
Dirk Wilden's avatar
Dirk Wilden committed
306
307
308
309
310
311
312
313
314
315
316
317
        id = i;
        break;
      }
    }


    if ( id == -1 ) {
      std::cerr << "Unknown sender plugin when adding Toolbox!" << std::endl;
      return;
    }
  }

318
  spinBoxEventFilter_.hookUpToWidgetTree(_widget);
Jan Möbius's avatar
Jan Möbius committed
319
320
  plugins_[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
  plugins_[id].toolboxIcons.push_back( _icon );
Dirk Wilden's avatar
Dirk Wilden committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337

  // add widget name to viewMode 'all'
  if ( !viewModes_[0]->visibleToolboxes.contains(_name) ){
    viewModes_[0]->visibleToolboxes << _name;
    viewModes_[0]->visibleToolboxes.sort();
  }

  setViewMode( OpenFlipper::Options::currentViewMode() );
}

void Core::setToolBoxActive(QString _toolBoxName, bool _active)
{
  if ( OpenFlipper::Options::gui() ){
    coreWidget_->toolBox_->setElementActive(_toolBoxName,_active);
  }
}

338
339
340
341
342
343
344
345
346
347
/// Block the scenegraph updates
void Core::blockSceneGraphUpdates() {
  OpenFlipper::Options::blockSceneGraphUpdates();
}

/// Unblock the scenegraph updates
void Core::unblockSceneGraphUpdates() {
  OpenFlipper::Options::unblockSceneGraphUpdates();
}

348
349
350
void Core::setView(QString view) {
    coreWidget_->slotSetView(view);
}
351

352
353
354
355
void Core::setViewAndWindowGeometry(QString view) {
    coreWidget_->slotSetViewAndWindowGeometry(view);
}

Jan Möbius's avatar
 
Jan Möbius committed
356
//=============================================================================
357
//== Script Special Functions =================================================
Jan Möbius's avatar
 
Jan Möbius committed
358
359
//=============================================================================

Jan Möbius's avatar
Jan Möbius committed
360
361
362
363
void Core::slotScriptError(const QScriptValue &error) {
    emit log(LOGERR, tr("Script error: ") + error.toString());
}

Jan Möbius's avatar
 
Jan Möbius committed
364
365
366
367
368
369
370
371
QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
{
  QString result;
  for (int i = 0; i < context->argumentCount(); ++i) {
    if (i > 0)
        result.append(" ");
    result.append(context->argument(i).toString());
  }
372

Jan Möbius's avatar
 
Jan Möbius committed
373
374
375
  // Get the textedit for Output ( Set in Core.cc )
  QScriptValue calleeData = context->callee().property("textedit");
  Core *widget = qobject_cast<Core*>(calleeData.toQObject());
376
377
378

  widget->scriptLogFunction(result);

Jan Möbius's avatar
 
Jan Möbius committed
379
380
381
  return engine->undefinedValue();
}

382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
QScriptValue printToFileFunction(QScriptContext *context, QScriptEngine *engine)
{
  if ( context->argumentCount() < 2 ) {
    context->throwError( QScriptContext::SyntaxError, "Error! printToFileFunction needs at least two arguments, filename and what should be printed" );
    return  engine->undefinedValue();
  }

  QString result;
  for (int i = 1; i < context->argumentCount(); ++i) {
    if (i > 1)
        result.append(" ");
    result.append(context->argument(i).toString());
  }

  QFile file(context->argument(0).toString());

  file.open(QIODevice::Append);
  QTextStream stream(&file);

  stream << result << "\n";

  file.close();

  return engine->undefinedValue();
}

Jan Möbius's avatar
Jan Möbius committed
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
QScriptValue helpFunction(QScriptContext *context, QScriptEngine *engine)
{
  if ( context->argumentCount() != 1 ) {
    context->throwError( QScriptContext::SyntaxError, "Error! helpFunction needs one argument" );
    return  engine->undefinedValue();
  }

  QString helpString = context->argument(0).toString();

  // Get the corewidget poiter ( Set in Core.cc )
  QScriptValue calleeData = context->callee().property("core");
  Core *core = qobject_cast<Core*>(calleeData.toQObject());

  const std::vector<PluginInfo> plugins = core->plugins();

  for (unsigned int i=0; i < plugins.size(); i++) {
    if (plugins[i].rpcName == helpString) {
      core->scriptLogFunction( "=======================================================\n" );
      core->scriptLogFunction( "Found Plugin \"" + plugins[i].name +  "\" \n" );
      core->scriptLogFunction( "Description: " + plugins[i].description + " \n");
      core->scriptLogFunction( "=======================================================\n" );
      core->scriptLogFunction( "Scripting functions: \n");

      for ( int j = 0 ; j < plugins[i].rpcFunctions.size() ; ++j ) {
        core->scriptLogFunction( plugins[i].rpcFunctions[j]+"\n");
      }

      core->scriptLogFunction( "\n\n");
    }
  }



  return engine->undefinedValue();
}