60 #include <PythonInterpreter/PythonInterpreter.hh> 68 emit
scriptInfo( _pluginName , _functionName );
92 if ( OpenFlipper::Options::gui()) {
95 QFile uiFile(_uiFilename);
97 if ( !uiFile.exists() ) {
98 emit
log(
LOGERR,tr(
"File does not exist : ") + _uiFilename );
102 uiFile.open(QIODevice::ReadOnly);
107 emit
log(
LOGERR,tr(
"Unable to create QWidget from ui file for ") + _objectName );
111 QScriptValue scriptUi =
scriptEngine_.newQObject(ui, QScriptEngine::ScriptOwnership);
113 if ( !scriptUi.isValid() ) {
114 emit
log(
LOGERR,tr(
"Unable to generate script interface for ") + _objectName );
118 scriptEngine_.globalObject().setProperty(_objectName, scriptUi);
121 if(_show) ui->show();
123 emit
log(
LOGERR,tr(
"Error! Script tried to create Widget in ui less batc mode! Creation Aborted!"));
139 if ( OpenFlipper::Options::gui() )
146 return OpenFlipper::Options::currentViewMode();
153 if ( OpenFlipper::Options::gui() ){
155 QFile file(_iconName);
156 QFileInfo fileInfo(file);
158 if ( ! file.exists() ){
159 emit
log(
LOGERR, tr(
"Icon not found (%1)").arg(_iconName) );
163 file.copy(OpenFlipper::Options::configDirStr() + QDir::separator() +
"Icons" + QDir::separator() +
"viewMode_" + fileInfo.fileName() );
173 if(OpenFlipper::Options::gui()) {
182 if(OpenFlipper::Options::gui()) {
191 QStringList list = _toolboxList.split(
";");
199 QStringList list = _toolbarList.split(
";");
207 QStringList list = _contextMenuList.split(
";");
219 if(_side.toLower() ==
"left") {
221 }
else if(_side.toLower() ==
"right") {
224 emit
log(
LOGERR, QString(
"Could not display toolboxes on side '%1'. Use either 'left' or 'right' as string!").arg(_side));
231 std::vector<PluginInfo>::const_iterator pluginIt =
plugins().end();
232 for (std::vector<PluginInfo>::const_iterator it =
plugins().begin(), it_end =
plugins().end(); it != it_end; ++it) {
233 if (it->name == _pluginName) {
237 if (pluginIt ==
plugins().end())
return 0;
239 for (std::vector<std::pair<QString , QWidget*> >::const_iterator it = pluginIt->toolboxWidgets.begin(), it_end = pluginIt->toolboxWidgets.end();
240 it != it_end; ++it) {
241 if (it->first == _toolboxName)
262 QWidget *_headerAreaWidget) {
266 for ( uint i = 0 ; i <
plugins().size(); ++i ) {
267 if (
plugins()[i].plugin == sender() ) {
275 for ( uint i = 0 ; i <
plugins().size(); ++i ) {
276 if (
plugins()[i].name ==
"Scripting" ) {
284 std::cerr <<
"Unknown sender plugin when adding Toolbox!" << std::endl;
289 spinBoxEventFilter_.hookUpToWidgetTree(_widget);
290 plugins()[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
291 plugins()[id].toolboxIcons.push_back( _icon );
292 plugins()[id].headerAreaWidgets.push_back( std::pair< QString,QWidget* >( _name , _headerAreaWidget) );
295 if ( !
viewModes_[0]->visibleToolboxes.contains(_name) ){
300 setViewMode( OpenFlipper::Options::currentViewMode() );
305 if ( OpenFlipper::Options::gui() ){
312 OpenFlipper::Options::blockSceneGraphUpdates();
317 OpenFlipper::Options::unblockSceneGraphUpdates();
333 emit
log(
LOGERR, tr(
"Script error: ") + error.toString());
339 for (
int i = 0; i < context->argumentCount(); ++i) {
342 result.append(context->argument(i).toString());
346 QScriptValue calleeData = context->callee().property(
"textedit");
347 Core *widget = qobject_cast<
Core*>(calleeData.toQObject());
351 return engine->undefinedValue();
356 if ( context->argumentCount() < 2 ) {
357 context->throwError( QScriptContext::SyntaxError,
"Error! printToFileFunction needs at least two arguments, filename and what should be printed" );
358 return engine->undefinedValue();
362 for (
int i = 1; i < context->argumentCount(); ++i) {
365 result.append(context->argument(i).toString());
368 QFile file(context->argument(0).toString());
370 file.open(QIODevice::Append);
371 QTextStream stream(&file);
373 stream << result <<
"\n";
377 return engine->undefinedValue();
380 QScriptValue
helpFunction(QScriptContext *context, QScriptEngine *engine)
382 if ( context->argumentCount() != 1 ) {
383 context->throwError( QScriptContext::SyntaxError,
"Error! helpFunction needs one argument" );
384 return engine->undefinedValue();
387 QString helpString = context->argument(0).toString();
390 QScriptValue calleeData = context->callee().property(
"core");
391 Core *core = qobject_cast<
Core*>(calleeData.toQObject());
395 for (
unsigned int i=0; i < plugins.size(); i++) {
396 if (plugins[i].rpcName == helpString) {
397 core->
scriptLogFunction(
"=======================================================\n" );
400 core->
scriptLogFunction(
"=======================================================\n" );
403 for (
int j = 0 ; j < plugins[i].rpcFunctions.size() ; ++j ) {
413 return engine->undefinedValue();
418 QFile file(_filename);
420 if (!file.exists()) {
421 emit
scriptLog(
"Unable to load file " + _filename +
" as python script. File not found!");
425 file.open(QIODevice::ReadOnly| QFile::Text);
427 QTextStream in(&file);
429 QString script = in.readAll();
431 std::cerr <<
"Script : " << std::endl;
432 std::cerr << script.toStdString() << std::endl;
440 #ifdef PYTHON_ENABLED 444 emit
scriptLog(
"Python scripting Error! No build in python support. Unable to execute script. Build OpenFlipper with Python support to enable Python based scripting.");
void setView(QString view)
Called when a plugin requests an update in the viewer.
void moveToolBoxToBottom(QString _name)
Move a specific toolbox widget to the bottom of the side area.
void setViewMode(QString _mode, bool _expandAll=false)
Set the view Mode to the given Mode.
QStringList scriptingFunctions_
List of all registered scripting functions.
void slotExecuteScript(QString _script)
void slotSetViewModeIcon(QString _mode, QString _iconName)
Sets the Icon for a given View Mode (non-userdefined viewMode)
static PythonInterpreter * getInstance()
Creates or returns an instance of the interpreter.
void setViewMode(QString _viewMode)
Set the active ViewMode.
void executePythonScript(QString _script)
execute the given string as a python script
int getObjectId(QString _filename)
Get object id from filename.
QScriptValue printToFileFunction(QScriptContext *context, QScriptEngine *engine)
Special print function for sending output to a file.
void slotGetAllAvailableFunctions(QStringList &_functions)
Core scripting engine.
int getObjectId(const QString &_name)
void scriptLogFunction(QString _output)
stream for logging to file
void setToolBoxOrientationOnTheRight(bool _toolBoxRight)
Set orientation of tool box (either on the right or the left side of the screen)
void addViewModeToolbars(QString _modeName, QString _toolbarList)
Scripting function to set toolbars in a view mode.
void addViewModeToolboxes(QString _modeName, QString _toolboxList)
Scripting function to set toolboxes in a view mode.
void createWidget(QString _objectName, QString _uiFilename, bool _show=true)
Create an script object from a ui file.
void executeFileScript(QString _filename)
Core scripting engine.
void addViewModeContextMenus(QString _modeName, QString _contextMenuList)
Scripting function to set context menus in a view mode.
void executePythonScriptFile(QString _filename)
Open the given file and execute its contents as a python script.
void setViewModeIcon(QString _mode, QString _iconName)
Set the icon of a viewMode.
void slotScriptError(const QScriptValue &error)
Core scripting engine.
void slotAddViewModeContextMenus(QString _mode, QStringList _usedToolbars)
Add or change Toolbars for a ViewMode (non-userdefined viewMode)
void addToolbox(QString _name, QWidget *_widget)
Add a Toolbox from a plugin or from scripting.
SideArea * toolBox_
Toolbox.
void log(Logtype _type, QString _message)
Logg with OUT,WARN or ERR as type.
void moveToolBoxToTop(QString _name)
Move a specific toolbox widget to the top of the side area.
CoreWidget * coreWidget_
The main applications widget ( only created in gui mode )
QScriptEngine scriptEngine_
Core scripting engine.
void setToolBoxSide(QString _side)
Scripting function to set the side of the main window on which the toolbox should be displayed...
std::vector< PluginInfo > & plugins()
Index of Plugins toolbox widget.
QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
Special print function for core logger.
void slotSetViewAndWindowGeometry(QString view)
Set the supplied serialized view.
void slotSetView(QString view)
Set the supplied serialized view.
void slotAddViewModeToolbars(QString _mode, QStringList _usedToolbars)
Add or change Toolbars for a ViewMode (non-userdefined viewMode)
void scriptInfo(QString _pluginName, QString _functionName)
Core scripting engine.
void slotScriptInfo(QString _pluginName, QString _functionName)
Core scripting engine.
QString getCurrentViewMode()
Get current view mode.
void setElementActive(QString _name, bool _active)
set the active state of given element
bool runScript(QString _script)
Run a script. Output is passed to the standard logging facilities of OpenFlipper. ...
QScriptValue helpFunction(QScriptContext *context, QScriptEngine *engine)
Function to print help about scripting functions.
void expandToolBoxWidget(QWidget *widget, bool expand)
void executeScript(QString _script)
Core scripting engine.
void blockSceneGraphUpdates()
Block the scenegraph updates.
void slotGetScriptingEngine(QScriptEngine *&_engine)
Core scripting engine.
void addViewModeIcon(QString _modeName, QString _iconName)
Scripting function to set an icon for a view mode.
void activateToolbox(QString _pluginName, QString _toolboxName, bool activate)
expand or collapse a toolbox
QVector< ViewMode * > viewModes_
List of available draw modes.
void unblockSceneGraphUpdates()
Unblock the scenegraph updates.
This class provides OpenFlippers Python interpreter.
void moveToolBoxToTop(QString _name)
Move selected toolbox to top of side area.
QWidget * getToolbox(QString _pluginName, QString _toolboxName)
void scriptLog(QString _message)
Logging signal for ScriptEngine.
void setToolBoxActive(QString _toolBoxName, bool _active)
Scripting function to activate or deactivate a toolbox.
void moveToolBoxToBottom(QString _name)
Move selected toolbox to bottom of side area.
void slotExecuteFileScript(QString _filename)
void setViewAndWindowGeometry(QString view)
Called when a plugin requests an update in the viewer.
void slotAddViewModeToolboxes(QString _mode, QStringList _usedWidgets)
Add or change Toolboxes for a ViewMode (non-userdefined viewMode)