Skip to content

Commit ab06574

Browse files
author
wonder
committed
Moved python support to a library, QGIS app now during the initialization tries to load that library.
git-svn-id: http://svn.osgeo.org/qgis/trunk@8530 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 2542e85 commit ab06574

File tree

8 files changed

+87
-36
lines changed

8 files changed

+87
-36
lines changed

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
SUBDIRS(core ui gui app providers plugins helpviewer)
33

4+
IF (HAVE_PYTHON AND WITH_BINDINGS)
5+
SUBDIRS(python)
6+
ENDIF (HAVE_PYTHON AND WITH_BINDINGS)
7+
48
IF (APPLE)
59
SUBDIRS(mac)
610
ENDIF(APPLE)

src/app/CMakeLists.txt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,6 @@ IF (POSTGRES_FOUND)
153153
)
154154
ENDIF (POSTGRES_FOUND)
155155

156-
# Python support
157-
IF (PYTHON_FOUND)
158-
SET (QGIS_APP_SRCS ${QGIS_APP_SRCS} qgspythonutils.cpp)
159-
ENDIF (PYTHON_FOUND)
160156

161157
QT4_WRAP_CPP(QGIS_APP_MOC_SRCS ${QGIS_APP_MOC_HDRS})
162158

@@ -198,6 +194,7 @@ INCLUDE_DIRECTORIES(
198194
../core/raster ../core/renderer ../core/symbology
199195
../gui
200196
../plugins
197+
../python
201198
${PROJ_INCLUDE_DIR}
202199
${SQLITE3_INCLUDE_DIR}
203200
${GEOS_INCLUDE_DIR}
@@ -208,10 +205,6 @@ IF (POSTGRES_FOUND)
208205
INCLUDE_DIRECTORIES(${POSTGRES_INCLUDE_DIR})
209206
ENDIF (POSTGRES_FOUND)
210207

211-
IF (PYTHON_FOUND)
212-
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
213-
ENDIF (PYTHON_FOUND)
214-
215208
#############
216209

217210
IF (WIN32)
@@ -250,10 +243,6 @@ SET_TARGET_PROPERTIES(qgis PROPERTIES
250243
INSTALL_RPATH_USE_LINK_PATH true
251244
)
252245

253-
IF (PYTHON_FOUND)
254-
TARGET_LINK_LIBRARIES(qgis ${PYTHON_LIBRARIES})
255-
ENDIF (PYTHON_FOUND)
256-
257246
IF (POSTGRES_FOUND)
258247
TARGET_LINK_LIBRARIES (qgis ${POSTGRES_LIBRARY})
259248
ENDIF (POSTGRES_FOUND)

src/app/qgisapp.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@
169169

170170
#include "qgspythondialog.h"
171171
#include "qgspythonutils.h"
172-
#ifdef HAVE_PYTHON
173-
#include "qgspythonutilsimpl.h"
174-
#endif
175172

176173
#ifndef WIN32
177174
#include <dlfcn.h>
@@ -360,17 +357,44 @@ static void customSrsValidation_(QgsSpatialRefSys* srs)
360357
qApp->processEvents();
361358
QgsApplication::initQgis();
362359

363-
#ifdef HAVE_PYTHON
364360
mSplash->showMessage(tr("Starting Python"), Qt::AlignHCenter | Qt::AlignBottom);
365361
qApp->processEvents();
366-
367-
mPythonUtils = QgsPythonUtilsImpl::instance();
368-
369-
mPythonUtils->initPython(mQgisInterface);
370-
#endif
371362

372-
if (!mPythonUtils || !mPythonUtils->isEnabled())
363+
// try to load python support
364+
QLibrary pythonlib("qgispython");
365+
// It's necessary to set these two load hints, otherwise Python library won't work correctly
366+
// see http://lists.kde.org/?l=pykde&m=117190116820758&w=2
367+
pythonlib.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint);
368+
if (pythonlib.load())
369+
{
370+
QgsDebugMsg("Python support library loaded successfully.");
371+
typedef QgsPythonUtils* (*inst)();
372+
inst pythonlib_inst = (inst) pythonlib.resolve("instance");
373+
if (pythonlib_inst)
374+
{
375+
QgsDebugMsg("Python support library's instance() symbol resolved.");
376+
mPythonUtils = pythonlib_inst();
377+
mPythonUtils->initPython(mQgisInterface);
378+
}
379+
else
380+
{
381+
QgsDebugMsg("Couldn't resolve python support library's instance() symbol.");
382+
}
383+
}
384+
else
385+
{
386+
QgsDebugMsg("Couldn't load Python support library.");
387+
}
388+
389+
if (mPythonUtils && mPythonUtils->isEnabled())
390+
{
391+
QgsDebugMsg("Python support ENABLED :-)");
392+
}
393+
else
394+
{
373395
mActionShowPythonDialog->setEnabled(false);
396+
QgsDebugMsg("Python support DISABLED :-(");
397+
}
374398

375399
// Create the plugin registry and load plugins
376400
// load any plugins that were running in the last session

src/python/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
SET(QGISPYTHON_SRCS qgispython.cpp qgspythonutilsimpl.cpp)
3+
4+
INCLUDE_DIRECTORIES(
5+
../core
6+
../core/raster ../core/renderer ../core/symbology
7+
../gui
8+
${PYTHON_INCLUDE_PATH})
9+
10+
ADD_LIBRARY (qgispython SHARED ${QGISPYTHON_SRCS})
11+
12+
SET_TARGET_PROPERTIES(qgispython PROPERTIES
13+
VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}
14+
SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR})
15+
16+
17+
TARGET_LINK_LIBRARIES(qgispython
18+
${QT_QTCORE_LIBRARY}
19+
${QT_QTGUI_LIBRARY}
20+
${PYTHON_LIBRARIES}
21+
)
22+
23+
INSTALL(TARGETS qgispython
24+
RUNTIME DESTINATION ${QGIS_BIN_DIR}
25+
LIBRARY DESTINATION ${QGIS_LIB_DIR})

src/python/qgispython.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/***************************************************************************
2+
qgispython.cpp - python support in QGIS
3+
---------------------
4+
begin : May 2008
5+
copyright : (C) 2008 by Martin Dobias
6+
email : wonder.sk at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
/* $Id$ */
16+
17+
#include "qgis.h"
18+
#include "qgspythonutilsimpl.h"
19+
20+
QGISEXTERN QgsPythonUtils* instance()
21+
{
22+
return new QgsPythonUtilsImpl();
23+
}
File renamed without changes.

src/app/qgspythonutils.cpp renamed to src/python/qgspythonutilsimpl.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <QStringList>
2929
#include <QDir>
3030

31-
QgsPythonUtilsImpl* QgsPythonUtilsImpl::mInstance = NULL;
3231

3332
QgsPythonUtilsImpl::QgsPythonUtilsImpl()
3433
{
@@ -41,14 +40,6 @@ QgsPythonUtilsImpl::~QgsPythonUtilsImpl()
4140
{
4241
}
4342

44-
QgsPythonUtilsImpl* QgsPythonUtilsImpl::instance()
45-
{
46-
if (mInstance == NULL)
47-
mInstance = new QgsPythonUtilsImpl();
48-
return mInstance;
49-
}
50-
51-
5243
void QgsPythonUtilsImpl::initPython(QgisInterface* interface)
5344
{
5445
// initialize python
@@ -63,7 +54,6 @@ void QgsPythonUtilsImpl::initPython(QgisInterface* interface)
6354
runString("import traceback"); // for formatting stack traces
6455
runString("import __main__"); // to access explicitly global variables
6556

66-
6757
// expect that bindings are installed locally, so add the path to modules
6858
// also add path to plugins
6959
runString("sys.path = [\"" + pythonPath() + "\", \"" + homePluginsPath() + "\", \"" + pluginsPath() + "\"] + sys.path");

src/app/qgspythonutilsimpl.h renamed to src/python/qgspythonutilsimpl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
3535

3636
virtual ~QgsPythonUtilsImpl();
3737

38-
static QgsPythonUtilsImpl* instance();
39-
4038
/* general purpose functions */
4139

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

125123
//! flag determining that python support is enabled
126124
bool mPythonEnabled;
127-
128-
static QgsPythonUtilsImpl* mInstance;
129125
};
130126

131127

0 commit comments

Comments
 (0)