mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
[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)
This commit is contained in:
parent
e69bebdda0
commit
954b39d389
@ -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,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();
|
||||
|
||||
@ -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() );
|
||||
@ -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 );
|
||||
|
@ -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();
|
||||
|
@ -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" );
|
||||
@ -2851,6 +2851,11 @@ QString QgsVectorLayer::editFormInitCode()
|
||||
return mEditFormInitCode;
|
||||
}
|
||||
|
||||
bool QgsVectorLayer::editFormInitUseCode()
|
||||
{
|
||||
return mEditFormInitUseCode;
|
||||
}
|
||||
|
||||
void QgsVectorLayer::setEditFormInit( const QString& function )
|
||||
{
|
||||
mEditFormInit = function;
|
||||
|
@ -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 ) );
|
||||
|
||||
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." );
|
||||
}
|
||||
}
|
||||
|
||||
/* 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 );
|
||||
|
||||
QgsPythonRunner::run( "import inspect" );
|
||||
QString numArgs;
|
||||
|
@ -6,7 +6,7 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>719</width>
|
||||
<width>742</width>
|
||||
<height>634</height>
|
||||
</rect>
|
||||
</property>
|
||||
@ -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>
|
||||
@ -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">
|
||||
@ -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>
|
||||
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user