From 0229ea931f45a5090550826b8317f46495c11ac3 Mon Sep 17 00:00:00 2001 From: Nathan Woodrow Date: Sun, 31 Mar 2013 07:36:57 +1000 Subject: [PATCH] Add eval to QgsPythonRunner. --- python/core/qgspythonrunner.sip | 5 +++++ src/app/qgisapp.cpp | 10 ++++++++++ src/core/qgspythonrunner.cpp | 13 +++++++++++++ src/core/qgspythonrunner.h | 5 +++++ 4 files changed, 33 insertions(+) diff --git a/python/core/qgspythonrunner.sip b/python/core/qgspythonrunner.sip index b9a89f6ce372..e71329aa8d41 100644 --- a/python/core/qgspythonrunner.sip +++ b/python/core/qgspythonrunner.sip @@ -12,6 +12,9 @@ class QgsPythonRunner /** execute a python statement */ static bool run( QString command, QString messageOnError = QString() ); + /** Eval a python statement */ + static bool eval( QString command, QString& result); + /** assign an instance of python runner so that run() can be used. This method should be called during app initialization. Takes ownership of the object, deletes previous instance. */ @@ -23,4 +26,6 @@ class QgsPythonRunner virtual ~QgsPythonRunner(); virtual bool runCommand( QString command, QString messageOnError = QString() ) = 0; + + virtual bool evalCommand( QString command, QString& result ) = 0; }; diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 894face2d47a..b3bc2c889710 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -6232,6 +6232,7 @@ class QgsPythonRunnerImpl : public QgsPythonRunner { public: QgsPythonRunnerImpl( QgsPythonUtils* pythonUtils ) : mPythonUtils( pythonUtils ) {} + virtual bool runCommand( QString command, QString messageOnError = QString() ) { if ( mPythonUtils && mPythonUtils->isEnabled() ) @@ -6241,6 +6242,15 @@ class QgsPythonRunnerImpl : public QgsPythonRunner return false; } + virtual bool evalCommand( QString command, QString &result ) + { + if ( mPythonUtils && mPythonUtils->isEnabled() ) + { + return mPythonUtils->evalString( command, result ); + } + return false; + } + protected: QgsPythonUtils* mPythonUtils; }; diff --git a/src/core/qgspythonrunner.cpp b/src/core/qgspythonrunner.cpp index 57593484f7a2..0bedaed86f24 100644 --- a/src/core/qgspythonrunner.cpp +++ b/src/core/qgspythonrunner.cpp @@ -38,6 +38,19 @@ bool QgsPythonRunner::run( QString command, QString messageOnError ) } } +bool QgsPythonRunner::eval( QString command, QString& result ) +{ + if ( mInstance ) + { + return mInstance->evalCommand( command, result ); + } + else + { + QgsDebugMsg( "Unable to run Python command: runner not available!" ); + return false; + } +} + void QgsPythonRunner::setInstance( QgsPythonRunner* runner ) { delete mInstance; diff --git a/src/core/qgspythonrunner.h b/src/core/qgspythonrunner.h index 0a7eadcad90a..bae0b1943acb 100644 --- a/src/core/qgspythonrunner.h +++ b/src/core/qgspythonrunner.h @@ -37,6 +37,9 @@ class CORE_EXPORT QgsPythonRunner /** execute a python statement */ static bool run( QString command, QString messageOnError = QString() ); + /** Eval a python statement */ + static bool eval( QString command, QString& result); + /** assign an instance of python runner so that run() can be used. This method should be called during app initialization. Takes ownership of the object, deletes previous instance. */ @@ -49,6 +52,8 @@ class CORE_EXPORT QgsPythonRunner virtual bool runCommand( QString command, QString messageOnError = QString() ) = 0; + virtual bool evalCommand( QString command, QString& result ) = 0; + static QgsPythonRunner* mInstance; };