/
qgsattributeform.h
443 lines (363 loc) · 13.8 KB
/
qgsattributeform.h
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/***************************************************************************
qgsattributeform.h
--------------------------------------
Date : 3.5.2014
Copyright : (C) 2014 Matthias Kuhn
Email : matthias at opengis dot ch
***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#ifndef QGSATTRIBUTEFORM_H
#define QGSATTRIBUTEFORM_H
#include "qgsfeature.h"
#include "qgis.h"
#include "qgsattributeeditorcontext.h"
#include "qgseditorwidgetwrapper.h"
#include <QWidget>
#include <QSvgWidget>
#include <QLabel>
#include <QDialogButtonBox>
#include "qgis_gui.h"
class QgsAttributeFormInterface;
class QgsAttributeFormEditorWidget;
class QgsMessageBar;
class QgsMessageBarItem;
class QgsWidgetWrapper;
class QgsTabWidget;
class QgsAttributeFormWidget;
/**
* \ingroup gui
* \class QgsAttributeForm
*/
class GUI_EXPORT QgsAttributeForm : public QWidget
{
Q_OBJECT
public:
//! Form modes
enum Mode
{
SingleEditMode, //!< Single edit mode, for editing a single feature
AddFeatureMode, /*!< Add feature mode, for setting attributes for a new feature. In this mode the dialog will be editable even with an invalid feature and
will add a new feature when the form is accepted. */
MultiEditMode, //!< Multi edit mode, for editing fields of multiple features at once
SearchMode, //!< Form values are used for searching/filtering the layer
AggregateSearchMode, //!< Form is in aggregate search mode, show each widget in this mode \since QGIS 3.0
IdentifyMode //!< Identify the feature \since QGIS 3.0
};
//! Filter types
enum FilterType
{
ReplaceFilter, //!< Filter should replace any existing filter
FilterAnd, //!< Filter should be combined using "AND"
FilterOr, //!< Filter should be combined using "OR"
};
explicit QgsAttributeForm( QgsVectorLayer *vl,
const QgsFeature &feature = QgsFeature(),
const QgsAttributeEditorContext &context = QgsAttributeEditorContext(),
QWidget *parent SIP_TRANSFERTHIS = nullptr );
~QgsAttributeForm() override;
const QgsFeature &feature() { return mFeature; }
/**
* Hides the button box (OK/Cancel) and enables auto-commit
* \note set Embed in QgsAttributeEditorContext in constructor instead
*/
// TODO QGIS 3.0 - make private
void hideButtonBox();
/**
* Shows the button box (OK/Cancel) and disables auto-commit
* \note set Embed in QgsAttributeEditorContext in constructor instead
*/
// TODO QGIS 3.0 - make private
void showButtonBox();
/**
* Disconnects the button box (OK/Cancel) from the accept/resetValues slots
* If this method is called, you have to create these connections from outside
*/
// TODO QGIS 3.0 - make private
void disconnectButtonBox();
/**
* Takes ownership
* \param iface
*/
void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
/**
* Returns the layer for which this form is shown
*
* \returns Layer
*/
QgsVectorLayer *layer() { return mLayer; }
/**
* Returns if the form is currently in editable mode.
*
* \returns Editable mode of this form
*/
bool editable();
/**
* Returns the current mode of the form.
* \since QGIS 2.16
* \see setMode()
*/
Mode mode() const { return mMode; }
/**
* Sets the current mode of the form.
* \param mode form mode
* \since QGIS 2.16
* \see mode()
*/
void setMode( Mode mode );
/**
* Sets the edit command message (Undo) that will be used when the dialog is accepted
*
* \param message The message
*/
void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
/**
* Intercepts keypress on custom form (escape should not close it)
*
* \param object The object for which the event has been sent
* \param event The event which is being filtered
*
* \returns true if the event has been handled (key was ESC)
*/
bool eventFilter( QObject *object, QEvent *event ) override;
/**
* Sets all feature IDs which are to be edited if the form is in multiedit mode
* \param fids feature ID list
* \since QGIS 2.16
*/
void setMultiEditFeatureIds( const QgsFeatureIds &fids );
/**
* Sets the message bar to display feedback from the form in. This is used in the search/filter
* mode to display the count of selected features.
* \param messageBar target message bar
* \since QGIS 2.16
*/
void setMessageBar( QgsMessageBar *messageBar );
/**
* The aggregate filter is only useful if the form is in AggregateFilter mode.
* In this case it will return a combined expression according to the chosen filters
* on all attribute widgets.
*
* \since QGIS 3.0
*/
QString aggregateFilter() const;
signals:
/**
* Notifies about changes of attributes
*
* \param attribute The name of the attribute that changed.
* \param value The new value of the attribute.
* \deprecated since 3.0
*/
Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value );
/**
* Notifies about changes of attributes
*
* \param attribute The name of the attribute that changed.
* \param value The new value of the attribute.
* \param attributeChanged If true, it corresponds to an actual change of the feature attribute
* \since QGIS 3.0.1
*/
void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
/**
* Will be emitted before the feature is saved. Use this signal to perform sanity checks.
* You can set the parameter ok to false to notify the form that you don't want it to be saved.
* If you want the form to be saved, leave the parameter untouched.
*
* \param ok Set this parameter to false if you don't want the form to be saved
* \note not available in Python bindings
*/
void beforeSave( bool &ok ) SIP_SKIP;
/**
* Is emitted, when a feature is changed or added
*/
void featureSaved( const QgsFeature &feature );
/**
* Is emitted when a filter expression is set using the form.
* \param expression filter expression
* \param type filter type
* \since QGIS 2.16
*/
void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
/**
* Emitted when the form changes mode.
* \param mode new mode
*/
void modeChanged( QgsAttributeForm::Mode mode );
/**
* Emitted when the user selects the close option from the form's button bar.
* \since QGIS 2.16
*/
void closed();
/**
* Emitted when the user chooses to zoom to a filtered set of features.
* \since QGIS 3.0
*/
void zoomToFeatures( const QString &filter );
/**
* Emitted when the user chooses to flash a filtered set of features.
* \since QGIS 3.0
*/
void flashFeatures( const QString &filter );
public slots:
/**
* Call this to change the content of a given attribute. Will update the editor(s) related to this field.
*
* \param field The field to change
* \param value The new value
* \param hintText A hint text for non existent joined features
*/
void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
/**
* Update all editors to correspond to a different feature.
*
* \param feature The feature which will be represented by the form
*/
void setFeature( const QgsFeature &feature );
/**
* Save all the values from the editors to the layer.
*
* \returns True if successful
*/
bool save();
/**
* Sets all values to the values of the current feature
*/
void resetValues();
/**
* Resets the search/filter form values.
* \since QGIS 2.16
*/
void resetSearch();
/**
* reload current feature
*/
void refreshFeature();
private slots:
void onAttributeChanged( const QVariant &value );
void onAttributeAdded( int idx );
void onAttributeDeleted( int idx );
void onUpdatedFields();
void onConstraintStatusChanged( const QString &constraint,
const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
void preventFeatureRefresh();
void synchronizeEnabledState();
void layerSelectionChanged();
//! Save multi edit changes
bool saveMultiEdits();
void resetMultiEdit( bool promptToSave = false );
void multiEditMessageClicked( const QString &link );
void filterAndTriggered();
void filterOrTriggered();
void filterTriggered();
void searchZoomTo();
void searchFlash();
void searchSetSelection();
void searchAddToSelection();
void searchRemoveFromSelection();
void searchIntersectSelection();
private:
void init();
void cleanPython();
void initPython();
void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
bool fieldIsEditable( int fieldIndex ) const;
bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
struct WidgetInfo
{
QWidget *widget = nullptr;
QString labelText;
QString hint;
bool labelOnTop = false;
bool labelAlignRight = false;
bool showLabel = true;
};
WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
void addWidgetWrapper( QgsEditorWidgetWrapper *eww );
/**
* Creates widget wrappers for all suitable widgets found.
* Called once maximally.
*/
void createWrappers();
void afterWidgetInit();
void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
//! Save single feature or add feature edits
bool saveEdits();
int messageTimeout();
void clearMultiEditMessages();
void pushSelectedFeaturesMessage();
void runSearchSelect( QgsVectorLayer::SelectBehavior behavior );
QString createFilterExpression() const;
//! constraints management
void updateAllConstraints();
void updateConstraints( QgsEditorWidgetWrapper *w );
void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
bool currentFormFeature( QgsFeature &feature );
bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
QgsVectorLayer *mLayer = nullptr;
QgsFeature mFeature;
QgsMessageBar *mMessageBar = nullptr;
bool mOwnsMessageBar;
QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
QList<QgsWidgetWrapper *> mWidgets;
QgsAttributeEditorContext mContext;
QDialogButtonBox *mButtonBox = nullptr;
QWidget *mSearchButtonBox = nullptr;
QList<QgsAttributeFormInterface *> mInterfaces;
QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
QList< QgsAttributeFormWidget *> mFormWidgets;
QgsExpressionContext mExpressionContext;
QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
bool mValuesInitialized = false;
bool mDirty = false;
bool mIsSettingFeature = false;
struct ContainerInformation
{
ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
: tabWidget( tabWidget )
, widget( widget )
, expression( expression )
, isVisible( true )
{}
ContainerInformation( QWidget *widget, const QgsExpression &expression )
: widget( widget )
, expression( expression )
, isVisible( true )
{}
QgsTabWidget *tabWidget = nullptr;
QWidget *widget = nullptr;
QgsExpression expression;
bool isVisible;
void apply( QgsExpressionContext *expressionContext );
};
void registerContainerInformation( ContainerInformation *info );
void updateIcon( QgsEditorWidgetWrapper *eww );
void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
// Contains information about tabs and groupboxes, their visibility state visibility conditions
QVector<ContainerInformation *> mContainerVisibilityInformation;
QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
// Variables below are used for Python
static int sFormCounter;
int mFormNr;
QString mPyFormVarName;
//! Set to true while saving to prevent recursive saves
bool mIsSaving;
//! Flag to prevent refreshFeature() to change mFeature
bool mPreventFeatureRefresh;
bool mIsSettingMultiEditFeatures;
QgsFeatureIds mMultiEditFeatureIds;
bool mUnsavedMultiEditChanges;
QString mEditCommandMessage;
Mode mMode;
QMap<QWidget *, QSvgWidget *> mIconMap;
friend class TestQgsDualView;
friend class TestQgsAttributeForm;
};
#endif // QGSATTRIBUTEFORM_H