Skip to content

Commit 9213a84

Browse files
committedNov 24, 2014
[FEATURE] Support drag and drop of python scripts onto QGIS window
1 parent fe3e385 commit 9213a84

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4061,7 +4061,18 @@ void QgisApp::openProject( QAction *action )
40614061
int myProjectionEnabledFlag =
40624062
QgsProject::instance()->readNumEntry( "SpatialRefSys", "/ProjectionsEnabled", 0 );
40634063
mMapCanvas->setCrsTransformEnabled( myProjectionEnabledFlag );
4064-
} // QgisApp::openProject
4064+
}
4065+
4066+
void QgisApp::runScript( const QString &filePath )
4067+
{
4068+
if ( !mPythonUtils || !mPythonUtils->isEnabled() )
4069+
return;
4070+
4071+
mPythonUtils->runString(
4072+
QString( "import sys\n"
4073+
"execfile(\"%1\".replace(\"\\\\\", \"/\").encode(sys.getfilesystemencoding()))\n" ).arg( filePath )
4074+
, tr( "Failed to run Python script:" ), false );
4075+
}
40654076

40664077

40674078
/**
@@ -4154,6 +4165,10 @@ void QgisApp::openFile( const QString & fileName )
41544165
{
41554166
openLayerDefinition( fileName );
41564167
}
4168+
else if ( fi.completeSuffix() == "py" )
4169+
{
4170+
runScript( fileName );
4171+
}
41574172
else
41584173
{
41594174
QgsDebugMsg( "Adding " + fileName + " to the map canvas" );

‎src/app/qgisapp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,11 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
853853
//! Open the project file corresponding to the
854854
//! text)= of the given action.
855855
void openProject( QAction *action );
856+
/** Attempts to run a python script
857+
* @param filePath full path to python script
858+
* @note added in QGIS 2.7
859+
*/
860+
void runScript( const QString& filePath );
856861
//! Save the map view as an image - user is prompted for image name using a dialog
857862
void saveMapAsImage();
858863
//! Open a project

0 commit comments

Comments
 (0)