Skip to content

Commit 954b39d

Browse files
committedNov 4, 2015
[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)
1 parent e69bebd commit 954b39d

File tree

6 files changed

+95
-32
lines changed

6 files changed

+95
-32
lines changed
 

‎python/core/qgsvectorlayer.sip

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,18 @@ class QgsVectorLayer : QgsMapLayer
10951095
/** Set python function for edit form initialization */
10961096
void setEditFormInit( const QString& function );
10971097

1098+
/** Get python code for edit form initialization */
1099+
QString editFormInitCode();
1100+
1101+
/** Reeturn if python code has to be loaded for edit form initialization */
1102+
bool editFormInitUseCode();
1103+
1104+
/** Set python code for edit form initialization */
1105+
void setEditFormInitCode( const QString& code );
1106+
1107+
/** Set python code for edit form initialization */
1108+
void setEditFormInitUseCode( const bool useCode );
1109+
10981110
/**
10991111
* Access value map
11001112
* @deprecated Use editorWidgetV2Config() instead

‎src/app/qgsfieldsproperties.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,32 @@ QgsFieldsProperties::QgsFieldsProperties( QgsVectorLayer *layer, QWidget* parent
115115
mRelationsList->setHorizontalHeaderItem( RelFieldCol, new QTableWidgetItem( tr( "Field" ) ) );
116116
mRelationsList->verticalHeader()->hide();
117117

118+
// Python init function and code
118119
leEditForm->setText( layer->editForm() );
119120
leEditFormInit->setText( layer->editFormInit() );
121+
leEditFormInitUseCode->setChecked( layer->editFormInitUseCode() );
122+
QString code( layer->editFormInitCode() );
123+
if ( code.isEmpty( ) )
124+
{
125+
code.append( tr( "\"\"\"\n"
126+
"QGIS forms can have a Python function that is called when the form is\n"
127+
"opened.\n"
128+
"\n"
129+
"Use this function to add extra logic to your forms.\n"
130+
"\n"
131+
"Enter the name of the function in the \"Python Init function\"\n"
132+
"field.\n"
133+
"An example follows:\n"
134+
"\"\"\"\n"
135+
"def my_form_open(dialog, layer, feature):\n"
136+
" geom = feature.geometry()\n"
137+
" control = dialog.findChild(QWidget, \"MyLineEdit\")\n" ) );
138+
139+
}
140+
leEditFormInitCode->setText( code );
141+
// Show or hide as needed
142+
mPythonInitCodeGroupBox->setVisible( layer->editFormInitUseCode() );
143+
connect( leEditFormInitUseCode, SIGNAL( toggled( bool ) ), this, SLOT( on_leEditFormInitUseCodeToggled( bool ) ) );
120144

121145
loadRelations();
122146

@@ -424,6 +448,11 @@ void QgsFieldsProperties::on_mMoveUpItem_clicked()
424448
}
425449
}
426450

451+
void QgsFieldsProperties::on_leEditFormInitUseCodeToggled( bool checked )
452+
{
453+
mPythonInitCodeGroupBox->setVisible( checked );
454+
}
455+
427456
void QgsFieldsProperties::attributeTypeDialog()
428457
{
429458
QPushButton *pb = qobject_cast<QPushButton *>( sender() );
@@ -844,7 +873,7 @@ void QgsFieldsProperties::apply()
844873
mLayer->setEditForm( leEditForm->text() );
845874
mLayer->setEditFormInit( leEditFormInit->text() );
846875
mLayer->setEditFormInitUseCode( leEditFormInitUseCode->isChecked() );
847-
// TODO: mLayer->setEditFormInitCode( leEditFormInitCode->text() );
876+
mLayer->setEditFormInitCode( leEditFormInitCode->text() );
848877
mLayer->setFeatureFormSuppress(( QgsVectorLayer::FeatureFormSuppress )mFormSuppressCmbBx->currentIndex() );
849878

850879
mLayer->setExcludeAttributesWMS( excludeAttributesWMS );

‎src/app/qgsfieldsproperties.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
170170
void onAttributeSelectionChanged();
171171
void on_pbnSelectEditForm_clicked();
172172
void on_mEditorLayoutComboBox_currentIndexChanged( int index );
173-
173+
void on_leEditFormInitUseCodeToggled( bool checked );
174174
void attributeAdded( int idx );
175175
void attributeDeleted( int idx );
176176
void attributeTypeDialog();

‎src/core/qgsvectorlayer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,7 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
20602060
node.appendChild( efiucField );
20612061

20622062
QDomElement eficField = doc.createElement( "editforminitcode" );
2063-
eficField.appendChild( doc.createTextNode( mEditFormInitCode ) );
2063+
eficField.appendChild( doc.createCDATASection( mEditFormInitCode ) );
20642064
node.appendChild( eficField );
20652065

20662066
QDomElement fFSuppElem = doc.createElement( "featformsuppress" );
@@ -2851,6 +2851,11 @@ QString QgsVectorLayer::editFormInitCode()
28512851
return mEditFormInitCode;
28522852
}
28532853

2854+
bool QgsVectorLayer::editFormInitUseCode()
2855+
{
2856+
return mEditFormInitUseCode;
2857+
}
2858+
28542859
void QgsVectorLayer::setEditFormInit( const QString& function )
28552860
{
28562861
mEditFormInit = function;

‎src/gui/qgsattributeform.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -580,18 +580,30 @@ void QgsAttributeForm::initPython()
580580
QString module = mLayer->editFormInit();
581581

582582
int pos = module.lastIndexOf( '.' );
583-
if ( pos >= 0 )
583+
584+
if ( pos >= 0 ) // It's a module
584585
{
585586
QgsPythonRunner::run( QString( "import %1" ).arg( module.left( pos ) ) );
586-
}
587+
/* Reload the module if the DEBUGMODE switch has been set in the module.
588+
If set to False you have to reload QGIS to reset it to True due to Python
589+
module caching */
590+
QString reload = QString( "if hasattr(%1,'DEBUGMODE') and %1.DEBUGMODE:"
591+
" reload(%1)" ).arg( module.left( pos ) );
587592

588-
/* Reload the module if the DEBUGMODE switch has been set in the module.
589-
If set to False you have to reload QGIS to reset it to True due to Python
590-
module caching */
591-
QString reload = QString( "if hasattr(%1,'DEBUGMODE') and %1.DEBUGMODE:"
592-
" reload(%1)" ).arg( module.left( pos ) );
593+
QgsPythonRunner::run( reload );
594+
}
595+
else // Must be supplied code
596+
{
597+
if ( mLayer->editFormInitUseCode() )
598+
{
599+
QgsPythonRunner::run( mLayer->editFormInitCode() );
600+
}
601+
else
602+
{
603+
QgsDebugMsg( "No dot in editFormInit and no custom python code provided! There is nothing to run." );
604+
}
605+
}
593606

594-
QgsPythonRunner::run( reload );
595607

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

‎src/ui/qgsfieldspropertiesbase.ui

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>719</width>
9+
<width>742</width>
1010
<height>634</height>
1111
</rect>
1212
</property>
@@ -115,15 +115,11 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
115115
</item>
116116
<item>
117117
<widget class="QCheckBox" name="leEditFormInitUseCode">
118-
<property name="text">
119-
<string>use code</string>
118+
<property name="toolTip">
119+
<string>Check this box to provide python init function code here instead of using an external file.</string>
120120
</property>
121-
</widget>
122-
</item>
123-
<item>
124-
<widget class="QPushButton" name="leButtonOpenEditor">
125121
<property name="text">
126-
<string>Open editor</string>
122+
<string>Use provided code</string>
127123
</property>
128124
</widget>
129125
</item>
@@ -141,19 +137,7 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
141137
<verstretch>0</verstretch>
142138
</sizepolicy>
143139
</property>
144-
<layout class="QGridLayout" name="gridLayout">
145-
<property name="leftMargin">
146-
<number>0</number>
147-
</property>
148-
<property name="topMargin">
149-
<number>0</number>
150-
</property>
151-
<property name="rightMargin">
152-
<number>0</number>
153-
</property>
154-
<property name="bottomMargin">
155-
<number>0</number>
156-
</property>
140+
<layout class="QGridLayout" name="gridLayout_4">
157141
<item row="0" column="0">
158142
<widget class="QSplitter" name="splitter">
159143
<property name="orientation">
@@ -266,6 +250,21 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
266250
<bool>true</bool>
267251
</property>
268252
<layout class="QGridLayout" name="mRelationsFrameLayout"/>
253+
<zorder>leEditFormInitCode</zorder>
254+
<zorder>mPythonInitCodeGroupBox</zorder>
255+
</widget>
256+
<widget class="QgsCollapsibleGroupBox" name="mPythonInitCodeGroupBox">
257+
<property name="enabled">
258+
<bool>true</bool>
259+
</property>
260+
<property name="title">
261+
<string>Python init code</string>
262+
</property>
263+
<layout class="QGridLayout" name="gridLayout">
264+
<item row="0" column="0">
265+
<widget class="QgsCodeEditorPython" name="leEditFormInitCode" native="true"/>
266+
</item>
267+
</layout>
269268
</widget>
270269
</widget>
271270
</item>
@@ -548,6 +547,12 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
548547
<header>qgscollapsiblegroupbox.h</header>
549548
<container>1</container>
550549
</customwidget>
550+
<customwidget>
551+
<class>QgsCodeEditorPython</class>
552+
<extends>QWidget</extends>
553+
<header>qgscodeeditorpython.h</header>
554+
<container>1</container>
555+
</customwidget>
551556
</customwidgets>
552557
<tabstops>
553558
<tabstop>mEditorLayoutComboBox</tabstop>

0 commit comments

Comments
 (0)
Please sign in to comment.