Skip to content

Commit

Permalink
Moved python support to a library, QGIS app now during the initializa…
Browse files Browse the repository at this point in the history
…tion tries to load that library.

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8530 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed May 28, 2008
1 parent c16313b commit 54e5b00
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 36 deletions.
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
@@ -1,6 +1,10 @@

SUBDIRS(core ui gui app providers plugins helpviewer)

IF (HAVE_PYTHON AND WITH_BINDINGS)
SUBDIRS(python)
ENDIF (HAVE_PYTHON AND WITH_BINDINGS)

IF (APPLE)
SUBDIRS(mac)
ENDIF(APPLE)
13 changes: 1 addition & 12 deletions src/app/CMakeLists.txt
Expand Up @@ -153,10 +153,6 @@ IF (POSTGRES_FOUND)
)
ENDIF (POSTGRES_FOUND)

# Python support
IF (PYTHON_FOUND)
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 Expand Up @@ -198,6 +194,7 @@ INCLUDE_DIRECTORIES(
../core/raster ../core/renderer ../core/symbology
../gui
../plugins
../python
${PROJ_INCLUDE_DIR}
${SQLITE3_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
Expand All @@ -208,10 +205,6 @@ IF (POSTGRES_FOUND)
INCLUDE_DIRECTORIES(${POSTGRES_INCLUDE_DIR})
ENDIF (POSTGRES_FOUND)

IF (PYTHON_FOUND)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
ENDIF (PYTHON_FOUND)

#############

IF (WIN32)
Expand Down Expand Up @@ -250,10 +243,6 @@ SET_TARGET_PROPERTIES(qgis PROPERTIES
INSTALL_RPATH_USE_LINK_PATH true
)

IF (PYTHON_FOUND)
TARGET_LINK_LIBRARIES(qgis ${PYTHON_LIBRARIES})
ENDIF (PYTHON_FOUND)

IF (POSTGRES_FOUND)
TARGET_LINK_LIBRARIES (qgis ${POSTGRES_LIBRARY})
ENDIF (POSTGRES_FOUND)
Expand Down
44 changes: 34 additions & 10 deletions src/app/qgisapp.cpp
Expand Up @@ -169,9 +169,6 @@

#include "qgspythondialog.h"
#include "qgspythonutils.h"
#ifdef HAVE_PYTHON
#include "qgspythonutilsimpl.h"
#endif

#ifndef WIN32
#include <dlfcn.h>
Expand Down Expand Up @@ -360,17 +357,44 @@ static void customSrsValidation_(QgsSpatialRefSys* srs)
qApp->processEvents();
QgsApplication::initQgis();

#ifdef HAVE_PYTHON
mSplash->showMessage(tr("Starting Python"), Qt::AlignHCenter | Qt::AlignBottom);
qApp->processEvents();

mPythonUtils = QgsPythonUtilsImpl::instance();

mPythonUtils->initPython(mQgisInterface);
#endif

if (!mPythonUtils || !mPythonUtils->isEnabled())
// try to load python support
QLibrary pythonlib("qgispython");
// It's necessary to set these two load hints, otherwise Python library won't work correctly
// see http://lists.kde.org/?l=pykde&m=117190116820758&w=2
pythonlib.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint);
if (pythonlib.load())
{
QgsDebugMsg("Python support library loaded successfully.");
typedef QgsPythonUtils* (*inst)();
inst pythonlib_inst = (inst) pythonlib.resolve("instance");
if (pythonlib_inst)
{
QgsDebugMsg("Python support library's instance() symbol resolved.");
mPythonUtils = pythonlib_inst();
mPythonUtils->initPython(mQgisInterface);
}
else
{
QgsDebugMsg("Couldn't resolve python support library's instance() symbol.");
}
}
else
{
QgsDebugMsg("Couldn't load Python support library.");
}

if (mPythonUtils && mPythonUtils->isEnabled())
{
QgsDebugMsg("Python support ENABLED :-)");
}
else
{
mActionShowPythonDialog->setEnabled(false);
QgsDebugMsg("Python support DISABLED :-(");
}

// Create the plugin registry and load plugins
// load any plugins that were running in the last session
Expand Down
25 changes: 25 additions & 0 deletions src/python/CMakeLists.txt
@@ -0,0 +1,25 @@

SET(QGISPYTHON_SRCS qgispython.cpp qgspythonutilsimpl.cpp)

INCLUDE_DIRECTORIES(
../core
../core/raster ../core/renderer ../core/symbology
../gui
${PYTHON_INCLUDE_PATH})

ADD_LIBRARY (qgispython SHARED ${QGISPYTHON_SRCS})

SET_TARGET_PROPERTIES(qgispython PROPERTIES
VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}
SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR})


TARGET_LINK_LIBRARIES(qgispython
${QT_QTCORE_LIBRARY}
${QT_QTGUI_LIBRARY}
${PYTHON_LIBRARIES}
)

INSTALL(TARGETS qgispython
RUNTIME DESTINATION ${QGIS_BIN_DIR}
LIBRARY DESTINATION ${QGIS_LIB_DIR})
23 changes: 23 additions & 0 deletions src/python/qgispython.cpp
@@ -0,0 +1,23 @@
/***************************************************************************
qgispython.cpp - python support in QGIS
---------------------
begin : May 2008
copyright : (C) 2008 by Martin Dobias
email : wonder.sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */

#include "qgis.h"
#include "qgspythonutilsimpl.h"

QGISEXTERN QgsPythonUtils* instance()
{
return new QgsPythonUtilsImpl();
}
File renamed without changes.
10 changes: 0 additions & 10 deletions src/app/qgspythonutils.cpp → src/python/qgspythonutilsimpl.cpp
Expand Up @@ -28,7 +28,6 @@
#include <QStringList>
#include <QDir>

QgsPythonUtilsImpl* QgsPythonUtilsImpl::mInstance = NULL;

QgsPythonUtilsImpl::QgsPythonUtilsImpl()
{
Expand All @@ -41,14 +40,6 @@ QgsPythonUtilsImpl::~QgsPythonUtilsImpl()
{
}

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


void QgsPythonUtilsImpl::initPython(QgisInterface* interface)
{
// initialize python
Expand All @@ -63,7 +54,6 @@ void QgsPythonUtilsImpl::initPython(QgisInterface* interface)
runString("import traceback"); // for formatting stack traces
runString("import __main__"); // to access explicitly global variables


// expect that bindings are installed locally, so add the path to modules
// also add path to plugins
runString("sys.path = [\"" + pythonPath() + "\", \"" + homePluginsPath() + "\", \"" + pluginsPath() + "\"] + sys.path");
Expand Down
Expand Up @@ -35,8 +35,6 @@ class QgsPythonUtilsImpl : public QgsPythonUtils

virtual ~QgsPythonUtilsImpl();

static QgsPythonUtilsImpl* instance();

/* general purpose functions */

//! initialize python and import bindings
Expand Down Expand Up @@ -124,8 +122,6 @@ class QgsPythonUtilsImpl : public QgsPythonUtils

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

static QgsPythonUtilsImpl* mInstance;
};


Expand Down

0 comments on commit 54e5b00

Please sign in to comment.