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

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sun May 25 18:41:57 EDT 2008


Author: wonder
Date: 2008-05-25 18:41:57 -0400 (Sun, 25 May 2008)
New Revision: 8516

Modified:
   trunk/qgis/src/app/qgisapp.cpp
   trunk/qgis/src/app/qgspluginmanager.cpp
   trunk/qgis/src/app/qgspythondialog.cpp
   trunk/qgis/src/app/qgspythonutils.cpp
   trunk/qgis/src/app/qgspythonutils.h
Log:
Made QgsPythonUtils a singleton instead of a class with all members being static.


Modified: trunk/qgis/src/app/qgisapp.cpp
===================================================================
--- trunk/qgis/src/app/qgisapp.cpp	2008-05-25 14:06:21 UTC (rev 8515)
+++ trunk/qgis/src/app/qgisapp.cpp	2008-05-25 22:41:57 UTC (rev 8516)
@@ -361,9 +361,9 @@
 #ifdef HAVE_PYTHON
   mSplash->showMessage(tr("Starting Python"), Qt::AlignHCenter | Qt::AlignBottom);
   qApp->processEvents();
-  QgsPythonUtils::initPython(mQgisInterface);
+  QgsPythonUtils::instance()->initPython(mQgisInterface);
 
-  if (!QgsPythonUtils::isEnabled())
+  if (!QgsPythonUtils::instance()->isEnabled())
     mActionShowPythonDialog->setEnabled(false);
 #endif
 
@@ -1708,28 +1708,30 @@
 #ifdef HAVE_PYTHON
   QString pluginName, description, version;
   
-  if (QgsPythonUtils::isEnabled())
+  QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();
+  
+  if (pythonUtils->isEnabled())
   {
   
     // check for python plugins system-wide
-    QStringList pluginList = QgsPythonUtils::pluginList();
+    QStringList pluginList = pythonUtils->pluginList();
 
     for (int i = 0; i < pluginList.size(); i++)
     {
       QString packageName = pluginList[i];
    
       // import plugin's package
-      if (!QgsPythonUtils::loadPlugin(packageName))
+      if (!pythonUtils->loadPlugin(packageName))
         continue;
       
       // get information from the plugin
       // if there are some problems, don't continue with metadata retreival
-      pluginName = QgsPythonUtils::getPluginMetadata(packageName, "name");
+      pluginName = pythonUtils->getPluginMetadata(packageName, "name");
       if (pluginName != "__error__")
       {
-        description = QgsPythonUtils::getPluginMetadata(packageName, "description");
+        description = pythonUtils->getPluginMetadata(packageName, "description");
         if (description != "__error__")
-          version = QgsPythonUtils::getPluginMetadata(packageName, "version");
+          version = pythonUtils->getPluginMetadata(packageName, "version");
       }
       
       if (pluginName == "__error__" || description == "__error__" || version == "__error__")
@@ -3881,7 +3883,9 @@
 void QgisApp::loadPythonPlugin(QString packageName, QString pluginName)
 {
 #ifdef HAVE_PYTHON
-  if (!QgsPythonUtils::isEnabled())
+  QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();
+  
+  if (!pythonUtils->isEnabled())
     return;
   
   QgsDebugMsg("I should load python plugin: " + pluginName + " (package: " + packageName + ")");
@@ -3891,8 +3895,8 @@
   // is loaded already?
   if (pRegistry->library(pluginName).isEmpty())
   {
-    QgsPythonUtils::loadPlugin(packageName);
-    QgsPythonUtils::startPlugin(packageName);
+    pythonUtils->loadPlugin(packageName);
+    pythonUtils->startPlugin(packageName);
   
     // TODO: test success
   

Modified: trunk/qgis/src/app/qgspluginmanager.cpp
===================================================================
--- trunk/qgis/src/app/qgspluginmanager.cpp	2008-05-25 14:06:21 UTC (rev 8515)
+++ trunk/qgis/src/app/qgspluginmanager.cpp	2008-05-25 22:41:57 UTC (rev 8516)
@@ -119,24 +119,26 @@
 void QgsPluginManager::getPythonPluginDescriptions()
 {
 #ifdef HAVE_PYTHON
-  if (!QgsPythonUtils::isEnabled())
+  QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();
+  
+  if (!pythonUtils->isEnabled())
     return;
   
   // look for plugins systemwide
-  QStringList pluginList = QgsPythonUtils::pluginList();
+  QStringList pluginList = pythonUtils->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 (!QgsPythonUtils::loadPlugin(packageName))
+    if (!pythonUtils->loadPlugin(packageName))
       continue;
     
     // get information from the plugin
-    QString pluginName  = QgsPythonUtils::getPluginMetadata(packageName, "name");
-    QString description = QgsPythonUtils::getPluginMetadata(packageName, "description");
-    QString version     = QgsPythonUtils::getPluginMetadata(packageName, "version");
+    QString pluginName  = pythonUtils->getPluginMetadata(packageName, "name");
+    QString description = pythonUtils->getPluginMetadata(packageName, "description");
+    QString version     = pythonUtils->getPluginMetadata(packageName, "version");
     
     if (pluginName == "???" || description == "???" || version == "???")
       continue;
@@ -383,7 +385,7 @@
       {
 #ifdef HAVE_PYTHON
         QString packageName = pRegistry->library(pluginName);
-        QgsPythonUtils::unloadPlugin(packageName);
+        QgsPythonUtils::instance()->unloadPlugin(packageName);
           //disable it to the qsettings file
         settings.setValue("/PythonPlugins/" + packageName, false);
 #endif

Modified: trunk/qgis/src/app/qgspythondialog.cpp
===================================================================
--- trunk/qgis/src/app/qgspythondialog.cpp	2008-05-25 14:06:21 UTC (rev 8515)
+++ trunk/qgis/src/app/qgspythondialog.cpp	2008-05-25 22:41:57 UTC (rev 8516)
@@ -41,12 +41,14 @@
   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 (QgsPythonUtils::runStringUnsafe(command))
+  if (pythonUtils->runStringUnsafe(command))
   {
-    QgsPythonUtils::evalString("sys.stdout.get_and_clean_data()", output);
-    QString result = QgsPythonUtils::getResult();
+    pythonUtils->evalString("sys.stdout.get_and_clean_data()", output);
+    QString result = pythonUtils->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);
@@ -57,7 +59,7 @@
   else
   {
     QString className, errorText;
-    QgsPythonUtils::getError(className, errorText);
+    pythonUtils->getError(className, errorText);
     
     output = "<font color=\"red\">" + escapeHtml(className) + ": " + escapeHtml(errorText) + "</font><br>";
   }
@@ -76,12 +78,12 @@
 {
   QDialog::showEvent(event);
   
-  QgsPythonUtils::installConsoleHooks();
+  QgsPythonUtils::instance()->installConsoleHooks();
 }
 
 void QgsPythonDialog::closeEvent(QCloseEvent* event)
 {
-  QgsPythonUtils::uninstallConsoleHooks();
+  QgsPythonUtils::instance()->uninstallConsoleHooks();
   
   QDialog::closeEvent(event);
 }

Modified: trunk/qgis/src/app/qgspythonutils.cpp
===================================================================
--- trunk/qgis/src/app/qgspythonutils.cpp	2008-05-25 14:06:21 UTC (rev 8515)
+++ trunk/qgis/src/app/qgspythonutils.cpp	2008-05-25 22:41:57 UTC (rev 8516)
@@ -28,12 +28,27 @@
 #include <QStringList>
 #include <QDir>
 
-QString QgsPythonUtils::mPluginsPath;
-PyObject* QgsPythonUtils::mMainModule;
-PyObject* QgsPythonUtils::mMainDict;
-bool QgsPythonUtils::mPythonEnabled = false;
+QgsPythonUtils* QgsPythonUtils::mInstance = NULL;
 
+QgsPythonUtils::QgsPythonUtils()
+{
+  mMainModule = NULL;
+  mMainDict = NULL;
+  mPythonEnabled = false;
+}
 
+QgsPythonUtils::~QgsPythonUtils()
+{
+}
+
+QgsPythonUtils* QgsPythonUtils::instance()
+{
+  if (mInstance == NULL)
+    mInstance = new QgsPythonUtils();
+  return mInstance;
+}
+
+
 void QgsPythonUtils::initPython(QgisInterface* interface)
 {
   // initialize python

Modified: trunk/qgis/src/app/qgspythonutils.h
===================================================================
--- trunk/qgis/src/app/qgspythonutils.h	2008-05-25 14:06:21 UTC (rev 8515)
+++ trunk/qgis/src/app/qgspythonutils.h	2008-05-25 22:41:57 UTC (rev 8516)
@@ -27,104 +27,110 @@
 
 /**
  All calls to Python functions in QGIS come here.
+ This class is a singleton.
  
- All functions here are static - it's not needed to create an instance of this class
+ Default path for python plugins is:
+ - QgsApplication::qgisSettingsDirPath() + "/python/plugins"
+ - QgsApplication::pkgDataPath() + "/python/plugins"
 
- For now, default path for python plugins is QgsApplication::pluginPath() + "/python"
-
  */
 class QgsPythonUtils
 {
   public:
     
+    QgsPythonUtils();
+    
+    ~QgsPythonUtils();
+        
+    static QgsPythonUtils* instance();
+    
     /* general purpose functions */
 
     //! initialize python and import bindings
-    static void initPython(QgisInterface* interface);
+    void initPython(QgisInterface* interface);
     
     //! close python interpreter
-    static void exitPython();
+    void exitPython();
 
     //! returns true if python support is ready to use (must be inited first)
-    static bool isEnabled();
+    bool isEnabled();
     
     //! returns path where QGIS python stuff is located
-    static QString pythonPath();
+    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
-    static bool runString(const QString& command, QString msgOnError = QString());
+    bool runString(const QString& command, QString msgOnError = QString());
     
     //! run a statement, error reporting is not done
     //! @return true if no error occured
-    static bool runStringUnsafe(const QString& command);
+    bool runStringUnsafe(const QString& command);
     
-    static bool evalString(const QString& command, QString& result);
+    bool evalString(const QString& command, QString& result);
     
     //! @return object's type name as a string
-    static QString getTypeAsString(PyObject* obj);
+    QString getTypeAsString(PyObject* obj);
 
     //! get information about error to the supplied arguments
     //! @return false if there was no python error
-    static bool getError(QString& errorClassName, QString& errorText);
+    bool getError(QString& errorClassName, QString& errorText);
     
     //! get variable from main dictionary
-    static QString getVariableFromMain(QString name);
+    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
-    static void installConsoleHooks();
+    void installConsoleHooks();
     
     //! get back to the original settings (i.e. write output to stdout)
-    static void uninstallConsoleHooks();
+    void uninstallConsoleHooks();
     
     //! get result from the last statement as a string
-    static QString getResult();
+    QString getResult();
 
     /* plugins related functions */
     
     //! return current path for python plugins
-    static QString pluginsPath();
+    QString pluginsPath();
 
     //! return current path for home directory python plugins
-    static QString homePluginsPath();
+    QString homePluginsPath();
 
     //! return list of all available python plugins
-    static QStringList pluginList();
+    QStringList pluginList();
         
     //! load python plugin (import)
-    static bool loadPlugin(QString packageName);
+    bool loadPlugin(QString packageName);
     
     //! start plugin: add to active plugins and call initGui()
-    static bool startPlugin(QString packageName);
+    bool startPlugin(QString packageName);
     
     //! helper function to get some information about plugin
     //! @param function one of these strings: name, tpye, version, description
-    static QString getPluginMetadata(QString pluginName, QString function);
+    QString getPluginMetadata(QString pluginName, QString function);
 
     //! unload plugin
-    static bool unloadPlugin(QString packageName);
+    bool unloadPlugin(QString packageName);
 
   protected:
     
-    static void installErrorHook();
+    void installErrorHook();
 
-    static QString getTraceback();
+    QString getTraceback();
 
-    //! path where 
-    static QString mPluginsPath;
-    
     //! reference to module __main__
-    static PyObject* mMainModule;
+    PyObject* mMainModule;
     
     //! dictionary of module __main__
-    static PyObject* mMainDict;
+    PyObject* mMainDict;
     
     //! flag determining that python support is enabled
-    static bool mPythonEnabled;
+    bool mPythonEnabled;
+    
+    static QgsPythonUtils* mInstance;
 };



More information about the QGIS-commit mailing list