46 #include <pybind11/pybind11.h>
47 #include <pybind11/embed.h>
48 #include <OpenFlipper/PythonInterpreter/PythonInterpreter.hh>
51#include "pythonWidget.hh"
52#include "PythonSyntaxHighlighter.hh"
54#include <OpenFlipper/BasePlugin/PythonFunctionsCore.hh>
60PythonWidget::PythonWidget(QWidget *parent )
67 connect (RunButton, SIGNAL( clicked() ),
this, SLOT( runScript()) );
70 icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"window-close.png");
71 actionClose->setIcon(icon);
72 closeButton->setIcon(icon);
75 iconRun.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+
"arrow-right.png");
76 RunButton->setIcon(iconRun);
78 setWindowTitle(tr(
"%1 Python Interpreter").arg(
TOSTRING(PRODUCT_NAME)));
80 connect( actionClose , SIGNAL(triggered() ) ,
this, SLOT(hide()) );
81 connect( closeButton , SIGNAL(clicked() ) ,
this, SLOT(hide()) );
82 closeButton->setFocus();
85 pythonInfo->setAlignment(Qt::AlignLeft);
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");
92 QStringList pythonPlugins = getPythonPlugins();
94 for (
int i = 0 ; i < pythonPlugins.size() ; ++i ) {
95 pythonInfo->append(
"Module " + pythonPlugins[i] +
"\t\t providing instance " + pythonPlugins[i].toLower());
99 if ( OpenFlipper::Options::gui() )
100 connect(interpreter,SIGNAL(log(
Logtype,QString)) ,
this, SLOT(slotLocalLog(
Logtype,QString)));
103 QTextEdit* coreInfo =
new QTextEdit( infoTab );
104 moduleTab->addTab(coreInfo,
"Core");
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"
112 coreInfo->setPlainText(
"Cannot display docs due to Python error: " + QString(e.what()));
116 for (
int i = 0 ; i < pythonPlugins.size() ; ++i ) {
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]);
122 }
catch (pybind11::error_already_set &e) {
123 std::cerr <<
"Python error when trying to get documentation for plugin "
124 << pythonPlugins[i].toStdString()
136void PythonWidget::runScript() {
140 interpreter->
runScript(scriptWidget->toPlainText());
142 std::cerr <<
"OpenFlipper is not compiled with python support. Unable to execute script!" << std::endl;
147void PythonWidget::slotLocalLog(
Logtype _type ,QString _logString) {
151 pythonOutput->setTextColor( QColor(Qt::darkGreen) );
154 pythonOutput->setTextColor( QPalette{}.windowText().color() );
157 pythonOutput->setTextColor( QColor(160,160,0) );
160 pythonOutput->setTextColor( QColor(Qt::red) );
163 pythonOutput->setTextColor( QColor(Qt::blue) );
167 pythonOutput->append(_logString);
170void PythonWidget::showScript(QString _script) {
171 scriptWidget->setPlainText(_script);
174void PythonWidget::clearEditor() {
175 scriptWidget->clear();
178void PythonWidget::loadScriptFromFile(QString _filename) {
179 QFile file(_filename);
181 if (!file.exists()) {
182 std::cerr <<
"PythonWidget::loadScriptFromFile : File " << _filename.toStdString() <<
" does not exist" << std::endl;
186 file.open(QIODevice::ReadOnly| QFile::Text);
188 QTextStream in(&file);
190 QString script = in.readAll();
#define TOSTRING(x)
QSettings object containing all program settings of OpenFlipper.
Logtype
Log types for Message Window.
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.