Skip to content

Commit 91ad702

Browse files
author
wonder
committed
Made QgsPythonUtils a singleton instead of a class with all members being static.
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8516 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 361109b commit 91ad702

File tree

5 files changed

+88
-59
lines changed

5 files changed

+88
-59
lines changed

src/app/qgisapp.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,9 @@ static void customSrsValidation_(QgsSpatialRefSys* srs)
361361
#ifdef HAVE_PYTHON
362362
mSplash->showMessage(tr("Starting Python"), Qt::AlignHCenter | Qt::AlignBottom);
363363
qApp->processEvents();
364-
QgsPythonUtils::initPython(mQgisInterface);
364+
QgsPythonUtils::instance()->initPython(mQgisInterface);
365365

366-
if (!QgsPythonUtils::isEnabled())
366+
if (!QgsPythonUtils::instance()->isEnabled())
367367
mActionShowPythonDialog->setEnabled(false);
368368
#endif
369369

@@ -1708,28 +1708,30 @@ void QgisApp::restoreSessionPlugins(QString thePluginDirString)
17081708
#ifdef HAVE_PYTHON
17091709
QString pluginName, description, version;
17101710

1711-
if (QgsPythonUtils::isEnabled())
1711+
QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();
1712+
1713+
if (pythonUtils->isEnabled())
17121714
{
17131715

17141716
// check for python plugins system-wide
1715-
QStringList pluginList = QgsPythonUtils::pluginList();
1717+
QStringList pluginList = pythonUtils->pluginList();
17161718

17171719
for (int i = 0; i < pluginList.size(); i++)
17181720
{
17191721
QString packageName = pluginList[i];
17201722

17211723
// import plugin's package
1722-
if (!QgsPythonUtils::loadPlugin(packageName))
1724+
if (!pythonUtils->loadPlugin(packageName))
17231725
continue;
17241726

17251727
// get information from the plugin
17261728
// if there are some problems, don't continue with metadata retreival
1727-
pluginName = QgsPythonUtils::getPluginMetadata(packageName, "name");
1729+
pluginName = pythonUtils->getPluginMetadata(packageName, "name");
17281730
if (pluginName != "__error__")
17291731
{
1730-
description = QgsPythonUtils::getPluginMetadata(packageName, "description");
1732+
description = pythonUtils->getPluginMetadata(packageName, "description");
17311733
if (description != "__error__")
1732-
version = QgsPythonUtils::getPluginMetadata(packageName, "version");
1734+
version = pythonUtils->getPluginMetadata(packageName, "version");
17331735
}
17341736

17351737
if (pluginName == "__error__" || description == "__error__" || version == "__error__")
@@ -3881,7 +3883,9 @@ void QgisApp::showPluginManager()
38813883
void QgisApp::loadPythonPlugin(QString packageName, QString pluginName)
38823884
{
38833885
#ifdef HAVE_PYTHON
3884-
if (!QgsPythonUtils::isEnabled())
3886+
QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();
3887+
3888+
if (!pythonUtils->isEnabled())
38853889
return;
38863890

38873891
QgsDebugMsg("I should load python plugin: " + pluginName + " (package: " + packageName + ")");
@@ -3891,8 +3895,8 @@ void QgisApp::loadPythonPlugin(QString packageName, QString pluginName)
38913895
// is loaded already?
38923896
if (pRegistry->library(pluginName).isEmpty())
38933897
{
3894-
QgsPythonUtils::loadPlugin(packageName);
3895-
QgsPythonUtils::startPlugin(packageName);
3898+
pythonUtils->loadPlugin(packageName);
3899+
pythonUtils->startPlugin(packageName);
38963900

38973901
// TODO: test success
38983902

src/app/qgspluginmanager.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,26 @@ void QgsPluginManager::sortModel(int column)
119119
void QgsPluginManager::getPythonPluginDescriptions()
120120
{
121121
#ifdef HAVE_PYTHON
122-
if (!QgsPythonUtils::isEnabled())
122+
QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();
123+
124+
if (!pythonUtils->isEnabled())
123125
return;
124126

125127
// look for plugins systemwide
126-
QStringList pluginList = QgsPythonUtils::pluginList();
128+
QStringList pluginList = pythonUtils->pluginList();
127129

128130
for (int i = 0; i < pluginList.size(); i++)
129131
{
130132
QString packageName = pluginList[i];
131133

132134
// import plugin's package - skip loading it if an error occured
133-
if (!QgsPythonUtils::loadPlugin(packageName))
135+
if (!pythonUtils->loadPlugin(packageName))
134136
continue;
135137

136138
// get information from the plugin
137-
QString pluginName = QgsPythonUtils::getPluginMetadata(packageName, "name");
138-
QString description = QgsPythonUtils::getPluginMetadata(packageName, "description");
139-
QString version = QgsPythonUtils::getPluginMetadata(packageName, "version");
139+
QString pluginName = pythonUtils->getPluginMetadata(packageName, "name");
140+
QString description = pythonUtils->getPluginMetadata(packageName, "description");
141+
QString version = pythonUtils->getPluginMetadata(packageName, "version");
140142

141143
if (pluginName == "???" || description == "???" || version == "???")
142144
continue;
@@ -383,7 +385,7 @@ void QgsPluginManager::unload()
383385
{
384386
#ifdef HAVE_PYTHON
385387
QString packageName = pRegistry->library(pluginName);
386-
QgsPythonUtils::unloadPlugin(packageName);
388+
QgsPythonUtils::instance()->unloadPlugin(packageName);
387389
//disable it to the qsettings file
388390
settings.setValue("/PythonPlugins/" + packageName, false);
389391
#endif

src/app/qgspythondialog.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed()
4141
QString command = edtCmdLine->text();
4242
QString output;
4343

44+
QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();
45+
4446
// when using Py_single_input the return value will be always null
4547
// we're using custom hooks for output and exceptions to show output in console
46-
if (QgsPythonUtils::runStringUnsafe(command))
48+
if (pythonUtils->runStringUnsafe(command))
4749
{
48-
QgsPythonUtils::evalString("sys.stdout.get_and_clean_data()", output);
49-
QString result = QgsPythonUtils::getResult();
50+
pythonUtils->evalString("sys.stdout.get_and_clean_data()", output);
51+
QString result = pythonUtils->getResult();
5052
// escape the result so python objects display properly and
5153
// we can still use html output to get nicely formatted display
5254
output = escapeHtml(output) + escapeHtml(result);
@@ -57,7 +59,7 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed()
5759
else
5860
{
5961
QString className, errorText;
60-
QgsPythonUtils::getError(className, errorText);
62+
pythonUtils->getError(className, errorText);
6163

6264
output = "<font color=\"red\">" + escapeHtml(className) + ": " + escapeHtml(errorText) + "</font><br>";
6365
}
@@ -76,12 +78,12 @@ void QgsPythonDialog::showEvent(QShowEvent* event)
7678
{
7779
QDialog::showEvent(event);
7880

79-
QgsPythonUtils::installConsoleHooks();
81+
QgsPythonUtils::instance()->installConsoleHooks();
8082
}
8183

8284
void QgsPythonDialog::closeEvent(QCloseEvent* event)
8385
{
84-
QgsPythonUtils::uninstallConsoleHooks();
86+
QgsPythonUtils::instance()->uninstallConsoleHooks();
8587

8688
QDialog::closeEvent(event);
8789
}

src/app/qgspythonutils.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,25 @@
2828
#include <QStringList>
2929
#include <QDir>
3030

31-
QString QgsPythonUtils::mPluginsPath;
32-
PyObject* QgsPythonUtils::mMainModule;
33-
PyObject* QgsPythonUtils::mMainDict;
34-
bool QgsPythonUtils::mPythonEnabled = false;
31+
QgsPythonUtils* QgsPythonUtils::mInstance = NULL;
32+
33+
QgsPythonUtils::QgsPythonUtils()
34+
{
35+
mMainModule = NULL;
36+
mMainDict = NULL;
37+
mPythonEnabled = false;
38+
}
39+
40+
QgsPythonUtils::~QgsPythonUtils()
41+
{
42+
}
43+
44+
QgsPythonUtils* QgsPythonUtils::instance()
45+
{
46+
if (mInstance == NULL)
47+
mInstance = new QgsPythonUtils();
48+
return mInstance;
49+
}
3550

3651

3752
void QgsPythonUtils::initPython(QgisInterface* interface)

src/app/qgspythonutils.h

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,104 +27,110 @@ class QgisInterface;
2727

2828
/**
2929
All calls to Python functions in QGIS come here.
30+
This class is a singleton.
3031
31-
All functions here are static - it's not needed to create an instance of this class
32-
33-
For now, default path for python plugins is QgsApplication::pluginPath() + "/python"
32+
Default path for python plugins is:
33+
- QgsApplication::qgisSettingsDirPath() + "/python/plugins"
34+
- QgsApplication::pkgDataPath() + "/python/plugins"
3435
3536
*/
3637
class QgsPythonUtils
3738
{
3839
public:
3940

41+
QgsPythonUtils();
42+
43+
~QgsPythonUtils();
44+
45+
static QgsPythonUtils* instance();
46+
4047
/* general purpose functions */
4148

4249
//! initialize python and import bindings
43-
static void initPython(QgisInterface* interface);
50+
void initPython(QgisInterface* interface);
4451

4552
//! close python interpreter
46-
static void exitPython();
53+
void exitPython();
4754

4855
//! returns true if python support is ready to use (must be inited first)
49-
static bool isEnabled();
56+
bool isEnabled();
5057

5158
//! returns path where QGIS python stuff is located
52-
static QString pythonPath();
59+
QString pythonPath();
5360

5461
//! run a statement (wrapper for PyRun_String)
5562
//! this command is more advanced as enables error checking etc.
5663
//! when an exception is raised, it shows dialog with exception details
5764
//! @return true if no error occured
58-
static bool runString(const QString& command, QString msgOnError = QString());
65+
bool runString(const QString& command, QString msgOnError = QString());
5966

6067
//! run a statement, error reporting is not done
6168
//! @return true if no error occured
62-
static bool runStringUnsafe(const QString& command);
69+
bool runStringUnsafe(const QString& command);
6370

64-
static bool evalString(const QString& command, QString& result);
71+
bool evalString(const QString& command, QString& result);
6572

6673
//! @return object's type name as a string
67-
static QString getTypeAsString(PyObject* obj);
74+
QString getTypeAsString(PyObject* obj);
6875

6976
//! get information about error to the supplied arguments
7077
//! @return false if there was no python error
71-
static bool getError(QString& errorClassName, QString& errorText);
78+
bool getError(QString& errorClassName, QString& errorText);
7279

7380
//! get variable from main dictionary
74-
static QString getVariableFromMain(QString name);
81+
QString getVariableFromMain(QString name);
7582

7683
/* python console related functions */
7784

7885
//! change displayhook and excepthook
7986
//! our hooks will just save the result to special variables
8087
//! and those can be used in the program
81-
static void installConsoleHooks();
88+
void installConsoleHooks();
8289

8390
//! get back to the original settings (i.e. write output to stdout)
84-
static void uninstallConsoleHooks();
91+
void uninstallConsoleHooks();
8592

8693
//! get result from the last statement as a string
87-
static QString getResult();
94+
QString getResult();
8895

8996
/* plugins related functions */
9097

9198
//! return current path for python plugins
92-
static QString pluginsPath();
99+
QString pluginsPath();
93100

94101
//! return current path for home directory python plugins
95-
static QString homePluginsPath();
102+
QString homePluginsPath();
96103

97104
//! return list of all available python plugins
98-
static QStringList pluginList();
105+
QStringList pluginList();
99106

100107
//! load python plugin (import)
101-
static bool loadPlugin(QString packageName);
108+
bool loadPlugin(QString packageName);
102109

103110
//! start plugin: add to active plugins and call initGui()
104-
static bool startPlugin(QString packageName);
111+
bool startPlugin(QString packageName);
105112

106113
//! helper function to get some information about plugin
107114
//! @param function one of these strings: name, tpye, version, description
108-
static QString getPluginMetadata(QString pluginName, QString function);
115+
QString getPluginMetadata(QString pluginName, QString function);
109116

110117
//! unload plugin
111-
static bool unloadPlugin(QString packageName);
118+
bool unloadPlugin(QString packageName);
112119

113120
protected:
114121

115-
static void installErrorHook();
122+
void installErrorHook();
116123

117-
static QString getTraceback();
124+
QString getTraceback();
118125

119-
//! path where
120-
static QString mPluginsPath;
121-
122126
//! reference to module __main__
123-
static PyObject* mMainModule;
127+
PyObject* mMainModule;
124128

125129
//! dictionary of module __main__
126-
static PyObject* mMainDict;
130+
PyObject* mMainDict;
127131

128132
//! flag determining that python support is enabled
129-
static bool mPythonEnabled;
133+
bool mPythonEnabled;
134+
135+
static QgsPythonUtils* mInstance;
130136
};

0 commit comments

Comments
 (0)