Developer Documentation
Loading...
Searching...
No Matches
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/pybind11.h>
47 #include <pybind11/embed.h>
48 #include <OpenFlipper/PythonInterpreter/PythonInterpreter.hh>
49#endif
50
51#include "pythonWidget.hh"
52#include "PythonSyntaxHighlighter.hh"
54#include <OpenFlipper/BasePlugin/PythonFunctionsCore.hh>
55#include <iostream>
56#include <QObject>
57#include <QTextStream>
58
59
60PythonWidget::PythonWidget(QWidget *parent )
61 : QMainWindow(parent)
62{
63 setupUi(this);
64
65 PythonSyntaxHighlighter *pythonHighlighter = new PythonSyntaxHighlighter(scriptWidget->document());
66
67 connect (RunButton, SIGNAL( clicked() ), this, SLOT( runScript()) );
68
69 QIcon icon;
70 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"window-close.png");
71 actionClose->setIcon(icon);
72 closeButton->setIcon(icon);
73
74 QIcon iconRun;
75 iconRun.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"arrow-right.png");
76 RunButton->setIcon(iconRun);
77
78 setWindowTitle(tr("%1 Python Interpreter").arg(TOSTRING(PRODUCT_NAME)));
79
80 connect( actionClose , SIGNAL(triggered() ) , this, SLOT(hide()) );
81 connect( closeButton , SIGNAL(clicked() ) , this, SLOT(hide()) );
82 closeButton->setFocus();
83
84#ifdef PYTHON_ENABLED
85 pythonInfo->setAlignment(Qt::AlignLeft);
86
87
88 pythonInfo->append("Each module is automatically loaded by the core with the name given below.");
89 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");
90 pythonInfo->append("Available plugins with Python support:\n");
91
92 QStringList pythonPlugins = getPythonPlugins();
93
94 for ( int i = 0 ; i < pythonPlugins.size() ; ++i ) {
95 pythonInfo->append("Module " + pythonPlugins[i] + "\t\t providing instance " + pythonPlugins[i].toLower());
96 }
97
99 if ( OpenFlipper::Options::gui() )
100 connect(interpreter,SIGNAL(log(Logtype,QString)) , this, SLOT(slotLocalLog(Logtype,QString)));
101
102
103 QTextEdit* coreInfo = new QTextEdit( infoTab );
104 moduleTab->addTab(coreInfo,"Core");
105 try {
106 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)");
107 coreInfo->setHtml(coreDoc);
108 } catch (pybind11::error_already_set &e) {
109 std::cerr << "Python error when trying to get documentation for openflipper module"
110 << ": " << e.what()
111 << std::endl;
112 coreInfo->setPlainText("Cannot display docs due to Python error: " + QString(e.what()));
113 }
114
115 // Collect all Python Documentation for the OpenFlipper Plugin Modules
116 for ( int i = 0 ; i < pythonPlugins.size() ; ++i ) {
117 try {
118 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)");
119 QTextEdit* edit = new QTextEdit( moduleTab );
120 moduleTab->addTab(edit,pythonPlugins[i]);
121 edit->setHtml(data);
122 } catch (pybind11::error_already_set &e) {
123 std::cerr << "Python error when trying to get documentation for plugin "
124 << pythonPlugins[i].toStdString()
125 << ": " << e.what()
126 << std::endl;
127 }
128 }
129
130#endif
131
132}
133
134
135
136void PythonWidget::runScript() {
137
138#ifdef PYTHON_ENABLED
140 interpreter->runScript(scriptWidget->toPlainText());
141#else
142 std::cerr << "OpenFlipper is not compiled with python support. Unable to execute script!" << std::endl;
143#endif
144
145}
146
147void PythonWidget::slotLocalLog(Logtype _type ,QString _logString) {
148
149 switch (_type) {
150 case LOGINFO:
151 pythonOutput->setTextColor( QColor(Qt::darkGreen) );
152 break;
153 case LOGOUT:
154 pythonOutput->setTextColor( QPalette{}.windowText().color() );
155 break;
156 case LOGWARN:
157 pythonOutput->setTextColor( QColor(160,160,0) );
158 break;
159 case LOGERR:
160 pythonOutput->setTextColor( QColor(Qt::red) );
161 break;
162 case LOGSTATUS:
163 pythonOutput->setTextColor( QColor(Qt::blue) );
164 break;
165 }
166
167 pythonOutput->append(_logString);
168}
169
170void PythonWidget::showScript(QString _script) {
171 scriptWidget->setPlainText(_script);
172}
173
174void PythonWidget::clearEditor() {
175 scriptWidget->clear();
176}
177
178void PythonWidget::loadScriptFromFile(QString _filename) {
179 QFile file(_filename);
180
181 if (!file.exists()) {
182 std::cerr << "PythonWidget::loadScriptFromFile : File " << _filename.toStdString() << " does not exist" << std::endl;
183 return;
184 }
185
186 file.open(QIODevice::ReadOnly| QFile::Text);
187
188 QTextStream in(&file);
189
190 QString script = in.readAll();
191
192 showScript(script);
193}
194
195
196
197
#define TOSTRING(x)
QSettings object containing all program settings of OpenFlipper.
Logtype
Log types for Message Window.
@ LOGERR
@ LOGWARN
@ LOGSTATUS
@ LOGINFO
@ LOGOUT
This class provides OpenFlippers Python interpreter.
bool runScript(const QString &_script)
Run a script. Output is passed to the standard logging facilities of OpenFlipper.
QString runScriptOutput(const QString &_script)
static PythonInterpreter * getInstance()
Creates or returns an instance of the interpreter.
Implementation of highlighting for Python code.