[QGIS Commit] r12239 - in trunk/qgis/src: app python

svn_qgis at osgeo.org svn_qgis at osgeo.org
Tue Nov 24 08:14:46 EST 2009


Author: jef
Date: 2009-11-24 08:14:44 -0500 (Tue, 24 Nov 2009)
New Revision: 12239

Modified:
   trunk/qgis/src/app/qgisapp.cpp
   trunk/qgis/src/app/qgisapp.h
   trunk/qgis/src/python/qgspythonutilsimpl.cpp
   trunk/qgis/src/python/qgspythonutilsimpl.h
Log:
[FEATURE] include ~/.qgis/python and project directory in python path

Modified: trunk/qgis/src/app/qgisapp.cpp
===================================================================
--- trunk/qgis/src/app/qgisapp.cpp	2009-11-24 08:47:33 UTC (rev 12238)
+++ trunk/qgis/src/app/qgisapp.cpp	2009-11-24 13:14:44 UTC (rev 12239)
@@ -1801,6 +1801,12 @@
   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()
@@ -4465,10 +4471,10 @@
   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();
@@ -6459,8 +6465,35 @@
 
 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 );
+}

Modified: trunk/qgis/src/app/qgisapp.h
===================================================================
--- trunk/qgis/src/app/qgisapp.h	2009-11-24 08:47:33 UTC (rev 12238)
+++ trunk/qgis/src/app/qgisapp.h	2009-11-24 13:14:44 UTC (rev 12239)
@@ -59,6 +59,8 @@
 class QgsUndoWidget;
 class QgsVectorLayer;
 
+class QDomDocument;
+
 #include <QMainWindow>
 #include <QToolBar>
 #include <QAbstractSocket>
@@ -631,6 +633,9 @@
 
     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) */

Modified: trunk/qgis/src/python/qgspythonutilsimpl.cpp
===================================================================
--- trunk/qgis/src/python/qgspythonutilsimpl.cpp	2009-11-24 08:47:33 UTC (rev 12238)
+++ trunk/qgis/src/python/qgspythonutilsimpl.cpp	2009-11-24 13:14:44 UTC (rev 12239)
@@ -59,7 +59,7 @@
 
   // 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",
@@ -412,9 +412,14 @@
   return pythonPath() + "/plugins";
 }
 
+QString QgsPythonUtilsImpl::homePythonPath()
+{
+  return QgsApplication::qgisSettingsDirPath() + "/python";
+}
+
 QString QgsPythonUtilsImpl::homePluginsPath()
 {
-  return QgsApplication::qgisSettingsDirPath() + "/python/plugins";
+  return homePythonPath() + "/plugins";
 }
 
 QStringList QgsPythonUtilsImpl::pluginList()

Modified: trunk/qgis/src/python/qgspythonutilsimpl.h
===================================================================
--- trunk/qgis/src/python/qgspythonutilsimpl.h	2009-11-24 08:47:33 UTC (rev 12238)
+++ trunk/qgis/src/python/qgspythonutilsimpl.h	2009-11-24 13:14:44 UTC (rev 12239)
@@ -89,6 +89,9 @@
     //! 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();
 



More information about the QGIS-commit mailing list