scripting.cc 10.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-2011 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 <QtUiTools/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
71



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

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

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

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

void Core::scriptLogFunction( QString _output) {
80
81
82
83
84
85
86
   emit scriptLog(_output);
}

void Core::createWidget(QString _objectName, QString _uiFilename) {
  QUiLoader loader;

  QFile uiFile(_uiFilename);
87
88

  if ( !uiFile.exists() ) {
89
    emit log(LOGERR,tr("File does not exist : ") + _uiFilename );
90
91
92
    return;
  }

93
94
95
96
  uiFile.open(QIODevice::ReadOnly);
  QWidget *ui = loader.load(&uiFile);
  uiFile.close();

97
  if ( ui == 0 ) {
98
    emit log(LOGERR,tr("Unable to create QWidget from ui file for ") + _objectName );
99
100
101
	return;
  }

102
  QScriptValue scriptUi = scriptEngine_.newQObject(ui, QScriptEngine::ScriptOwnership);
103
104

  if ( !scriptUi.isValid() ) {
105
    emit log(LOGERR,tr("Unable to generate script interface for ") + _objectName );
106
107
108
	return;
  }

109
110
111
112
113
  scriptEngine_.globalObject().setProperty(_objectName, scriptUi);


  ui->show();

Jan Möbius's avatar
   
Jan Möbius committed
114
115
}

116
117
//-----------------------------------------------------------------------------

118
119
120
121
122
123
124
int Core::getObjectId( const QString _name ) {

	return PluginFunctions::getObjectId(_name);
}

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

125
126
127
128
129
130
131
132
void Core::setViewMode(QString _viewMode){

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

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

Dirk Wilden's avatar
Dirk Wilden committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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() );
  }
}

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

153
void Core::moveToolBoxToTop(QString _name) {
154
155
    
  if(OpenFlipper::Options::gui()) {
156
    coreWidget_->moveToolBoxToTop(_name);
157
158
159
160
161
  }
}

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

162
void Core::moveToolBoxToBottom(QString _name) {
163
164
    
  if(OpenFlipper::Options::gui()) {
165
    coreWidget_->moveToolBoxToBottom(_name);
166
167
168
169
170
  }
}

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

171
void Core::addViewModeToolboxes(QString _modeName, QString _toolboxList) {
172
173

  QStringList list = _toolboxList.split(";");
Jan Möbius's avatar
Jan Möbius committed
174
  coreWidget_->slotAddViewModeToolboxes(_modeName,list);
175
176
177
178
}

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

179
180
181
182
183
184
185
186
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
187
188
189
190
191
192
void Core::addViewModeContextMenus(QString _modeName, QString _contextMenuList) {
  
  QStringList list = _contextMenuList.split(";");
  coreWidget_->slotAddViewModeContextMenus(_modeName,list);
}

193
194
195
196
void Core::addViewModeIcon(QString _modeName, QString _iconName) {
  coreWidget_->slotSetViewModeIcon(_modeName,true,_iconName);
}

Jan Möbius's avatar
Marlin:    
Jan Möbius committed
197
198
//-----------------------------------------------------------------------------

199
200
201
202
203
204
205
206
207
208
209
210
211
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));
    }
}

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

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
void Core::addToolbox(QString _name ,QWidget* _widget) {
  int id = -1;

  // Find the plugin which added this Toolbox
  for ( uint i = 0 ; i < plugins.size(); ++i ) {
    if ( plugins[i].plugin == sender() ) {
      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 ) {
    for ( uint i = 0 ; i < plugins.size(); ++i ) {
      if ( plugins[i].name == "Scripting" ) {
        id = i;
        break;
      }
    }


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

239
  plugins[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
Dirk Wilden's avatar
Dirk Wilden committed
240
  plugins[id].toolboxIcons.push_back( 0 );
241
242

  // add widget name to viewMode 'all'
Jan Möbius's avatar
Jan Möbius committed
243
244
245
  if ( !viewModes_[0]->visibleToolboxes.contains(_name) ){
    viewModes_[0]->visibleToolboxes << _name;
    viewModes_[0]->visibleToolboxes.sort();
246
247
  }

Jan Möbius's avatar
Marlin:    
Jan Möbius committed
248
  setViewMode( OpenFlipper::Options::currentViewMode() );
249
250
}

Dirk Wilden's avatar
Dirk Wilden committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//-----------------------------------------------------------------------------

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

  // Find the plugin which added this Toolbox
  for ( uint i = 0 ; i < plugins.size(); ++i ) {
    if ( plugins[i].plugin == sender() ) {
      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 ) {
    for ( uint i = 0 ; i < plugins.size(); ++i ) {
      if ( plugins[i].name == "Scripting" ) {
        id = i;
        break;
      }
    }


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

  plugins[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
  plugins[id].toolboxIcons.push_back( _icon );

  // 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);
  }
}

Jan Möbius's avatar
   
Jan Möbius committed
299
//=============================================================================
300
//== Script Special Functions =================================================
Jan Möbius's avatar
   
Jan Möbius committed
301
302
303
304
305
306
307
308
309
310
//=============================================================================

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());
  }
311

Jan Möbius's avatar
   
Jan Möbius committed
312
313
314
  // Get the textedit for Output ( Set in Core.cc )
  QScriptValue calleeData = context->callee().property("textedit");
  Core *widget = qobject_cast<Core*>(calleeData.toQObject());
315
316
317

  widget->scriptLogFunction(result);

Jan Möbius's avatar
   
Jan Möbius committed
318
319
320
  return engine->undefinedValue();
}