[QGIS Commit] r8517 - trunk/qgis/src/app

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sun May 25 20:02:29 EDT 2008


Author: wonder
Date: 2008-05-25 20:02:29 -0400 (Sun, 25 May 2008)
New Revision: 8517

Added:
   trunk/qgis/src/app/qgspythonutilsimpl.h
Modified:
   trunk/qgis/src/app/CMakeLists.txt
   trunk/qgis/src/app/qgisapp.cpp
   trunk/qgis/src/app/qgisapp.h
   trunk/qgis/src/app/qgspluginmanager.cpp
   trunk/qgis/src/app/qgspluginmanager.h
   trunk/qgis/src/app/qgspythondialog.cpp
   trunk/qgis/src/app/qgspythondialog.h
   trunk/qgis/src/app/qgspythonutils.cpp
   trunk/qgis/src/app/qgspythonutils.h
Log:
Python utilities separated to an interface and implementation.


Modified: trunk/qgis/src/app/CMakeLists.txt
===================================================================
--- trunk/qgis/src/app/CMakeLists.txt	2008-05-25 22:41:57 UTC (rev 8516)
+++ trunk/qgis/src/app/CMakeLists.txt	2008-05-26 00:02:29 UTC (rev 8517)
@@ -55,6 +55,7 @@
   qgspluginmanager.cpp
   qgspluginmetadata.cpp
   qgspluginregistry.cpp
+  qgspythondialog.cpp
   qgsprojectproperties.cpp
   qgsrasterlayerproperties.cpp
   qgssearchquerybuilder.cpp
@@ -115,6 +116,7 @@
   qgsoptions.h
   qgspastetransformations.h
   qgspluginmanager.h
+  qgspythondialog.h
   qgsprojectproperties.h
   qgsrasterlayerproperties.h
   qgssearchquerybuilder.h
@@ -153,8 +155,7 @@
 
 # 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})

Modified: trunk/qgis/src/app/qgisapp.cpp
===================================================================
--- trunk/qgis/src/app/qgisapp.cpp	2008-05-25 22:41:57 UTC (rev 8516)
+++ trunk/qgis/src/app/qgisapp.cpp	2008-05-26 00:02:29 UTC (rev 8517)
@@ -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 @@
 // 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,11 +363,14 @@
 #ifdef HAVE_PYTHON
   mSplash->showMessage(tr("Starting Python"), Qt::AlignHCenter | Qt::AlignBottom);
   qApp->processEvents();
-  QgsPythonUtils::instance()->initPython(mQgisInterface);
+  
+  mPythonUtils = QgsPythonUtilsImpl::instance();
+  
+  mPythonUtils->initPython(mQgisInterface);
+#endif
 
-  if (!QgsPythonUtils::instance()->isEnabled())
+  if (!mPythonUtils || !mPythonUtils->isEnabled())
     mActionShowPythonDialog->setEnabled(false);
-#endif
 
   // Create the plugin registry and load plugins
   // load any plugins that were running in the last session
@@ -439,9 +444,8 @@
   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 @@
   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 @@
   // 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 @@
     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 @@
       }
     }
   }
-#endif
+  
   QgsDebugMsg("Loading plugins completed");
   QgsDebugMsg("*************************************************\n\n");
 }
@@ -3857,7 +3854,7 @@
 
 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::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 @@
   // 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 @@
     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)

Modified: trunk/qgis/src/app/qgisapp.h
===================================================================
--- trunk/qgis/src/app/qgisapp.h	2008-05-25 22:41:57 UTC (rev 8516)
+++ trunk/qgis/src/app/qgisapp.h	2008-05-26 00:02:29 UTC (rev 8517)
@@ -50,6 +50,7 @@
 class QgsPoint;
 class QgsProviderRegistry;
 class QgsPythonDialog;
+class QgsPythonUtils;
 class QgsRasterLayer;
 class QgsRect;
 class QgsVectorLayer;
@@ -524,9 +525,8 @@
   QAction *mActionShowAllToolbars;
   QAction *mActionHideAllToolbars;
   QAction *mActionToggleFullScreen;
-#ifdef HAVE_PYTHON
   QAction *mActionShowPythonDialog;
-#endif
+  
   //
   //tool groups -------------------------------------
   QActionGroup *mMapToolGroup;
@@ -659,9 +659,8 @@
   
   //!flag to indicat wehter we are in fullscreen mode or not
   bool mFullScreenMode;
-#ifdef HAVE_PYTHON
   QgsPythonDialog* mPythonConsole;
-#endif
+  QgsPythonUtils* mPythonUtils;
 };
 
 #endif

Modified: trunk/qgis/src/app/qgspluginmanager.cpp
===================================================================
--- trunk/qgis/src/app/qgspluginmanager.cpp	2008-05-25 22:41:57 UTC (rev 8516)
+++ trunk/qgis/src/app/qgspluginmanager.cpp	2008-05-26 00:02:29 UTC (rev 8517)
@@ -39,9 +39,7 @@
 #include <qgsdetaileditemwidget.h>
 #include <qgsdetaileditemdata.h>
 
-#ifdef HAVE_PYTHON
 #include <qgspythonutils.h>
-#endif
 
 #define TESTLIB
 #ifdef TESTLIB
@@ -52,10 +50,13 @@
 #include <dlfcn.h>
 #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::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 @@
     myItems << mypDetailItem << mypLibraryNameItem;
     mModelPlugins->appendRow(myItems);
   }
-#endif
 }
 
 
@@ -383,12 +380,13 @@
       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
       {

Modified: trunk/qgis/src/app/qgspluginmanager.h
===================================================================
--- trunk/qgis/src/app/qgspluginmanager.h	2008-05-25 22:41:57 UTC (rev 8516)
+++ trunk/qgis/src/app/qgspluginmanager.h	2008-05-26 00:02:29 UTC (rev 8517)
@@ -28,6 +28,7 @@
 #include "qgisgui.h"
 
 class QgsPluginItem;
+class QgsPythonUtils;
 class QTableView;
 
 /*!
@@ -39,7 +40,7 @@
   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 @@
   private:
     QStandardItemModel *mModelPlugins;
     QSortFilterProxyModel * mModelProxy;
+    
+    QgsPythonUtils* mPythonUtils;
 };
 
 #endif

Modified: trunk/qgis/src/app/qgspythondialog.cpp
===================================================================
--- trunk/qgis/src/app/qgspythondialog.cpp	2008-05-25 22:41:57 UTC (rev 8516)
+++ trunk/qgis/src/app/qgspythondialog.cpp	2008-05-26 00:02:29 UTC (rev 8517)
@@ -20,11 +20,12 @@
 #include <QShowEvent>
 #include <QCloseEvent>
 
-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 @@
   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 @@
   else
   {
     QString className, errorText;
-    pythonUtils->getError(className, errorText);
+    mPythonUtils->getError(className, errorText);
     
     output = "<font color=\"red\">" + escapeHtml(className) + ": " + escapeHtml(errorText) + "</font><br>";
   }
@@ -78,12 +78,12 @@
 {
   QDialog::showEvent(event);
   
-  QgsPythonUtils::instance()->installConsoleHooks();
+  mPythonUtils->installConsoleHooks();
 }
 
 void QgsPythonDialog::closeEvent(QCloseEvent* event)
 {
-  QgsPythonUtils::instance()->uninstallConsoleHooks();
+  mPythonUtils->uninstallConsoleHooks();
   
   QDialog::closeEvent(event);
 }

Modified: trunk/qgis/src/app/qgspythondialog.h
===================================================================
--- trunk/qgis/src/app/qgspythondialog.h	2008-05-25 22:41:57 UTC (rev 8516)
+++ trunk/qgis/src/app/qgspythondialog.h	2008-05-26 00:02:29 UTC (rev 8517)
@@ -20,6 +20,7 @@
 #include "ui_qgspythondialog.h"
 
 class QgisInterface;
+class QgsPythonUtils;
 class QCloseEvent;
 class QShowEvent;
 
@@ -28,7 +29,7 @@
   Q_OBJECT
   
   public:
-    QgsPythonDialog(QgisInterface* pIface, QWidget *parent = 0);
+    QgsPythonDialog(QgisInterface* pIface, QgsPythonUtils* pythonUtils, QWidget *parent = 0);
     
     ~QgsPythonDialog();
 
@@ -46,6 +47,7 @@
   private:
     
     QgisInterface* mIface;
+    QgsPythonUtils* mPythonUtils;
 };
 
 #endif

Modified: trunk/qgis/src/app/qgspythonutils.cpp
===================================================================
--- trunk/qgis/src/app/qgspythonutils.cpp	2008-05-25 22:41:57 UTC (rev 8516)
+++ trunk/qgis/src/app/qgspythonutils.cpp	2008-05-26 00:02:29 UTC (rev 8517)
@@ -18,7 +18,7 @@
 // otherwise issues some warnings
 #include <Python.h>
 
-#include "qgspythonutils.h"
+#include "qgspythonutilsimpl.h"
 
 #include "qgsapplication.h"
 #include "qgslogger.h"
@@ -28,28 +28,28 @@
 #include <QStringList>
 #include <QDir>
 
-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::exitPython()
+void QgsPythonUtilsImpl::exitPython()
 {  
   Py_Finalize();
   mMainModule = NULL;
@@ -146,17 +146,17 @@
 }
 
 
-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 @@
   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 @@
 }
 
 
-QString QgsPythonUtils::getTraceback()
+QString QgsPythonUtilsImpl::getTraceback()
 {
 #define TRACEBACK_FETCH_ERROR(what) {errMsg = what; goto done;}
 
@@ -280,7 +280,7 @@
   return result;
 }
 
-QString QgsPythonUtils::getTypeAsString(PyObject* obj)
+QString QgsPythonUtilsImpl::getTypeAsString(PyObject* obj)
 {
   if (obj == NULL)
     return NULL;
@@ -307,7 +307,7 @@
   }
 }
 
-bool QgsPythonUtils::getError(QString& errorClassName, QString& errorText)
+bool QgsPythonUtilsImpl::getError(QString& errorClassName, QString& errorText)
 {
   if (!PyErr_Occurred())
     return false;
@@ -341,12 +341,12 @@
   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 @@
   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 @@
 }
 
 
-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 @@
   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 @@
 }
 
 
-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::startPlugin(QString packageName)
+bool QgsPythonUtilsImpl::startPlugin(QString packageName)
 {
   QString pluginPythonVar = "plugins['" + packageName + "']";
   
@@ -499,7 +499,7 @@
 }
 
 
-bool QgsPythonUtils::unloadPlugin(QString packageName)
+bool QgsPythonUtilsImpl::unloadPlugin(QString packageName)
 {
   // unload and delete plugin!
   QString varName = "plugins['" + packageName + "']";

Modified: trunk/qgis/src/app/qgspythonutils.h
===================================================================
--- trunk/qgis/src/app/qgspythonutils.h	2008-05-25 22:41:57 UTC (rev 8516)
+++ trunk/qgis/src/app/qgspythonutils.h	2008-05-26 00:02:29 UTC (rev 8517)
@@ -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 <QString>
 #include <QStringList>
 
-// forward declaration for PyObject
-#ifndef PyObject_HEAD
-struct _object;
-typedef _object PyObject;
-#endif
 
 class QgisInterface;
 
@@ -34,103 +32,62 @@
  - 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();
+    /* console */
     
-    //! 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);
+    virtual bool runStringUnsafe(const QString& command) = 0;
     
-    bool evalString(const QString& command, QString& result);
+    virtual bool evalString(const QString& command, QString& result) = 0;
     
-    //! @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();
+    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;
     
-    //! return current path for python plugins
-    QString pluginsPath();
-
-    //! return current path for home directory python plugins
-    QString homePluginsPath();
-
+    /* plugins */
+    
     //! 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

Added: trunk/qgis/src/app/qgspythonutilsimpl.h
===================================================================
--- trunk/qgis/src/app/qgspythonutilsimpl.h	                        (rev 0)
+++ trunk/qgis/src/app/qgspythonutilsimpl.h	2008-05-26 00:02:29 UTC (rev 8517)
@@ -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



More information about the QGIS-commit mailing list