Skip to content

Commit 5d144d4

Browse files
committed
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
1 parent ab0f9a6 commit 5d144d4

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

src/core/qgsvectorlayer.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,18 +1780,33 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
17801780
mEditFormConfig->setInitFunction( editFormInitNode.toElement().text() );
17811781
}
17821782

1783+
QDomNode editFormInitCodeSourceNode = node.namedItem( "editforminitcodesource" );
1784+
if ( !editFormInitCodeSourceNode.isNull() || ( !editFormInitCodeSourceNode.isNull() && !editFormInitCodeSourceNode.toElement().text().isEmpty() ) )
1785+
{
1786+
mEditFormConfig->setInitCodeSource(( QgsEditFormConfig::PythonInitCodeSource ) editFormInitCodeSourceNode.toElement().text().toInt() );
1787+
}
1788+
17831789
QDomNode editFormInitCodeNode = node.namedItem( "editforminitcode" );
17841790
if ( !editFormInitCodeNode.isNull() )
17851791
{
17861792
mEditFormConfig->setInitCode( editFormInitCodeNode.toElement().text() );
17871793
}
17881794

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

1809+
17951810
QDomNode editFormInitFilePathNode = node.namedItem( "editforminitfilepath" );
17961811
if ( !editFormInitFilePathNode.isNull() || ( !editFormInitFilePathNode.isNull() && !editFormInitFilePathNode.toElement().text().isEmpty() ) )
17971812
{

src/gui/qgsattributeform.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -633,34 +633,41 @@ void QgsAttributeForm::initPython()
633633

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

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

641-
QString form = QString( "%1 = sip.wrapinstance( %2, qgis.gui.QgsAttributeForm )" )
642-
.arg( mPyFormVarName )
643-
.arg(( unsigned long ) this );
643+
QString form = QString( "%1 = sip.wrapinstance( %2, qgis.gui.QgsAttributeForm )" )
644+
.arg( mPyFormVarName )
645+
.arg(( unsigned long ) this );
644646

645-
QgsPythonRunner::run( form );
647+
QgsPythonRunner::run( form );
646648

647-
QgsDebugMsg( QString( "running featureForm init: %1" ).arg( mPyFormVarName ) );
649+
QgsDebugMsg( QString( "running featureForm init: %1" ).arg( mPyFormVarName ) );
648650

649-
// Legacy
650-
if ( numArgs == "3" )
651-
{
652-
addInterface( new QgsAttributeFormLegacyInterface( initFunction, mPyFormVarName, this ) );
651+
// Legacy
652+
if ( numArgs == "3" )
653+
{
654+
addInterface( new QgsAttributeFormLegacyInterface( initFunction, mPyFormVarName, this ) );
655+
}
656+
else
657+
{
658+
#if 0
659+
QString expr = QString( "%1(%2)" )
660+
.arg( mLayer->editFormInit() )
661+
.arg( mPyFormVarName );
662+
QgsAttributeFormInterface* iface = QgsPythonRunner::evalToSipObject<QgsAttributeFormInterface*>( expr, "QgsAttributeFormInterface" );
663+
if ( iface )
664+
addInterface( iface );
665+
#endif
666+
}
653667
}
654668
else
655669
{
656-
#if 0
657-
QString expr = QString( "%1(%2)" )
658-
.arg( mLayer->editFormInit() )
659-
.arg( mPyFormVarName );
660-
QgsAttributeFormInterface* iface = QgsPythonRunner::evalToSipObject<QgsAttributeFormInterface*>( expr, "QgsAttributeFormInterface" );
661-
if ( iface )
662-
addInterface( iface );
663-
#endif
670+
QgsLogger::warning( QString( "There was an error evaluating the python init function!" ) );
664671
}
665672
}
666673
}

0 commit comments

Comments
 (0)