Skip to content
Permalink
Browse files

Added b/w compatibility for "dot" module/function

When loading a function init with a dot, this
is transformed to a proper import and the
python init configuration is changed to support
the new python loading system.

Also check for inspect errors.

Still missing: warn the user on configuration and
trapped runtime errors
  • Loading branch information
elpaso committed Nov 24, 2015
1 parent ab0f9a6 commit 5d144d4040df3a85474c27f09c75998e2704c820
Showing with 45 additions and 23 deletions.
  1. +18 −3 src/core/qgsvectorlayer.cpp
  2. +27 −20 src/gui/qgsattributeform.cpp
@@ -1780,18 +1780,33 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
mEditFormConfig->setInitFunction( editFormInitNode.toElement().text() );
}

QDomNode editFormInitCodeSourceNode = node.namedItem( "editforminitcodesource" );
if ( !editFormInitCodeSourceNode.isNull() || ( !editFormInitCodeSourceNode.isNull() && !editFormInitCodeSourceNode.toElement().text().isEmpty() ) )
{
mEditFormConfig->setInitCodeSource(( QgsEditFormConfig::PythonInitCodeSource ) editFormInitCodeSourceNode.toElement().text().toInt() );
}

QDomNode editFormInitCodeNode = node.namedItem( "editforminitcode" );
if ( !editFormInitCodeNode.isNull() )
{
mEditFormConfig->setInitCode( editFormInitCodeNode.toElement().text() );
}

QDomNode editFormInitCodeSourceNode = node.namedItem( "editforminitcodesource" );
if ( !editFormInitCodeSourceNode.isNull() || ( !editFormInitCodeSourceNode.isNull() && !editFormInitCodeSourceNode.toElement().text().isEmpty() ) )
// Temporary < 2.12 b/w compatibility "dot" support patch
// @see: https://github.com/qgis/QGIS/pull/2498
// For b/w compatibility, check if there's a dot in the function name
// and if yes, transform it in an import statement for the module
// and set the PythonInitCodeSource to CodeSourceDialog
QString initFunction = mEditFormConfig->initFunction();
int dotPos = initFunction.lastIndexOf( '.' );
if ( dotPos >= 0 ) // It's a module
{
mEditFormConfig->setInitCodeSource(( QgsEditFormConfig::PythonInitCodeSource ) editFormInitCodeSourceNode.toElement().text().toInt() );
mEditFormConfig->setInitCodeSource( QgsEditFormConfig::PythonInitCodeSource::CodeSourceDialog );
mEditFormConfig->setInitFunction( initFunction.mid( dotPos + 1 ) );
mEditFormConfig->setInitCode( QString( "from %1 import %2\n" ).arg( initFunction.left( dotPos ), initFunction.mid( dotPos + 1 ) ) );
}


QDomNode editFormInitFilePathNode = node.namedItem( "editforminitfilepath" );
if ( !editFormInitFilePathNode.isNull() || ( !editFormInitFilePathNode.isNull() && !editFormInitFilePathNode.toElement().text().isEmpty() ) )
{
@@ -633,34 +633,41 @@ void QgsAttributeForm::initPython()

QgsPythonRunner::run( "import inspect" );
QString numArgs;
QgsPythonRunner::eval( QString( "len(inspect.getargspec(%1)[0])" ).arg( initFunction ), numArgs );

static int sFormId = 0;
mPyFormVarName = QString( "_qgis_featureform_%1_%2" ).arg( mFormNr ).arg( sFormId++ );
// Check for eval result
if ( QgsPythonRunner::eval( QString( "len(inspect.getargspec(%1)[0])" ).arg( initFunction ), numArgs ) )
{
static int sFormId = 0;
mPyFormVarName = QString( "_qgis_featureform_%1_%2" ).arg( mFormNr ).arg( sFormId++ );

QString form = QString( "%1 = sip.wrapinstance( %2, qgis.gui.QgsAttributeForm )" )
.arg( mPyFormVarName )
.arg(( unsigned long ) this );
QString form = QString( "%1 = sip.wrapinstance( %2, qgis.gui.QgsAttributeForm )" )
.arg( mPyFormVarName )
.arg(( unsigned long ) this );

QgsPythonRunner::run( form );
QgsPythonRunner::run( form );

QgsDebugMsg( QString( "running featureForm init: %1" ).arg( mPyFormVarName ) );
QgsDebugMsg( QString( "running featureForm init: %1" ).arg( mPyFormVarName ) );

// Legacy
if ( numArgs == "3" )
{
addInterface( new QgsAttributeFormLegacyInterface( initFunction, mPyFormVarName, this ) );
// Legacy
if ( numArgs == "3" )
{
addInterface( new QgsAttributeFormLegacyInterface( initFunction, mPyFormVarName, this ) );
}
else
{
#if 0
QString expr = QString( "%1(%2)" )
.arg( mLayer->editFormInit() )
.arg( mPyFormVarName );
QgsAttributeFormInterface* iface = QgsPythonRunner::evalToSipObject<QgsAttributeFormInterface*>( expr, "QgsAttributeFormInterface" );
if ( iface )
addInterface( iface );
#endif
}
}
else
{
#if 0
QString expr = QString( "%1(%2)" )
.arg( mLayer->editFormInit() )
.arg( mPyFormVarName );
QgsAttributeFormInterface* iface = QgsPythonRunner::evalToSipObject<QgsAttributeFormInterface*>( expr, "QgsAttributeFormInterface" );
if ( iface )
addInterface( iface );
#endif
QgsLogger::warning( QString( "There was an error evaluating the python init function!" ) );
}
}
}

0 comments on commit 5d144d4

Please sign in to comment.
You can’t perform that action at this time.