-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsfeatureaction.cpp
259 lines (213 loc) · 8.18 KB
/
qgsfeatureaction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/***************************************************************************
qgsfeatureaction.cpp - description
-------------------
begin : 2010-09-20
copyright : (C) 2010 by Juergen E. Fischer
email : jef at norbit dot de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgisapp.h"
#include "qgsattributedialog.h"
#include "qgsdistancearea.h"
#include "qgsfeatureaction.h"
#include "qgsguivectorlayertools.h"
#include "qgsidentifyresultsdialog.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsproject.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsactionmanager.h"
#include "qgsaction.h"
#include "qgsvectorlayerutils.h"
#include "qgssettings.h"
#include <QPushButton>
QgsFeatureAction::QgsFeatureAction( const QString &name, QgsFeature &f, QgsVectorLayer *layer, const QUuid &actionId, int defaultAttr, QObject *parent )
: QAction( name, parent )
, mLayer( layer )
, mFeature( &f )
, mActionId( actionId )
, mIdx( defaultAttr )
, mFeatureSaved( false )
{
}
void QgsFeatureAction::execute()
{
mLayer->actions()->doAction( mActionId, *mFeature, mIdx );
}
QgsAttributeDialog *QgsFeatureAction::newDialog( bool cloneFeature )
{
QgsFeature *f = cloneFeature ? new QgsFeature( *mFeature ) : mFeature;
QgsAttributeEditorContext context;
QgsDistanceArea myDa;
myDa.setSourceCrs( mLayer->crs() );
myDa.setEllipsoidalMode( true );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );
context.setDistanceArea( myDa );
context.setVectorLayerTools( QgisApp::instance()->vectorLayerTools() );
context.setFormMode( QgsAttributeEditorContext::StandaloneDialog );
QgsAttributeDialog *dialog = new QgsAttributeDialog( mLayer, f, cloneFeature, parentWidget(), true, context );
dialog->setWindowFlags( dialog->windowFlags() | Qt::Tool );
QList<QgsAction> actions = mLayer->actions()->actions( QStringLiteral( "Feature" ) );
if ( !actions.isEmpty() )
{
dialog->setContextMenuPolicy( Qt::ActionsContextMenu );
QAction *a = new QAction( tr( "Run actions" ), dialog );
a->setEnabled( false );
dialog->addAction( a );
Q_FOREACH ( const QgsAction &action, actions )
{
if ( !action.runable() )
continue;
QgsFeature &feat = const_cast<QgsFeature &>( *dialog->feature() );
QgsFeatureAction *a = new QgsFeatureAction( action.name(), feat, mLayer, action.id(), -1, dialog );
dialog->addAction( a );
connect( a, SIGNAL( triggered() ), a, SLOT( execute() ) );
QAbstractButton *pb = dialog->findChild<QAbstractButton *>( action.name() );
if ( pb )
connect( pb, SIGNAL( clicked() ), a, SLOT( execute() ) );
}
}
return dialog;
}
bool QgsFeatureAction::viewFeatureForm( QgsHighlight *h )
{
if ( !mLayer )
return false;
QgsAttributeDialog *dialog = newDialog( true );
dialog->setHighlight( h );
// delete the dialog when it is closed
dialog->setAttribute( Qt::WA_DeleteOnClose );
dialog->show();
return true;
}
bool QgsFeatureAction::editFeature( bool showModal )
{
if ( !mLayer )
return false;
if ( showModal )
{
std::unique_ptr<QgsAttributeDialog> dialog( newDialog( false ) );
if ( !mFeature->isValid() )
dialog->setMode( QgsAttributeForm::AddFeatureMode );
int rv = dialog->exec();
mFeature->setAttributes( dialog->feature()->attributes() );
return rv;
}
else
{
QgsAttributeDialog *dialog = newDialog( false );
if ( !mFeature->isValid() )
dialog->setMode( QgsAttributeForm::AddFeatureMode );
// delete the dialog when it is closed
dialog->setAttribute( Qt::WA_DeleteOnClose );
dialog->show();
}
return true;
}
bool QgsFeatureAction::addFeature( const QgsAttributeMap &defaultAttributes, bool showModal )
{
if ( !mLayer || !mLayer->isEditable() )
return false;
QgsSettings settings;
bool reuseLastValues = settings.value( QStringLiteral( "qgis/digitizing/reuseLastValues" ), false ).toBool();
QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) );
QgsFields fields = mLayer->fields();
QgsAttributeMap initialAttributeValues;
for ( int idx = 0; idx < fields.count(); ++idx )
{
QVariant v;
if ( defaultAttributes.contains( idx ) )
{
initialAttributeValues.insert( idx, defaultAttributes.value( idx ) );
}
else if ( reuseLastValues && sLastUsedValues.contains( mLayer ) && sLastUsedValues[ mLayer ].contains( idx ) )
{
initialAttributeValues.insert( idx, sLastUsedValues[ mLayer ][idx] );
}
}
// create new feature template - this will initialize the attributes to valid values, handling default
// values and field constraints
QgsExpressionContext context = mLayer->createExpressionContext();
QgsFeature newFeature = QgsVectorLayerUtils::createFeature( mLayer, mFeature->geometry(), initialAttributeValues,
&context );
*mFeature = newFeature;
//show the dialog to enter attribute values
//only show if enabled in settings and layer has fields
bool isDisabledAttributeValuesDlg = ( fields.count() == 0 ) || settings.value( QStringLiteral( "qgis/digitizing/disable_enter_attribute_values_dialog" ), false ).toBool();
// override application-wide setting with any layer setting
switch ( mLayer->editFormConfig().suppress() )
{
case QgsEditFormConfig::SuppressOn:
isDisabledAttributeValuesDlg = true;
break;
case QgsEditFormConfig::SuppressOff:
isDisabledAttributeValuesDlg = false;
break;
case QgsEditFormConfig::SuppressDefault:
break;
}
if ( isDisabledAttributeValuesDlg )
{
mLayer->beginEditCommand( text() );
mFeatureSaved = mLayer->addFeature( *mFeature );
if ( mFeatureSaved )
mLayer->endEditCommand();
else
mLayer->destroyEditCommand();
}
else
{
QgsAttributeDialog *dialog = newDialog( false );
// delete the dialog when it is closed
dialog->setAttribute( Qt::WA_DeleteOnClose );
dialog->setMode( QgsAttributeForm::AddFeatureMode );
dialog->setEditCommandMessage( text() );
connect( dialog->attributeForm(), SIGNAL( featureSaved( const QgsFeature & ) ), this, SLOT( onFeatureSaved( const QgsFeature & ) ) );
if ( !showModal )
{
setParent( dialog ); // keep dialog until the dialog is closed and destructed
dialog->show();
mFeature = nullptr;
return true;
}
dialog->exec();
}
// Will be set in the onFeatureSaved SLOT
return mFeatureSaved;
}
void QgsFeatureAction::onFeatureSaved( const QgsFeature &feature )
{
QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( sender() );
Q_UNUSED( form ) // only used for Q_ASSERT
Q_ASSERT( form );
// Assign provider generated values
if ( mFeature )
*mFeature = feature;
mFeatureSaved = true;
QgsSettings settings;
bool reuseLastValues = settings.value( QStringLiteral( "qgis/digitizing/reuseLastValues" ), false ).toBool();
QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) );
if ( reuseLastValues )
{
QgsFields fields = mLayer->fields();
for ( int idx = 0; idx < fields.count(); ++idx )
{
QgsAttributes newValues = feature.attributes();
QgsAttributeMap origValues = sLastUsedValues[ mLayer ];
if ( origValues[idx] != newValues.at( idx ) )
{
QgsDebugMsg( QString( "saving %1 for %2" ).arg( sLastUsedValues[ mLayer ][idx].toString() ).arg( idx ) );
sLastUsedValues[ mLayer ][idx] = newValues.at( idx );
}
}
}
}
QHash<QgsVectorLayer *, QgsAttributeMap> QgsFeatureAction::sLastUsedValues;