Skip to content

Commit 6f6ff9a

Browse files
author
wonder
committed
Python utilities separated to an interface and implementation.
git-svn-id: http://svn.osgeo.org/qgis/trunk@8517 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 32915f2 commit 6f6ff9a

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

0 commit comments

Comments
 (0)