Skip to content
Permalink
Browse files

Python utilities separated to an interface and implementation.

git-svn-id: http://svn.osgeo.org/qgis/trunk@8517 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed May 26, 2008
1 parent 32915f2 commit 6f6ff9ac38b9c32456e0182dca9f90b88808aa76
@@ -55,6 +55,7 @@ SET(QGIS_APP_SRCS
qgspluginmanager.cpp
qgspluginmetadata.cpp
qgspluginregistry.cpp
qgspythondialog.cpp
qgsprojectproperties.cpp
qgsrasterlayerproperties.cpp
qgssearchquerybuilder.cpp
@@ -115,6 +116,7 @@ SET (QGIS_APP_MOC_HDRS
qgsoptions.h
qgspastetransformations.h
qgspluginmanager.h
qgspythondialog.h
qgsprojectproperties.h
qgsrasterlayerproperties.h
qgssearchquerybuilder.h
@@ -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})
@@ -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
@@ -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);
@@ -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
@@ -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();
@@ -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()
@@ -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
@@ -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__")
@@ -1746,7 +1743,7 @@ void QgisApp::restoreSessionPlugins(QString thePluginDirString)
}
}
}
#endif
QgsDebugMsg("Loading plugins completed");
QgsDebugMsg("*************************************************\n\n");
}
@@ -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())
{
@@ -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 + ")");

@@ -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

@@ -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)
@@ -50,6 +50,7 @@ class QgsMapTool;
class QgsPoint;
class QgsProviderRegistry;
class QgsPythonDialog;
class QgsPythonUtils;
class QgsRasterLayer;
class QgsRect;
class QgsVectorLayer;
@@ -524,9 +525,8 @@ public slots:
QAction *mActionShowAllToolbars;
QAction *mActionHideAllToolbars;
QAction *mActionToggleFullScreen;
#ifdef HAVE_PYTHON
QAction *mActionShowPythonDialog;
#endif
//
//tool groups -------------------------------------
QActionGroup *mMapToolGroup;
@@ -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
@@ -39,9 +39,7 @@
#include <qgsdetaileditemwidget.h>
#include <qgsdetaileditemdata.h>

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

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


@@ -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
{
@@ -28,6 +28,7 @@
#include "qgisgui.h"

class QgsPluginItem;
class QgsPythonUtils;
class QTableView;

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

QgsPythonUtils* mPythonUtils;
};

#endif

0 comments on commit 6f6ff9a

Please sign in to comment.
You can’t perform that action at this time.