@@ -42,6 +42,8 @@ QgsAttributeForm::QgsAttributeForm( QgsVectorLayer* vl, const QgsFeature feature
4242 , mContext( context )
4343 , mFormNr( sFormCounter ++ )
4444 , mIsSaving( false )
45+ , mIsAddDialog( false )
46+ , mEditCommandMessage( tr( " Attributes changed" ) )
4547{
4648 init ();
4749 initPython ();
@@ -60,11 +62,16 @@ QgsAttributeForm::~QgsAttributeForm()
6062void QgsAttributeForm::hideButtonBox ()
6163{
6264 mButtonBox ->hide ();
65+
66+ // Make sure that changes are taken into account if somebody tries to figure out if there have been some
67+ connect ( mLayer , SIGNAL ( beforeModifiedCheck () ), this , SLOT ( save () ) );
6368}
6469
6570void QgsAttributeForm::showButtonBox ()
6671{
6772 mButtonBox ->show ();
73+
74+ disconnect ( mLayer , SIGNAL ( beforeModifiedCheck () ), this , SLOT ( save () ) );
6875}
6976
7077void QgsAttributeForm::addInterface ( QgsAttributeFormInterface* iface )
@@ -77,6 +84,13 @@ bool QgsAttributeForm::editable()
7784 return mFeature .isValid () && mLayer ->isEditable () ;
7885}
7986
87+ void QgsAttributeForm::setIsAddDialog ( bool isAddDialog )
88+ {
89+ mIsAddDialog = isAddDialog;
90+
91+ synchronizeEnabledState ();
92+ }
93+
8094void QgsAttributeForm::changeAttribute ( const QString& field, const QVariant& value )
8195{
8296 Q_FOREACH ( QgsWidgetWrapper* ww, mWidgets )
@@ -118,7 +132,8 @@ bool QgsAttributeForm::save()
118132 if ( !success )
119133 return false ;
120134
121- if ( mFeature .isValid () )
135+
136+ if ( mFeature .isValid () || mIsAddDialog )
122137 {
123138 bool doUpdate = false ;
124139
@@ -154,28 +169,43 @@ bool QgsAttributeForm::save()
154169
155170 if ( doUpdate )
156171 {
157- mLayer ->beginEditCommand ( tr ( " Attributes changed" ) );
158-
159- for ( int i = 0 ; i < dst.count (); ++i )
160- {
161- if ( dst[i] == src[i] || !src[i].isValid () )
162- continue ;
172+ mLayer ->beginEditCommand ( mEditCommandMessage );
163173
164- success &= mLayer ->changeAttributeValue ( mFeature .id (), i, dst[i], src[i] );
165- }
166-
167- if ( success )
174+ if ( mIsAddDialog )
168175 {
169- mLayer ->endEditCommand ();
170- mFeature .setAttributes ( dst );
176+ mFeature .setValid ( true );
177+ mLayer ->beginEditCommand ( mEditCommandMessage );
178+ bool res = mLayer ->addFeature ( updatedFeature );
179+ if ( res )
180+ mLayer ->endEditCommand ();
181+ else
182+ mLayer ->destroyEditCommand ();
171183 }
172184 else
173185 {
174- mLayer ->destroyEditCommand ();
186+ for ( int i = 0 ; i < dst.count (); ++i )
187+ {
188+ if ( dst[i] == src[i] || !src[i].isValid () )
189+ continue ;
190+
191+ success &= mLayer ->changeAttributeValue ( mFeature .id (), i, dst[i], src[i] );
192+ }
193+
194+ if ( success )
195+ {
196+ mLayer ->endEditCommand ();
197+ mFeature .setAttributes ( dst );
198+ }
199+ else
200+ {
201+ mLayer ->destroyEditCommand ();
202+ }
175203 }
176204 }
177205 }
178206
207+ emit featureSaved ( mFeature );
208+
179209 mIsSaving = false ;
180210
181211 return success;
@@ -227,21 +257,16 @@ void QgsAttributeForm::onAttributeDeleted( int idx )
227257
228258void QgsAttributeForm::synchronizeEnabledState ()
229259{
260+ bool isEditable = ( mFeature .isValid () || mIsAddDialog ) && mLayer ->isEditable ();
261+
230262 Q_FOREACH ( QgsWidgetWrapper* ww, mWidgets )
231263 {
232- if ( mFeature .isValid () && mLayer ->isEditable () )
233- {
234- ww->setEnabled ( true );
235- }
236- else
237- {
238- ww->setEnabled ( false );
239- }
264+ ww->setEnabled ( isEditable );
240265 }
241266
242267 QPushButton* okButton = mButtonBox ->button ( QDialogButtonBox::Ok );
243268 if ( okButton )
244- okButton->setEnabled ( mFeature . isValid () && mLayer -> isEditable () );
269+ okButton->setEnabled ( isEditable );
245270}
246271
247272void QgsAttributeForm::init ()
@@ -360,7 +385,6 @@ void QgsAttributeForm::init()
360385 connect ( mButtonBox , SIGNAL ( accepted () ), this , SLOT ( accept () ) );
361386 connect ( mButtonBox , SIGNAL ( rejected () ), this , SLOT ( resetValues () ) );
362387
363- connect ( mLayer , SIGNAL ( beforeModifiedCheck () ), this , SLOT ( save () ) );
364388 connect ( mLayer , SIGNAL ( editingStarted () ), this , SLOT ( synchronizeEnabledState () ) );
365389 connect ( mLayer , SIGNAL ( editingStopped () ), this , SLOT ( synchronizeEnabledState () ) );
366390
0 commit comments