60 #include <PythonInterpreter/PythonInterpreter.hh>
65#if QT_VERSION_MAJOR < 6
68 emit
scriptInfo( _pluginName , _functionName );
85 if ( _filename.endsWith(
"ofs",Qt::CaseInsensitive) ) {
90 if ( _filename.endsWith(
"ofp",Qt::CaseInsensitive) ) {
94 if ( !OpenFlipper::Options::nogui() ) {
102 std::cerr <<
"No Python Script support. Unable to execute: " + _filename.toStdString() ;
108 if ( _filename.endsWith(
"ofp",Qt::CaseInsensitive) ) {
109 #ifdef PYTHON_ENABLED
111 if ( !OpenFlipper::Options::nogui() ) {
120 std::cerr <<
"No Python Script support. Unable to execute: " + _filename.toStdString() ;
135 if ( OpenFlipper::Options::gui()) {
138 QFile uiFile(_uiFilename);
140 if ( !uiFile.exists() ) {
141 emit
log(
LOGERR,tr(
"File does not exist : ") + _uiFilename );
145 uiFile.open(QIODevice::ReadOnly);
150 emit
log(
LOGERR,tr(
"Unable to create QWidget from ui file for ") + _objectName );
154#if QT_VERSION_MAJOR < 6
156 QScriptValue scriptUi =
scriptEngine_.newQObject(ui, QScriptEngine::ScriptOwnership);
158 if ( !scriptUi.isValid() ) {
159 emit
log(
LOGERR,tr(
"Unable to generate script interface for ") + _objectName );
163 scriptEngine_.globalObject().setProperty(_objectName, scriptUi);
167 if(_show) ui->show();
169 emit
log(
LOGERR,tr(
"Error! Script tried to create Widget in ui less batc mode! Creation Aborted!"));
185 if ( OpenFlipper::Options::gui() )
192 return OpenFlipper::Options::currentViewMode();
199 if ( OpenFlipper::Options::gui() ){
201 QFile file(_iconName);
202 QFileInfo fileInfo(file);
204 if ( ! file.exists() ){
205 emit
log(
LOGERR, tr(
"Icon not found (%1)").arg(_iconName) );
209 file.copy(OpenFlipper::Options::configDirStr() + QDir::separator() +
"Icons" + QDir::separator() +
"viewMode_" + fileInfo.fileName() );
219 if(OpenFlipper::Options::gui()) {
228 if(OpenFlipper::Options::gui()) {
237 QStringList list = _toolboxList.split(
";");
245 QStringList list = _toolbarList.split(
";");
253 QStringList list = _contextMenuList.split(
";");
265 if(_side.toLower() ==
"left") {
267 }
else if(_side.toLower() ==
"right") {
270 emit
log(
LOGERR, QString(
"Could not display toolboxes on side '%1'. Use either 'left' or 'right' as string!").arg(_side));
277 std::vector<PluginInfo>::const_iterator pluginIt =
plugins().end();
278 for (std::vector<PluginInfo>::const_iterator it =
plugins().begin(), it_end =
plugins().end(); it != it_end; ++it) {
279 if (it->name == _pluginName) {
283 if (pluginIt ==
plugins().end())
return 0;
285 for (std::vector<std::pair<QString , QWidget*> >::const_iterator it = pluginIt->toolboxWidgets.begin(), it_end = pluginIt->toolboxWidgets.end();
286 it != it_end; ++it) {
287 if (it->first == _toolboxName)
308 QWidget *_headerAreaWidget) {
312 for ( uint i = 0 ; i <
plugins().size(); ++i ) {
313 if (
plugins()[i].plugin == sender() ) {
321 for ( uint i = 0 ; i <
plugins().size(); ++i ) {
322 if (
plugins()[i].name ==
"Scripting" ) {
330 std::cerr <<
"Unknown sender plugin when adding Toolbox!" << std::endl;
335 spinBoxEventFilter_.hookUpToWidgetTree(_widget);
336 plugins()[id].toolboxWidgets.push_back( std::pair< QString,QWidget* >( _name , _widget) );
337 plugins()[id].toolboxIcons.push_back( _icon );
338 plugins()[id].headerAreaWidgets.push_back( std::pair< QString,QWidget* >( _name , _headerAreaWidget) );
341 if ( !
viewModes_[0]->visibleToolboxes.contains(_name) ){
345 viewModes_[0]->toolboxIconMap.insert(_name, *_icon);
349 setViewMode( OpenFlipper::Options::currentViewMode() );
354 if ( OpenFlipper::Options::gui() ){
361 OpenFlipper::Options::blockSceneGraphUpdates();
366 OpenFlipper::Options::unblockSceneGraphUpdates();
381#if QT_VERSION_MAJOR < 6
384 emit
log(
LOGERR, tr(
"Script error: ") + error.toString());
390 for (
int i = 0; i < context->argumentCount(); ++i) {
393 result.append(context->argument(i).toString());
397 QScriptValue calleeData = context->callee().property(
"textedit");
398 Core *widget = qobject_cast<Core*>(calleeData.toQObject());
402 return engine->undefinedValue();
407 if ( context->argumentCount() < 2 ) {
408 context->throwError( QScriptContext::SyntaxError,
"Error! printToFileFunction needs at least two arguments, filename and what should be printed" );
409 return engine->undefinedValue();
413 for (
int i = 1; i < context->argumentCount(); ++i) {
416 result.append(context->argument(i).toString());
419 QFile file(context->argument(0).toString());
421 file.open(QIODevice::Append);
422 QTextStream stream(&file);
424 stream << result <<
"\n";
428 return engine->undefinedValue();
431QScriptValue
helpFunction(QScriptContext *context, QScriptEngine *engine)
433 if ( context->argumentCount() != 1 ) {
434 context->throwError( QScriptContext::SyntaxError,
"Error! helpFunction needs one argument" );
435 return engine->undefinedValue();
438 QString helpString = context->argument(0).toString();
441 QScriptValue calleeData = context->callee().property(
"core");
442 Core *core = qobject_cast<Core*>(calleeData.toQObject());
444 const std::vector<PluginInfo> plugins = core->
plugins();
446 for (
unsigned int i=0; i < plugins.size(); i++) {
447 if (plugins[i].rpcName == helpString) {
448 core->
scriptLogFunction(
"=======================================================\n" );
451 core->
scriptLogFunction(
"=======================================================\n" );
454 for (
int j = 0 ; j < plugins[i].rpcFunctions.size() ; ++j ) {
464 return engine->undefinedValue();
470 QFile file(_filename);
472 if (!file.exists()) {
473 emit
scriptLog(
"Unable to load file " + _filename +
" as python script. File not found!");
476 if (OpenFlipper::Options::nogui())
482 file.open(QIODevice::ReadOnly| QFile::Text);
484 QTextStream in(&file);
486 QString script = in.readAll();
498 emit
scriptLog(
"Python scripting Error! No build in python support. Unable to execute script. Build OpenFlipper with Python support to enable Python based scripting.");
502void Core::slotExecutePythonScript(
const QString& _script) {
506void Core::slotOpenPythonScriptInEditor(QString _script) {
QScriptValue printToFileFunction(QScriptContext *context, QScriptEngine *engine)
Special print function for sending output to a file.
QScriptValue helpFunction(QScriptContext *context, QScriptEngine *engine)
Function to print help about scripting functions.
QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
Special print function for core logger.
void slotGetAllAvailableFunctions(QStringList &_functions)
Core scripting engine.
void executeFileScript(QString _filename)
Core scripting engine.
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.
QWidget * getToolbox(const QString &_pluginName, const QString &_toolboxName)
void setToolBoxSide(QString _side)
Scripting function to set the side of the main window on which the toolbox should be displayed.
void addViewModeContextMenus(QString _modeName, QString _contextMenuList)
Scripting function to set context menus in a view mode.
void setViewAndWindowGeometry(QString view)
Called when a plugin requests an update in the viewer.
void slotScriptError(const QScriptValue &error)
Core scripting engine.
void blockSceneGraphUpdates()
Block the scenegraph updates.
void addViewModeIcon(QString _modeName, QString _iconName)
Scripting function to set an icon for a view mode.
void slotExecuteScript(const QString &_script)
void scriptLog(QString _message)
Logging signal for ScriptEngine.
void scriptInfo(QString _pluginName, QString _functionName)
Core scripting engine.
QStringList scriptingFunctions_
List of all registered scripting functions.
void moveToolBoxToBottom(QString _name)
Move selected toolbox to bottom of side area.
void executeScript(QString _script)
Core scripting engine.
void addToolbox(const QString &_name, QWidget *_widget)
Add a Toolbox from a plugin or from scripting.
QVector< ViewMode * > viewModes_
List of available draw modes.
void setToolBoxActive(QString _toolBoxName, bool _active)
Scripting function to activate or deactivate a toolbox.
void slotShowScriptInEditor(const QString &_filename)
void unblockSceneGraphUpdates()
Unblock the scenegraph updates.
void setViewModeIcon(QString _mode, QString _iconName)
Set the icon of a viewMode.
void moveToolBoxToTop(QString _name)
Move selected toolbox to top of side area.
void executePythonScriptFile(QString _filename)
Open the given file and execute its contents as a python script.
std::vector< PluginInfo > & plugins()
Index of Plugins toolbox widget.
QScriptEngine scriptEngine_
Core scripting engine.
void slotScriptInfo(const QString &_pluginName, const QString &_functionName)
Core scripting engine.
void createWidget(const QString &_objectName, const QString &_uiFilename, bool _show=true)
Create an script object from a ui file.
void slotExecuteFileScript(const QString &_filename)
void setView(QString view)
Called when a plugin requests an update in the viewer.
void log(Logtype _type, QString _message)
Logg with OUT,WARN or ERR as type.
int getObjectId(QString _filename)
Get object id from filename.
void setViewMode(QString _viewMode)
Set the active ViewMode.
void activateToolbox(QString _pluginName, QString _toolboxName, bool activate)
expand or collapse a toolbox
void executePythonScript(const QString &_script)
execute the given string as a python script
void scriptLogFunction(const QString &_output)
stream for logging to file
QString getCurrentViewMode()
Get current view mode.
void slotGetScriptingEngine(QScriptEngine *&_engine)
Core scripting engine.
CoreWidget * coreWidget_
The main applications widget ( only created in gui mode )
This class provides OpenFlippers Python interpreter.
bool runScript(const QString &_script)
Run a script. Output is passed to the standard logging facilities of OpenFlipper.
static PythonInterpreter * getInstance()
Creates or returns an instance of the interpreter.
void setElementActive(const QString &_name, bool _active)
set the active state of given element
int getObjectId(const QString &_name)