scripting.cc 14.2 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
4 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 33 34 35 36 37 38
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
41

42
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
48
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62



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


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

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

65 66

//== IMPLEMENTATION ==========================================================
Jan Möbius's avatar
 
Jan Möbius committed
67 68 69 70 71 72 73 74 75 76 77



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
78 79 80 81
void Core::slotExecuteFileScript( QString _filename ) {
  emit executeFileScript( _filename );
}

Jan Möbius's avatar
 
Jan Möbius committed
82
void Core::slotGetScriptingEngine( QScriptEngine*& _engine  ) {
83
  _engine = &scriptEngine_;
Jan Möbius's avatar
 
Jan Möbius committed
84 85 86 87 88 89 90
}

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

void Core::scriptLogFunction( QString _output) {
91 92 93
   emit scriptLog(_output);
}

94
void Core::createWidget(QString _objectName, QString _uiFilename, bool _show) {
95 96
  if ( OpenFlipper::Options::gui()) {
    QUiLoader loader;
97

98
    QFile uiFile(_uiFilename);
99

100 101 102 103
    if ( !uiFile.exists() ) {
      emit log(LOGERR,tr("File does not exist : ") + _uiFilename );
      return;
    }
104

105
    uiFile.open(QIODevice::ReadOnly);
106
    QWidget *ui = loader.load(&uiFile, coreWidget_);
107
    uiFile.close();
108

109 110 111 112
    if ( ui == 0 ) {
      emit log(LOGERR,tr("Unable to create QWidget from ui file for ") + _objectName );
      return;
    }
113

114
    QScriptValue scriptUi = scriptEngine_.newQObject(ui, QScriptEngine::ScriptOwnership);
115

116 117 118 119
    if ( !scriptUi.isValid() ) {
      emit log(LOGERR,tr("Unable to generate script interface for ") + _objectName );
      return;
    }
120

121
    scriptEngine_.globalObject().setProperty(_objectName, scriptUi);
122 123


124
    if(_show) ui->show();
125 126 127
  } else {
    emit log(LOGERR,tr("Error! Script tried to create Widget in ui less batc mode! Creation Aborted!"));
  }
128

Jan Möbius's avatar
 
Jan Möbius committed
129 130
}

131 132
//-----------------------------------------------------------------------------

133 134 135 136 137 138 139
int Core::getObjectId( const QString _name ) {

	return PluginFunctions::getObjectId(_name);
}

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

140 141 142 143 144 145 146 147
void Core::setViewMode(QString _viewMode){

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

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

148 149 150 151 152 153
QString Core::getCurrentViewMode() {
  return OpenFlipper::Options::currentViewMode();
}

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

Dirk Wilden's avatar
Dirk Wilden committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
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() );
  }
}

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

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(";");
Jan Möbius's avatar
Jan Möbius committed
195
  coreWidget_->slotAddViewModeToolboxes(_modeName,list);
196 197 198 199
}

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

200 201 202 203 204 205 206 207
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
208 209 210 211 212 213
void Core::addViewModeContextMenus(QString _modeName, QString _contextMenuList) {
  
  QStringList list = _contextMenuList.split(";");
  coreWidget_->slotAddViewModeContextMenus(_modeName,list);
}

214 215 216 217
void Core::addViewModeIcon(QString _modeName, QString _iconName) {
  coreWidget_->slotSetViewModeIcon(_modeName,true,_iconName);
}

Jan Möbius's avatar
Marlin:  
Jan Möbius committed
218 219
//-----------------------------------------------------------------------------

220 221 222 223 224 225 226 227 228 229 230 231 232
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));
    }
}

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

233
QWidget *Core::getToolbox(QString _pluginName, QString _toolboxName) {
234 235
    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) {
236 237 238 239
        if (it->name == _pluginName) {
            pluginIt = it;
        }
    }
240
    if (pluginIt == plugins().end()) return 0;
241 242 243 244 245 246 247 248 249 250

    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
251 252 253 254 255
void Core::activateToolbox(QString _pluginName, QString _toolboxName, bool activate) {
    QWidget *toolbox = Core::getToolbox(_pluginName, _toolboxName);
    coreWidget_->expandToolBoxWidget(toolbox, activate);
}

256
void Core::addToolbox(QString _name ,QWidget* _widget) {
257
    addToolbox(_name, _widget, 0, 0);
258 259
}

Dirk Wilden's avatar
Dirk Wilden committed
260
void Core::addToolbox(QString _name ,QWidget* _widget, QIcon* _icon) {
261 262 263 264 265
    addToolbox(_name, _widget, _icon, 0);
}

void Core::addToolbox(QString _name ,QWidget* _widget, QIcon* _icon,
        QWidget *_headerAreaWidget) {
Dirk Wilden's avatar
Dirk Wilden committed
266 267 268
  int id = -1;

  // Find the plugin which added this Toolbox
269 270
  for ( uint i = 0 ; i < plugins().size(); ++i ) {
    if ( plugins()[i].plugin == sender() ) {
Dirk Wilden's avatar
Dirk Wilden committed
271 272 273 274 275 276 277
      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 ) {
278 279
    for ( uint i = 0 ; i < plugins().size(); ++i ) {
      if ( plugins()[i].name == "Scripting" ) {
Dirk Wilden's avatar
Dirk Wilden committed
280 281 282 283 284 285 286 287 288 289 290 291
        id = i;
        break;
      }
    }


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

292
  spinBoxEventFilter_.hookUpToWidgetTree(_widget);
293 294 295
  plugins()[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
  plugins()[id].toolboxIcons.push_back( _icon );
  plugins()[id].headerAreaWidgets.push_back( std::pair< QString,QWidget* >( _name , _headerAreaWidget) );
Dirk Wilden's avatar
Dirk Wilden committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

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

313 314 315 316 317 318 319 320 321 322
/// Block the scenegraph updates
void Core::blockSceneGraphUpdates() {
  OpenFlipper::Options::blockSceneGraphUpdates();
}

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

323 324 325
void Core::setView(QString view) {
    coreWidget_->slotSetView(view);
}
326

327 328 329 330
void Core::setViewAndWindowGeometry(QString view) {
    coreWidget_->slotSetViewAndWindowGeometry(view);
}

Jan Möbius's avatar
 
Jan Möbius committed
331
//=============================================================================
332
//== Script Special Functions =================================================
Jan Möbius's avatar
 
Jan Möbius committed
333 334
//=============================================================================

Jan Möbius's avatar
Jan Möbius committed
335 336 337 338
void Core::slotScriptError(const QScriptValue &error) {
    emit log(LOGERR, tr("Script error: ") + error.toString());
}

Jan Möbius's avatar
 
Jan Möbius committed
339 340 341 342 343 344 345 346
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());
  }
347

Jan Möbius's avatar
 
Jan Möbius committed
348 349 350
  // Get the textedit for Output ( Set in Core.cc )
  QScriptValue calleeData = context->callee().property("textedit");
  Core *widget = qobject_cast<Core*>(calleeData.toQObject());
351 352 353

  widget->scriptLogFunction(result);

Jan Möbius's avatar
 
Jan Möbius committed
354 355 356
  return engine->undefinedValue();
}

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
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
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 408 409 410 411 412 413 414 415 416 417 418
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();
}