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 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)");
106 coreInfo->setHtml(coreDoc);
107
108 // Collect all Python Documentation for the OpenFlipper Plugin Modules
109 for ( int i = 0 ; i < pythonPlugins.size() ; ++i ) {
110 QTextEdit* edit = new QTextEdit( moduleTab );
111 moduleTab->addTab(edit,pythonPlugins[i]);
112 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)");
113 edit->setHtml(data);
114 }
115
116#endif
117
118}
119
120
121
122void PythonWidget::runScript() {
123
124#ifdef PYTHON_ENABLED
126 interpreter->runScript(scriptWidget->toPlainText());
127#else
128 std::cerr << "OpenFlipper is not compiled with python support. Unable to execute script!" << std::endl;
129#endif
130
131}
132
133void PythonWidget::slotLocalLog(Logtype _type ,QString _logString) {
134
135 switch (_type) {
136 case LOGINFO:
137 pythonOutput->setTextColor( QColor(Qt::darkGreen) );
138 break;
139 case LOGOUT:
140 pythonOutput->setTextColor( QPalette{}.windowText().color() );
141 break;
142 case LOGWARN:
143 pythonOutput->setTextColor( QColor(160,160,0) );
144 break;
145 case LOGERR:
146 pythonOutput->setTextColor( QColor(Qt::red) );
147 break;
148 case LOGSTATUS:
149 pythonOutput->setTextColor( QColor(Qt::blue) );
150 break;
151 }
152
153 pythonOutput->append(_logString);
154}
155
156void PythonWidget::showScript(QString _script) {
157 scriptWidget->setPlainText(_script);
158}
159
160void PythonWidget::clearEditor() {
161 scriptWidget->clear();
162}
163
164void PythonWidget::loadScriptFromFile(QString _filename) {
165 QFile file(_filename);
166
167 if (!file.exists()) {
168 std::cerr << "PythonWidget::loadScriptFromFile : File " << _filename.toStdString() << " does not exist" << std::endl;
169 return;
170 }
171
172 file.open(QIODevice::ReadOnly| QFile::Text);
173
174 QTextStream in(&file);
175
176 QString script = in.readAll();
177
178 showScript(script);
179}
180
181
182
183
#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.