QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsattributeform.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsattributeform.h
3  --------------------------------------
4  Date : 3.5.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias at opengis dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSATTRIBUTEFORM_H
17 #define QGSATTRIBUTEFORM_H
18 
19 #include "qgsfeature.h"
20 #include "qgis_sip.h"
22 #include "qgseditorwidgetwrapper.h"
23 
24 #include <QWidget>
25 #include <QSvgWidget>
26 #include <QLabel>
27 #include <QDialogButtonBox>
28 #include "qgis_gui.h"
29 
30 
33 class QgsMessageBar;
34 class QgsMessageBarItem;
35 class QgsWidgetWrapper;
36 class QgsTabWidget;
39 
44 class GUI_EXPORT QgsAttributeForm : public QWidget
45 {
46  Q_OBJECT
47 
48  public:
49 
51  enum Mode
52  {
54  AddFeatureMode,
59  IdentifyMode
60  };
61 
64  {
68  };
69 
70  explicit QgsAttributeForm( QgsVectorLayer *vl,
71  const QgsFeature &feature = QgsFeature(),
73  QWidget *parent SIP_TRANSFERTHIS = nullptr );
74  ~QgsAttributeForm() override;
75 
76  const QgsFeature &feature() { return mFeature; }
77 
84  void displayWarning( const QString &message );
85 
86  // TODO QGIS 4.0 - make private
87 
92  void hideButtonBox();
93 
94  // TODO QGIS 4.0 - make private
95 
100  void showButtonBox();
101 
102  // TODO QGIS 4.0 - make private
103 
108  void disconnectButtonBox();
109 
114  void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
115 
121  QgsVectorLayer *layer() { return mLayer; }
122 
128  bool editable();
129 
135  QgsAttributeEditorContext::Mode mode() const { return mMode; }
136 
143  void setMode( QgsAttributeEditorContext::Mode mode );
144 
150  void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
151 
160  bool eventFilter( QObject *object, QEvent *event ) override;
161 
167  void setMultiEditFeatureIds( const QgsFeatureIds &fids );
168 
175  void setMessageBar( QgsMessageBar *messageBar );
176 
184  QString aggregateFilter() const;
185 
186  signals:
187 
196  Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
197 
206  void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
207 
216  void beforeSave( bool &ok ) SIP_SKIP;
217 
221  void featureSaved( const QgsFeature &feature );
222 
229  void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
230 
235  void modeChanged( QgsAttributeEditorContext::Mode mode );
236 
241  void closed();
242 
247  void zoomToFeatures( const QString &filter );
248 
253  void flashFeatures( const QString &filter );
254 
255  public slots:
256 
264  void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
265 
271  void setFeature( const QgsFeature &feature );
272 
278  bool save();
279 
283  void resetValues();
284 
289  void resetSearch();
290 
294  void refreshFeature();
295 
305  void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
306 
307 
308  private slots:
309  void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
310  void onAttributeAdded( int idx );
311  void onAttributeDeleted( int idx );
312  void onUpdatedFields();
313  void onConstraintStatusChanged( const QString &constraint,
314  const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
315  void preventFeatureRefresh();
316  void synchronizeEnabledState();
317  void layerSelectionChanged();
318 
320  bool saveMultiEdits();
321  void resetMultiEdit( bool promptToSave = false );
322  void multiEditMessageClicked( const QString &link );
323 
324  void filterAndTriggered();
325  void filterOrTriggered();
326  void filterTriggered();
327 
328  void searchZoomTo();
329  void searchFlash();
330  void searchSetSelection();
331  void searchAddToSelection();
332  void searchRemoveFromSelection();
333  void searchIntersectSelection();
334 
335  private:
336  void init();
337 
338  void cleanPython();
339 
340  void initPython();
341 
342  void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
343 
344  bool fieldIsEditable( int fieldIndex ) const;
345 
346  bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
347 
348  void updateDefaultValueDependencies();
349 
350  struct WidgetInfo
351  {
352  QWidget *widget = nullptr;
353  QString labelText;
354  QString toolTip;
355  QString hint;
356  bool labelOnTop = false;
357  bool labelAlignRight = false;
358  bool showLabel = true;
359  };
360 
361  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
362 
363  void addWidgetWrapper( QgsEditorWidgetWrapper *eww );
364 
369  void createWrappers();
370  void afterWidgetInit();
371 
372  void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
373 
375  bool saveEdits();
376 
378  void createDefaultValueDependencies();
379 
381  bool updateDefaultValues( const int originIdx );
382 
383  int messageTimeout();
384  void clearMultiEditMessages();
385  void pushSelectedFeaturesMessage();
386  void runSearchSelect( QgsVectorLayer::SelectBehavior behavior );
387 
388  QString createFilterExpression() const;
389 
391  void updateAllConstraints();
392  void updateConstraints( QgsEditorWidgetWrapper *w );
393  void updateContainersVisibility();
394  void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
395  void updateLabels();
396  bool currentFormFeature( QgsFeature &feature );
397  bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
398  QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
399 
400  QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context );
401 
402  QgsVectorLayer *mLayer = nullptr;
403  QgsFeature mFeature;
404  QgsMessageBar *mMessageBar = nullptr;
405  bool mOwnsMessageBar;
406  QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
407  QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
408  QList<QgsWidgetWrapper *> mWidgets;
409  QgsAttributeEditorContext mContext;
410  QDialogButtonBox *mButtonBox = nullptr;
411  QWidget *mSearchButtonBox = nullptr;
412  QList<QgsAttributeFormInterface *> mInterfaces;
413  QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
414  QList< QgsAttributeFormWidget *> mFormWidgets;
415  QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
416  QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
417  bool mValuesInitialized = false;
418  bool mDirty = false;
419  bool mIsSettingFeature = false;
420 
421  struct ContainerInformation
422  {
423  ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
424  : tabWidget( tabWidget )
425  , widget( widget )
426  , expression( expression )
427  , isVisible( true )
428  {}
429 
430  ContainerInformation( QWidget *widget, const QgsExpression &expression )
431  : widget( widget )
432  , expression( expression )
433  , isVisible( true )
434  {}
435 
436  QgsTabWidget *tabWidget = nullptr;
437  QWidget *widget = nullptr;
438  QgsExpression expression;
439  bool isVisible;
440 
441  void apply( QgsExpressionContext *expressionContext );
442  };
443 
444  void registerContainerInformation( ContainerInformation *info );
445 
446  void updateIcon( QgsEditorWidgetWrapper *eww );
447 
448  void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
449 
450  // Contains information about tabs and groupboxes, their visibility state visibility conditions
451  QVector<ContainerInformation *> mContainerVisibilityInformation;
452  QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
453 
454  // Variables below are used for Python
455  static int sFormCounter;
456  int mFormNr;
457  QString mPyFormVarName;
458 
460  bool mIsSaving;
461 
463  bool mPreventFeatureRefresh;
464 
465  bool mIsSettingMultiEditFeatures;
466 
467  QgsFeatureIds mMultiEditFeatureIds;
468  bool mUnsavedMultiEditChanges;
469 
470  QString mEditCommandMessage;
471 
473 
474  QMap<QWidget *, QSvgWidget *> mIconMap;
475 
480  QMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
481 
483  QList<int> mAlreadyUpdatedFields;
484 
485  friend class TestQgsDualView;
486  friend class TestQgsAttributeForm;
487 };
488 
489 #endif // QGSATTRIBUTEFORM_H
490 
QgsAttributeEditorElement
Definition: qgsattributeeditorelement.h:38
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsAttributeFormInterface
Definition: qgsattributeforminterface.h:28
qgseditorwidgetwrapper.h
QgsWidgetWrapper
Definition: qgswidgetwrapper.h:52
QgsRelationWidgetWrapper
Definition: qgsrelationwidgetwrapper.h:30
QgsAttributeForm::feature
const QgsFeature & feature()
Definition: qgsattributeform.h:76
qgsfeature.h
QgsAttributeForm::setEditCommandMessage
void setEditCommandMessage(const QString &message)
Sets the edit command message (Undo) that will be used when the dialog is accepted.
Definition: qgsattributeform.h:150
QgsAttributeForm::AggregateSearchMode
@ AggregateSearchMode
Form is in aggregate search mode, show each widget in this mode.
Definition: qgsattributeform.h:58
QgsEditorWidgetWrapper::ConstraintResult
ConstraintResult
Result of constraint checks.
Definition: qgseditorwidgetwrapper.h:60
QgsEditorWidgetWrapper
Definition: qgseditorwidgetwrapper.h:47
QgsAttributeForm::layer
QgsVectorLayer * layer()
Returns the layer for which this form is shown.
Definition: qgsattributeform.h:121
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsAttributeEditorContext::Mode
Mode
modes
Definition: qgsattributeeditorcontext.h:47
QgsAttributeForm::mode
QgsAttributeEditorContext::Mode mode() const
Returns the current mode of the form.
Definition: qgsattributeform.h:135
QgsAttributeForm::FilterOr
@ FilterOr
Filter should be combined using "OR".
Definition: qgsattributeform.h:67
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsMessageBarItem
Definition: qgsmessagebaritem.h:38
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsMessageBar
Definition: qgsmessagebar.h:60
QgsAttributeForm::MultiEditMode
@ MultiEditMode
Multi edit mode, for editing fields of multiple features at once.
Definition: qgsattributeform.h:56
QgsAttributeForm::SingleEditMode
@ SingleEditMode
Single edit mode, for editing a single feature.
Definition: qgsattributeform.h:53
qgsattributeeditorcontext.h
QgsVectorLayer::SelectBehavior
SelectBehavior
Selection behavior.
Definition: qgsvectorlayer.h:410
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsTabWidget
Definition: qgstabwidget.h:29
QgsAttributeForm::SearchMode
@ SearchMode
Form values are used for searching/filtering the layer.
Definition: qgsattributeform.h:57
QgsAttributeForm::FilterType
FilterType
Filter types.
Definition: qgsattributeform.h:63
QgsAttributeForm::FilterAnd
@ FilterAnd
Filter should be combined using "AND".
Definition: qgsattributeform.h:66
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsAttributeFormEditorWidget
Definition: qgsattributeformeditorwidget.h:42
QgsRelation
Definition: qgsrelation.h:41
QgsFeature
Definition: qgsfeature.h:55
QgsAttributeForm
Definition: qgsattributeform.h:44
QgsExpression
Definition: qgsexpression.h:113
QgsAttributeEditorContext
Definition: qgsattributeeditorcontext.h:40
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsAttributeForm::ReplaceFilter
@ ReplaceFilter
Filter should replace any existing filter.
Definition: qgsattributeform.h:65
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsAttributeFormWidget
Definition: qgsattributeformwidget.h:38
QgsFeatureId
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
QgsAttributeForm::Mode
Mode
Form modes.
Definition: qgsattributeform.h:51