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: 8520 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2010-02-10 15:56:59 +0100 (Mi, 10. Feb 2010) $                   *
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::setViewModeIcon(QString _mode, QString _iconName){
00134 
00135   if ( OpenFlipper::Options::gui() ){
00136 
00137     QFile file(_iconName);
00138     QFileInfo fileInfo(file);
00139   
00140     if ( ! file.exists() ){
00141       emit log(LOGERR, tr("Icon not found (%1)").arg(_iconName) );
00142       return;
00143     }
00144 
00145     file.copy(OpenFlipper::Options::configDirStr() + QDir::separator() + "Icons" + QDir::separator() + "viewMode_" + fileInfo.fileName() );
00146 
00147     coreWidget_->slotSetViewModeIcon( _mode, "viewMode_" + fileInfo.fileName() );
00148   }
00149 }
00150 
00151 //-----------------------------------------------------------------------------
00152 
00153 void Core::addViewModeToolboxes(QString _modeName, QString _toolboxList) {
00154 
00155   QStringList list = _toolboxList.split(";");
00156   coreWidget_->slotAddViewModeToolboxes(_modeName,list);
00157 }
00158 
00159 //-----------------------------------------------------------------------------
00160 
00161 void Core::addViewModeToolbars(QString _modeName, QString _toolbarList) {
00162   
00163   QStringList list = _toolbarList.split(";");
00164   coreWidget_->slotAddViewModeToolbars(_modeName,list);
00165 }
00166 
00167 //-----------------------------------------------------------------------------
00168 
00169 void Core::addToolbox(QString _name ,QWidget* _widget) {
00170   int id = -1;
00171 
00172   // Find the plugin which added this Toolbox
00173   for ( uint i = 0 ; i < plugins.size(); ++i ) {
00174     if ( plugins[i].plugin == sender() ) {
00175       id = i;
00176       break;
00177     }
00178   }
00179 
00180   // Find the scripting plugin because we assign this toolBox to it as we did not find the original sender
00181   if ( id == -1 ) {
00182     for ( uint i = 0 ; i < plugins.size(); ++i ) {
00183       if ( plugins[i].name == "Scripting" ) {
00184         id = i;
00185         break;
00186       }
00187     }
00188 
00189 
00190     if ( id == -1 ) {
00191       std::cerr << "Unknown sender plugin when adding Toolbox!" << std::endl;
00192       return;
00193     }
00194   }
00195 
00196   plugins[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
00197 
00198   // add widget name to viewMode 'all'
00199   if ( !viewModes_[0]->visibleToolboxes.contains(_name) ){
00200     viewModes_[0]->visibleToolboxes << _name;
00201     viewModes_[0]->visibleToolboxes.sort();
00202   }
00203 
00204   setViewMode( OpenFlipper::Options::defaultToolboxMode() );
00205 }
00206 
00207 //=============================================================================
00208 //== Script Special Functions =================================================
00209 //=============================================================================
00210 
00211 QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
00212 {
00213   QString result;
00214   for (int i = 0; i < context->argumentCount(); ++i) {
00215     if (i > 0)
00216         result.append(" ");
00217     result.append(context->argument(i).toString());
00218   }
00219 
00220   // Get the textedit for Output ( Set in Core.cc )
00221   QScriptValue calleeData = context->callee().property("textedit");
00222   Core *widget = qobject_cast<Core*>(calleeData.toQObject());
00223 
00224   widget->scriptLogFunction(result);
00225 
00226   return engine->undefinedValue();
00227 }
00228 

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