mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -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 */
|
/** Set python function for edit form initialization */
|
||||||
void setEditFormInit( const QString& function );
|
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
|
* Access value map
|
||||||
* @deprecated Use editorWidgetV2Config() instead
|
* @deprecated Use editorWidgetV2Config() instead
|
||||||
|
@ -115,8 +115,32 @@ QgsFieldsProperties::QgsFieldsProperties( QgsVectorLayer *layer, QWidget* parent
|
|||||||
mRelationsList->setHorizontalHeaderItem( RelFieldCol, new QTableWidgetItem( tr( "Field" ) ) );
|
mRelationsList->setHorizontalHeaderItem( RelFieldCol, new QTableWidgetItem( tr( "Field" ) ) );
|
||||||
mRelationsList->verticalHeader()->hide();
|
mRelationsList->verticalHeader()->hide();
|
||||||
|
|
||||||
|
// Python init function and code
|
||||||
leEditForm->setText( layer->editForm() );
|
leEditForm->setText( layer->editForm() );
|
||||||
leEditFormInit->setText( layer->editFormInit() );
|
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();
|
loadRelations();
|
||||||
|
|
||||||
@ -424,6 +448,11 @@ void QgsFieldsProperties::on_mMoveUpItem_clicked()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QgsFieldsProperties::on_leEditFormInitUseCodeToggled( bool checked )
|
||||||
|
{
|
||||||
|
mPythonInitCodeGroupBox->setVisible( checked );
|
||||||
|
}
|
||||||
|
|
||||||
void QgsFieldsProperties::attributeTypeDialog()
|
void QgsFieldsProperties::attributeTypeDialog()
|
||||||
{
|
{
|
||||||
QPushButton *pb = qobject_cast<QPushButton *>( sender() );
|
QPushButton *pb = qobject_cast<QPushButton *>( sender() );
|
||||||
@ -844,7 +873,7 @@ void QgsFieldsProperties::apply()
|
|||||||
mLayer->setEditForm( leEditForm->text() );
|
mLayer->setEditForm( leEditForm->text() );
|
||||||
mLayer->setEditFormInit( leEditFormInit->text() );
|
mLayer->setEditFormInit( leEditFormInit->text() );
|
||||||
mLayer->setEditFormInitUseCode( leEditFormInitUseCode->isChecked() );
|
mLayer->setEditFormInitUseCode( leEditFormInitUseCode->isChecked() );
|
||||||
// TODO: mLayer->setEditFormInitCode( leEditFormInitCode->text() );
|
mLayer->setEditFormInitCode( leEditFormInitCode->text() );
|
||||||
mLayer->setFeatureFormSuppress(( QgsVectorLayer::FeatureFormSuppress )mFormSuppressCmbBx->currentIndex() );
|
mLayer->setFeatureFormSuppress(( QgsVectorLayer::FeatureFormSuppress )mFormSuppressCmbBx->currentIndex() );
|
||||||
|
|
||||||
mLayer->setExcludeAttributesWMS( excludeAttributesWMS );
|
mLayer->setExcludeAttributesWMS( excludeAttributesWMS );
|
||||||
|
@ -170,7 +170,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
|
|||||||
void onAttributeSelectionChanged();
|
void onAttributeSelectionChanged();
|
||||||
void on_pbnSelectEditForm_clicked();
|
void on_pbnSelectEditForm_clicked();
|
||||||
void on_mEditorLayoutComboBox_currentIndexChanged( int index );
|
void on_mEditorLayoutComboBox_currentIndexChanged( int index );
|
||||||
|
void on_leEditFormInitUseCodeToggled( bool checked );
|
||||||
void attributeAdded( int idx );
|
void attributeAdded( int idx );
|
||||||
void attributeDeleted( int idx );
|
void attributeDeleted( int idx );
|
||||||
void attributeTypeDialog();
|
void attributeTypeDialog();
|
||||||
|
@ -2060,7 +2060,7 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
|
|||||||
node.appendChild( efiucField );
|
node.appendChild( efiucField );
|
||||||
|
|
||||||
QDomElement eficField = doc.createElement( "editforminitcode" );
|
QDomElement eficField = doc.createElement( "editforminitcode" );
|
||||||
eficField.appendChild( doc.createTextNode( mEditFormInitCode ) );
|
eficField.appendChild( doc.createCDATASection( mEditFormInitCode ) );
|
||||||
node.appendChild( eficField );
|
node.appendChild( eficField );
|
||||||
|
|
||||||
QDomElement fFSuppElem = doc.createElement( "featformsuppress" );
|
QDomElement fFSuppElem = doc.createElement( "featformsuppress" );
|
||||||
@ -2851,6 +2851,11 @@ QString QgsVectorLayer::editFormInitCode()
|
|||||||
return mEditFormInitCode;
|
return mEditFormInitCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool QgsVectorLayer::editFormInitUseCode()
|
||||||
|
{
|
||||||
|
return mEditFormInitUseCode;
|
||||||
|
}
|
||||||
|
|
||||||
void QgsVectorLayer::setEditFormInit( const QString& function )
|
void QgsVectorLayer::setEditFormInit( const QString& function )
|
||||||
{
|
{
|
||||||
mEditFormInit = function;
|
mEditFormInit = function;
|
||||||
|
@ -580,18 +580,30 @@ void QgsAttributeForm::initPython()
|
|||||||
QString module = mLayer->editFormInit();
|
QString module = mLayer->editFormInit();
|
||||||
|
|
||||||
int pos = module.lastIndexOf( '.' );
|
int pos = module.lastIndexOf( '.' );
|
||||||
if ( pos >= 0 )
|
|
||||||
|
if ( pos >= 0 ) // It's a module
|
||||||
{
|
{
|
||||||
QgsPythonRunner::run( QString( "import %1" ).arg( module.left( pos ) ) );
|
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" );
|
QgsPythonRunner::run( "import inspect" );
|
||||||
QString numArgs;
|
QString numArgs;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>719</width>
|
<width>742</width>
|
||||||
<height>634</height>
|
<height>634</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
@ -115,15 +115,11 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
|
|||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QCheckBox" name="leEditFormInitUseCode">
|
<widget class="QCheckBox" name="leEditFormInitUseCode">
|
||||||
<property name="text">
|
<property name="toolTip">
|
||||||
<string>use code</string>
|
<string>Check this box to provide python init function code here instead of using an external file.</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QPushButton" name="leButtonOpenEditor">
|
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Open editor</string>
|
<string>Use provided code</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
@ -141,19 +137,7 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
|
|||||||
<verstretch>0</verstretch>
|
<verstretch>0</verstretch>
|
||||||
</sizepolicy>
|
</sizepolicy>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QGridLayout" name="gridLayout">
|
<layout class="QGridLayout" name="gridLayout_4">
|
||||||
<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>
|
|
||||||
<item row="0" column="0">
|
<item row="0" column="0">
|
||||||
<widget class="QSplitter" name="splitter">
|
<widget class="QSplitter" name="splitter">
|
||||||
<property name="orientation">
|
<property name="orientation">
|
||||||
@ -266,6 +250,21 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
|
|||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QGridLayout" name="mRelationsFrameLayout"/>
|
<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>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
@ -548,6 +547,12 @@ MyForms.py must live on PYTHONPATH, .qgis/python, or inside the project folder.<
|
|||||||
<header>qgscollapsiblegroupbox.h</header>
|
<header>qgscollapsiblegroupbox.h</header>
|
||||||
<container>1</container>
|
<container>1</container>
|
||||||
</customwidget>
|
</customwidget>
|
||||||
|
<customwidget>
|
||||||
|
<class>QgsCodeEditorPython</class>
|
||||||
|
<extends>QWidget</extends>
|
||||||
|
<header>qgscodeeditorpython.h</header>
|
||||||
|
<container>1</container>
|
||||||
|
</customwidget>
|
||||||
</customwidgets>
|
</customwidgets>
|
||||||
<tabstops>
|
<tabstops>
|
||||||
<tabstop>mEditorLayoutComboBox</tabstop>
|
<tabstop>mEditorLayoutComboBox</tabstop>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user