pythonWidget.cc 6.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
*                                                                            *
\*===========================================================================*/


43 44
// If OpenFlipper builds with python support, we can integrate the interpreter.
// Otherwise we build only a dummy widget with no interpreter backend.
45
#ifdef PYTHON_ENABLED
46 47 48
  #include <pybind11/include/pybind11/pybind11.h>
  #include <pybind11/include/pybind11/embed.h>
  #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 56 57
#include <iostream>
#include <QObject>

58

59 60 61 62 63
PythonWidget::PythonWidget(QWidget *parent )
    : QMainWindow(parent)
{
  setupUi(this);

64
  PythonSyntaxHighlighter *pythonHighlighter = new PythonSyntaxHighlighter(scriptWidget->document());
65 66 67

  connect (RunButton, SIGNAL( clicked() ), this, SLOT( runScript()) );

68 69 70 71 72 73 74 75 76 77 78 79 80 81
  QIcon icon;
  icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"window-close.png");
  actionClose->setIcon(icon);
  closeButton->setIcon(icon);

  QIcon iconRun;
  iconRun.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"arrow-right.png");
  RunButton->setIcon(iconRun);

  setWindowTitle(tr("%1 Python Interpreter").arg(TOSTRING(PRODUCT_NAME)));

  connect( actionClose , SIGNAL(triggered() ) , this, SLOT(hide()) );
  connect( closeButton , SIGNAL(clicked()   ) , this, SLOT(hide()) );
  closeButton->setFocus();
82

83
#ifdef PYTHON_ENABLED
Jan Möbius's avatar
Jan Möbius committed
84 85
  pythonInfo->setAlignment(Qt::AlignLeft);

86 87 88 89

  pythonInfo->append("Each module is automatically loaded by the core with the name given below.");
  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");
  pythonInfo->append("Available plugins with Python support:\n");
90 91 92 93

  QStringList pythonPlugins = getPythonPlugins();

  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 99 100
  if ( OpenFlipper::Options::gui() )
    connect(interpreter,SIGNAL(log(Logtype,QString)) , this, SLOT(slotLocalLog(Logtype,QString)));

101 102

  QTextEdit* coreInfo = new QTextEdit( infoTab );
103
  moduleTab->addTab(coreInfo,"Core");
104 105 106 107 108
  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)");
  coreInfo->setHtml(coreDoc);

  // Collect all Python Documentation for the OpenFlipper Plugin Modules
  for ( int i = 0 ; i < pythonPlugins.size() ; ++i ) {
109 110
    QTextEdit* edit = new QTextEdit( moduleTab );
    moduleTab->addTab(edit,pythonPlugins[i]);
111 112 113 114
    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)");
    edit->setHtml(data);
  }

115
#endif
116

117 118 119 120 121 122
}



void PythonWidget::runScript() {

123
#ifdef PYTHON_ENABLED
124 125
  PythonInterpreter* interpreter = PythonInterpreter::getInstance();
  interpreter->runScript(scriptWidget->toPlainText());
126 127
#else
  std::cerr << "OpenFlipper is not compiled with python support. Unable to execute script!" << std::endl;
128
#endif
129 130 131

}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
void PythonWidget::slotLocalLog(Logtype _type ,QString _logString) {

  switch (_type) {
    case LOGINFO:
      pythonOutput->setTextColor( QColor(Qt::darkGreen) );
      break;
    case LOGOUT:
      pythonOutput->setTextColor( QPalette{}.windowText().color() );
      break;
    case LOGWARN:
      pythonOutput->setTextColor( QColor(160,160,0) );
      break;
    case LOGERR:
      pythonOutput->setTextColor( QColor(Qt::red) );
      break;
    case LOGSTATUS:
      pythonOutput->setTextColor( QColor(Qt::blue) );
      break;
  }

  pythonOutput->append(_logString);
}

155 156 157 158 159