scripting.cc 8.2 KB
Newer Older
1 2 3 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
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           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/>.                                      *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
34

35 36 37 38 39 40 41
/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
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::addViewModeToolboxes(QString _modeName, QString _toolboxList) {
154 155

  QStringList list = _toolboxList.split(";");
Jan Möbius's avatar
Jan Möbius committed
156
  coreWidget_->slotAddViewModeToolboxes(_modeName,list);
157 158 159 160
}

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

161 162 163 164 165 166 167 168
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
169 170 171 172 173 174 175 176
void Core::addViewModeContextMenus(QString _modeName, QString _contextMenuList) {
  
  QStringList list = _contextMenuList.split(";");
  coreWidget_->slotAddViewModeContextMenus(_modeName,list);
}

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

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
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;
    }
  }

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

  // add widget name to viewMode 'all'
Jan Möbius's avatar
Jan Möbius committed
207 208 209
  if ( !viewModes_[0]->visibleToolboxes.contains(_name) ){
    viewModes_[0]->visibleToolboxes << _name;
    viewModes_[0]->visibleToolboxes.sort();
210 211
  }

Jan Möbius's avatar
Marlin:  
Jan Möbius committed
212
  setViewMode( OpenFlipper::Options::currentViewMode() );
213 214
}

Jan Möbius's avatar
 
Jan Möbius committed
215
//=============================================================================
216
//== Script Special Functions =================================================
Jan Möbius's avatar
 
Jan Möbius committed
217 218 219 220 221 222 223 224 225 226
//=============================================================================

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

Jan Möbius's avatar
 
Jan Möbius committed
228 229 230
  // Get the textedit for Output ( Set in Core.cc )
  QScriptValue calleeData = context->callee().property("textedit");
  Core *widget = qobject_cast<Core*>(calleeData.toQObject());
231 232 233

  widget->scriptLogFunction(result);

Jan Möbius's avatar
 
Jan Möbius committed
234 235 236
  return engine->undefinedValue();
}