Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] store python init code into the project
Adds an option and code editor to store python form
init code into the project (and the DB, since it's in
the style section)
  • Loading branch information
elpaso committed Nov 4, 2015
1 parent e69bebd commit 954b39d
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 32 deletions.
12 changes: 12 additions & 0 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion src/app/qgsfieldsproperties.cpp
Expand Up @@ -115,8 +115,32 @@ 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( "\"\"\"\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"
"def my_form_open(dialog, layer, feature):\n"
" geom = feature.geometry()\n"
" control = 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();

Expand Down Expand Up @@ -424,6 +448,11 @@ void QgsFieldsProperties::on_mMoveUpItem_clicked()
}
}

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

void QgsFieldsProperties::attributeTypeDialog()
{
QPushButton *pb = qobject_cast<QPushButton *>( sender() );
Expand Down Expand Up @@ -844,7 +873,7 @@ void QgsFieldsProperties::apply()
mLayer->setEditForm( leEditForm->text() );
mLayer->setEditFormInit( leEditFormInit->text() );
mLayer->setEditFormInitUseCode( leEditFormInitUseCode->isChecked() );
// TODO: mLayer->setEditFormInitCode( leEditFormInitCode->text() );
mLayer->setEditFormInitCode( leEditFormInitCode->text() );
mLayer->setFeatureFormSuppress(( QgsVectorLayer::FeatureFormSuppress )mFormSuppressCmbBx->currentIndex() );

mLayer->setExcludeAttributesWMS( excludeAttributesWMS );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsfieldsproperties.h
Expand Up @@ -170,7 +170,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();
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsvectorlayer.cpp
Expand Up @@ -2060,7 +2060,7 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
node.appendChild( efiucField );

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

QDomElement fFSuppElem = doc.createElement( "featformsuppress" );
Expand Down Expand Up @@ -2851,6 +2851,11 @@ QString QgsVectorLayer::editFormInitCode()
return mEditFormInitCode;
}

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

void QgsVectorLayer::setEditFormInit( const QString& function )
{
mEditFormInit = function;
Expand Down
28 changes: 20 additions & 8 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -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;
Expand Down
47 changes: 26 additions & 21 deletions src/ui/qgsfieldspropertiesbase.ui
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>719</width>
<width>742</width>
<height>634</height>
</rect>
</property>
Expand Down Expand Up @@ -115,15 +115,11 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
</item>
<item>
<widget class="QCheckBox" name="leEditFormInitUseCode">
<property name="text">
<string>use code</string>
<property name="toolTip">
<string>Check this box to provide python init function code here instead of using an external file.</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="leButtonOpenEditor">
<property name="text">
<string>Open editor</string>
<string>Use provided code</string>
</property>
</widget>
</item>
Expand All @@ -141,19 +137,7 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QSplitter" name="splitter">
<property name="orientation">
Expand Down Expand Up @@ -266,6 +250,21 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
<bool>true</bool>
</property>
<layout class="QGridLayout" name="mRelationsFrameLayout"/>
<zorder>leEditFormInitCode</zorder>
<zorder>mPythonInitCodeGroupBox</zorder>
</widget>
<widget class="QgsCollapsibleGroupBox" name="mPythonInitCodeGroupBox">
<property name="enabled">
<bool>true</bool>
</property>
<property name="title">
<string>Python init code</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QgsCodeEditorPython" name="leEditFormInitCode" native="true"/>
</item>
</layout>
</widget>
</widget>
</item>
Expand Down Expand Up @@ -548,6 +547,12 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsCodeEditorPython</class>
<extends>QWidget</extends>
<header>qgscodeeditorpython.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mEditorLayoutComboBox</tabstop>
Expand Down

0 comments on commit 954b39d

Please sign in to comment.