diff --git a/BasePlugin/RPCInterface.hh b/BasePlugin/RPCInterface.hh index 50f825abb7851a22182752110e735febb2e32f59..a33db316f4bb6c6c13690d36c9ad6e9b1c999ab5 100644 --- a/BasePlugin/RPCInterface.hh +++ b/BasePlugin/RPCInterface.hh @@ -44,12 +44,14 @@ #ifndef RPCINTERFACE_HH #define RPCINTERFACE_HH +#include + /** Interface for all Plugins which do remote procedure calls ( and cross plugin calls) */ class RPCInterface { public: - + /// Destructor virtual ~RPCInterface() {}; diff --git a/BasePlugin/RPCWrappers.cc b/BasePlugin/RPCWrappers.cc new file mode 100644 index 0000000000000000000000000000000000000000..f4025e769b898ec6a80c6f368a58a13add299799 --- /dev/null +++ b/BasePlugin/RPCWrappers.cc @@ -0,0 +1,97 @@ +//============================================================================= +// +// OpenFlipper +// Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen +// www.openflipper.org +// +//----------------------------------------------------------------------------- +// +// License +// +// 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. +// +// 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 Lesser General Public License +// along with OpenFlipper. If not, see . +// +//----------------------------------------------------------------------------- +// +// $Revision: 4737 $ +// $Author: kremer $ +// $Date: 2009-02-10 11:46:02 +0100 (Di, 10. Feb 2009) $ +// +//============================================================================= + + + + +// +// C++ Interface: RPCInterface +// +// Description: +// +// +// Author: Jan Moebius , (C) 2007 +// + +#include +#include + +namespace RPC { + +static QScriptEngine* engine_; + +QScriptValue callFunction( QString _plugin, QString _functionName , std::vector< QScriptValue > _parameters ) { + + QString command = _plugin+"."+_functionName+ "("; + // Make the parameters available in the scripting environment + for ( uint i = 0 ; i < _parameters.size(); ++i ) { + engine_->globalObject().setProperty("ParameterData" + QString::number(i) , _parameters[i] ); + command += "ParameterData" + QString::number(i); + if ( (i + 1) < _parameters.size() ) + command+=","; + } + command += ")"; + + QScriptValue returnValue = engine_->evaluate(command); + if ( returnValue.isError() ) { + QString error = returnValue.toString(); + std::cerr << "Error : " << error.toStdString() << std::endl; + return returnValue; + } + + return returnValue; +} + +QScriptValue callFunction( QString _plugin, QString _functionName ) { + + QString command = _plugin+"."+_functionName+ "()"; + + QScriptValue returnValue = engine_->evaluate(command); + if ( returnValue.isError() ) { + QString error = returnValue.toString(); + std::cerr << "Error : " << error.toStdString() << std::endl; + return returnValue; + } + + return returnValue; +} + + +void setScriptEngine( QScriptEngine* _engine ) { + engine_ = _engine; +} + +QScriptEngine* getScriptEngine() { + return engine_; +} + +} + diff --git a/BasePlugin/RPCWrappers.hh b/BasePlugin/RPCWrappers.hh new file mode 100644 index 0000000000000000000000000000000000000000..4f40cf8dad4ae85d6bcd36d3a251e517f7d49e06 --- /dev/null +++ b/BasePlugin/RPCWrappers.hh @@ -0,0 +1,165 @@ +//============================================================================= +// +// OpenFlipper +// Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen +// www.openflipper.org +// +//----------------------------------------------------------------------------- +// +// License +// +// 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. +// +// 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 Lesser General Public License +// along with OpenFlipper. If not, see . +// +//----------------------------------------------------------------------------- +// +// $Revision: 4737 $ +// $Author: kremer $ +// $Date: 2009-02-10 11:46:02 +0100 (Di, 10. Feb 2009) $ +// +//============================================================================= + +// +// C++ Interface: RPCInterface +// +// Description: +// +// +// Author: Jan Moebius , (C) 2007 +// + +/** + * \file RPCWrappers.hh + * This file contains functions to call functions and procedures across plugins. + * The Qt Scripting system is used to pass the calls between different plugins. + */ + +#ifndef RPCWRAPPERS_HH +#define RPCWRAPPERS_HH + +#include +#include + +/** Namespace containing RPC helper functions used to call functions across plugins + */ + +/** + * Example code : + * QString version = RPC::callFunctionValue< QString >("backup","version"); + * int count = RPC::callFunctionValue< int >("info","vertexCount",2); + * + */ +namespace RPC { + +/** \brief get a pointer to the scripting engine + * + */ +QScriptEngine* getScriptEngine(); + +/** \brief call a function provided by a plugin + * + * @param _plugin Plugin name ( Scripting name ) + * @param _functionName Name of the remote function + */ +QScriptValue callFunction( QString _plugin, QString _functionName ); + +/** \brief call a function provided by a plugin getting multiple parameters + * + * @param _plugin Plugin name ( Scripting name ) + * @param _functionName Name of the remote function + * @param _parameters vector of scriptvalues containing the functions parameters in the right order + */ +QScriptValue callFunction( QString _plugin, QString _functionName , std::vector< QScriptValue > _parameters ); + + +template +void callFunction( QString _plugin, QString _functionName, T0 _t0) { + QScriptEngine* engine = getScriptEngine(); + std::vector< QScriptValue > parameters; + parameters.push_back( QScriptValue( engine,_t0 ) ); + callFunction(_plugin,_functionName,parameters); +} + +template +void callFunction( QString _plugin, QString _functionName, T0 _t0 , T1 _t1) { + QScriptEngine* engine = getScriptEngine(); + std::vector< QScriptValue > parameters; + parameters.push_back( QScriptValue( engine,_t0 ) ); + parameters.push_back( QScriptValue( engine,_t1 ) ); + callFunction(_plugin,_functionName,parameters); +} + +template +void callFunction( QString _plugin, QString _functionName, T0 _t0 , T1 _t1 , T2 _t2 ) { + QScriptEngine* engine = getScriptEngine(); + std::vector< QScriptValue > parameters; + parameters.push_back( QScriptValue( engine,_t0 ) ); + parameters.push_back( QScriptValue( engine,_t1 ) ); + parameters.push_back( QScriptValue( engine,_t2 ) ); + callFunction(_plugin,_functionName,parameters); +} + +template +void callFunction( QString _plugin, QString _functionName, T0 _t0 , T1 _t1 , T2 _t2 , T3 _t3 ) { + QScriptEngine* engine = getScriptEngine(); + std::vector< QScriptValue > parameters; + parameters.push_back( QScriptValue( engine,_t0 ) ); + parameters.push_back( QScriptValue( engine,_t1 ) ); + parameters.push_back( QScriptValue( engine,_t2 ) ); + parameters.push_back( QScriptValue( engine,_t3 ) ); + callFunction(_plugin,_functionName,parameters); +} + +template +ReturnValue callFunctionValue( QString _plugin, QString _functionName) { + return qscriptvalue_cast< ReturnValue >( callFunction(_plugin,_functionName) ); +} + +template +ReturnValue callFunctionValue( QString _plugin, QString _functionName, T0 _t0) { + QScriptEngine* engine = getScriptEngine(); + std::vector< QScriptValue > parameters; + parameters.push_back( QScriptValue( engine,_t0 ) ); + return qscriptvalue_cast( callFunction(_plugin,_functionName,parameters) ); +} + +template +ReturnValue callFunctionValue( QString _plugin, QString _functionName, T0 _t0 , T1 _t1) { + QScriptEngine* engine = getScriptEngine(); + std::vector< QScriptValue > parameters; + parameters.push_back( QScriptValue( engine,_t0 ) ); + parameters.push_back( QScriptValue( engine,_t1 ) ); + return qscriptvalue_cast( callFunction(_plugin,_functionName,parameters) ); +} + +template +ReturnValue callFunctionValue( QString _plugin, QString _functionName, T0 _t0 , T1 _t1 , T2 _t2 , T3 _t3 ) { + QScriptEngine* engine = getScriptEngine(); + std::vector< QScriptValue > parameters; + parameters.push_back( QScriptValue( engine,_t0 ) ); + parameters.push_back( QScriptValue( engine,_t1 ) ); + parameters.push_back( QScriptValue( engine,_t2 ) ); + parameters.push_back( QScriptValue( engine,_t3 ) ); + return qscriptvalue_cast( callFunction(_plugin,_functionName,parameters) ); +} + + +/** \brief DONT USE! (Function to set the internal reference to the script Engine) + * + * Function to set the internal reference to the script engine from the core + */ +void setScriptEngine( QScriptEngine* _engine ); + +} + +#endif // RPCWRAPPERS_HH diff --git a/Core/Core.cc b/Core/Core.cc index 6481be6d8a8b2fe3676e1208559126142feaf381..12c0634e24e1b46a208b48ea8a627a11e353a537 100644 --- a/Core/Core.cc +++ b/Core/Core.cc @@ -85,6 +85,8 @@ #include "OpenFlipper/BasePlugin/PluginFunctions.hh" #include "OpenFlipper/BasePlugin/PluginFunctionsCore.hh" +#include "OpenFlipper/BasePlugin/RPCWrappers.hh" + #include #define WIDGET_HEIGHT 800 @@ -341,6 +343,9 @@ Core::init() { ///@todo reimplement // connect(module_list,SIGNAL(currentChanged(int)),this,SLOT(slotToolboxSwitched(int))); + // Set a reference to the scriptengine for simple rpc calls + RPC::setScriptEngine(&scriptEngine_); + // process Events every 500 msecs during script execution scriptEngine_.setProcessEventsInterval( 500 );