Skip to content

Commit 9652bb0

Browse files
author
jef
committedNov 24, 2009
[FEATURE] include ~/.qgis/python and project directory in python path
git-svn-id: http://svn.osgeo.org/qgis/trunk@12239 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 854358d commit 9652bb0

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,12 @@ void QgisApp::setupConnections()
18011801
connect( mActionUndo, SIGNAL( triggered() ), mUndoWidget, SLOT( undo() ) );
18021802
connect( mActionRedo, SIGNAL( triggered() ), mUndoWidget, SLOT( redo() ) );
18031803
connect( mUndoWidget, SIGNAL( undoStackChanged() ), this, SLOT( updateUndoActions() ) );
1804+
1805+
connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ),
1806+
this, SLOT( projectChanged( const QDomDocument & ) ) );
1807+
connect( QgsProject::instance(), SIGNAL( writeProject( QDomDocument & ) ),
1808+
this, SLOT( projectChanged( QDomDocument & ) ) );
1809+
18041810
}
18051811

18061812
void QgisApp::createCanvas()
@@ -4465,10 +4471,10 @@ void QgisApp::deselectAll()
44654471
mMapCanvas->setRenderFlag( false );
44664472

44674473
QMap<QString, QgsMapLayer*> layers = QgsMapLayerRegistry::instance()->mapLayers();
4468-
for ( QMap<QString, QgsMapLayer*>::iterator it = layers.begin(); it!=layers.end(); it++ )
4474+
for ( QMap<QString, QgsMapLayer*>::iterator it = layers.begin(); it != layers.end(); it++ )
44694475
{
44704476
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( it.value() );
4471-
if( !vl )
4477+
if ( !vl )
44724478
continue;
44734479

44744480
vl->removeSelection();
@@ -6459,8 +6465,35 @@ void QgisApp::updateUndoActions()
64596465

64606466
void QgisApp::runPythonString( const QString &expr )
64616467
{
6462-
if ( mPythonUtils )
6468+
if ( mPythonUtils && mPythonUtils->isEnabled() )
64636469
{
64646470
mPythonUtils->runStringUnsafe( expr );
64656471
}
64666472
}
6473+
6474+
// add project directory to python path
6475+
void QgisApp::projectChanged( const QDomDocument &doc )
6476+
{
6477+
QgsProject *project = qobject_cast<QgsProject*>( sender() );
6478+
if ( !project )
6479+
return;
6480+
6481+
QFileInfo fi( project->fileName() );
6482+
if ( !fi.exists() )
6483+
return;
6484+
6485+
static QString prevProjectDir = QString::null;
6486+
6487+
if ( prevProjectDir == fi.canonicalPath() )
6488+
return;
6489+
6490+
QString expr;
6491+
if ( !prevProjectDir.isNull() )
6492+
expr = QString( "sys.path.remove('%1'); " ).arg( prevProjectDir );
6493+
6494+
prevProjectDir = fi.canonicalPath();
6495+
6496+
expr += QString( "sys.path.append('%1')" ).arg( prevProjectDir );
6497+
6498+
runPythonString( expr );
6499+
}

‎src/app/qgisapp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class QgsRectangle;
5959
class QgsUndoWidget;
6060
class QgsVectorLayer;
6161

62+
class QDomDocument;
63+
6264
#include <QMainWindow>
6365
#include <QToolBar>
6466
#include <QAbstractSocket>
@@ -631,6 +633,9 @@ class QgisApp : public QMainWindow
631633

632634
void showStyleManagerV2();
633635

636+
//! project changed
637+
void projectChanged( const QDomDocument & );
638+
634639
signals:
635640
/** emitted when a key is pressed and we want non widget sublasses to be able
636641
to pick up on this (e.g. maplayer) */

‎src/python/qgspythonutilsimpl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
5959

6060
// expect that bindings are installed locally, so add the path to modules
6161
// also add path to plugins
62-
runString( "sys.path = [\"" + pythonPath() + "\", \"" + homePluginsPath() + "\", \"" + pluginsPath() + "\"] + sys.path" );
62+
runString( "sys.path = [\"" + pythonPath() + "\", \"" + homePythonPath() + "\", \"" + homePluginsPath() + "\", \"" + pluginsPath() + "\"] + sys.path" );
6363

6464
// import SIP
6565
if ( !runString( "from sip import wrapinstance, unwrapinstance",
@@ -412,9 +412,14 @@ QString QgsPythonUtilsImpl::pluginsPath()
412412
return pythonPath() + "/plugins";
413413
}
414414

415+
QString QgsPythonUtilsImpl::homePythonPath()
416+
{
417+
return QgsApplication::qgisSettingsDirPath() + "/python";
418+
}
419+
415420
QString QgsPythonUtilsImpl::homePluginsPath()
416421
{
417-
return QgsApplication::qgisSettingsDirPath() + "/python/plugins";
422+
return homePythonPath() + "/plugins";
418423
}
419424

420425
QStringList QgsPythonUtilsImpl::pluginList()

‎src/python/qgspythonutilsimpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
8989
//! return current path for python plugins
9090
QString pluginsPath();
9191

92+
//! return current path for python in home directory
93+
QString homePythonPath();
94+
9295
//! return current path for home directory python plugins
9396
QString homePluginsPath();
9497

0 commit comments

Comments
 (0)
Please sign in to comment.