Skip to content
Permalink
Browse files

Merge pull request #2420 from elpaso/bugfix-13410

[FEATURE] Bugfix 13410
  • Loading branch information
elpaso committed Nov 5, 2015
2 parents 700fc04 + cf4693e commit f982c842eb17b47ee3026336a2591a0b1c5f3199
@@ -13,7 +13,7 @@ class QgsPythonRunner
static bool run( const QString& command, const QString& messageOnError = QString() );

/** Eval a python statement */
static bool eval( const QString& command, QString& result );
static bool eval( const QString& command, QString& result /Out/ );

/** Assign an instance of python runner so that run() can be used.
This method should be called during app initialization.
@@ -1095,6 +1095,18 @@ class QgsVectorLayer : QgsMapLayer
/** Set python function for edit form initialization */
void setEditFormInit( const QString& function );

/** Get python code for edit form initialization */
QString editFormInitCode();

/** Reeturn if python code has to be loaded for edit form initialization */
bool editFormInitUseCode();

/** Set python code for edit form initialization */
void setEditFormInitCode( const QString& code );

/** Set python code for edit form initialization */
void setEditFormInitUseCode( const bool useCode );

/**
* Access value map
* @deprecated Use editorWidgetV2Config() instead
@@ -115,8 +115,33 @@ QgsFieldsProperties::QgsFieldsProperties( QgsVectorLayer *layer, QWidget* parent
mRelationsList->setHorizontalHeaderItem( RelFieldCol, new QTableWidgetItem( tr( "Field" ) ) );
mRelationsList->verticalHeader()->hide();

// Python init function and code
leEditForm->setText( layer->editForm() );
leEditFormInit->setText( layer->editFormInit() );
leEditFormInitUseCode->setChecked( layer->editFormInitUseCode() );
QString code( layer->editFormInitCode() );
if ( code.isEmpty( ) )
{
code.append( tr( "# -*- coding: utf-8 -*-\n\"\"\"\n"
"QGIS forms can have a Python function that is called when the form is\n"
"opened.\n"
"\n"
"Use this function to add extra logic to your forms.\n"
"\n"
"Enter the name of the function in the \"Python Init function\"\n"
"field.\n"
"An example follows:\n"
"\"\"\"\n"
"from PyQt4.QtGui import QWidget\n\n"
"def my_form_open(dialog, layer, feature):\n"
"\tgeom = feature.geometry()\n"
"\tcontrol = dialog.findChild(QWidget, \"MyLineEdit\")\n" ) );

}
leEditFormInitCode->setText( code );
// Show or hide as needed
mPythonInitCodeGroupBox->setVisible( layer->editFormInitUseCode() );
connect( leEditFormInitUseCode, SIGNAL( toggled( bool ) ), this, SLOT( on_leEditFormInitUseCodeToggled( bool ) ) );

loadRelations();

@@ -186,6 +211,15 @@ QTreeWidgetItem *QgsFieldsProperties::loadAttributeEditorTreeItem( QgsAttributeE
return newWidget;
}

void QgsFieldsProperties::setEditFormInit( const QString &editForm, const QString &editFormInit, const QString &editFormInitCode, const bool editFormInitUseCode )
{
leEditForm->setText( editForm );
leEditFormInit->setText( editFormInit );
leEditFormInitCode->setText( editFormInitCode );
leEditFormInitUseCode->setChecked( editFormInitUseCode );
}


void QgsFieldsProperties::loadAttributeEditorTree()
{
// tabs and groups info
@@ -424,6 +458,11 @@ void QgsFieldsProperties::on_mMoveUpItem_clicked()
}
}

void QgsFieldsProperties::on_leEditFormInitUseCodeToggled( bool checked )
{
mPythonInitCodeGroupBox->setVisible( checked );
}

void QgsFieldsProperties::attributeTypeDialog()
{
QPushButton *pb = qobject_cast<QPushButton *>( sender() );
@@ -843,6 +882,8 @@ void QgsFieldsProperties::apply()
if ( mEditorLayoutComboBox->currentIndex() == QgsVectorLayer::UiFileLayout )
mLayer->setEditForm( leEditForm->text() );
mLayer->setEditFormInit( leEditFormInit->text() );
mLayer->setEditFormInitUseCode( leEditFormInitUseCode->isChecked() );
mLayer->setEditFormInitCode( leEditFormInitCode->text() );
mLayer->setFeatureFormSuppress(( QgsVectorLayer::FeatureFormSuppress )mFormSuppressCmbBx->currentIndex() );

mLayer->setExcludeAttributesWMS( excludeAttributesWMS );
@@ -160,6 +160,15 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
void loadAttributeEditorTree();
QTreeWidgetItem *loadAttributeEditorTreeItem( QgsAttributeEditorElement* const widgetDef, QTreeWidgetItem* parent );

/**
* @brief setEditFormInit set the private ui fields
* @param editForm
* @param editFormInit
* @param editFormInitCode
* @param editFormInitUseCode
*/
void setEditFormInit( const QString &editForm, const QString &editFormInit, const QString &editFormInitCode, const bool editFormInitUseCode );

signals:
void toggleEditing();

@@ -170,7 +179,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
void onAttributeSelectionChanged();
void on_pbnSelectEditForm_clicked();
void on_mEditorLayoutComboBox_currentIndexChanged( int index );

void on_leEditFormInitUseCodeToggled( bool checked );
void attributeAdded( int idx );
void attributeDeleted( int idx );
void attributeTypeDialog();
@@ -505,6 +505,10 @@ void QgsVectorLayerProperties::syncToLayer( void )

// set initial state for variable editor
updateVariableEditor();

// updates the init python code and ui
updateFieldsPropertiesDialog();

} // syncToLayer()


@@ -1299,3 +1303,8 @@ void QgsVectorLayerProperties::updateVariableEditor()
mVariableEditor->reloadContext();
mVariableEditor->setEditableScopeIndex( 2 );
}

void QgsVectorLayerProperties::updateFieldsPropertiesDialog()
{
mFieldsPropertiesDialog->setEditFormInit( layer->editForm(), layer->editFormInit(), layer->editFormInitCode(), layer->editFormInitUseCode() );
}
@@ -145,6 +145,11 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
*/
void updateVariableEditor();

/**
* @brief updates the FieldsPropertiesDialog when syncing the layer properties
*/
void updateFieldsPropertiesDialog();

protected:

void saveStyleAs( StyleType styleType );
@@ -138,6 +138,7 @@ QgsVectorLayer::QgsVectorLayer( const QString& vectorLayerPath,
, mLayerTransparency( 0 )
, mVertexMarkerOnlyForSelection( false )
, mEditorLayout( GeneratedLayout )
, mEditFormInitUseCode( false )
, mFeatureFormSuppress( SuppressDefault )
, mCache( new QgsGeometryCache() )
, mEditBuffer( 0 )
@@ -149,6 +150,7 @@ QgsVectorLayer::QgsVectorLayer( const QString& vectorLayerPath,
, mLazyExtent( true )
, mSymbolFeatureCounted( false )
, mEditCommandActive( false )

{
mActions = new QgsAttributeAction( this );
mConditionalStyles = new QgsConditionalLayerStyles();
@@ -1632,6 +1634,7 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
return writeSymbology( layer_node, document, errorMsg );
} // bool QgsVectorLayer::writeXml


bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage )
{
emit readCustomSymbology( node.toElement(), errorMessage );
@@ -1780,6 +1783,18 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
mEditFormInit = editFormInitNode.toElement().text();
}

QDomNode editFormInitCodeNode = node.namedItem( "editforminitcode" );
if ( !editFormInitCodeNode.isNull() )
{
mEditFormInitCode = editFormInitCodeNode.toElement().text();
}

QDomNode editFormInitUseCodeNode = node.namedItem( "editforminitusecode" );
if ( !editFormInitCodeNode.isNull() )
{
mEditFormInitUseCode = ( bool ) editFormInitUseCodeNode.toElement().text().toInt();
}

QDomNode fFSuppNode = node.namedItem( "featformsuppress" );
if ( fFSuppNode.isNull() )
{
@@ -2042,6 +2057,14 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
efiField.appendChild( doc.createTextNode( mEditFormInit ) );
node.appendChild( efiField );

QDomElement efiucField = doc.createElement( "editforminitusecode" );
efiucField.appendChild( doc.createTextNode( mEditFormInitUseCode ? "1" : "0" ) );
node.appendChild( efiucField );

QDomElement eficField = doc.createElement( "editforminitcode" );
eficField.appendChild( doc.createCDATASection( mEditFormInitCode ) );
node.appendChild( eficField );

QDomElement fFSuppElem = doc.createElement( "featformsuppress" );
QDomText fFSuppText = doc.createTextNode( QString::number( featureFormSuppress() ) );
fFSuppElem.appendChild( fFSuppText );
@@ -2825,11 +2848,31 @@ QString QgsVectorLayer::editFormInit()
return mEditFormInit;
}

QString QgsVectorLayer::editFormInitCode()
{
return mEditFormInitCode;
}

bool QgsVectorLayer::editFormInitUseCode()
{
return mEditFormInitUseCode;
}

void QgsVectorLayer::setEditFormInit( const QString& function )
{
mEditFormInit = function;
}

void QgsVectorLayer::setEditFormInitUseCode( const bool useCode )
{
mEditFormInitUseCode = useCode;
}

void QgsVectorLayer::setEditFormInitCode( const QString& code )
{
mEditFormInitCode = code;
}

QMap< QString, QVariant > QgsVectorLayer::valueMap( int idx )
{
return editorWidgetV2Config( idx );
@@ -1628,9 +1628,21 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Get python function for edit form initialization */
QString editFormInit();

/** Get python code for edit form initialization */
QString editFormInitCode();

/** Reeturn if python code has to be loaded for edit form initialization */
bool editFormInitUseCode();

/** Set python function for edit form initialization */
void setEditFormInit( const QString& function );

/** Set python code for edit form initialization */
void setEditFormInitCode( const QString& code );

/** Set python code for edit form initialization */
void setEditFormInitUseCode( const bool useCode );

/**
* Access value map
* @deprecated Use editorWidgetV2Config() instead
@@ -2141,7 +2153,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Defines the default layout to use for the attribute editor (Drag and drop, UI File, Generated) */
EditorLayout mEditorLayout;

QString mEditForm, mEditFormInit;
QString mEditForm, mEditFormInit, mEditFormInitCode;
bool mEditFormInitUseCode;

/** Type of feature form suppression after feature creation
* @note added in 2.1 */
@@ -580,18 +580,30 @@ void QgsAttributeForm::initPython()
QString module = mLayer->editFormInit();

int pos = module.lastIndexOf( '.' );
if ( pos >= 0 )

if ( pos >= 0 ) // It's a module
{
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 ) );

/* 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 );
}
else // Must be supplied code
{
if ( mLayer->editFormInitUseCode() )
{
QgsPythonRunner::run( mLayer->editFormInitCode() );
}
else
{
QgsDebugMsg( "No dot in editFormInit and no custom python code provided! There is nothing to run." );
}
}

QgsPythonRunner::run( reload );

QgsPythonRunner::run( "import inspect" );
QString numArgs;

0 comments on commit f982c84

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