31
31
#include < QMouseEvent>
32
32
#include < QSettings>
33
33
34
- #ifdef Q_OS_WIN
35
- #include < qgisapp.h>
36
- #endif
37
-
38
34
QgsMapToolAddFeature::QgsMapToolAddFeature ( QgsMapCanvas* canvas, CaptureMode tool ): QgsMapToolCapture( canvas, tool )
39
35
{
40
36
@@ -45,8 +41,79 @@ QgsMapToolAddFeature::~QgsMapToolAddFeature()
45
41
46
42
}
47
43
44
+ bool QgsMapToolAddFeature::addFeature ( QgsVectorLayer *vlayer, QgsFeature *f )
45
+ {
46
+ bool res = false ;
47
+ QgsVectorDataProvider* provider = vlayer->dataProvider ();
48
+
49
+ QSettings settings;
50
+ bool reuseLastValues = settings.value ( " /qgis/digitizing/reuseLastValues" , false ).toBool ();
51
+ QgsDebugMsg ( QString ( " reuseLastValues: %1" ).arg ( reuseLastValues ) );
52
+
53
+ // add the fields to the QgsFeature
54
+ const QgsFieldMap fields = vlayer->pendingFields ();
55
+ for ( QgsFieldMap::const_iterator it = fields.constBegin (); it != fields.constEnd (); ++it )
56
+ {
57
+ if ( reuseLastValues && mLastUsedValues .contains ( vlayer ) && mLastUsedValues [ vlayer ].contains ( it.key () ) )
58
+ {
59
+ QgsDebugMsg ( QString ( " reusing %1 for %2" ).arg ( mLastUsedValues [ vlayer ][ it.key ()].toString () ).arg ( it.key () ) );
60
+ f->addAttribute ( it.key (), mLastUsedValues [ vlayer ][ it.key ()] );
61
+ }
62
+ else
63
+ {
64
+ f->addAttribute ( it.key (), provider->defaultValue ( it.key () ) );
65
+ }
66
+ }
67
+
68
+ // show the dialog to enter attribute values
69
+ bool isDisabledAttributeValuesDlg = settings.value ( " /qgis/digitizing/disable_enter_attribute_values_dialog" , true ).toBool ();
70
+ if ( isDisabledAttributeValuesDlg )
71
+ {
72
+ res = vlayer->addFeature ( *f );
73
+ }
74
+ else
75
+ {
76
+ QgsAttributeDialog *mypDialog = new QgsAttributeDialog ( vlayer, f );
77
+
78
+ QgsAttributeMap origValues;
79
+ if ( reuseLastValues )
80
+ origValues = f->attributeMap ();
81
+
82
+ if ( mypDialog->exec () )
83
+ {
84
+ if ( reuseLastValues )
85
+ {
86
+ for ( QgsFieldMap::const_iterator it = fields.constBegin (); it != fields.constEnd (); ++it )
87
+ {
88
+ const QgsAttributeMap &newValues = f->attributeMap ();
89
+ if ( newValues.contains ( it.key () )
90
+ && origValues.contains ( it.key () )
91
+ && origValues[ it.key ()] != newValues[ it.key ()] )
92
+ {
93
+ QgsDebugMsg ( QString ( " saving %1 for %2" ).arg ( mLastUsedValues [ vlayer ][ it.key ()].toString () ).arg ( it.key () ) );
94
+ mLastUsedValues [ vlayer ][ it.key ()] = newValues[ it.key ()];
95
+ }
96
+ }
97
+ }
98
+
99
+ res = vlayer->addFeature ( *f );
100
+ }
101
+ else
102
+ {
103
+ QgsDebugMsg ( " Adding feature to layer failed" );
104
+ res = false ;
105
+ }
106
+
107
+ mypDialog->deleteLater ();
108
+ }
109
+
110
+ return res;
111
+ }
112
+
48
113
void QgsMapToolAddFeature::canvasReleaseEvent ( QMouseEvent * e )
49
114
{
115
+ QgsDebugMsg ( " entered." );
116
+
50
117
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas ->currentLayer () );
51
118
52
119
if ( !vlayer )
@@ -160,45 +227,21 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
160
227
}
161
228
162
229
f->setGeometryAndOwnership ( &wkb[0 ], size );
163
- // add the fields to the QgsFeature
164
- const QgsFieldMap fields = vlayer->pendingFields ();
165
- for ( QgsFieldMap::const_iterator it = fields.constBegin (); it != fields.constEnd (); ++it )
166
- {
167
- f->addAttribute ( it.key (), provider->defaultValue ( it.key () ) );
168
- }
169
230
170
231
vlayer->beginEditCommand ( tr ( " Feature added" ) );
171
232
172
- // show the dialog to enter attribute values
173
- QSettings settings;
174
- bool isDisabledAttributeValuesDlg = settings.value ( " /qgis/digitizing/disable_enter_attribute_values_dialog" , false ).toBool ();
175
- if ( isDisabledAttributeValuesDlg )
233
+ if ( addFeature ( vlayer, f ) )
176
234
{
177
- QgsDebugMsg ( " Adding feature to layer" );
178
- vlayer->addFeature ( *f );
179
235
vlayer->endEditCommand ();
180
236
}
181
237
else
182
238
{
183
- QgsAttributeDialog *mypDialog = new QgsAttributeDialog ( vlayer, f );
184
- if ( mypDialog->exec () )
185
- {
186
- QgsDebugMsg ( " Adding feature to layer" );
187
- vlayer->addFeature ( *f );
188
- vlayer->endEditCommand ();
189
- }
190
- else
191
- {
192
- vlayer->destroyEditCommand ();
193
- QgsDebugMsg ( " Adding feature to layer failed" );
194
- delete f;
195
- }
196
- delete mypDialog;
239
+ delete f;
240
+ vlayer->destroyEditCommand ();
197
241
}
198
242
199
243
mCanvas ->refresh ();
200
244
}
201
-
202
245
}
203
246
else if ( mode () == CaptureLine || mode () == CapturePolygon )
204
247
{
@@ -395,7 +438,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
395
438
position += sizeof ( int );
396
439
double x, y;
397
440
QList<QgsPoint>::iterator it;
398
- for ( it = begin (); it != end (); ++it )// add the captured points to the polygon
441
+ for ( it = begin (); it != end (); ++it ) // add the captured points to the polygon
399
442
{
400
443
QgsPoint savePoint = *it;
401
444
x = savePoint.x ();
@@ -441,53 +484,26 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
441
484
{
442
485
QMessageBox::critical ( 0 , tr ( " Error" ), tr ( " An error was reported during intersection removal" ) );
443
486
}
444
-
445
-
446
487
}
447
488
448
- // add the fields to the QgsFeature
449
- const QgsFieldMap fields = vlayer->pendingFields ();
450
- for ( QgsFieldMap::const_iterator it = fields.begin (); it != fields.end (); ++it )
451
- {
452
- f->addAttribute ( it.key (), provider->defaultValue ( it.key () ) );
453
- }
489
+ vlayer->beginEditCommand ( tr ( " Feature added" ) );
454
490
455
- QSettings settings;
456
- bool isDisabledAttributeValuesDlg = settings.value ( " /qgis/digitizing/disable_enter_attribute_values_dialog" , false ).toBool ();
457
- if ( isDisabledAttributeValuesDlg )
491
+ if ( addFeature ( vlayer, f ) )
458
492
{
459
- vlayer->beginEditCommand ( tr ( " Feature added" ) );
460
- if ( vlayer->addFeature ( *f ) )
493
+ // add points to other features to keep topology up-to-date
494
+ int topologicalEditing = QgsProject::instance ()->readNumEntry ( " Digitizing" , " /TopologicalEditing" , 0 );
495
+ if ( topologicalEditing )
461
496
{
462
- // add points to other features to keep topology up-to-date
463
- int topologicalEditing = QgsProject::instance ()->readNumEntry ( " Digitizing" , " /TopologicalEditing" , 0 );
464
- if ( topologicalEditing )
465
- {
466
- vlayer->addTopologicalPoints ( f->geometry () );
467
- }
497
+ vlayer->addTopologicalPoints ( f->geometry () );
468
498
}
499
+
469
500
vlayer->endEditCommand ();
470
501
}
471
502
else
472
503
{
473
- QgsAttributeDialog * mypDialog = new QgsAttributeDialog ( vlayer, f );
474
- if ( mypDialog->exec () )
475
- {
476
- vlayer->beginEditCommand ( tr ( " Feature added" ) );
477
- if ( vlayer->addFeature ( *f ) )
478
- {
479
- // add points to other features to keep topology up-to-date
480
- int topologicalEditing = QgsProject::instance ()->readNumEntry ( " Digitizing" , " /TopologicalEditing" , 0 );
481
- if ( topologicalEditing )
482
- {
483
- vlayer->addTopologicalPoints ( f->geometry () );
484
- }
485
- }
486
- vlayer->endEditCommand ();
487
- }
488
- mypDialog->deleteLater ();
504
+ delete f;
505
+ vlayer->destroyEditCommand ();
489
506
}
490
- delete f;
491
507
492
508
stopCapturing ();
493
509
}
0 commit comments