QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  QgsFeature currentFormFeature() const { return mCurrentFormFeature; }
85 
92  void displayWarning( const QString &message );
93 
94  // TODO QGIS 4.0 - make private
95 
100  void hideButtonBox();
101 
102  // TODO QGIS 4.0 - make private
103 
108  void showButtonBox();
109 
110  // TODO QGIS 4.0 - make private
111 
116  void disconnectButtonBox();
117 
122  void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
123 
129  QgsVectorLayer *layer() { return mLayer; }
130 
136  bool editable();
137 
143  QgsAttributeEditorContext::Mode mode() const { return mMode; }
144 
151  void setMode( QgsAttributeEditorContext::Mode mode );
152 
158  void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
159 
168  bool eventFilter( QObject *object, QEvent *event ) override;
169 
175  void setMultiEditFeatureIds( const QgsFeatureIds &fids );
176 
183  void setMessageBar( QgsMessageBar *messageBar );
184 
192  QString aggregateFilter() const;
193 
200  void setExtraContextScope( QgsExpressionContextScope *extraScope SIP_TRANSFER );
201 
202  signals:
203 
212  Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
213 
222  void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
223 
232  void beforeSave( bool &ok ) SIP_SKIP;
233 
237  void featureSaved( const QgsFeature &feature );
238 
245  void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
246 
252 
257  void closed();
258 
263  void zoomToFeatures( const QString &filter );
264 
269  void flashFeatures( const QString &filter );
270 
271  public slots:
272 
280  void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
281 
287  void setFeature( const QgsFeature &feature );
288 
294  bool save();
295 
299  void resetValues();
300 
305  void resetSearch();
306 
310  void refreshFeature();
311 
321  void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
322 
323 
324  private slots:
325  void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
326  void onAttributeAdded( int idx );
327  void onAttributeDeleted( int idx );
328  void onUpdatedFields();
329  void onConstraintStatusChanged( const QString &constraint,
330  const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
331  void preventFeatureRefresh();
332  void synchronizeEnabledState();
333  void layerSelectionChanged();
334 
336  bool saveMultiEdits();
337  void resetMultiEdit( bool promptToSave = false );
338  void multiEditMessageClicked( const QString &link );
339 
340  void filterAndTriggered();
341  void filterOrTriggered();
342  void filterTriggered();
343 
344  void searchZoomTo();
345  void searchFlash();
346  void searchSetSelection();
347  void searchAddToSelection();
348  void searchRemoveFromSelection();
349  void searchIntersectSelection();
350 
351  private:
352  void init();
353 
354  void cleanPython();
355 
356  void initPython();
357 
358  void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
359 
360  bool fieldIsEditable( int fieldIndex ) const;
361 
362  bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
363 
364  void updateDefaultValueDependencies();
365 
366  struct WidgetInfo
367  {
368  QWidget *widget = nullptr;
369  QString labelText;
370  QString toolTip;
371  QString hint;
372  bool labelOnTop = false;
373  bool labelAlignRight = false;
374  bool showLabel = true;
375  };
376 
377  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
378 
379  void addWidgetWrapper( QgsEditorWidgetWrapper *eww );
380 
385  void createWrappers();
386  void afterWidgetInit();
387 
388  void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
389 
391  bool saveEdits();
392 
394  void createDefaultValueDependencies();
395 
397  bool updateDefaultValues( const int originIdx );
398 
399  int messageTimeout();
400  void clearMultiEditMessages();
401  void pushSelectedFeaturesMessage();
402  void runSearchSelect( QgsVectorLayer::SelectBehavior behavior );
403 
404  QString createFilterExpression() const;
405 
406  QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
407 
409  void updateAllConstraints();
410  void updateConstraints( QgsEditorWidgetWrapper *w );
411  void updateContainersVisibility();
412  void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
413  void updateLabels();
414  bool currentFormValuesFeature( QgsFeature &feature );
415  bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
416  QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
417 
418  QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context );
419 
420  QgsVectorLayer *mLayer = nullptr;
421  QgsFeature mFeature;
422  QgsFeature mCurrentFormFeature;
423  QgsMessageBar *mMessageBar = nullptr;
424  bool mOwnsMessageBar;
425  QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
426  QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
427  QList<QgsWidgetWrapper *> mWidgets;
428  QgsAttributeEditorContext mContext;
429  std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
430  QDialogButtonBox *mButtonBox = nullptr;
431  QWidget *mSearchButtonBox = nullptr;
432  QList<QgsAttributeFormInterface *> mInterfaces;
433  QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
434  QList< QgsAttributeFormWidget *> mFormWidgets;
435  QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
436  QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
437  bool mValuesInitialized = false;
438  bool mDirty = false;
439  bool mIsSettingFeature = false;
440 
441  struct ContainerInformation
442  {
443  ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
444  : tabWidget( tabWidget )
445  , widget( widget )
446  , expression( expression )
447  , isVisible( true )
448  {}
449 
450  ContainerInformation( QWidget *widget, const QgsExpression &expression )
451  : widget( widget )
452  , expression( expression )
453  , isVisible( true )
454  {}
455 
456  QgsTabWidget *tabWidget = nullptr;
457  QWidget *widget = nullptr;
458  QgsExpression expression;
459  bool isVisible;
460 
461  void apply( QgsExpressionContext *expressionContext );
462  };
463 
464  void registerContainerInformation( ContainerInformation *info );
465 
466  void updateIcon( QgsEditorWidgetWrapper *eww );
467 
468  void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
469 
470  // Contains information about tabs and groupboxes, their visibility state visibility conditions
471  QVector<ContainerInformation *> mContainerVisibilityInformation;
472  QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
473 
474  // Variables below are used for Python
475  static int sFormCounter;
476  int mFormNr;
477  QString mPyFormVarName;
478 
480  bool mIsSaving;
481 
483  bool mPreventFeatureRefresh;
484 
485  bool mIsSettingMultiEditFeatures;
486 
487  QgsFeatureIds mMultiEditFeatureIds;
488  bool mUnsavedMultiEditChanges;
489 
490  QString mEditCommandMessage;
491 
493 
494  QMap<QWidget *, QSvgWidget *> mIconMap;
495 
500  QMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
501 
503  QList<int> mAlreadyUpdatedFields;
504 
505  friend class TestQgsDualView;
506  friend class TestQgsAttributeForm;
507 };
508 
509 #endif // QGSATTRIBUTEFORM_H
510 
QgsAttributeEditorElement
This is an abstract base class for any elements of a drag and drop form.
Definition: qgsattributeeditorelement.h:40
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsAttributeForm::modeChanged
void modeChanged(QgsAttributeEditorContext::Mode mode)
Emitted when the form changes mode.
QgsAttributeFormInterface
Definition: qgsattributeforminterface.h:29
QgsAttributeForm::flashFeatures
void flashFeatures(const QString &filter)
Emitted when the user chooses to flash a filtered set of features.
qgseditorwidgetwrapper.h
QgsWidgetWrapper
Manages an editor widget Widget and wrapper share the same parent.
Definition: qgswidgetwrapper.h:53
QgsRelationWidgetWrapper
Definition: qgsrelationwidgetwrapper.h:31
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:158
QgsAttributeForm::AggregateSearchMode
@ AggregateSearchMode
Form is in aggregate search mode, show each widget in this mode.
Definition: qgsattributeform.h:58
QgsAttributeForm::zoomToFeatures
void zoomToFeatures(const QString &filter)
Emitted when the user chooses to zoom to a filtered set of features.
QgsEditorWidgetWrapper::ConstraintResult
ConstraintResult
Result of constraint checks.
Definition: qgseditorwidgetwrapper.h:61
field
const QgsField & field
Definition: qgsfield.h:456
QgsEditorWidgetWrapper
Manages an editor widget Widget and wrapper share the same parent.
Definition: qgseditorwidgetwrapper.h:48
QgsAttributeForm::layer
QgsVectorLayer * layer()
Returns the layer for which this form is shown.
Definition: qgsattributeform.h:129
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsAttributeEditorContext::Mode
Mode
modes
Definition: qgsattributeeditorcontext.h:48
QgsAttributeForm::mode
QgsAttributeEditorContext::Mode mode() const
Returns the current mode of the form.
Definition: qgsattributeform.h:143
QgsAttributeForm::FilterOr
@ FilterOr
Filter should be combined using "OR".
Definition: qgsattributeform.h:67
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsAttributeForm::currentFormFeature
QgsFeature currentFormFeature() const
Returns the feature that is currently displayed in the form with all the changes received on editing ...
Definition: qgsattributeform.h:84
QgsMessageBarItem
Represents an item shown within a QgsMessageBar widget.
Definition: qgsmessagebaritem.h:39
QgsAttributeForm::closed
void closed()
Emitted when the user selects the close option from the form's button bar.
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsMessageBar
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
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
QgsAttributeForm::beforeSave
void beforeSave(bool &ok)
Will be emitted before the feature is saved.
QgsVectorLayer::SelectBehavior
SelectBehavior
Selection behavior.
Definition: qgsvectorlayer.h:412
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext.
Definition: qgsexpressioncontext.h:112
QgsTabWidget
The QgsTabWidget class is the same as the QTabWidget but with additional methods to temporarily hide/...
Definition: qgstabwidget.h:30
QgsAttributeForm::SearchMode
@ SearchMode
Form values are used for searching/filtering the layer.
Definition: qgsattributeform.h:57
QgsAttributeForm::FilterType
FilterType
Filter types.
Definition: qgsattributeform.h:64
QgsAttributeForm::FilterAnd
@ FilterAnd
Filter should be combined using "AND".
Definition: qgsattributeform.h:66
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsAttributeForm::attributeChanged
Q_DECL_DEPRECATED void attributeChanged(const QString &attribute, const QVariant &value)
Notifies about changes of attributes, this signal is not emitted when the value is set back to the or...
QgsAttributeFormEditorWidget
A widget consisting of both an editor widget and additional widgets for controlling the behavior of t...
Definition: qgsattributeformeditorwidget.h:43
QgsRelation
Definition: qgsrelation.h:42
QgsAttributeForm::filterExpressionSet
void filterExpressionSet(const QString &expression, QgsAttributeForm::FilterType type)
Emitted when a filter expression is set using the form.
QgsAttributeForm::widgetValueChanged
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsAttributeForm
Definition: qgsattributeform.h:45
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
QgsAttributeForm::featureSaved
void featureSaved(const QgsFeature &feature)
Emitted when a feature is changed or added.
QgsAttributeEditorContext
This class contains context information for attribute editor widgets.
Definition: qgsattributeeditorcontext.h:41
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:265
QgsAttributeForm::ReplaceFilter
@ ReplaceFilter
Filter should replace any existing filter.
Definition: qgsattributeform.h:65
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsAttributeFormWidget
Base class for all widgets shown on a QgsAttributeForm.
Definition: qgsattributeformwidget.h:39
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QgsAttributeForm::Mode
Mode
Form modes.
Definition: qgsattributeform.h:52