Skip to content

Commit

Permalink
[FEATURE] include ~/.qgis/python and project directory in python path
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@12239 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Nov 24, 2009
1 parent 854358d commit 9652bb0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
39 changes: 36 additions & 3 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,12 @@ void QgisApp::setupConnections()
connect( mActionUndo, SIGNAL( triggered() ), mUndoWidget, SLOT( undo() ) );
connect( mActionRedo, SIGNAL( triggered() ), mUndoWidget, SLOT( redo() ) );
connect( mUndoWidget, SIGNAL( undoStackChanged() ), this, SLOT( updateUndoActions() ) );

connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ),
this, SLOT( projectChanged( const QDomDocument & ) ) );
connect( QgsProject::instance(), SIGNAL( writeProject( QDomDocument & ) ),
this, SLOT( projectChanged( QDomDocument & ) ) );

}

void QgisApp::createCanvas()
Expand Down Expand Up @@ -4465,10 +4471,10 @@ void QgisApp::deselectAll()
mMapCanvas->setRenderFlag( false );

QMap<QString, QgsMapLayer*> layers = QgsMapLayerRegistry::instance()->mapLayers();
for ( QMap<QString, QgsMapLayer*>::iterator it = layers.begin(); it!=layers.end(); it++ )
for ( QMap<QString, QgsMapLayer*>::iterator it = layers.begin(); it != layers.end(); it++ )
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( it.value() );
if( !vl )
if ( !vl )
continue;

vl->removeSelection();
Expand Down Expand Up @@ -6459,8 +6465,35 @@ void QgisApp::updateUndoActions()

void QgisApp::runPythonString( const QString &expr )
{
if ( mPythonUtils )
if ( mPythonUtils && mPythonUtils->isEnabled() )
{
mPythonUtils->runStringUnsafe( expr );
}
}

// add project directory to python path
void QgisApp::projectChanged( const QDomDocument &doc )
{
QgsProject *project = qobject_cast<QgsProject*>( sender() );
if ( !project )
return;

QFileInfo fi( project->fileName() );
if ( !fi.exists() )
return;

static QString prevProjectDir = QString::null;

if ( prevProjectDir == fi.canonicalPath() )
return;

QString expr;
if ( !prevProjectDir.isNull() )
expr = QString( "sys.path.remove('%1'); " ).arg( prevProjectDir );

prevProjectDir = fi.canonicalPath();

expr += QString( "sys.path.append('%1')" ).arg( prevProjectDir );

runPythonString( expr );
}
5 changes: 5 additions & 0 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class QgsRectangle;
class QgsUndoWidget;
class QgsVectorLayer;

class QDomDocument;

#include <QMainWindow>
#include <QToolBar>
#include <QAbstractSocket>
Expand Down Expand Up @@ -631,6 +633,9 @@ class QgisApp : public QMainWindow

void showStyleManagerV2();

//! project changed
void projectChanged( const QDomDocument & );

signals:
/** emitted when a key is pressed and we want non widget sublasses to be able
to pick up on this (e.g. maplayer) */
Expand Down
9 changes: 7 additions & 2 deletions src/python/qgspythonutilsimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )

// 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" );
runString( "sys.path = [\"" + pythonPath() + "\", \"" + homePythonPath() + "\", \"" + homePluginsPath() + "\", \"" + pluginsPath() + "\"] + sys.path" );

// import SIP
if ( !runString( "from sip import wrapinstance, unwrapinstance",
Expand Down Expand Up @@ -412,9 +412,14 @@ QString QgsPythonUtilsImpl::pluginsPath()
return pythonPath() + "/plugins";
}

QString QgsPythonUtilsImpl::homePythonPath()
{
return QgsApplication::qgisSettingsDirPath() + "/python";
}

QString QgsPythonUtilsImpl::homePluginsPath()
{
return QgsApplication::qgisSettingsDirPath() + "/python/plugins";
return homePythonPath() + "/plugins";
}

QStringList QgsPythonUtilsImpl::pluginList()
Expand Down
3 changes: 3 additions & 0 deletions src/python/qgspythonutilsimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
//! return current path for python plugins
QString pluginsPath();

//! return current path for python in home directory
QString homePythonPath();

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

Expand Down

0 comments on commit 9652bb0

Please sign in to comment.