Skip to content

Commit f4bff9a

Browse files
author
wonder
committedMay 26, 2008
Python utilities separated to an interface and implementation.
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8517 c8812cc2-4d05-0410-92ff-de0c093fc19c

10 files changed

+259
-175
lines changed
 

‎src/app/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ SET(QGIS_APP_SRCS
5555
qgspluginmanager.cpp
5656
qgspluginmetadata.cpp
5757
qgspluginregistry.cpp
58+
qgspythondialog.cpp
5859
qgsprojectproperties.cpp
5960
qgsrasterlayerproperties.cpp
6061
qgssearchquerybuilder.cpp
@@ -115,6 +116,7 @@ SET (QGIS_APP_MOC_HDRS
115116
qgsoptions.h
116117
qgspastetransformations.h
117118
qgspluginmanager.h
119+
qgspythondialog.h
118120
qgsprojectproperties.h
119121
qgsrasterlayerproperties.h
120122
qgssearchquerybuilder.h
@@ -153,8 +155,7 @@ ENDIF (POSTGRES_FOUND)
153155

154156
# Python support
155157
IF (PYTHON_FOUND)
156-
SET (QGIS_APP_SRCS ${QGIS_APP_SRCS} qgspythondialog.cpp qgspythonutils.cpp)
157-
SET (QGIS_APP_MOC_HDRS ${QGIS_APP_MOC_HDRS} qgspythondialog.h)
158+
SET (QGIS_APP_SRCS ${QGIS_APP_SRCS} qgspythonutils.cpp)
158159
ENDIF (PYTHON_FOUND)
159160

160161
QT4_WRAP_CPP(QGIS_APP_MOC_SRCS ${QGIS_APP_MOC_HDRS})

‎src/app/qgisapp.cpp

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@
167167
#include "qgsdbsourceselect.h"
168168
#endif
169169

170-
#ifdef HAVE_PYTHON
171170
#include "qgspythondialog.h"
172171
#include "qgspythonutils.h"
172+
#ifdef HAVE_PYTHON
173+
#include "qgspythonutilsimpl.h"
173174
#endif
174175

175176
#ifndef WIN32
@@ -304,7 +305,8 @@ static void customSrsValidation_(QgsSpatialRefSys* srs)
304305
// constructor starts here
305306
QgisApp::QgisApp(QSplashScreen *splash, QWidget * parent, Qt::WFlags fl)
306307
: QMainWindow(parent,fl),
307-
mSplash(splash)
308+
mSplash(splash),
309+
mPythonUtils(NULL), mPythonConsole(NULL)
308310
{
309311
// setupUi(this);
310312
resize(640, 480);
@@ -361,11 +363,14 @@ static void customSrsValidation_(QgsSpatialRefSys* srs)
361363
#ifdef HAVE_PYTHON
362364
mSplash->showMessage(tr("Starting Python"), Qt::AlignHCenter | Qt::AlignBottom);
363365
qApp->processEvents();
364-
QgsPythonUtils::instance()->initPython(mQgisInterface);
366+
367+
mPythonUtils = QgsPythonUtilsImpl::instance();
368+
369+
mPythonUtils->initPython(mQgisInterface);
370+
#endif
365371

366-
if (!QgsPythonUtils::instance()->isEnabled())
372+
if (!mPythonUtils || !mPythonUtils->isEnabled())
367373
mActionShowPythonDialog->setEnabled(false);
368-
#endif
369374

370375
// Create the plugin registry and load plugins
371376
// load any plugins that were running in the last session
@@ -439,9 +444,8 @@ QgisApp::~QgisApp()
439444
delete mMapTools.mAddRing;
440445
delete mMapTools.mAddIsland;
441446

442-
#ifdef HAVE_PYTHON
443447
delete mPythonConsole;
444-
#endif
448+
delete mPythonUtils;
445449

446450
// delete map layer registry and provider registry
447451
QgsApplication::exitQgis();
@@ -858,20 +862,18 @@ void QgisApp::createActions()
858862
connect ( mActionMapTips, SIGNAL ( triggered() ), this, SLOT ( toggleMapTips() ) );
859863
mActionMapTips->setCheckable(true);
860864

861-
#ifdef HAVE_PYTHON
862865
mActionShowPythonDialog = new QAction(tr("Python console"), this);
863866
connect(mActionShowPythonDialog, SIGNAL(triggered()), this, SLOT(showPythonDialog()));
864-
mPythonConsole = NULL;
865-
#endif
866867
}
867868

868869
void QgisApp::showPythonDialog()
869870
{
870-
#ifdef HAVE_PYTHON
871+
if (!mPythonUtils || !mPythonUtils->isEnabled())
872+
return;
873+
871874
if (mPythonConsole == NULL)
872-
mPythonConsole = new QgsPythonDialog(mQgisInterface);
875+
mPythonConsole = new QgsPythonDialog(mQgisInterface, mPythonUtils);
873876
mPythonConsole->show();
874-
#endif
875877
}
876878

877879
void QgisApp::createActionGroups()
@@ -992,9 +994,7 @@ void QgisApp::createMenus()
992994
// Plugins Menu
993995
mPluginMenu = menuBar()->addMenu(tr("&Plugins"));
994996
mPluginMenu->addAction(mActionShowPluginManager);
995-
#ifdef HAVE_PYTHON
996997
mPluginMenu->addAction(mActionShowPythonDialog);
997-
#endif
998998
mPluginMenu->addSeparator();
999999

10001000
// Add the plugin manager action to it
@@ -1705,33 +1705,30 @@ void QgisApp::restoreSessionPlugins(QString thePluginDirString)
17051705
delete myLib;
17061706
}
17071707

1708-
#ifdef HAVE_PYTHON
17091708
QString pluginName, description, version;
17101709

1711-
QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();
1712-
1713-
if (pythonUtils->isEnabled())
1710+
if (mPythonUtils && mPythonUtils->isEnabled())
17141711
{
17151712

17161713
// check for python plugins system-wide
1717-
QStringList pluginList = pythonUtils->pluginList();
1714+
QStringList pluginList = mPythonUtils->pluginList();
17181715

17191716
for (int i = 0; i < pluginList.size(); i++)
17201717
{
17211718
QString packageName = pluginList[i];
17221719

17231720
// import plugin's package
1724-
if (!pythonUtils->loadPlugin(packageName))
1721+
if (!mPythonUtils->loadPlugin(packageName))
17251722
continue;
17261723

17271724
// get information from the plugin
17281725
// if there are some problems, don't continue with metadata retreival
1729-
pluginName = pythonUtils->getPluginMetadata(packageName, "name");
1726+
pluginName = mPythonUtils->getPluginMetadata(packageName, "name");
17301727
if (pluginName != "__error__")
17311728
{
1732-
description = pythonUtils->getPluginMetadata(packageName, "description");
1729+
description = mPythonUtils->getPluginMetadata(packageName, "description");
17331730
if (description != "__error__")
1734-
version = pythonUtils->getPluginMetadata(packageName, "version");
1731+
version = mPythonUtils->getPluginMetadata(packageName, "version");
17351732
}
17361733

17371734
if (pluginName == "__error__" || description == "__error__" || version == "__error__")
@@ -1746,7 +1743,7 @@ void QgisApp::restoreSessionPlugins(QString thePluginDirString)
17461743
}
17471744
}
17481745
}
1749-
#endif
1746+
17501747
QgsDebugMsg("Loading plugins completed");
17511748
QgsDebugMsg("*************************************************\n\n");
17521749
}
@@ -3857,7 +3854,7 @@ void QgisApp::zoomToLayerExtent()
38573854

38583855
void QgisApp::showPluginManager()
38593856
{
3860-
QgsPluginManager *pm = new QgsPluginManager(this);
3857+
QgsPluginManager *pm = new QgsPluginManager(mPythonUtils, this);
38613858
pm->resizeColumnsToContents();
38623859
if (pm->exec())
38633860
{
@@ -3882,11 +3879,11 @@ void QgisApp::showPluginManager()
38823879

38833880
void QgisApp::loadPythonPlugin(QString packageName, QString pluginName)
38843881
{
3885-
#ifdef HAVE_PYTHON
3886-
QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();
3887-
3888-
if (!pythonUtils->isEnabled())
3882+
if (!mPythonUtils || !mPythonUtils->isEnabled())
3883+
{
3884+
QgsDebugMsg("Python is not enabled in QGIS.");
38893885
return;
3886+
}
38903887

38913888
QgsDebugMsg("I should load python plugin: " + pluginName + " (package: " + packageName + ")");
38923889

@@ -3895,8 +3892,8 @@ void QgisApp::loadPythonPlugin(QString packageName, QString pluginName)
38953892
// is loaded already?
38963893
if (pRegistry->library(pluginName).isEmpty())
38973894
{
3898-
pythonUtils->loadPlugin(packageName);
3899-
pythonUtils->startPlugin(packageName);
3895+
mPythonUtils->loadPlugin(packageName);
3896+
mPythonUtils->startPlugin(packageName);
39003897

39013898
// TODO: test success
39023899

@@ -3907,11 +3904,6 @@ void QgisApp::loadPythonPlugin(QString packageName, QString pluginName)
39073904
QSettings settings;
39083905
settings.writeEntry("/PythonPlugins/" + packageName, true);
39093906
}
3910-
3911-
3912-
#else
3913-
QgsDebugMsg("Python is not enabled in QGIS.");
3914-
#endif
39153907
}
39163908

39173909
void QgisApp::loadPlugin(QString name, QString description, QString theFullPathName)

‎src/app/qgisapp.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class QgsMapTool;
5050
class QgsPoint;
5151
class QgsProviderRegistry;
5252
class QgsPythonDialog;
53+
class QgsPythonUtils;
5354
class QgsRasterLayer;
5455
class QgsRect;
5556
class QgsVectorLayer;
@@ -524,9 +525,8 @@ public slots:
524525
QAction *mActionShowAllToolbars;
525526
QAction *mActionHideAllToolbars;
526527
QAction *mActionToggleFullScreen;
527-
#ifdef HAVE_PYTHON
528528
QAction *mActionShowPythonDialog;
529-
#endif
529+
530530
//
531531
//tool groups -------------------------------------
532532
QActionGroup *mMapToolGroup;
@@ -659,9 +659,8 @@ class Tools
659659

660660
//!flag to indicat wehter we are in fullscreen mode or not
661661
bool mFullScreenMode;
662-
#ifdef HAVE_PYTHON
663662
QgsPythonDialog* mPythonConsole;
664-
#endif
663+
QgsPythonUtils* mPythonUtils;
665664
};
666665

667666
#endif

‎src/app/qgspluginmanager.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939
#include <qgsdetaileditemwidget.h>
4040
#include <qgsdetaileditemdata.h>
4141

42-
#ifdef HAVE_PYTHON
4342
#include <qgspythonutils.h>
44-
#endif
4543

4644
#define TESTLIB
4745
#ifdef TESTLIB
@@ -52,10 +50,13 @@
5250
#include <dlfcn.h>
5351
#endif
5452
#endif
55-
QgsPluginManager::QgsPluginManager(QWidget * parent, Qt::WFlags fl)
53+
QgsPluginManager::QgsPluginManager(QgsPythonUtils* pythonUtils, QWidget * parent, Qt::WFlags fl)
5654
: QDialog(parent, fl)
5755
{
5856
setupUi(this);
57+
58+
mPythonUtils = pythonUtils;
59+
5960
// set the default lib dir to the qgis install directory/lib (this info is
6061
// available from the provider registry so we use it here)
6162
QgsProviderRegistry *pr = QgsProviderRegistry::instance();
@@ -118,27 +119,24 @@ void QgsPluginManager::sortModel(int column)
118119

119120
void QgsPluginManager::getPythonPluginDescriptions()
120121
{
121-
#ifdef HAVE_PYTHON
122-
QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();
123-
124-
if (!pythonUtils->isEnabled())
122+
if (!mPythonUtils || !mPythonUtils->isEnabled())
125123
return;
126124

127125
// look for plugins systemwide
128-
QStringList pluginList = pythonUtils->pluginList();
126+
QStringList pluginList = mPythonUtils->pluginList();
129127

130128
for (int i = 0; i < pluginList.size(); i++)
131129
{
132130
QString packageName = pluginList[i];
133131

134132
// import plugin's package - skip loading it if an error occured
135-
if (!pythonUtils->loadPlugin(packageName))
133+
if (!mPythonUtils->loadPlugin(packageName))
136134
continue;
137135

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

143141
if (pluginName == "???" || description == "???" || version == "???")
144142
continue;
@@ -184,7 +182,6 @@ void QgsPluginManager::getPythonPluginDescriptions()
184182
myItems << mypDetailItem << mypLibraryNameItem;
185183
mModelPlugins->appendRow(myItems);
186184
}
187-
#endif
188185
}
189186

190187

@@ -383,12 +380,13 @@ void QgsPluginManager::unload()
383380
QString pluginName = mModelPlugins->data(myIndex,Qt::UserRole).toString();
384381
if (pRegistry->isPythonPlugin(pluginName))
385382
{
386-
#ifdef HAVE_PYTHON
387-
QString packageName = pRegistry->library(pluginName);
388-
QgsPythonUtils::instance()->unloadPlugin(packageName);
389-
//disable it to the qsettings file
390-
settings.setValue("/PythonPlugins/" + packageName, false);
391-
#endif
383+
if (mPythonUtils && mPythonUtils->isEnabled())
384+
{
385+
QString packageName = pRegistry->library(pluginName);
386+
mPythonUtils->unloadPlugin(packageName);
387+
//disable it to the qsettings file
388+
settings.setValue("/PythonPlugins/" + packageName, false);
389+
}
392390
}
393391
else // C++ plugin
394392
{

‎src/app/qgspluginmanager.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "qgisgui.h"
2929

3030
class QgsPluginItem;
31+
class QgsPythonUtils;
3132
class QTableView;
3233

3334
/*!
@@ -39,7 +40,7 @@ class QgsPluginManager : public QDialog, private Ui::QgsPluginManagerBase
3940
Q_OBJECT
4041
public:
4142
//! Constructor
42-
QgsPluginManager(QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags);
43+
QgsPluginManager(QgsPythonUtils* pythonUtils, QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags);
4344
//! Destructor
4445
~QgsPluginManager();
4546
//! Get description of plugins (name, etc)
@@ -70,6 +71,8 @@ class QgsPluginManager : public QDialog, private Ui::QgsPluginManagerBase
7071
private:
7172
QStandardItemModel *mModelPlugins;
7273
QSortFilterProxyModel * mModelProxy;
74+
75+
QgsPythonUtils* mPythonUtils;
7376
};
7477

7578
#endif

‎src/app/qgspythondialog.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
#include <QShowEvent>
2121
#include <QCloseEvent>
2222

23-
QgsPythonDialog::QgsPythonDialog(QgisInterface* pIface, QWidget *parent)
23+
QgsPythonDialog::QgsPythonDialog(QgisInterface* pIface, QgsPythonUtils* pythonUtils, QWidget *parent)
2424
: QDialog(parent)
2525
{
2626
setupUi(this);
2727
mIface = pIface;
28+
mPythonUtils = pythonUtils;
2829
}
2930

3031
QgsPythonDialog::~QgsPythonDialog()
@@ -41,14 +42,13 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed()
4142
QString command = edtCmdLine->text();
4243
QString output;
4344

44-
QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();
4545

4646
// when using Py_single_input the return value will be always null
4747
// we're using custom hooks for output and exceptions to show output in console
48-
if (pythonUtils->runStringUnsafe(command))
48+
if (mPythonUtils->runStringUnsafe(command))
4949
{
50-
pythonUtils->evalString("sys.stdout.get_and_clean_data()", output);
51-
QString result = pythonUtils->getResult();
50+
mPythonUtils->evalString("sys.stdout.get_and_clean_data()", output);
51+
QString result = mPythonUtils->getResult();
5252
// escape the result so python objects display properly and
5353
// we can still use html output to get nicely formatted display
5454
output = escapeHtml(output) + escapeHtml(result);
@@ -59,7 +59,7 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed()
5959
else
6060
{
6161
QString className, errorText;
62-
pythonUtils->getError(className, errorText);
62+
mPythonUtils->getError(className, errorText);
6363

6464
output = "<font color=\"red\">" + escapeHtml(className) + ": " + escapeHtml(errorText) + "</font><br>";
6565
}
@@ -78,12 +78,12 @@ void QgsPythonDialog::showEvent(QShowEvent* event)
7878
{
7979
QDialog::showEvent(event);
8080

81-
QgsPythonUtils::instance()->installConsoleHooks();
81+
mPythonUtils->installConsoleHooks();
8282
}
8383

8484
void QgsPythonDialog::closeEvent(QCloseEvent* event)
8585
{
86-
QgsPythonUtils::instance()->uninstallConsoleHooks();
86+
mPythonUtils->uninstallConsoleHooks();
8787

8888
QDialog::closeEvent(event);
8989
}

‎src/app/qgspythondialog.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "ui_qgspythondialog.h"
2121

2222
class QgisInterface;
23+
class QgsPythonUtils;
2324
class QCloseEvent;
2425
class QShowEvent;
2526

@@ -28,7 +29,7 @@ class QgsPythonDialog : public QDialog, private Ui::QgsPythonDialog
2829
Q_OBJECT
2930

3031
public:
31-
QgsPythonDialog(QgisInterface* pIface, QWidget *parent = 0);
32+
QgsPythonDialog(QgisInterface* pIface, QgsPythonUtils* pythonUtils, QWidget *parent = 0);
3233

3334
~QgsPythonDialog();
3435

@@ -46,6 +47,7 @@ class QgsPythonDialog : public QDialog, private Ui::QgsPythonDialog
4647
private:
4748

4849
QgisInterface* mIface;
50+
QgsPythonUtils* mPythonUtils;
4951
};
5052

5153
#endif

‎src/app/qgspythonutils.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// otherwise issues some warnings
1919
#include <Python.h>
2020

21-
#include "qgspythonutils.h"
21+
#include "qgspythonutilsimpl.h"
2222

2323
#include "qgsapplication.h"
2424
#include "qgslogger.h"
@@ -28,28 +28,28 @@
2828
#include <QStringList>
2929
#include <QDir>
3030

31-
QgsPythonUtils* QgsPythonUtils::mInstance = NULL;
31+
QgsPythonUtilsImpl* QgsPythonUtilsImpl::mInstance = NULL;
3232

33-
QgsPythonUtils::QgsPythonUtils()
33+
QgsPythonUtilsImpl::QgsPythonUtilsImpl()
3434
{
3535
mMainModule = NULL;
3636
mMainDict = NULL;
3737
mPythonEnabled = false;
3838
}
3939

40-
QgsPythonUtils::~QgsPythonUtils()
40+
QgsPythonUtilsImpl::~QgsPythonUtilsImpl()
4141
{
4242
}
4343

44-
QgsPythonUtils* QgsPythonUtils::instance()
44+
QgsPythonUtilsImpl* QgsPythonUtilsImpl::instance()
4545
{
4646
if (mInstance == NULL)
47-
mInstance = new QgsPythonUtils();
47+
mInstance = new QgsPythonUtilsImpl();
4848
return mInstance;
4949
}
5050

5151

52-
void QgsPythonUtils::initPython(QgisInterface* interface)
52+
void QgsPythonUtilsImpl::initPython(QgisInterface* interface)
5353
{
5454
// initialize python
5555
Py_Initialize();
@@ -137,7 +137,7 @@ void QgsPythonUtils::initPython(QgisInterface* interface)
137137

138138
}
139139

140-
void QgsPythonUtils::exitPython()
140+
void QgsPythonUtilsImpl::exitPython()
141141
{
142142
Py_Finalize();
143143
mMainModule = NULL;
@@ -146,38 +146,38 @@ void QgsPythonUtils::exitPython()
146146
}
147147

148148

149-
bool QgsPythonUtils::isEnabled()
149+
bool QgsPythonUtilsImpl::isEnabled()
150150
{
151151
return mPythonEnabled;
152152
}
153153

154-
void QgsPythonUtils::installErrorHook()
154+
void QgsPythonUtilsImpl::installErrorHook()
155155
{
156156
runString("sys.excepthook = qgis_except_hook");
157157
}
158158

159-
void QgsPythonUtils::installConsoleHooks()
159+
void QgsPythonUtilsImpl::installConsoleHooks()
160160
{
161161
runString("sys.displayhook = console_display_hook\n");
162162

163163
runString("_old_stdout = sys.stdout\n");
164164
runString("sys.stdout = QgisOutputCatcher()\n");
165165
}
166166

167-
void QgsPythonUtils::uninstallConsoleHooks()
167+
void QgsPythonUtilsImpl::uninstallConsoleHooks()
168168
{
169169
runString("sys.displayhook = sys.__displayhook__");
170170
runString("sys.stdout = _old_stdout");
171171
}
172172

173173

174-
bool QgsPythonUtils::runStringUnsafe(const QString& command)
174+
bool QgsPythonUtilsImpl::runStringUnsafe(const QString& command)
175175
{
176176
PyRun_String(command.toLocal8Bit().data(), Py_single_input, mMainDict, mMainDict);
177177
return (PyErr_Occurred() == 0);
178178
}
179179

180-
bool QgsPythonUtils::runString(const QString& command, QString msgOnError)
180+
bool QgsPythonUtilsImpl::runString(const QString& command, QString msgOnError)
181181
{
182182
bool res = runStringUnsafe(command);
183183
if (res)
@@ -208,7 +208,7 @@ bool QgsPythonUtils::runString(const QString& command, QString msgOnError)
208208
}
209209

210210

211-
QString QgsPythonUtils::getTraceback()
211+
QString QgsPythonUtilsImpl::getTraceback()
212212
{
213213
#define TRACEBACK_FETCH_ERROR(what) {errMsg = what; goto done;}
214214

@@ -280,7 +280,7 @@ QString QgsPythonUtils::getTraceback()
280280
return result;
281281
}
282282

283-
QString QgsPythonUtils::getTypeAsString(PyObject* obj)
283+
QString QgsPythonUtilsImpl::getTypeAsString(PyObject* obj)
284284
{
285285
if (obj == NULL)
286286
return NULL;
@@ -307,7 +307,7 @@ QString QgsPythonUtils::getTypeAsString(PyObject* obj)
307307
}
308308
}
309309

310-
bool QgsPythonUtils::getError(QString& errorClassName, QString& errorText)
310+
bool QgsPythonUtilsImpl::getError(QString& errorClassName, QString& errorText)
311311
{
312312
if (!PyErr_Occurred())
313313
return false;
@@ -341,12 +341,12 @@ bool QgsPythonUtils::getError(QString& errorClassName, QString& errorText)
341341
return true;
342342
}
343343

344-
QString QgsPythonUtils::getResult()
344+
QString QgsPythonUtilsImpl::getResult()
345345
{
346346
return getVariableFromMain("__result");
347347
}
348348

349-
QString QgsPythonUtils::getVariableFromMain(QString name)
349+
QString QgsPythonUtilsImpl::getVariableFromMain(QString name)
350350
{
351351
PyObject* obj;
352352
PyObject* obj_str;
@@ -372,7 +372,7 @@ QString QgsPythonUtils::getVariableFromMain(QString name)
372372
return output;
373373
}
374374

375-
bool QgsPythonUtils::evalString(const QString& command, QString& result)
375+
bool QgsPythonUtilsImpl::evalString(const QString& command, QString& result)
376376
{
377377
PyObject* res = PyRun_String(command.toLocal8Bit().data(), Py_eval_input, mMainDict, mMainDict);
378378

@@ -387,27 +387,27 @@ bool QgsPythonUtils::evalString(const QString& command, QString& result)
387387
}
388388

389389

390-
QString QgsPythonUtils::pythonPath()
390+
QString QgsPythonUtilsImpl::pythonPath()
391391
{
392392
return QgsApplication::pkgDataPath() + "/python";
393393
}
394394

395-
QString QgsPythonUtils::pluginsPath()
395+
QString QgsPythonUtilsImpl::pluginsPath()
396396
{
397397
return pythonPath() + "/plugins";
398398
}
399399

400-
QString QgsPythonUtils::homePluginsPath()
400+
QString QgsPythonUtilsImpl::homePluginsPath()
401401
{
402402
return QgsApplication::qgisSettingsDirPath() + "/python/plugins";
403403
}
404404

405-
QStringList QgsPythonUtils::pluginList()
405+
QStringList QgsPythonUtilsImpl::pluginList()
406406
{
407-
QDir pluginDir(QgsPythonUtils::pluginsPath(), "*",
407+
QDir pluginDir(QgsPythonUtilsImpl::pluginsPath(), "*",
408408
QDir::Name | QDir::IgnoreCase, QDir::Dirs | QDir::NoDotAndDotDot);
409409

410-
QDir homePluginDir(QgsPythonUtils::homePluginsPath(), "*",
410+
QDir homePluginDir(QgsPythonUtilsImpl::homePluginsPath(), "*",
411411
QDir::Name | QDir::IgnoreCase, QDir::Dirs | QDir::NoDotAndDotDot);
412412

413413
QStringList pluginList = pluginDir.entryList();
@@ -423,7 +423,7 @@ QStringList QgsPythonUtils::pluginList()
423423
return pluginList;
424424
}
425425

426-
QString QgsPythonUtils::getPluginMetadata(QString pluginName, QString function)
426+
QString QgsPythonUtilsImpl::getPluginMetadata(QString pluginName, QString function)
427427
{
428428
QString command = pluginName + "." + function + "()";
429429
QString retval = "???";
@@ -449,7 +449,7 @@ QString QgsPythonUtils::getPluginMetadata(QString pluginName, QString function)
449449
}
450450

451451

452-
bool QgsPythonUtils::loadPlugin(QString packageName)
452+
bool QgsPythonUtilsImpl::loadPlugin(QString packageName)
453453
{
454454
// load plugin's package and ensure that plugin is reloaded when changed
455455
runString(
@@ -480,7 +480,7 @@ bool QgsPythonUtils::loadPlugin(QString packageName)
480480
}
481481

482482

483-
bool QgsPythonUtils::startPlugin(QString packageName)
483+
bool QgsPythonUtilsImpl::startPlugin(QString packageName)
484484
{
485485
QString pluginPythonVar = "plugins['" + packageName + "']";
486486

@@ -499,7 +499,7 @@ bool QgsPythonUtils::startPlugin(QString packageName)
499499
}
500500

501501

502-
bool QgsPythonUtils::unloadPlugin(QString packageName)
502+
bool QgsPythonUtilsImpl::unloadPlugin(QString packageName)
503503
{
504504
// unload and delete plugin!
505505
QString varName = "plugins['" + packageName + "']";

‎src/app/qgspythonutils.h

Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***************************************************************************
2-
qgspythonutils.h - routines for interfacing Python
2+
qgspythonutils.h - abstract interface for Python routines
33
---------------------
44
begin : October 2006
55
copyright : (C) 2006 by Martin Dobias
@@ -14,14 +14,12 @@
1414
***************************************************************************/
1515
/* $Id$ */
1616

17+
#ifndef QGSPYTHONUTILS_H
18+
#define QGSPYTHONUTILS_H
19+
1720
#include <QString>
1821
#include <QStringList>
1922

20-
// forward declaration for PyObject
21-
#ifndef PyObject_HEAD
22-
struct _object;
23-
typedef _object PyObject;
24-
#endif
2523

2624
class QgisInterface;
2725

@@ -34,103 +32,62 @@ class QgisInterface;
3432
- QgsApplication::pkgDataPath() + "/python/plugins"
3533
3634
*/
35+
3736
class QgsPythonUtils
3837
{
3938
public:
4039

41-
QgsPythonUtils();
42-
43-
~QgsPythonUtils();
44-
45-
static QgsPythonUtils* instance();
40+
virtual ~QgsPythonUtils() {}
4641

47-
/* general purpose functions */
48-
42+
//! returns true if python support is ready to use (must be inited first)
43+
virtual bool isEnabled() = 0;
44+
4945
//! initialize python and import bindings
50-
void initPython(QgisInterface* interface);
46+
virtual void initPython(QgisInterface* interface) = 0;
5147

5248
//! close python interpreter
53-
void exitPython();
49+
virtual void exitPython() = 0;
5450

55-
//! returns true if python support is ready to use (must be inited first)
56-
bool isEnabled();
57-
58-
//! returns path where QGIS python stuff is located
59-
QString pythonPath();
60-
61-
//! run a statement (wrapper for PyRun_String)
62-
//! this command is more advanced as enables error checking etc.
63-
//! when an exception is raised, it shows dialog with exception details
64-
//! @return true if no error occured
65-
bool runString(const QString& command, QString msgOnError = QString());
51+
/* console */
6652

6753
//! run a statement, error reporting is not done
6854
//! @return true if no error occured
69-
bool runStringUnsafe(const QString& command);
70-
71-
bool evalString(const QString& command, QString& result);
72-
73-
//! @return object's type name as a string
74-
QString getTypeAsString(PyObject* obj);
75-
76-
//! get information about error to the supplied arguments
77-
//! @return false if there was no python error
78-
bool getError(QString& errorClassName, QString& errorText);
55+
virtual bool runStringUnsafe(const QString& command) = 0;
7956

80-
//! get variable from main dictionary
81-
QString getVariableFromMain(QString name);
82-
83-
/* python console related functions */
57+
virtual bool evalString(const QString& command, QString& result) = 0;
8458

8559
//! change displayhook and excepthook
8660
//! our hooks will just save the result to special variables
8761
//! and those can be used in the program
88-
void installConsoleHooks();
62+
virtual void installConsoleHooks() = 0;
8963

9064
//! get back to the original settings (i.e. write output to stdout)
91-
void uninstallConsoleHooks();
65+
virtual void uninstallConsoleHooks() = 0;
9266

9367
//! get result from the last statement as a string
94-
QString getResult();
68+
virtual QString getResult() = 0;
9569

96-
/* plugins related functions */
70+
//! get information about error to the supplied arguments
71+
//! @return false if there was no python error
72+
virtual bool getError(QString& errorClassName, QString& errorText) = 0;
73+
74+
/* plugins */
9775

98-
//! return current path for python plugins
99-
QString pluginsPath();
100-
101-
//! return current path for home directory python plugins
102-
QString homePluginsPath();
103-
10476
//! return list of all available python plugins
105-
QStringList pluginList();
106-
77+
virtual QStringList pluginList() = 0;
78+
10779
//! load python plugin (import)
108-
bool loadPlugin(QString packageName);
80+
virtual bool loadPlugin(QString packageName) = 0;
10981

11082
//! start plugin: add to active plugins and call initGui()
111-
bool startPlugin(QString packageName);
83+
virtual bool startPlugin(QString packageName) = 0;
11284

11385
//! helper function to get some information about plugin
11486
//! @param function one of these strings: name, tpye, version, description
115-
QString getPluginMetadata(QString pluginName, QString function);
87+
virtual QString getPluginMetadata(QString pluginName, QString function) = 0;
11688

11789
//! unload plugin
118-
bool unloadPlugin(QString packageName);
119-
120-
protected:
121-
122-
void installErrorHook();
123-
124-
QString getTraceback();
125-
126-
//! reference to module __main__
127-
PyObject* mMainModule;
128-
129-
//! dictionary of module __main__
130-
PyObject* mMainDict;
131-
132-
//! flag determining that python support is enabled
133-
bool mPythonEnabled;
134-
135-
static QgsPythonUtils* mInstance;
90+
virtual bool unloadPlugin(QString packageName) = 0;
13691
};
92+
93+
#endif

‎src/app/qgspythonutilsimpl.h

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/***************************************************************************
2+
qgspythonutilsimpl.h - routines for interfacing Python
3+
---------------------
4+
begin : May 2008
5+
copyright : (C) 2008 by Martin Dobias
6+
email : wonder.sk at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
/* $Id$ */
16+
17+
18+
#ifndef QGSPYTHONUTILSIMPL_H
19+
#define QGSPYTHONUTILSIMPL_H
20+
21+
#include "qgspythonutils.h"
22+
23+
// forward declaration for PyObject
24+
#ifndef PyObject_HEAD
25+
struct _object;
26+
typedef _object PyObject;
27+
#endif
28+
29+
30+
class QgsPythonUtilsImpl : public QgsPythonUtils
31+
{
32+
public:
33+
34+
QgsPythonUtilsImpl();
35+
36+
virtual ~QgsPythonUtilsImpl();
37+
38+
static QgsPythonUtilsImpl* instance();
39+
40+
/* general purpose functions */
41+
42+
//! initialize python and import bindings
43+
void initPython(QgisInterface* interface);
44+
45+
//! close python interpreter
46+
void exitPython();
47+
48+
//! returns true if python support is ready to use (must be inited first)
49+
bool isEnabled();
50+
51+
//! returns path where QGIS python stuff is located
52+
QString pythonPath();
53+
54+
//! run a statement (wrapper for PyRun_String)
55+
//! this command is more advanced as enables error checking etc.
56+
//! when an exception is raised, it shows dialog with exception details
57+
//! @return true if no error occured
58+
bool runString(const QString& command, QString msgOnError = QString());
59+
60+
//! run a statement, error reporting is not done
61+
//! @return true if no error occured
62+
bool runStringUnsafe(const QString& command);
63+
64+
bool evalString(const QString& command, QString& result);
65+
66+
//! @return object's type name as a string
67+
QString getTypeAsString(PyObject* obj);
68+
69+
//! get information about error to the supplied arguments
70+
//! @return false if there was no python error
71+
bool getError(QString& errorClassName, QString& errorText);
72+
73+
//! get variable from main dictionary
74+
QString getVariableFromMain(QString name);
75+
76+
/* python console related functions */
77+
78+
//! change displayhook and excepthook
79+
//! our hooks will just save the result to special variables
80+
//! and those can be used in the program
81+
void installConsoleHooks();
82+
83+
//! get back to the original settings (i.e. write output to stdout)
84+
void uninstallConsoleHooks();
85+
86+
//! get result from the last statement as a string
87+
QString getResult();
88+
89+
/* plugins related functions */
90+
91+
//! return current path for python plugins
92+
QString pluginsPath();
93+
94+
//! return current path for home directory python plugins
95+
QString homePluginsPath();
96+
97+
//! return list of all available python plugins
98+
QStringList pluginList();
99+
100+
//! load python plugin (import)
101+
bool loadPlugin(QString packageName);
102+
103+
//! start plugin: add to active plugins and call initGui()
104+
bool startPlugin(QString packageName);
105+
106+
//! helper function to get some information about plugin
107+
//! @param function one of these strings: name, tpye, version, description
108+
QString getPluginMetadata(QString pluginName, QString function);
109+
110+
//! unload plugin
111+
bool unloadPlugin(QString packageName);
112+
113+
protected:
114+
115+
void installErrorHook();
116+
117+
QString getTraceback();
118+
119+
//! reference to module __main__
120+
PyObject* mMainModule;
121+
122+
//! dictionary of module __main__
123+
PyObject* mMainDict;
124+
125+
//! flag determining that python support is enabled
126+
bool mPythonEnabled;
127+
128+
static QgsPythonUtilsImpl* mInstance;
129+
};
130+
131+
132+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.