scripting.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 7237 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-09-30 15:54:25 +0200 (Mi, 30. Sep 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 //=============================================================================
00046 //
00047 //  CLASS Core - IMPLEMENTATION
00048 //
00049 //=============================================================================
00050 
00051 
00052 //== INCLUDES =================================================================
00053 
00054 // -------------------- mview
00055 #include "Core.hh"
00056 #include <QtUiTools/QUiLoader>
00057 
00058 
00059 //== IMPLEMENTATION ==========================================================
00060 
00061 
00062 
00063 void Core::slotScriptInfo( QString _pluginName , QString _functionName  ) {
00064   emit scriptInfo( _pluginName , _functionName );
00065 }
00066 
00067 void Core::slotExecuteScript( QString _script ) {
00068   emit executeScript( _script );
00069 }
00070 
00071 void Core::slotGetScriptingEngine( QScriptEngine*& _engine  ) {
00072   _engine = &scriptEngine_;
00073 }
00074 
00075 void Core::slotGetAllAvailableFunctions( QStringList& _functions  ) {
00076   _functions = scriptingFunctions_;
00077 }
00078 
00079 void Core::scriptLogFunction( QString _output) {
00080    emit scriptLog(_output);
00081 }
00082 
00083 void Core::createWidget(QString _objectName, QString _uiFilename) {
00084   QUiLoader loader;
00085 
00086   QFile uiFile(_uiFilename);
00087 
00088   if ( !uiFile.exists() ) {
00089     emit log(LOGERR,tr("File does not exist : ") + _uiFilename );
00090     return;
00091   }
00092 
00093   uiFile.open(QIODevice::ReadOnly);
00094   QWidget *ui = loader.load(&uiFile);
00095   uiFile.close();
00096 
00097   if ( ui == 0 ) {
00098     emit log(LOGERR,tr("Unable to create QWidget from ui file for ") + _objectName );
00099         return;
00100   }
00101 
00102   QScriptValue scriptUi = scriptEngine_.newQObject(ui, QScriptEngine::ScriptOwnership);
00103 
00104   if ( !scriptUi.isValid() ) {
00105     emit log(LOGERR,tr("Unable to generate script interface for ") + _objectName );
00106         return;
00107   }
00108 
00109   scriptEngine_.globalObject().setProperty(_objectName, scriptUi);
00110 
00111 
00112   ui->show();
00113 
00114 }
00115 
00116 //-----------------------------------------------------------------------------
00117 
00118 int Core::getObjectId( const QString _name ) {
00119 
00120         return PluginFunctions::getObjectId(_name);
00121 }
00122 
00123 //-----------------------------------------------------------------------------
00124 
00125 void Core::setViewMode(QString _viewMode){
00126 
00127   if ( OpenFlipper::Options::gui() )
00128     coreWidget_->setViewMode( _viewMode );
00129 }
00130 
00131 //-----------------------------------------------------------------------------
00132 
00133 void Core::addViewModeToolboxes(QString _modeName, QString _toolboxList) {
00134 
00135   QStringList list = _toolboxList.split(";");
00136   coreWidget_->slotAddViewModeToolboxes(_modeName,list);
00137 }
00138 
00139 //-----------------------------------------------------------------------------
00140 
00141 void Core::addViewModeToolbars(QString _modeName, QString _toolbarList) {
00142   
00143   QStringList list = _toolbarList.split(";");
00144   coreWidget_->slotAddViewModeToolbars(_modeName,list);
00145 }
00146 
00147 //-----------------------------------------------------------------------------
00148 
00149 void Core::addToolbox(QString _name ,QWidget* _widget) {
00150   int id = -1;
00151 
00152   // Find the plugin which added this Toolbox
00153   for ( uint i = 0 ; i < plugins.size(); ++i ) {
00154     if ( plugins[i].plugin == sender() ) {
00155       id = i;
00156       break;
00157     }
00158   }
00159 
00160   // Find the scripting plugin because we assign this toolBox to it as we did not find the original sender
00161   if ( id == -1 ) {
00162     for ( uint i = 0 ; i < plugins.size(); ++i ) {
00163       if ( plugins[i].name == "Scripting" ) {
00164         id = i;
00165         break;
00166       }
00167     }
00168 
00169 
00170     if ( id == -1 ) {
00171       std::cerr << "Unknown sender plugin when adding Toolbox!" << std::endl;
00172       return;
00173     }
00174   }
00175 
00176   plugins[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
00177 
00178   // add widget name to viewMode 'all'
00179   if ( !viewModes_[0]->visibleToolboxes.contains(_name) ){
00180     viewModes_[0]->visibleToolboxes << _name;
00181     viewModes_[0]->visibleToolboxes.sort();
00182   }
00183 
00184   setViewMode( OpenFlipper::Options::defaultToolboxMode() );
00185 }
00186 
00187 //=============================================================================
00188 //== Script Special Functions =================================================
00189 //=============================================================================
00190 
00191 QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
00192 {
00193   QString result;
00194   for (int i = 0; i < context->argumentCount(); ++i) {
00195     if (i > 0)
00196         result.append(" ");
00197     result.append(context->argument(i).toString());
00198   }
00199 
00200   // Get the textedit for Output ( Set in Core.cc )
00201   QScriptValue calleeData = context->callee().property("textedit");
00202   Core *widget = qobject_cast<Core*>(calleeData.toQObject());
00203 
00204   widget->scriptLogFunction(result);
00205 
00206   return engine->undefinedValue();
00207 }
00208 

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .