Skip to content

Commit

Permalink
Made QgsPythonUtils a singleton instead of a class with all members b…
Browse files Browse the repository at this point in the history
…eing static.

git-svn-id: http://svn.osgeo.org/qgis/trunk@8516 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed May 25, 2008
1 parent a1b4de6 commit 32915f2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 59 deletions.
26 changes: 15 additions & 11 deletions src/app/qgisapp.cpp
Expand Up @@ -361,9 +361,9 @@ static void customSrsValidation_(QgsSpatialRefSys* srs)
#ifdef HAVE_PYTHON
mSplash->showMessage(tr("Starting Python"), Qt::AlignHCenter | Qt::AlignBottom);
qApp->processEvents();
QgsPythonUtils::initPython(mQgisInterface);
QgsPythonUtils::instance()->initPython(mQgisInterface);

if (!QgsPythonUtils::isEnabled())
if (!QgsPythonUtils::instance()->isEnabled())
mActionShowPythonDialog->setEnabled(false);
#endif

Expand Down Expand Up @@ -1708,28 +1708,30 @@ void QgisApp::restoreSessionPlugins(QString thePluginDirString)
#ifdef HAVE_PYTHON
QString pluginName, description, version;

if (QgsPythonUtils::isEnabled())
QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();

if (pythonUtils->isEnabled())
{

// check for python plugins system-wide
QStringList pluginList = QgsPythonUtils::pluginList();
QStringList pluginList = pythonUtils->pluginList();

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

// import plugin's package
if (!QgsPythonUtils::loadPlugin(packageName))
if (!pythonUtils->loadPlugin(packageName))
continue;

// get information from the plugin
// if there are some problems, don't continue with metadata retreival
pluginName = QgsPythonUtils::getPluginMetadata(packageName, "name");
pluginName = pythonUtils->getPluginMetadata(packageName, "name");
if (pluginName != "__error__")
{
description = QgsPythonUtils::getPluginMetadata(packageName, "description");
description = pythonUtils->getPluginMetadata(packageName, "description");
if (description != "__error__")
version = QgsPythonUtils::getPluginMetadata(packageName, "version");
version = pythonUtils->getPluginMetadata(packageName, "version");
}

if (pluginName == "__error__" || description == "__error__" || version == "__error__")
Expand Down Expand Up @@ -3881,7 +3883,9 @@ void QgisApp::showPluginManager()
void QgisApp::loadPythonPlugin(QString packageName, QString pluginName)
{
#ifdef HAVE_PYTHON
if (!QgsPythonUtils::isEnabled())
QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();

if (!pythonUtils->isEnabled())
return;

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

// TODO: test success

Expand Down
16 changes: 9 additions & 7 deletions src/app/qgspluginmanager.cpp
Expand Up @@ -119,24 +119,26 @@ void QgsPluginManager::sortModel(int column)
void QgsPluginManager::getPythonPluginDescriptions()
{
#ifdef HAVE_PYTHON
if (!QgsPythonUtils::isEnabled())
QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();

if (!pythonUtils->isEnabled())
return;

// look for plugins systemwide
QStringList pluginList = QgsPythonUtils::pluginList();
QStringList pluginList = pythonUtils->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 (!QgsPythonUtils::loadPlugin(packageName))
if (!pythonUtils->loadPlugin(packageName))
continue;

// get information from the plugin
QString pluginName = QgsPythonUtils::getPluginMetadata(packageName, "name");
QString description = QgsPythonUtils::getPluginMetadata(packageName, "description");
QString version = QgsPythonUtils::getPluginMetadata(packageName, "version");
QString pluginName = pythonUtils->getPluginMetadata(packageName, "name");
QString description = pythonUtils->getPluginMetadata(packageName, "description");
QString version = pythonUtils->getPluginMetadata(packageName, "version");

if (pluginName == "???" || description == "???" || version == "???")
continue;
Expand Down Expand Up @@ -383,7 +385,7 @@ void QgsPluginManager::unload()
{
#ifdef HAVE_PYTHON
QString packageName = pRegistry->library(pluginName);
QgsPythonUtils::unloadPlugin(packageName);
QgsPythonUtils::instance()->unloadPlugin(packageName);
//disable it to the qsettings file
settings.setValue("/PythonPlugins/" + packageName, false);
#endif
Expand Down
14 changes: 8 additions & 6 deletions src/app/qgspythondialog.cpp
Expand Up @@ -41,12 +41,14 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed()
QString command = edtCmdLine->text();
QString output;

QgsPythonUtils* pythonUtils = QgsPythonUtils::instance();

// when using Py_single_input the return value will be always null
// we're using custom hooks for output and exceptions to show output in console
if (QgsPythonUtils::runStringUnsafe(command))
if (pythonUtils->runStringUnsafe(command))
{
QgsPythonUtils::evalString("sys.stdout.get_and_clean_data()", output);
QString result = QgsPythonUtils::getResult();
pythonUtils->evalString("sys.stdout.get_and_clean_data()", output);
QString result = pythonUtils->getResult();
// escape the result so python objects display properly and
// we can still use html output to get nicely formatted display
output = escapeHtml(output) + escapeHtml(result);
Expand All @@ -57,7 +59,7 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed()
else
{
QString className, errorText;
QgsPythonUtils::getError(className, errorText);
pythonUtils->getError(className, errorText);

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

QgsPythonUtils::installConsoleHooks();
QgsPythonUtils::instance()->installConsoleHooks();
}

void QgsPythonDialog::closeEvent(QCloseEvent* event)
{
QgsPythonUtils::uninstallConsoleHooks();
QgsPythonUtils::instance()->uninstallConsoleHooks();

QDialog::closeEvent(event);
}
23 changes: 19 additions & 4 deletions src/app/qgspythonutils.cpp
Expand Up @@ -28,10 +28,25 @@
#include <QStringList>
#include <QDir>

QString QgsPythonUtils::mPluginsPath;
PyObject* QgsPythonUtils::mMainModule;
PyObject* QgsPythonUtils::mMainDict;
bool QgsPythonUtils::mPythonEnabled = false;
QgsPythonUtils* QgsPythonUtils::mInstance = NULL;

QgsPythonUtils::QgsPythonUtils()
{
mMainModule = NULL;
mMainDict = NULL;
mPythonEnabled = false;
}

QgsPythonUtils::~QgsPythonUtils()
{
}

QgsPythonUtils* QgsPythonUtils::instance()
{
if (mInstance == NULL)
mInstance = new QgsPythonUtils();
return mInstance;
}


void QgsPythonUtils::initPython(QgisInterface* interface)
Expand Down
68 changes: 37 additions & 31 deletions src/app/qgspythonutils.h
Expand Up @@ -27,104 +27,110 @@ class QgisInterface;

/**
All calls to Python functions in QGIS come here.
This class is a singleton.
All functions here are static - it's not needed to create an instance of this class
For now, default path for python plugins is QgsApplication::pluginPath() + "/python"
Default path for python plugins is:
- QgsApplication::qgisSettingsDirPath() + "/python/plugins"
- QgsApplication::pkgDataPath() + "/python/plugins"
*/
class QgsPythonUtils
{
public:

QgsPythonUtils();

~QgsPythonUtils();

static QgsPythonUtils* instance();

/* general purpose functions */

//! initialize python and import bindings
static void initPython(QgisInterface* interface);
void initPython(QgisInterface* interface);

//! close python interpreter
static void exitPython();
void exitPython();

//! returns true if python support is ready to use (must be inited first)
static bool isEnabled();
bool isEnabled();

//! returns path where QGIS python stuff is located
static QString pythonPath();
QString pythonPath();

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

//! run a statement, error reporting is not done
//! @return true if no error occured
static bool runStringUnsafe(const QString& command);
bool runStringUnsafe(const QString& command);

static bool evalString(const QString& command, QString& result);
bool evalString(const QString& command, QString& result);

//! @return object's type name as a string
static QString getTypeAsString(PyObject* obj);
QString getTypeAsString(PyObject* obj);

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

//! get variable from main dictionary
static QString getVariableFromMain(QString name);
QString getVariableFromMain(QString name);

/* python console related functions */

//! change displayhook and excepthook
//! our hooks will just save the result to special variables
//! and those can be used in the program
static void installConsoleHooks();
void installConsoleHooks();

//! get back to the original settings (i.e. write output to stdout)
static void uninstallConsoleHooks();
void uninstallConsoleHooks();

//! get result from the last statement as a string
static QString getResult();
QString getResult();

/* plugins related functions */

//! return current path for python plugins
static QString pluginsPath();
QString pluginsPath();

//! return current path for home directory python plugins
static QString homePluginsPath();
QString homePluginsPath();

//! return list of all available python plugins
static QStringList pluginList();
QStringList pluginList();

//! load python plugin (import)
static bool loadPlugin(QString packageName);
bool loadPlugin(QString packageName);

//! start plugin: add to active plugins and call initGui()
static bool startPlugin(QString packageName);
bool startPlugin(QString packageName);

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

//! unload plugin
static bool unloadPlugin(QString packageName);
bool unloadPlugin(QString packageName);

protected:

static void installErrorHook();
void installErrorHook();

static QString getTraceback();
QString getTraceback();

//! path where
static QString mPluginsPath;

//! reference to module __main__
static PyObject* mMainModule;
PyObject* mMainModule;

//! dictionary of module __main__
static PyObject* mMainDict;
PyObject* mMainDict;

//! flag determining that python support is enabled
static bool mPythonEnabled;
bool mPythonEnabled;

static QgsPythonUtils* mInstance;
};

0 comments on commit 32915f2

Please sign in to comment.