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");
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);
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)");
122void PythonWidget::runScript() {
126 interpreter->
runScript(scriptWidget->toPlainText());
128 std::cerr <<
"OpenFlipper is not compiled with python support. Unable to execute script!" << std::endl;
133void PythonWidget::slotLocalLog(
Logtype _type ,QString _logString) {
137 pythonOutput->setTextColor( QColor(Qt::darkGreen) );
140 pythonOutput->setTextColor( QPalette{}.windowText().color() );
143 pythonOutput->setTextColor( QColor(160,160,0) );
146 pythonOutput->setTextColor( QColor(Qt::red) );
149 pythonOutput->setTextColor( QColor(Qt::blue) );
153 pythonOutput->append(_logString);
156void PythonWidget::showScript(QString _script) {
157 scriptWidget->setPlainText(_script);
160void PythonWidget::clearEditor() {
161 scriptWidget->clear();
164void PythonWidget::loadScriptFromFile(QString _filename) {
165 QFile file(_filename);
167 if (!file.exists()) {
168 std::cerr <<
"PythonWidget::loadScriptFromFile : File " << _filename.toStdString() <<
" does not exist" << std::endl;
172 file.open(QIODevice::ReadOnly| QFile::Text);
174 QTextStream in(&file);
176 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.