Skip to content

Commit

Permalink
Python utilities separated to an interface and implementation.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8517 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed May 26, 2008
1 parent 91ad702 commit f4bff9a
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 175 deletions.
5 changes: 3 additions & 2 deletions src/app/CMakeLists.txt
Expand Up @@ -55,6 +55,7 @@ SET(QGIS_APP_SRCS
qgspluginmanager.cpp
qgspluginmetadata.cpp
qgspluginregistry.cpp
qgspythondialog.cpp
qgsprojectproperties.cpp
qgsrasterlayerproperties.cpp
qgssearchquerybuilder.cpp
Expand Down Expand Up @@ -115,6 +116,7 @@ SET (QGIS_APP_MOC_HDRS
qgsoptions.h
qgspastetransformations.h
qgspluginmanager.h
qgspythondialog.h
qgsprojectproperties.h
qgsrasterlayerproperties.h
qgssearchquerybuilder.h
Expand Down Expand Up @@ -153,8 +155,7 @@ ENDIF (POSTGRES_FOUND)

# 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})
Expand Down
66 changes: 29 additions & 37 deletions src/app/qgisapp.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -304,7 +305,8 @@ static void customSrsValidation_(QgsSpatialRefSys* srs)
// 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);
Expand Down Expand Up @@ -361,11 +363,14 @@ static void customSrsValidation_(QgsSpatialRefSys* srs)
#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
Expand Down Expand Up @@ -439,9 +444,8 @@ QgisApp::~QgisApp()
delete mMapTools.mAddRing;
delete mMapTools.mAddIsland;

#ifdef HAVE_PYTHON
delete mPythonConsole;
#endif
delete mPythonUtils;

// delete map layer registry and provider registry
QgsApplication::exitQgis();
Expand Down Expand Up @@ -858,20 +862,18 @@ void QgisApp::createActions()
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()
Expand Down Expand Up @@ -992,9 +994,7 @@ void QgisApp::createMenus()
// 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
Expand Down Expand Up @@ -1705,33 +1705,30 @@ void QgisApp::restoreSessionPlugins(QString thePluginDirString)
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__")
Expand All @@ -1746,7 +1743,7 @@ void QgisApp::restoreSessionPlugins(QString thePluginDirString)
}
}
}
#endif
QgsDebugMsg("Loading plugins completed");
QgsDebugMsg("*************************************************\n\n");
}
Expand Down Expand Up @@ -3857,7 +3854,7 @@ void QgisApp::zoomToLayerExtent()

void QgisApp::showPluginManager()
{
QgsPluginManager *pm = new QgsPluginManager(this);
QgsPluginManager *pm = new QgsPluginManager(mPythonUtils, this);
pm->resizeColumnsToContents();
if (pm->exec())
{
Expand All @@ -3882,11 +3879,11 @@ void QgisApp::showPluginManager()

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 + ")");

Expand All @@ -3895,8 +3892,8 @@ void QgisApp::loadPythonPlugin(QString packageName, QString pluginName)
// is loaded already?
if (pRegistry->library(pluginName).isEmpty())
{
pythonUtils->loadPlugin(packageName);
pythonUtils->startPlugin(packageName);
mPythonUtils->loadPlugin(packageName);
mPythonUtils->startPlugin(packageName);

// TODO: test success

Expand All @@ -3907,11 +3904,6 @@ void QgisApp::loadPythonPlugin(QString packageName, QString pluginName)
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)
Expand Down
7 changes: 3 additions & 4 deletions src/app/qgisapp.h
Expand Up @@ -50,6 +50,7 @@ class QgsMapTool;
class QgsPoint;
class QgsProviderRegistry;
class QgsPythonDialog;
class QgsPythonUtils;
class QgsRasterLayer;
class QgsRect;
class QgsVectorLayer;
Expand Down Expand Up @@ -524,9 +525,8 @@ public slots:
QAction *mActionShowAllToolbars;
QAction *mActionHideAllToolbars;
QAction *mActionToggleFullScreen;
#ifdef HAVE_PYTHON
QAction *mActionShowPythonDialog;
#endif
//
//tool groups -------------------------------------
QActionGroup *mMapToolGroup;
Expand Down Expand Up @@ -659,9 +659,8 @@ class Tools

//!flag to indicat wehter we are in fullscreen mode or not
bool mFullScreenMode;
#ifdef HAVE_PYTHON
QgsPythonDialog* mPythonConsole;
#endif
QgsPythonUtils* mPythonUtils;
};

#endif
36 changes: 17 additions & 19 deletions src/app/qgspluginmanager.cpp
Expand Up @@ -39,9 +39,7 @@
#include <qgsdetaileditemwidget.h>
#include <qgsdetaileditemdata.h>

#ifdef HAVE_PYTHON
#include <qgspythonutils.h>
#endif

#define TESTLIB
#ifdef TESTLIB
Expand All @@ -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();
Expand Down Expand Up @@ -118,27 +119,24 @@ void QgsPluginManager::sortModel(int column)

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;
Expand Down Expand Up @@ -184,7 +182,6 @@ void QgsPluginManager::getPythonPluginDescriptions()
myItems << mypDetailItem << mypLibraryNameItem;
mModelPlugins->appendRow(myItems);
}
#endif
}


Expand Down Expand Up @@ -383,12 +380,13 @@ void QgsPluginManager::unload()
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
{
Expand Down
5 changes: 4 additions & 1 deletion src/app/qgspluginmanager.h
Expand Up @@ -28,6 +28,7 @@
#include "qgisgui.h"

class QgsPluginItem;
class QgsPythonUtils;
class QTableView;

/*!
Expand All @@ -39,7 +40,7 @@ class QgsPluginManager : public QDialog, private Ui::QgsPluginManagerBase
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)
Expand Down Expand Up @@ -70,6 +71,8 @@ class QgsPluginManager : public QDialog, private Ui::QgsPluginManagerBase
private:
QStandardItemModel *mModelPlugins;
QSortFilterProxyModel * mModelProxy;

QgsPythonUtils* mPythonUtils;
};

#endif

0 comments on commit f4bff9a

Please sign in to comment.