Ejemplo n.º 1
0
  void PythonEngine::loadScript(const QString &filename)
  {
    QFileInfo info(filename);
    initializePython(info.canonicalPath());
    PythonThread pt;

    PythonScript *script = new PythonScript(filename);
    m_identifier = script->identifier();

    if(script->module()) {
      // make sure there is an Engine class defined
      if (PyObject_HasAttrString(script->module().ptr(), "Engine")) {

        try {
          prepareToCatchError();
          // instatiate the new Engine
          m_instance = script->module().attr("Engine")();
        } catch (error_already_set const &) {
          catchError();
          return;
        }

        m_script = script;

      } else {
        delete script;
        PythonError::instance()->append(tr("PythonEngine: checking ") + filename + "...");
        PythonError::instance()->append(tr("  - script has no 'Engine' class defined"));
      }
    } else {
      delete script;
      PythonError::instance()->append(tr("PythonEngine: checking ") + filename + "...");
      PythonError::instance()->append(tr("  - no module"));
    }
  }
Ejemplo n.º 2
0
  QWidget* PythonTool::settingsWidget()
  {
    if (!m_script)
      return 0; // nothing we can do -- we don't have any real scripts

    PythonThread pt;

    if(!m_settingsWidget)
    {
      m_settingsWidget = new QWidget();
      m_settingsWidget->setLayout( new QVBoxLayout() );

      if (PyObject_HasAttrString(m_instance.ptr(), "settingsWidget")) {
        try {
          prepareToCatchError();
          QWidget *widget = extract<QWidget*>(m_instance.attr("settingsWidget")());
          if (widget)
            m_settingsWidget->layout()->addWidget(widget);
        } catch (error_already_set const &) {
          catchError();
        }
      }

      connect(m_settingsWidget, SIGNAL(destroyed()), this, SLOT(settingsWidgetDestroyed()));
    }

    return m_settingsWidget;
  }
Ejemplo n.º 3
0
  QString PythonTool::name() const
  {
    PythonThread pt;
    if (!PyObject_HasAttrString(m_instance.ptr(), "name"))
      return tr("Unknown Python Tool");

    try {
      prepareToCatchError();
      const char *name = extract<const char*>(m_instance.attr("name")());
      return QString(name);
    } catch(error_already_set const &) {
      catchError();
      return tr("Unknown Python Tool");
    }
  }
Ejemplo n.º 4
0
  QString PythonTool::description() const
  {
    PythonThread pt;
    if (!PyObject_HasAttrString(m_instance.ptr(), "description"))
      return tr("N/A");

    try {
      prepareToCatchError();
      const char *desc = extract<const char*>(m_instance.attr("description")());
      return QString(desc);
    } catch(error_already_set const &) {
      catchError();
      return tr("N/A");
    }
  }
Ejemplo n.º 5
0
  double PythonEngine::transparencyDepth() const
  {
    if (!m_script)
      return 0.0; // nothing we can do
    
    PythonThread pt;

    try {
      prepareToCatchError();
      // return transparencyDepth from python script if the function is defined
      if (PyObject_HasAttrString(m_instance.ptr(), "transparencyDepth"))
        return extract<double>(m_instance.attr("transparencyDepth")());
    } catch(error_already_set const &) {
      catchError();
    }

    return 0.0;
  }
Ejemplo n.º 6
0
  PythonTool::PythonTool(QObject *parent, const QString &filename) : Tool(parent), 
      m_script(0), m_settingsWidget(0)
  {
    loadScript(filename);

    QAction *action = activateAction();
    action->setIcon(QIcon(QString::fromUtf8(":/python/python.png")));

    PythonThread pt;
    if (PyObject_HasAttrString(m_instance.ptr(), "toolTip")) {
      try {
        prepareToCatchError();
        const char *toolTip = extract<const char*>(m_instance.attr("toolTip")());
        action->setToolTip(QString(toolTip));
      } catch(error_already_set const &) {
        catchError();
      }
    }
  }
Ejemplo n.º 7
0
  void PythonTool::loadScript(const QString &filename)
  {
    QFileInfo info(filename);
    initializePython(info.canonicalPath());
    
    PythonThread pt;

    PythonScript *script = new PythonScript(filename);
    m_identifier = script->identifier();

    if(script->module()) {
      // make sure there is a Tool class defined
      if (PyObject_HasAttrString(script->module().ptr(), "Tool")) {
        try {
          prepareToCatchError();
          // instantiate the new tool
          m_instance = script->module().attr("Tool")();
          // if we have a settings widget already, add the python content...
          if (m_settingsWidget) {
            if (PyObject_HasAttrString(m_instance.ptr(), "settingsWidget")) {
              QWidget *widget = extract<QWidget*>(m_instance.attr("settingsWidget")());
              if (widget)
                m_settingsWidget->layout()->addWidget(widget);
            }
          }
        } catch (error_already_set const &) {
          catchError();
          return;
        }

        m_script = script;

      } else {
        delete script;
        PythonError::instance()->append(tr("PythonTool: checking ") + filename + "...");
        PythonError::instance()->append(tr("  - script has no 'Tool' class defined"));
      }
    } else {
      delete script;
      PythonError::instance()->append(tr("PythonTool: checking ") + filename + "...");
      PythonError::instance()->append(tr("  - no module"));
    }
  }
Ejemplo n.º 8
0
  bool PythonTool::paint(GLWidget *widget)
  {
    PythonThread pt;
    if (!PyObject_HasAttrString(m_instance.ptr(), "paint"))
      return false;

    try {
      prepareToCatchError();
      boost::python::reference_existing_object::apply<GLWidget*>::type converter;
      PyObject *obj = converter(widget);
      object real_obj = object(handle<>(obj));

      m_instance.attr("paint")(real_obj);
    } catch(error_already_set const &) {
      catchError();
    }

    return true;
  }
Ejemplo n.º 9
0
  Engine::Layers PythonEngine::layers() const
  {
    if (!m_script)
      return Engine::Opaque; // nothing we can do

    PythonThread pt;

    try {
      prepareToCatchError();
      // return layers from python script if the function is defined
      if (PyObject_HasAttrString(m_instance.ptr(), "layers"))
        return extract<Engine::Layers>(m_instance.attr("layers")());
    } catch(error_already_set const &) {
      catchError();
    }

    // return NoFlags, don't print an error, don't want to overwhelm new users with errors
    return Engine::Opaque;
  }
Ejemplo n.º 10
0
  bool PythonEngine::renderOpaque(PainterDevice *pd)
  {
    PythonThread pt;
    if (!m_script)
      return false; // nothing we can do

    try {
      prepareToCatchError();
      boost::python::reference_existing_object::apply<PainterDevice*>::type converter;
      PyObject *obj = converter(pd);
      object real_obj = object(handle<>(obj));

      m_instance.attr("renderOpaque")(real_obj);
    } catch(error_already_set const &) {
      catchError();
    }

    return true;
  }
Ejemplo n.º 11
0
  object PythonScript::module() const
  {
    QFileInfo fileInfo(m_fileName);

    if(fileInfo.lastModified() > m_lastModified)
    {
      try
      {
        prepareToCatchError();
        m_module = object(handle<>(PyImport_ReloadModule(m_module.ptr())));
      }
      catch(error_already_set const &)
      {
        catchError();
      }

      m_lastModified = fileInfo.lastModified();
    }

    return m_module;
  }
Ejemplo n.º 12
0
 PythonScript::PythonScript(const QString &fileName)
 {
   m_fileName = fileName;
   QFileInfo info(fileName);
   m_moduleName = info.baseName();
   m_lastModified = info.lastModified();
    
   try
   {
     prepareToCatchError();
     // try to import the module
     m_module = import(m_moduleName.toAscii().data());
     // import doesn't really reload the module if it was already loaded
     // to be save, we always reload it
     m_module = object(handle<>(PyImport_ReloadModule(m_module.ptr())));
   }
   catch(error_already_set const &)
   {
     catchError();
   }
 }
Ejemplo n.º 13
0
  void PythonEngine::readSettings(QSettings &settings)
  {
    Engine::readSettings(settings);

    if (!m_script)
      return;

    if (!PyObject_HasAttrString(m_instance.ptr(), "readSettings"))
      return;

    try {
      prepareToCatchError();

      boost::python::return_by_value::apply<QSettings*>::type qconverter;
      PyObject *qobj = qconverter(&settings);
      object real_qobj = object(handle<>(qobj));

      m_instance.attr("readSettings")(real_qobj);
    } catch(error_already_set const &) {
      catchError();
    }
  }
Ejemplo n.º 14
0
  QUndoCommand* PythonTool::wheelEvent(GLWidget *widget, QWheelEvent *event)
  {
    PythonThread pt;
    if (!PyObject_HasAttrString(m_instance.ptr(), "wheelEvent"))
      return 0;

    try {
      prepareToCatchError();
      boost::python::reference_existing_object::apply<GLWidget*>::type converter;
      PyObject *obj = converter(widget);
      object real_obj = object(handle<>(obj));

      boost::python::return_by_value::apply<QWheelEvent*>::type qconverter;
      PyObject *qobj = qconverter(event);
      object real_qobj = object(handle<>(qobj));

      return extract<QUndoCommand*>(m_instance.attr("wheelEvent")(real_obj, real_qobj));
    } catch(error_already_set const &) {
      catchError();
    }

    return 0;
  }