QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
305  bool saveWithDetails( QString *error SIP_OUT = nullptr );
306 
310  void resetValues();
311 
316  void resetSearch();
317 
321  void refreshFeature();
322 
332  void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
333 
334 
335  private slots:
336  void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
337  void onAttributeAdded( int idx );
338  void onAttributeDeleted( int idx );
339  void onUpdatedFields();
340  void onConstraintStatusChanged( const QString &constraint,
341  const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
342  void preventFeatureRefresh();
343  void synchronizeState();
344  void layerSelectionChanged();
345 
347  bool saveMultiEdits();
348  void resetMultiEdit( bool promptToSave = false );
349  void multiEditMessageClicked( const QString &link );
350 
351  void filterAndTriggered();
352  void filterOrTriggered();
353  void filterTriggered();
354 
355  void searchZoomTo();
356  void searchFlash();
357  void searchSetSelection();
358  void searchAddToSelection();
359  void searchRemoveFromSelection();
360  void searchIntersectSelection();
361 
362  private:
363  void init();
364 
365  void cleanPython();
366 
367  void initPython();
368 
369  void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
370 
371  bool fieldIsEditable( int fieldIndex ) const;
372 
373  bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
374 
375  void updateDefaultValueDependencies();
376 
377  struct WidgetInfo
378  {
379  QWidget *widget = nullptr;
380  QString labelText;
381  QString toolTip;
382  QString hint;
383  bool labelOnTop = false;
384  bool labelAlignRight = false;
385  bool showLabel = true;
386  };
387 
388  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
389 
390  void addWidgetWrapper( QgsEditorWidgetWrapper *eww );
391 
396  void createWrappers();
397  void afterWidgetInit();
398 
399  void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
400 
402  bool saveEdits( QString *error );
403 
405  void createDefaultValueDependencies();
406 
408  bool updateDefaultValues( const int originIdx );
409 
410  void clearMultiEditMessages();
411  void pushSelectedFeaturesMessage();
412  void runSearchSelect( QgsVectorLayer::SelectBehavior behavior );
413 
414  QString createFilterExpression() const;
415 
416  QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
417 
419  void updateAllConstraints();
420  void updateConstraints( QgsEditorWidgetWrapper *w );
421  void updateContainersVisibility();
422  void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
423  void updateLabels();
424  bool currentFormValuesFeature( QgsFeature &feature );
425  bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
426  QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
427 
428  Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
429  QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QString &relationWidgetTypeId, const QgsRelation &rel, const QgsAttributeEditorContext &context );
430 
431  QgsVectorLayer *mLayer = nullptr;
432  QgsFeature mFeature;
433  QgsFeature mCurrentFormFeature;
434  QgsMessageBar *mMessageBar = nullptr;
435  bool mOwnsMessageBar;
436  QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
437  QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
438  QList<QgsWidgetWrapper *> mWidgets;
439  QgsAttributeEditorContext mContext;
440  std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
441  QDialogButtonBox *mButtonBox = nullptr;
442  QWidget *mSearchButtonBox = nullptr;
443  QList<QgsAttributeFormInterface *> mInterfaces;
444  QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
445  QList< QgsAttributeFormWidget *> mFormWidgets;
446  QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
447  QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
448  bool mValuesInitialized = false;
449  bool mDirty = false;
450  bool mIsSettingFeature = false;
451 
452  bool mValidConstraints = true;
453  QgsMessageBarItem *mConstraintsFailMessageBarItem = nullptr;
454 
455  struct ContainerInformation
456  {
457  ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
458  : tabWidget( tabWidget )
459  , widget( widget )
460  , expression( expression )
461  , isVisible( true )
462  {}
463 
464  ContainerInformation( QWidget *widget, const QgsExpression &expression )
465  : widget( widget )
466  , expression( expression )
467  , isVisible( true )
468  {}
469 
470  QgsTabWidget *tabWidget = nullptr;
471  QWidget *widget = nullptr;
472  QgsExpression expression;
473  bool isVisible;
474 
475  void apply( QgsExpressionContext *expressionContext );
476  };
477 
478  void registerContainerInformation( ContainerInformation *info );
479 
480  void updateIcon( QgsEditorWidgetWrapper *eww );
481 
482  void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
483 
484  // Contains information about tabs and groupboxes, their visibility state visibility conditions
485  QVector<ContainerInformation *> mContainerVisibilityInformation;
486  QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
487 
488  // Variables below are used for Python
489  static int sFormCounter;
490  int mFormNr;
491  QString mPyFormVarName;
492 
494  bool mIsSaving;
495 
497  bool mPreventFeatureRefresh;
498 
499  bool mIsSettingMultiEditFeatures;
500 
501  QgsFeatureIds mMultiEditFeatureIds;
502  bool mUnsavedMultiEditChanges;
503 
504  QString mEditCommandMessage;
505 
507 
508  QMap<QWidget *, QSvgWidget *> mIconMap;
509 
514  QMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
515 
517  QList<int> mAlreadyUpdatedFields;
518 
519  friend class TestQgsDualView;
520  friend class TestQgsAttributeForm;
521 };
522 
523 #endif // QGSATTRIBUTEFORM_H
524 
This class contains context information for attribute editor widgets.
A widget consisting of both an editor widget and additional widgets for controlling the behavior of t...
Base class for all widgets shown on a QgsAttributeForm.
void beforeSave(bool &ok)
Will be emitted before the feature is saved.
QgsVectorLayer * layer()
Returns the layer for which this form is shown.
@ SearchMode
Form values are used for searching/filtering the layer.
@ MultiEditMode
Multi edit mode, for editing fields of multiple features at once.
@ AggregateSearchMode
Form is in aggregate search mode, show each widget in this mode.
@ SingleEditMode
Single edit mode, for editing a single feature.
void filterExpressionSet(const QString &expression, QgsAttributeForm::FilterType type)
Emitted when a filter expression is set using the form.
const QgsFeature & feature()
void closed()
Emitted when the user selects the close option from the form's button bar.
void setEditCommandMessage(const QString &message)
Sets the edit command message (Undo) that will be used when the dialog is accepted.
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
void flashFeatures(const QString &filter)
Emitted when the user chooses to flash a filtered set of features.
void modeChanged(QgsAttributeEditorContext::Mode mode)
Emitted when the form changes mode.
void featureSaved(const QgsFeature &feature)
Emitted when a feature is changed or added.
void zoomToFeatures(const QString &filter)
Emitted when the user chooses to zoom to a filtered set of features.
QgsFeature currentFormFeature() const
Returns the feature that is currently displayed in the form with all the changes received on editing ...
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...
FilterType
Filter types.
@ ReplaceFilter
Filter should replace any existing filter.
@ FilterOr
Filter should be combined using "OR".
@ FilterAnd
Filter should be combined using "AND".
QgsAttributeEditorContext::Mode mode() const
Returns the current mode of the form.
Manages an editor widget Widget and wrapper share the same parent.
ConstraintResult
Result of constraint checks.
Single scope for storing variables and functions for use within a QgsExpressionContext.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Represents an item shown within a QgsMessageBar widget.
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
The QgsTabWidget class is the same as the QTabWidget but with additional methods to temporarily hide/...
Definition: qgstabwidget.h:30
Represents a vector layer which manages a vector based data sets.
SelectBehavior
Selection behavior.
Manages an editor widget Widget and wrapper share the same parent.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
const QgsField & field
Definition: qgsfield.h:463