From f4bff9a1719cc54d293453124656ea36a2590dff Mon Sep 17 00:00:00 2001 From: wonder Date: Mon, 26 May 2008 00:02:29 +0000 Subject: [PATCH] Python utilities separated to an interface and implementation. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8517 c8812cc2-4d05-0410-92ff-de0c093fc19c --- src/app/CMakeLists.txt | 5 +- src/app/qgisapp.cpp | 68 ++++++++---------- src/app/qgisapp.h | 7 +- src/app/qgspluginmanager.cpp | 36 +++++----- src/app/qgspluginmanager.h | 5 +- src/app/qgspythondialog.cpp | 16 ++--- src/app/qgspythondialog.h | 4 +- src/app/qgspythonutils.cpp | 60 ++++++++-------- src/app/qgspythonutils.h | 103 ++++++++------------------- src/app/qgspythonutilsimpl.h | 132 +++++++++++++++++++++++++++++++++++ 10 files changed, 260 insertions(+), 176 deletions(-) create mode 100644 src/app/qgspythonutilsimpl.h diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index c64af1f42b7..df025db1555 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -55,6 +55,7 @@ SET(QGIS_APP_SRCS qgspluginmanager.cpp qgspluginmetadata.cpp qgspluginregistry.cpp + qgspythondialog.cpp qgsprojectproperties.cpp qgsrasterlayerproperties.cpp qgssearchquerybuilder.cpp @@ -115,6 +116,7 @@ SET (QGIS_APP_MOC_HDRS qgsoptions.h qgspastetransformations.h qgspluginmanager.h + qgspythondialog.h qgsprojectproperties.h qgsrasterlayerproperties.h qgssearchquerybuilder.h @@ -153,8 +155,7 @@ ENDIF (POSTGRES_FOUND) # Python support IF (PYTHON_FOUND) - SET (QGIS_APP_SRCS ${QGIS_APP_SRCS} qgspythondialog.cpp qgspythonutils.cpp) - SET (QGIS_APP_MOC_HDRS ${QGIS_APP_MOC_HDRS} qgspythondialog.h) + SET (QGIS_APP_SRCS ${QGIS_APP_SRCS} qgspythonutils.cpp) ENDIF (PYTHON_FOUND) QT4_WRAP_CPP(QGIS_APP_MOC_SRCS ${QGIS_APP_MOC_HDRS}) diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 29b463d9286..b6034335631 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -167,9 +167,10 @@ #include "qgsdbsourceselect.h" #endif -#ifdef HAVE_PYTHON #include "qgspythondialog.h" #include "qgspythonutils.h" +#ifdef HAVE_PYTHON +#include "qgspythonutilsimpl.h" #endif #ifndef WIN32 @@ -304,7 +305,8 @@ static void customSrsValidation_(QgsSpatialRefSys* srs) // constructor starts here QgisApp::QgisApp(QSplashScreen *splash, QWidget * parent, Qt::WFlags fl) : QMainWindow(parent,fl), - mSplash(splash) + mSplash(splash), + mPythonUtils(NULL), mPythonConsole(NULL) { // setupUi(this); resize(640, 480); @@ -361,12 +363,15 @@ static void customSrsValidation_(QgsSpatialRefSys* srs) #ifdef HAVE_PYTHON mSplash->showMessage(tr("Starting Python"), Qt::AlignHCenter | Qt::AlignBottom); qApp->processEvents(); - QgsPythonUtils::instance()->initPython(mQgisInterface); - - if (!QgsPythonUtils::instance()->isEnabled()) - mActionShowPythonDialog->setEnabled(false); + + mPythonUtils = QgsPythonUtilsImpl::instance(); + + mPythonUtils->initPython(mQgisInterface); #endif + if (!mPythonUtils || !mPythonUtils->isEnabled()) + mActionShowPythonDialog->setEnabled(false); + // Create the plugin registry and load plugins // load any plugins that were running in the last session mSplash->showMessage(tr("Restoring loaded plugins"), Qt::AlignHCenter | Qt::AlignBottom); @@ -439,9 +444,8 @@ QgisApp::~QgisApp() delete mMapTools.mAddRing; delete mMapTools.mAddIsland; -#ifdef HAVE_PYTHON delete mPythonConsole; -#endif + delete mPythonUtils; // delete map layer registry and provider registry QgsApplication::exitQgis(); @@ -858,20 +862,18 @@ void QgisApp::createActions() connect ( mActionMapTips, SIGNAL ( triggered() ), this, SLOT ( toggleMapTips() ) ); mActionMapTips->setCheckable(true); -#ifdef HAVE_PYTHON mActionShowPythonDialog = new QAction(tr("Python console"), this); connect(mActionShowPythonDialog, SIGNAL(triggered()), this, SLOT(showPythonDialog())); - mPythonConsole = NULL; -#endif } void QgisApp::showPythonDialog() { -#ifdef HAVE_PYTHON + if (!mPythonUtils || !mPythonUtils->isEnabled()) + return; + if (mPythonConsole == NULL) - mPythonConsole = new QgsPythonDialog(mQgisInterface); + mPythonConsole = new QgsPythonDialog(mQgisInterface, mPythonUtils); mPythonConsole->show(); -#endif } void QgisApp::createActionGroups() @@ -992,9 +994,7 @@ void QgisApp::createMenus() // Plugins Menu mPluginMenu = menuBar()->addMenu(tr("&Plugins")); mPluginMenu->addAction(mActionShowPluginManager); -#ifdef HAVE_PYTHON mPluginMenu->addAction(mActionShowPythonDialog); -#endif mPluginMenu->addSeparator(); // Add the plugin manager action to it @@ -1705,33 +1705,30 @@ void QgisApp::restoreSessionPlugins(QString thePluginDirString) delete myLib; } -#ifdef HAVE_PYTHON QString pluginName, description, version; - QgsPythonUtils* pythonUtils = QgsPythonUtils::instance(); - - if (pythonUtils->isEnabled()) + if (mPythonUtils && mPythonUtils->isEnabled()) { // check for python plugins system-wide - QStringList pluginList = pythonUtils->pluginList(); + QStringList pluginList = mPythonUtils->pluginList(); for (int i = 0; i < pluginList.size(); i++) { QString packageName = pluginList[i]; // import plugin's package - if (!pythonUtils->loadPlugin(packageName)) + if (!mPythonUtils->loadPlugin(packageName)) continue; // get information from the plugin // if there are some problems, don't continue with metadata retreival - pluginName = pythonUtils->getPluginMetadata(packageName, "name"); + pluginName = mPythonUtils->getPluginMetadata(packageName, "name"); if (pluginName != "__error__") { - description = pythonUtils->getPluginMetadata(packageName, "description"); + description = mPythonUtils->getPluginMetadata(packageName, "description"); if (description != "__error__") - version = pythonUtils->getPluginMetadata(packageName, "version"); + version = mPythonUtils->getPluginMetadata(packageName, "version"); } if (pluginName == "__error__" || description == "__error__" || version == "__error__") @@ -1746,7 +1743,7 @@ void QgisApp::restoreSessionPlugins(QString thePluginDirString) } } } -#endif + QgsDebugMsg("Loading plugins completed"); QgsDebugMsg("*************************************************\n\n"); } @@ -3857,7 +3854,7 @@ void QgisApp::zoomToLayerExtent() void QgisApp::showPluginManager() { - QgsPluginManager *pm = new QgsPluginManager(this); + QgsPluginManager *pm = new QgsPluginManager(mPythonUtils, this); pm->resizeColumnsToContents(); if (pm->exec()) { @@ -3882,11 +3879,11 @@ void QgisApp::showPluginManager() void QgisApp::loadPythonPlugin(QString packageName, QString pluginName) { -#ifdef HAVE_PYTHON - QgsPythonUtils* pythonUtils = QgsPythonUtils::instance(); - - if (!pythonUtils->isEnabled()) + if (!mPythonUtils || !mPythonUtils->isEnabled()) + { + QgsDebugMsg("Python is not enabled in QGIS."); return; + } QgsDebugMsg("I should load python plugin: " + pluginName + " (package: " + packageName + ")"); @@ -3895,8 +3892,8 @@ void QgisApp::loadPythonPlugin(QString packageName, QString pluginName) // is loaded already? if (pRegistry->library(pluginName).isEmpty()) { - pythonUtils->loadPlugin(packageName); - pythonUtils->startPlugin(packageName); + mPythonUtils->loadPlugin(packageName); + mPythonUtils->startPlugin(packageName); // TODO: test success @@ -3907,11 +3904,6 @@ void QgisApp::loadPythonPlugin(QString packageName, QString pluginName) QSettings settings; settings.writeEntry("/PythonPlugins/" + packageName, true); } - - -#else - QgsDebugMsg("Python is not enabled in QGIS."); -#endif } void QgisApp::loadPlugin(QString name, QString description, QString theFullPathName) diff --git a/src/app/qgisapp.h b/src/app/qgisapp.h index be380099e82..9622896ab2f 100644 --- a/src/app/qgisapp.h +++ b/src/app/qgisapp.h @@ -50,6 +50,7 @@ class QgsMapTool; class QgsPoint; class QgsProviderRegistry; class QgsPythonDialog; +class QgsPythonUtils; class QgsRasterLayer; class QgsRect; class QgsVectorLayer; @@ -524,9 +525,8 @@ private: QAction *mActionShowAllToolbars; QAction *mActionHideAllToolbars; QAction *mActionToggleFullScreen; -#ifdef HAVE_PYTHON QAction *mActionShowPythonDialog; -#endif + // //tool groups ------------------------------------- QActionGroup *mMapToolGroup; @@ -659,9 +659,8 @@ class Tools //!flag to indicat wehter we are in fullscreen mode or not bool mFullScreenMode; -#ifdef HAVE_PYTHON QgsPythonDialog* mPythonConsole; -#endif + QgsPythonUtils* mPythonUtils; }; #endif diff --git a/src/app/qgspluginmanager.cpp b/src/app/qgspluginmanager.cpp index e1ae2533d80..5d286ff203c 100644 --- a/src/app/qgspluginmanager.cpp +++ b/src/app/qgspluginmanager.cpp @@ -39,9 +39,7 @@ #include #include -#ifdef HAVE_PYTHON #include -#endif #define TESTLIB #ifdef TESTLIB @@ -52,10 +50,13 @@ #include #endif #endif -QgsPluginManager::QgsPluginManager(QWidget * parent, Qt::WFlags fl) +QgsPluginManager::QgsPluginManager(QgsPythonUtils* pythonUtils, QWidget * parent, Qt::WFlags fl) : QDialog(parent, fl) { setupUi(this); + + mPythonUtils = pythonUtils; + // set the default lib dir to the qgis install directory/lib (this info is // available from the provider registry so we use it here) QgsProviderRegistry *pr = QgsProviderRegistry::instance(); @@ -118,27 +119,24 @@ void QgsPluginManager::sortModel(int column) void QgsPluginManager::getPythonPluginDescriptions() { -#ifdef HAVE_PYTHON - QgsPythonUtils* pythonUtils = QgsPythonUtils::instance(); - - if (!pythonUtils->isEnabled()) + if (!mPythonUtils || !mPythonUtils->isEnabled()) return; // look for plugins systemwide - QStringList pluginList = pythonUtils->pluginList(); + QStringList pluginList = mPythonUtils->pluginList(); for (int i = 0; i < pluginList.size(); i++) { QString packageName = pluginList[i]; // import plugin's package - skip loading it if an error occured - if (!pythonUtils->loadPlugin(packageName)) + if (!mPythonUtils->loadPlugin(packageName)) continue; // get information from the plugin - QString pluginName = pythonUtils->getPluginMetadata(packageName, "name"); - QString description = pythonUtils->getPluginMetadata(packageName, "description"); - QString version = pythonUtils->getPluginMetadata(packageName, "version"); + QString pluginName = mPythonUtils->getPluginMetadata(packageName, "name"); + QString description = mPythonUtils->getPluginMetadata(packageName, "description"); + QString version = mPythonUtils->getPluginMetadata(packageName, "version"); if (pluginName == "???" || description == "???" || version == "???") continue; @@ -184,7 +182,6 @@ void QgsPluginManager::getPythonPluginDescriptions() myItems << mypDetailItem << mypLibraryNameItem; mModelPlugins->appendRow(myItems); } -#endif } @@ -383,12 +380,13 @@ void QgsPluginManager::unload() QString pluginName = mModelPlugins->data(myIndex,Qt::UserRole).toString(); if (pRegistry->isPythonPlugin(pluginName)) { -#ifdef HAVE_PYTHON - QString packageName = pRegistry->library(pluginName); - QgsPythonUtils::instance()->unloadPlugin(packageName); - //disable it to the qsettings file - settings.setValue("/PythonPlugins/" + packageName, false); -#endif + if (mPythonUtils && mPythonUtils->isEnabled()) + { + QString packageName = pRegistry->library(pluginName); + mPythonUtils->unloadPlugin(packageName); + //disable it to the qsettings file + settings.setValue("/PythonPlugins/" + packageName, false); + } } else // C++ plugin { diff --git a/src/app/qgspluginmanager.h b/src/app/qgspluginmanager.h index 8563b742125..2f207507c2f 100644 --- a/src/app/qgspluginmanager.h +++ b/src/app/qgspluginmanager.h @@ -28,6 +28,7 @@ #include "qgisgui.h" class QgsPluginItem; +class QgsPythonUtils; class QTableView; /*! @@ -39,7 +40,7 @@ class QgsPluginManager : public QDialog, private Ui::QgsPluginManagerBase Q_OBJECT public: //! Constructor - QgsPluginManager(QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags); + QgsPluginManager(QgsPythonUtils* pythonUtils, QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags); //! Destructor ~QgsPluginManager(); //! Get description of plugins (name, etc) @@ -70,6 +71,8 @@ class QgsPluginManager : public QDialog, private Ui::QgsPluginManagerBase private: QStandardItemModel *mModelPlugins; QSortFilterProxyModel * mModelProxy; + + QgsPythonUtils* mPythonUtils; }; #endif diff --git a/src/app/qgspythondialog.cpp b/src/app/qgspythondialog.cpp index 2b8d941cddd..46bfe73617c 100644 --- a/src/app/qgspythondialog.cpp +++ b/src/app/qgspythondialog.cpp @@ -20,11 +20,12 @@ #include #include -QgsPythonDialog::QgsPythonDialog(QgisInterface* pIface, QWidget *parent) +QgsPythonDialog::QgsPythonDialog(QgisInterface* pIface, QgsPythonUtils* pythonUtils, QWidget *parent) : QDialog(parent) { setupUi(this); mIface = pIface; + mPythonUtils = pythonUtils; } QgsPythonDialog::~QgsPythonDialog() @@ -41,14 +42,13 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed() QString command = edtCmdLine->text(); QString output; - QgsPythonUtils* pythonUtils = QgsPythonUtils::instance(); // when using Py_single_input the return value will be always null // we're using custom hooks for output and exceptions to show output in console - if (pythonUtils->runStringUnsafe(command)) + if (mPythonUtils->runStringUnsafe(command)) { - pythonUtils->evalString("sys.stdout.get_and_clean_data()", output); - QString result = pythonUtils->getResult(); + mPythonUtils->evalString("sys.stdout.get_and_clean_data()", output); + QString result = mPythonUtils->getResult(); // escape the result so python objects display properly and // we can still use html output to get nicely formatted display output = escapeHtml(output) + escapeHtml(result); @@ -59,7 +59,7 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed() else { QString className, errorText; - pythonUtils->getError(className, errorText); + mPythonUtils->getError(className, errorText); output = "" + escapeHtml(className) + ": " + escapeHtml(errorText) + "
"; } @@ -78,12 +78,12 @@ void QgsPythonDialog::showEvent(QShowEvent* event) { QDialog::showEvent(event); - QgsPythonUtils::instance()->installConsoleHooks(); + mPythonUtils->installConsoleHooks(); } void QgsPythonDialog::closeEvent(QCloseEvent* event) { - QgsPythonUtils::instance()->uninstallConsoleHooks(); + mPythonUtils->uninstallConsoleHooks(); QDialog::closeEvent(event); } diff --git a/src/app/qgspythondialog.h b/src/app/qgspythondialog.h index b55fb5c975b..01ee00ef440 100644 --- a/src/app/qgspythondialog.h +++ b/src/app/qgspythondialog.h @@ -20,6 +20,7 @@ #include "ui_qgspythondialog.h" class QgisInterface; +class QgsPythonUtils; class QCloseEvent; class QShowEvent; @@ -28,7 +29,7 @@ class QgsPythonDialog : public QDialog, private Ui::QgsPythonDialog Q_OBJECT public: - QgsPythonDialog(QgisInterface* pIface, QWidget *parent = 0); + QgsPythonDialog(QgisInterface* pIface, QgsPythonUtils* pythonUtils, QWidget *parent = 0); ~QgsPythonDialog(); @@ -46,6 +47,7 @@ class QgsPythonDialog : public QDialog, private Ui::QgsPythonDialog private: QgisInterface* mIface; + QgsPythonUtils* mPythonUtils; }; #endif diff --git a/src/app/qgspythonutils.cpp b/src/app/qgspythonutils.cpp index b4bd28bb466..a1ede161e78 100644 --- a/src/app/qgspythonutils.cpp +++ b/src/app/qgspythonutils.cpp @@ -18,7 +18,7 @@ // otherwise issues some warnings #include -#include "qgspythonutils.h" +#include "qgspythonutilsimpl.h" #include "qgsapplication.h" #include "qgslogger.h" @@ -28,28 +28,28 @@ #include #include -QgsPythonUtils* QgsPythonUtils::mInstance = NULL; +QgsPythonUtilsImpl* QgsPythonUtilsImpl::mInstance = NULL; -QgsPythonUtils::QgsPythonUtils() +QgsPythonUtilsImpl::QgsPythonUtilsImpl() { mMainModule = NULL; mMainDict = NULL; mPythonEnabled = false; } -QgsPythonUtils::~QgsPythonUtils() +QgsPythonUtilsImpl::~QgsPythonUtilsImpl() { } -QgsPythonUtils* QgsPythonUtils::instance() +QgsPythonUtilsImpl* QgsPythonUtilsImpl::instance() { if (mInstance == NULL) - mInstance = new QgsPythonUtils(); + mInstance = new QgsPythonUtilsImpl(); return mInstance; } -void QgsPythonUtils::initPython(QgisInterface* interface) +void QgsPythonUtilsImpl::initPython(QgisInterface* interface) { // initialize python Py_Initialize(); @@ -137,7 +137,7 @@ void QgsPythonUtils::initPython(QgisInterface* interface) } -void QgsPythonUtils::exitPython() +void QgsPythonUtilsImpl::exitPython() { Py_Finalize(); mMainModule = NULL; @@ -146,17 +146,17 @@ void QgsPythonUtils::exitPython() } -bool QgsPythonUtils::isEnabled() +bool QgsPythonUtilsImpl::isEnabled() { return mPythonEnabled; } -void QgsPythonUtils::installErrorHook() +void QgsPythonUtilsImpl::installErrorHook() { runString("sys.excepthook = qgis_except_hook"); } -void QgsPythonUtils::installConsoleHooks() +void QgsPythonUtilsImpl::installConsoleHooks() { runString("sys.displayhook = console_display_hook\n"); @@ -164,20 +164,20 @@ void QgsPythonUtils::installConsoleHooks() runString("sys.stdout = QgisOutputCatcher()\n"); } -void QgsPythonUtils::uninstallConsoleHooks() +void QgsPythonUtilsImpl::uninstallConsoleHooks() { runString("sys.displayhook = sys.__displayhook__"); runString("sys.stdout = _old_stdout"); } -bool QgsPythonUtils::runStringUnsafe(const QString& command) +bool QgsPythonUtilsImpl::runStringUnsafe(const QString& command) { PyRun_String(command.toLocal8Bit().data(), Py_single_input, mMainDict, mMainDict); return (PyErr_Occurred() == 0); } -bool QgsPythonUtils::runString(const QString& command, QString msgOnError) +bool QgsPythonUtilsImpl::runString(const QString& command, QString msgOnError) { bool res = runStringUnsafe(command); if (res) @@ -208,7 +208,7 @@ bool QgsPythonUtils::runString(const QString& command, QString msgOnError) } -QString QgsPythonUtils::getTraceback() +QString QgsPythonUtilsImpl::getTraceback() { #define TRACEBACK_FETCH_ERROR(what) {errMsg = what; goto done;} @@ -280,7 +280,7 @@ done: return result; } -QString QgsPythonUtils::getTypeAsString(PyObject* obj) +QString QgsPythonUtilsImpl::getTypeAsString(PyObject* obj) { if (obj == NULL) return NULL; @@ -307,7 +307,7 @@ QString QgsPythonUtils::getTypeAsString(PyObject* obj) } } -bool QgsPythonUtils::getError(QString& errorClassName, QString& errorText) +bool QgsPythonUtilsImpl::getError(QString& errorClassName, QString& errorText) { if (!PyErr_Occurred()) return false; @@ -341,12 +341,12 @@ bool QgsPythonUtils::getError(QString& errorClassName, QString& errorText) return true; } -QString QgsPythonUtils::getResult() +QString QgsPythonUtilsImpl::getResult() { return getVariableFromMain("__result"); } -QString QgsPythonUtils::getVariableFromMain(QString name) +QString QgsPythonUtilsImpl::getVariableFromMain(QString name) { PyObject* obj; PyObject* obj_str; @@ -372,7 +372,7 @@ QString QgsPythonUtils::getVariableFromMain(QString name) return output; } -bool QgsPythonUtils::evalString(const QString& command, QString& result) +bool QgsPythonUtilsImpl::evalString(const QString& command, QString& result) { PyObject* res = PyRun_String(command.toLocal8Bit().data(), Py_eval_input, mMainDict, mMainDict); @@ -387,27 +387,27 @@ bool QgsPythonUtils::evalString(const QString& command, QString& result) } -QString QgsPythonUtils::pythonPath() +QString QgsPythonUtilsImpl::pythonPath() { return QgsApplication::pkgDataPath() + "/python"; } -QString QgsPythonUtils::pluginsPath() +QString QgsPythonUtilsImpl::pluginsPath() { return pythonPath() + "/plugins"; } -QString QgsPythonUtils::homePluginsPath() +QString QgsPythonUtilsImpl::homePluginsPath() { return QgsApplication::qgisSettingsDirPath() + "/python/plugins"; } -QStringList QgsPythonUtils::pluginList() +QStringList QgsPythonUtilsImpl::pluginList() { - QDir pluginDir(QgsPythonUtils::pluginsPath(), "*", + QDir pluginDir(QgsPythonUtilsImpl::pluginsPath(), "*", QDir::Name | QDir::IgnoreCase, QDir::Dirs | QDir::NoDotAndDotDot); - QDir homePluginDir(QgsPythonUtils::homePluginsPath(), "*", + QDir homePluginDir(QgsPythonUtilsImpl::homePluginsPath(), "*", QDir::Name | QDir::IgnoreCase, QDir::Dirs | QDir::NoDotAndDotDot); QStringList pluginList = pluginDir.entryList(); @@ -423,7 +423,7 @@ QStringList QgsPythonUtils::pluginList() return pluginList; } -QString QgsPythonUtils::getPluginMetadata(QString pluginName, QString function) +QString QgsPythonUtilsImpl::getPluginMetadata(QString pluginName, QString function) { QString command = pluginName + "." + function + "()"; QString retval = "???"; @@ -449,7 +449,7 @@ QString QgsPythonUtils::getPluginMetadata(QString pluginName, QString function) } -bool QgsPythonUtils::loadPlugin(QString packageName) +bool QgsPythonUtilsImpl::loadPlugin(QString packageName) { // load plugin's package and ensure that plugin is reloaded when changed runString( @@ -480,7 +480,7 @@ bool QgsPythonUtils::loadPlugin(QString packageName) } -bool QgsPythonUtils::startPlugin(QString packageName) +bool QgsPythonUtilsImpl::startPlugin(QString packageName) { QString pluginPythonVar = "plugins['" + packageName + "']"; @@ -499,7 +499,7 @@ bool QgsPythonUtils::startPlugin(QString packageName) } -bool QgsPythonUtils::unloadPlugin(QString packageName) +bool QgsPythonUtilsImpl::unloadPlugin(QString packageName) { // unload and delete plugin! QString varName = "plugins['" + packageName + "']"; diff --git a/src/app/qgspythonutils.h b/src/app/qgspythonutils.h index 8e0c5397af6..602d75b7418 100644 --- a/src/app/qgspythonutils.h +++ b/src/app/qgspythonutils.h @@ -1,5 +1,5 @@ /*************************************************************************** - qgspythonutils.h - routines for interfacing Python + qgspythonutils.h - abstract interface for Python routines --------------------- begin : October 2006 copyright : (C) 2006 by Martin Dobias @@ -14,14 +14,12 @@ ***************************************************************************/ /* $Id$ */ +#ifndef QGSPYTHONUTILS_H +#define QGSPYTHONUTILS_H + #include #include -// forward declaration for PyObject -#ifndef PyObject_HEAD -struct _object; -typedef _object PyObject; -#endif class QgisInterface; @@ -34,103 +32,62 @@ class QgisInterface; - QgsApplication::pkgDataPath() + "/python/plugins" */ + class QgsPythonUtils { public: - QgsPythonUtils(); + virtual ~QgsPythonUtils() {} - ~QgsPythonUtils(); - - static QgsPythonUtils* instance(); - - /* general purpose functions */ - + //! returns true if python support is ready to use (must be inited first) + virtual bool isEnabled() = 0; + //! initialize python and import bindings - void initPython(QgisInterface* interface); + virtual void initPython(QgisInterface* interface) = 0; //! close python interpreter - void exitPython(); + virtual void exitPython() = 0; - //! returns true if python support is ready to use (must be inited first) - bool isEnabled(); - - //! returns path where QGIS python stuff is located - QString pythonPath(); - - //! run a statement (wrapper for PyRun_String) - //! this command is more advanced as enables error checking etc. - //! when an exception is raised, it shows dialog with exception details - //! @return true if no error occured - bool runString(const QString& command, QString msgOnError = QString()); + /* console */ //! run a statement, error reporting is not done //! @return true if no error occured - bool runStringUnsafe(const QString& command); + virtual bool runStringUnsafe(const QString& command) = 0; - bool evalString(const QString& command, QString& result); - - //! @return object's type name as a string - QString getTypeAsString(PyObject* obj); - - //! get information about error to the supplied arguments - //! @return false if there was no python error - bool getError(QString& errorClassName, QString& errorText); - - //! get variable from main dictionary - QString getVariableFromMain(QString name); - - /* python console related functions */ + virtual bool evalString(const QString& command, QString& result) = 0; //! change displayhook and excepthook //! our hooks will just save the result to special variables //! and those can be used in the program - void installConsoleHooks(); + virtual void installConsoleHooks() = 0; //! get back to the original settings (i.e. write output to stdout) - void uninstallConsoleHooks(); + virtual void uninstallConsoleHooks() = 0; //! get result from the last statement as a string - QString getResult(); + virtual QString getResult() = 0; - /* plugins related functions */ + //! get information about error to the supplied arguments + //! @return false if there was no python error + virtual bool getError(QString& errorClassName, QString& errorText) = 0; + + /* plugins */ - //! return current path for python plugins - QString pluginsPath(); - - //! return current path for home directory python plugins - QString homePluginsPath(); - //! return list of all available python plugins - QStringList pluginList(); - + virtual QStringList pluginList() = 0; + //! load python plugin (import) - bool loadPlugin(QString packageName); + virtual bool loadPlugin(QString packageName) = 0; //! start plugin: add to active plugins and call initGui() - bool startPlugin(QString packageName); + virtual bool startPlugin(QString packageName) = 0; //! helper function to get some information about plugin //! @param function one of these strings: name, tpye, version, description - QString getPluginMetadata(QString pluginName, QString function); + virtual QString getPluginMetadata(QString pluginName, QString function) = 0; //! unload plugin - bool unloadPlugin(QString packageName); - - protected: - - void installErrorHook(); - - QString getTraceback(); - - //! reference to module __main__ - PyObject* mMainModule; - - //! dictionary of module __main__ - PyObject* mMainDict; - - //! flag determining that python support is enabled - bool mPythonEnabled; - - static QgsPythonUtils* mInstance; + virtual bool unloadPlugin(QString packageName) = 0; }; + +#endif diff --git a/src/app/qgspythonutilsimpl.h b/src/app/qgspythonutilsimpl.h new file mode 100644 index 00000000000..f9753dca0e9 --- /dev/null +++ b/src/app/qgspythonutilsimpl.h @@ -0,0 +1,132 @@ +/*************************************************************************** + qgspythonutilsimpl.h - routines for interfacing Python + --------------------- + begin : May 2008 + copyright : (C) 2008 by Martin Dobias + email : wonder.sk at gmail dot com + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +/* $Id$ */ + + +#ifndef QGSPYTHONUTILSIMPL_H +#define QGSPYTHONUTILSIMPL_H + +#include "qgspythonutils.h" + +// forward declaration for PyObject +#ifndef PyObject_HEAD +struct _object; +typedef _object PyObject; +#endif + + +class QgsPythonUtilsImpl : public QgsPythonUtils +{ + public: + + QgsPythonUtilsImpl(); + + virtual ~QgsPythonUtilsImpl(); + + static QgsPythonUtilsImpl* instance(); + + /* general purpose functions */ + + //! initialize python and import bindings + void initPython(QgisInterface* interface); + + //! close python interpreter + void exitPython(); + + //! returns true if python support is ready to use (must be inited first) + bool isEnabled(); + + //! returns path where QGIS python stuff is located + QString pythonPath(); + + //! run a statement (wrapper for PyRun_String) + //! this command is more advanced as enables error checking etc. + //! when an exception is raised, it shows dialog with exception details + //! @return true if no error occured + bool runString(const QString& command, QString msgOnError = QString()); + + //! run a statement, error reporting is not done + //! @return true if no error occured + bool runStringUnsafe(const QString& command); + + bool evalString(const QString& command, QString& result); + + //! @return object's type name as a string + QString getTypeAsString(PyObject* obj); + + //! get information about error to the supplied arguments + //! @return false if there was no python error + bool getError(QString& errorClassName, QString& errorText); + + //! get variable from main dictionary + QString getVariableFromMain(QString name); + + /* python console related functions */ + + //! change displayhook and excepthook + //! our hooks will just save the result to special variables + //! and those can be used in the program + void installConsoleHooks(); + + //! get back to the original settings (i.e. write output to stdout) + void uninstallConsoleHooks(); + + //! get result from the last statement as a string + QString getResult(); + + /* plugins related functions */ + + //! return current path for python plugins + QString pluginsPath(); + + //! return current path for home directory python plugins + QString homePluginsPath(); + + //! return list of all available python plugins + QStringList pluginList(); + + //! load python plugin (import) + bool loadPlugin(QString packageName); + + //! start plugin: add to active plugins and call initGui() + bool startPlugin(QString packageName); + + //! helper function to get some information about plugin + //! @param function one of these strings: name, tpye, version, description + QString getPluginMetadata(QString pluginName, QString function); + + //! unload plugin + bool unloadPlugin(QString packageName); + + protected: + + void installErrorHook(); + + QString getTraceback(); + + //! reference to module __main__ + PyObject* mMainModule; + + //! dictionary of module __main__ + PyObject* mMainDict; + + //! flag determining that python support is enabled + bool mPythonEnabled; + + static QgsPythonUtilsImpl* mInstance; +}; + + +#endif