Skip to content

Commit 865fce9

Browse files
author
jef
committedNov 21, 2009
add support for a python feature form initialization function
git-svn-id: http://svn.osgeo.org/qgis/trunk@12215 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent c59f11c commit 865fce9

File tree

8 files changed

+132
-28
lines changed

8 files changed

+132
-28
lines changed
 

‎python/core/qgsvectorlayer.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,16 @@ public:
425425
*/
426426
void setEditForm( QString ui );
427427

428+
/** get edit form init function
429+
@note added in 1.4
430+
*/
431+
QString editFormInit();
432+
433+
/** set edit form init function
434+
@note added in 1.4
435+
*/
436+
void setEditFormInit( QString function );
437+
428438
/**access value map*/
429439
QMap<QString, QVariant> &valueMap(int idx);
430440

‎src/app/qgsattributedialog.cpp

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "qgssymbol.h"
2626
#include "qgsattributeeditor.h"
2727

28+
#include "qgisapp.h"
29+
2830
#include <QTableWidgetItem>
2931
#include <QSettings>
3032
#include <QLabel>
@@ -187,11 +189,34 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
187189
}
188190

189191
connect( buttonBox, SIGNAL( rejected() ), mDialog, SLOT( reject() ) );
190-
connect( buttonBox, SIGNAL( rejected() ), this, SLOT( rejected() ) );
191192
}
192193

193194
QMetaObject::connectSlotsByName( mDialog );
194195

196+
connect( mDialog, SIGNAL( destroyed() ), this, SLOT( dialogDestroyed() ) );
197+
198+
if ( !vl->editFormInit().isEmpty() )
199+
{
200+
#if 0
201+
// would be nice if only PyQt's QVariant.toPyObject() wouldn't take ownership
202+
vl->setProperty( "featureForm.dialog", QVariant::fromValue( qobject_cast<QObject*>( mDialog ) ) );
203+
vl->setProperty( "featureForm.id", QVariant( mpFeature->id() ) );
204+
#endif
205+
206+
QString module = vl->editFormInit();
207+
int pos = module.lastIndexOf( "." );
208+
if ( pos >= 0 )
209+
{
210+
QgisApp::instance()->runPythonString( QString( "import %1" ).arg( module.left( pos ) ) );
211+
}
212+
213+
QgisApp::instance()->runPythonString( QString( "_qgis_featureform_%1 = wrapinstance( %2, QtGui.QDialog )" ).arg( mLayer->getLayerID() ).arg(( unsigned long ) mDialog ) );
214+
215+
QString expr = QString( "%1(_qgis_featureform_%2,'%2',%3)" ).arg( vl->editFormInit() ).arg( vl->getLayerID() ).arg( mpFeature->id() );
216+
QgsDebugMsg( QString( "running featureForm init: %1" ).arg( expr ) );
217+
QgisApp::instance()->runPythonString( expr );
218+
}
219+
195220
restoreGeometry();
196221
}
197222

@@ -223,17 +248,41 @@ void QgsAttributeDialog::accept()
223248

224249
int QgsAttributeDialog::exec()
225250
{
226-
return mDialog->exec();
251+
if ( mDialog )
252+
{
253+
return mDialog->exec();
254+
}
255+
else
256+
{
257+
QgsDebugMsg( "No dialog" );
258+
return QDialog::Rejected;
259+
}
227260
}
228261

229262
void QgsAttributeDialog::saveGeometry()
230263
{
231-
QSettings settings;
232-
settings.setValue( mSettingsPath + "geometry", mDialog->saveGeometry() );
264+
if ( mDialog )
265+
{
266+
QSettings settings;
267+
settings.setValue( mSettingsPath + "geometry", mDialog->saveGeometry() );
268+
}
233269
}
234270

235271
void QgsAttributeDialog::restoreGeometry()
236272
{
237-
QSettings settings;
238-
mDialog->restoreGeometry( settings.value( mSettingsPath + "geometry" ).toByteArray() );
273+
if ( mDialog )
274+
{
275+
QSettings settings;
276+
mDialog->restoreGeometry( settings.value( mSettingsPath + "geometry" ).toByteArray() );
277+
}
278+
}
279+
280+
void QgsAttributeDialog::dialogDestroyed()
281+
{
282+
#if 0
283+
mLayer->setProperty( "featureForm.dialog", QVariant() );
284+
mLayer->setProperty( "featureForm.id", QVariant() );
285+
#endif
286+
QgisApp::instance()->runPythonString( QString( "del _qgis_featureform_%1" ).arg( mLayer->getLayerID() ) );
287+
mDialog = NULL;
239288
}

‎src/app/qgsattributedialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class QgsAttributeDialog : public QObject
5555

5656
int exec();
5757

58+
void dialogDestroyed();
59+
5860
private:
5961

6062
QDialog *mDialog;

‎src/app/qgsattributeeditor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
372372
connect( pb, SIGNAL( clicked() ), new QgsAttributeEditor( pb ), SLOT( selectFileName() ) );
373373
}
374374
break;
375+
376+
case QgsVectorLayer::Immutable:
377+
return NULL;
378+
375379
}
376380

377381
if ( editType == QgsVectorLayer::Immutable )

‎src/app/qgsvectorlayerproperties.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
132132
leSpatialRefSys->setCursorPosition( 0 );
133133

134134
leEditForm->setText( layer->editForm() );
135+
leEditFormInit->setText( layer->editFormInit() );
135136

136137
connect( sliderTransparency, SIGNAL( valueChanged( int ) ), this, SLOT( sliderTransparency_valueChanged( int ) ) );
137138

@@ -568,6 +569,7 @@ void QgsVectorLayerProperties::apply()
568569
layer->setDisplayField( displayFieldComboBox->currentText() );
569570

570571
layer->setEditForm( leEditForm->text() );
572+
layer->setEditFormInit( leEditFormInit->text() );
571573

572574
actionDialog->apply();
573575

‎src/core/qgsvectorlayer.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
102102
mEditable( false ),
103103
mModified( false ),
104104
mMaxUpdatedIndex( -1 ),
105+
mActiveCommand( NULL ),
105106
mRenderer( 0 ),
107+
mRendererV2( NULL ),
108+
mUsingRendererV2( false ),
106109
mLabel( 0 ),
107110
mLabelOn( false ),
108-
mActiveCommand( NULL ),
109111
mVertexMarkerOnlyForSelection( false ),
110-
mFetching( false ),
111-
mRendererV2( NULL ),
112-
mUsingRendererV2( false )
112+
mFetching( false )
113113
{
114114
mActions = new QgsAttributeAction;
115115

@@ -2687,6 +2687,12 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
26872687
mEditForm = QgsProject::instance()->readPath( e.text() );
26882688
}
26892689

2690+
QDomNode editFormInitNode = node.namedItem( "editforminit" );
2691+
if ( !editFormInitNode.isNull() )
2692+
{
2693+
mEditFormInit = editFormInitNode.toElement().text();
2694+
}
2695+
26902696
mAttributeAliasMap.clear();
26912697
QDomNode aliasesNode = node.namedItem( "aliases" );
26922698
if ( !aliasesNode.isNull() )
@@ -2828,6 +2834,11 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
28282834
efField.appendChild( efText );
28292835
node.appendChild( efField );
28302836

2837+
QDomElement efiField = doc.createElement( "editforminit" );
2838+
QDomText efiText = doc.createTextNode( mEditFormInit );
2839+
efiField.appendChild( efiText );
2840+
node.appendChild( efiField );
2841+
28312842
//attribute aliases
28322843
if ( mAttributeAliasMap.size() > 0 )
28332844
{
@@ -4026,6 +4037,16 @@ void QgsVectorLayer::setEditForm( QString ui )
40264037
mEditForm = ui;
40274038
}
40284039

4040+
QString QgsVectorLayer::editFormInit()
4041+
{
4042+
return mEditFormInit;
4043+
}
4044+
4045+
void QgsVectorLayer::setEditFormInit( QString function )
4046+
{
4047+
mEditFormInit = function;
4048+
}
4049+
40294050
QMap< QString, QVariant > &QgsVectorLayer::valueMap( int idx )
40304051
{
40314052
const QgsFieldMap &fields = pendingFields();

‎src/core/qgsvectorlayer.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,12 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
477477
/** set edit form (added in 1.4) */
478478
void setEditForm( QString ui );
479479

480+
/** get python function for edit form initialization (added in 1.4) */
481+
QString editFormInit();
482+
483+
/** set python function for edit form initialization (added in 1.4) */
484+
void setEditFormInit( QString function );
485+
480486
/**access value map*/
481487
QMap<QString, QVariant> &valueMap( int idx );
482488

@@ -754,7 +760,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
754760
QMap< QString, QMap<QString, QVariant> > mValueMaps;
755761
QMap< QString, RangeData > mRanges;
756762
QMap< QString, QPair<QString, QString> > mCheckedStates;
757-
QString mEditForm;
763+
764+
QString mEditForm, mEditFormInit;
758765

759766
bool mFetching;
760767
QgsRectangle mFetchRect;

‎src/ui/qgsvectorlayerpropertiesbase.ui

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<item row="0" column="2" colspan="3">
7575
<widget class="QStackedWidget" name="stackedWidget">
7676
<property name="currentIndex">
77-
<number>0</number>
77+
<number>3</number>
7878
</property>
7979
<widget class="QWidget" name="page_3">
8080
<layout class="QGridLayout" name="gridLayout_4">
@@ -172,7 +172,7 @@
172172
<enum>QFrame::Sunken</enum>
173173
</property>
174174
<property name="currentIndex">
175-
<number>1</number>
175+
<number>0</number>
176176
</property>
177177
<widget class="QWidget" name="page"/>
178178
<widget class="QWidget" name="page_2"/>
@@ -355,8 +355,8 @@
355355
<rect>
356356
<x>0</x>
357357
<y>0</y>
358-
<width>407</width>
359-
<height>394</height>
358+
<width>554</width>
359+
<height>397</height>
360360
</rect>
361361
</property>
362362
<layout class="QGridLayout" name="gridLayout_3">
@@ -409,7 +409,7 @@
409409
</property>
410410
</widget>
411411
</item>
412-
<item row="1" column="1">
412+
<item row="1" column="1" colspan="2">
413413
<layout class="QHBoxLayout" name="horizontalLayout_2">
414414
<item>
415415
<widget class="QLineEdit" name="leEditForm"/>
@@ -423,7 +423,24 @@
423423
</item>
424424
</layout>
425425
</item>
426-
<item row="1" column="2">
426+
<item row="1" column="3">
427+
<widget class="QPushButton" name="pbnIndex">
428+
<property name="text">
429+
<string>Create Spatial Index</string>
430+
</property>
431+
</widget>
432+
</item>
433+
<item row="3" column="0" colspan="4">
434+
<widget class="QLineEdit" name="leSpatialRefSys">
435+
<property name="readOnly">
436+
<bool>true</bool>
437+
</property>
438+
</widget>
439+
</item>
440+
<item row="2" column="1" colspan="2">
441+
<widget class="QLineEdit" name="leEditFormInit"/>
442+
</item>
443+
<item row="2" column="3">
427444
<widget class="QPushButton" name="pbnChangeSpatialRefSys">
428445
<property name="toolTip">
429446
<string>Specify the coordinate reference system of the layer's geometry.</string>
@@ -436,17 +453,10 @@
436453
</property>
437454
</widget>
438455
</item>
439-
<item row="1" column="3">
440-
<widget class="QPushButton" name="pbnIndex">
456+
<item row="2" column="0">
457+
<widget class="QLabel" name="label_3">
441458
<property name="text">
442-
<string>Create Spatial Index</string>
443-
</property>
444-
</widget>
445-
</item>
446-
<item row="2" column="0" colspan="4">
447-
<widget class="QLineEdit" name="leSpatialRefSys">
448-
<property name="readOnly">
449-
<bool>true</bool>
459+
<string>Init function</string>
450460
</property>
451461
</widget>
452462
</item>
@@ -650,7 +660,6 @@
650660
<tabstop>displayFieldComboBox</tabstop>
651661
<tabstop>leSpatialRefSys</tabstop>
652662
<tabstop>pbnIndex</tabstop>
653-
<tabstop>pbnChangeSpatialRefSys</tabstop>
654663
<tabstop>spinMinimumScale</tabstop>
655664
<tabstop>spinMaximumScale</tabstop>
656665
<tabstop>txtSubsetSQL</tabstop>

0 commit comments

Comments
 (0)
Please sign in to comment.