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!");
424 if (OpenFlipper::Options::nogui())
430 file.open(QIODevice::ReadOnly| QFile::Text);
432 QTextStream in(&file);
434 QString script = in.readAll();
442 #ifdef PYTHON_ENABLED 446 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 createWidget(QString _objectName, QString _uiFilename, bool _show=true)
Create an script object from a ui file.
QWidget * getToolbox(QString _pluginName, QString _toolboxName)
void setToolBoxActive(QString _toolBoxName, bool _active)
Scripting function to activate or deactivate a toolbox.
void setElementActive(QString _name, bool _active)
set the active state of given element
void slotExecuteScript(QString _script)
void addViewModeToolbars(QString _modeName, QString _toolbarList)
Scripting function to set toolbars in a view mode.
bool runScript(QString _script)
Run a script. Output is passed to the standard logging facilities of OpenFlipper. ...
void scriptLogFunction(QString _output)
stream for logging to file
static PythonInterpreter * getInstance()
Creates or returns an instance of the interpreter.
void setView(QString view)
Called when a plugin requests an update in the viewer.
int getObjectId(const QString &_name)
void executeFileScript(QString _filename)
Core scripting engine.
QScriptValue helpFunction(QScriptContext *context, QScriptEngine *engine)
Function to print help about scripting functions.
void addViewModeContextMenus(QString _modeName, QString _contextMenuList)
Scripting function to set context menus in a view mode.
QScriptValue printToFileFunction(QScriptContext *context, QScriptEngine *engine)
Special print function for sending output to a file.
void log(Logtype _type, QString _message)
Logg with OUT,WARN or ERR as type.
void executePythonScriptFile(QString _filename)
Open the given file and execute its contents as a python script.
void executeScript(QString _script)
Core scripting engine.
CoreWidget * coreWidget_
The main applications widget ( only created in gui mode )
QStringList scriptingFunctions_
List of all registered scripting functions.
This class provides OpenFlippers Python interpreter.
QScriptEngine scriptEngine_
Core scripting engine.
void addViewModeToolboxes(QString _modeName, QString _toolboxList)
Scripting function to set toolboxes in a view mode.
QVector< ViewMode * > viewModes_
List of available draw modes.
void setToolBoxSide(QString _side)
Scripting function to set the side of the main window on which the toolbox should be displayed...
QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
Special print function for core logger.
QString getCurrentViewMode()
Get current view mode.
void unblockSceneGraphUpdates()
Unblock the scenegraph updates.
void blockSceneGraphUpdates()
Block the scenegraph updates.
void slotScriptError(const QScriptValue &error)
Core scripting engine.
void slotGetScriptingEngine(QScriptEngine *&_engine)
Core scripting engine.
void setViewModeIcon(QString _mode, QString _iconName)
Set the icon of a viewMode.
void activateToolbox(QString _pluginName, QString _toolboxName, bool activate)
expand or collapse a toolbox
void scriptInfo(QString _pluginName, QString _functionName)
Core scripting engine.
void addToolbox(QString _name, QWidget *_widget)
Add a Toolbox from a plugin or from scripting.
void setViewMode(QString _viewMode)
Set the active ViewMode.
std::vector< PluginInfo > & plugins()
Index of Plugins toolbox widget.
void scriptLog(QString _message)
Logging signal for ScriptEngine.
void slotScriptInfo(QString _pluginName, QString _functionName)
Core scripting engine.
void moveToolBoxToTop(QString _name)
Move selected toolbox to top of side area.
void addViewModeIcon(QString _modeName, QString _iconName)
Scripting function to set an icon for a view mode.
void slotExecuteFileScript(QString _filename)
int getObjectId(QString _filename)
Get object id from filename.
void executePythonScript(QString _script)
execute the given string as a python script
void moveToolBoxToBottom(QString _name)
Move selected toolbox to bottom of side area.
void slotGetAllAvailableFunctions(QStringList &_functions)
Core scripting engine.
void setViewAndWindowGeometry(QString view)
Called when a plugin requests an update in the viewer.