Showing with 41 additions and 0 deletions.
  1. +5 −0 python/core/qgspythonrunner.sip
  2. +10 −0 src/app/qgisapp.cpp
  3. +13 −0 src/core/qgspythonrunner.cpp
  4. +5 −0 src/core/qgspythonrunner.h
  5. +8 −0 src/gui/qgsattributedialog.cpp
5 changes: 5 additions & 0 deletions python/core/qgspythonrunner.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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;
};
10 changes: 10 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand All @@ -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;
};
Expand Down
13 changes: 13 additions & 0 deletions src/core/qgspythonrunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgspythonrunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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;
};

Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsattributedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
QgsPythonRunner::run( QString( "import %1" ).arg( module.left( pos ) ) );
}

/* Reload the module if the DEBUGMODE switch has been set in the module.
If set to False you have to reload QGIS to reset it to True due to Python
module caching */
QString reload = QString("if hasattr(%1,'DEBUGMODE') and %1.DEBUGMODE:"
" reload(%1)").arg( module.left( pos ) );

QgsPythonRunner::run( reload );

mFormNr = smFormCounter++;

QString form = QString( "_qgis_featureform_%1 = wrapinstance( %2, QtGui.QDialog )" )
Expand Down