Developer Documentation

pythonWidget.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43 // If OpenFlipper builds with python support, we can integrate the interpreter.
44 // Otherwise we build only a dummy widget with no interpreter backend.
45 #ifdef PYTHON_ENABLED
46 #include <pybind11/include/pybind11/pybind11.h>
47 #include <pybind11/include/pybind11/embed.h>
48 #include <OpenFlipper/PythonInterpreter/PythonInterpreter.hh>
49 #endif
50
51 #include "pythonWidget.hh"
52 #include "PythonSyntaxHighlighter.hh"
53 #include <OpenFlipper/common/GlobalOptions.hh>
54 #include <OpenFlipper/BasePlugin/PythonFunctionsCore.hh>
55 #include <iostream>
56 #include <QObject>
57
58
59 PythonWidget::PythonWidget(QWidget *parent )
60 : QMainWindow(parent)
61 {
62 setupUi(this);
63
64 PythonSyntaxHighlighter *pythonHighlighter = new PythonSyntaxHighlighter(scriptWidget->document());
65
66 connect (RunButton, SIGNAL( clicked() ), this, SLOT( runScript()) );
67
68 QIcon icon;
69 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"window-close.png");
70 actionClose->setIcon(icon);
71 closeButton->setIcon(icon);
72
73 QIcon iconRun;
74 iconRun.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"arrow-right.png");
75 RunButton->setIcon(iconRun);
76
77 setWindowTitle(tr("%1 Python Interpreter").arg(TOSTRING(PRODUCT_NAME)));
78
79 connect( actionClose , SIGNAL(triggered() ) , this, SLOT(hide()) );
80 connect( closeButton , SIGNAL(clicked() ) , this, SLOT(hide()) );
81 closeButton->setFocus();
82
83 #ifdef PYTHON_ENABLED
84 pythonInfo->setAlignment(Qt::AlignLeft);
85
86
87 pythonInfo->append("Each module is automatically loaded by the core with the name given below.");
88 pythonInfo->append("An instance for each plugin is automatically generated with a lower case name of the module (E.g. the plugin Move will provide an instance move).\n");
89 pythonInfo->append("Available plugins with Python support:\n");
90
91 QStringList pythonPlugins = getPythonPlugins();
92
93 for ( int i = 0 ; i < pythonPlugins.size() ; ++i ) {
94 pythonInfo->append("Module " + pythonPlugins[i] + "\t\t providing instance " + pythonPlugins[i].toLower());
95 }
96
97 PythonInterpreter* interpreter = PythonInterpreter::getInstance();
98 if ( OpenFlipper::Options::gui() )
99 connect(interpreter,SIGNAL(log(Logtype,QString)) , this, SLOT(slotLocalLog(Logtype,QString)));
100
101
102 QTextEdit* coreInfo = new QTextEdit( infoTab );
103 infoTab->addTab(coreInfo,"Core");
104 QString coreDoc = interpreter->runScriptOutput("import pydoc ;import openflipper;html = pydoc.HTMLDoc();object, name = pydoc.resolve(openflipper);page = html.page(pydoc.describe(object), html.document(object, name));print(page)");
105 coreInfo->setHtml(coreDoc);
106
107 // Collect all Python Documentation for the OpenFlipper Plugin Modules
108 for ( int i = 0 ; i < pythonPlugins.size() ; ++i ) {
109 QTextEdit* edit = new QTextEdit( infoTab );
110 infoTab->addTab(edit,pythonPlugins[i]);
111 QString data = interpreter->runScriptOutput("import pydoc ;import "+pythonPlugins[i]+";html = pydoc.HTMLDoc();object, name = pydoc.resolve("+pythonPlugins[i]+");page = html.page(pydoc.describe(object), html.document(object, name));print(page)");
112 edit->setHtml(data);
113 }
114
115 #endif
116
117 }
118
119
120
121 void PythonWidget::runScript() {
122
123 #ifdef PYTHON_ENABLED
124 PythonInterpreter* interpreter = PythonInterpreter::getInstance();
125 interpreter->runScript(scriptWidget->toPlainText());
126 #else
127 std::cerr << "OpenFlipper is not compiled with python support. Unable to execute script!" << std::endl;
128 #endif
129
130 }
131
132 void PythonWidget::slotLocalLog(Logtype _type ,QString _logString) {
133
134 switch (_type) {
135 case LOGINFO:
136 pythonOutput->setTextColor( QColor(Qt::darkGreen) );
137 break;
138 case LOGOUT:
139 pythonOutput->setTextColor( QPalette{}.windowText().color() );
140 break;
141 case LOGWARN:
142 pythonOutput->setTextColor( QColor(160,160,0) );
143 break;
144 case LOGERR:
145 pythonOutput->setTextColor( QColor(Qt::red) );
146 break;
147 case LOGSTATUS:
148 pythonOutput->setTextColor( QColor(Qt::blue) );
149 break;
150 }
151
152 pythonOutput->append(_logString);
153 }
154
155
156
157
158
159
LOGWARN
Definition: LoggingInterface.hh:95
LOGINFO
Definition: LoggingInterface.hh:94
PythonInterpreter::runScriptOutput
QString runScriptOutput(QString _script)
Definition: PythonInterpreter.cc:309
PythonInterpreter::getInstance
static PythonInterpreter * getInstance()
Creates or returns an instance of the interpreter.
Definition: PythonInterpreter.cc:78
Logtype
Logtype
Log types for Message Window.
Definition: LoggingInterface.hh:92
PythonSyntaxHighlighter
Implementation of highlighting for Python code.
Definition: PythonSyntaxHighlighter.hh:50
LOGSTATUS
Definition: LoggingInterface.hh:92
LOGERR
Definition: LoggingInterface.hh:96
GlobalOptions.hh
PythonInterpreter::runScript
bool runScript(QString _script)
Run a script. Output is passed to the standard logging facilities of OpenFlipper. ...
Definition: PythonInterpreter.cc:233
TOSTRING
#define TOSTRING(x)
QSettings object containing all program settings of OpenFlipper.
Definition: GlobalOptions.hh:157
PythonInterpreter
This class provides OpenFlippers Python interpreter.
Definition: PythonInterpreter.hh:49
LOGOUT
Definition: LoggingInterface.hh:93