QGIS API Documentation  3.13.0-Master (788156190c)
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  bool currentFormFeature( QgsFeature &feature );
396  bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
397  QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
398 
399  QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context );
400 
401  QgsVectorLayer *mLayer = nullptr;
402  QgsFeature mFeature;
403  QgsMessageBar *mMessageBar = nullptr;
404  bool mOwnsMessageBar;
405  QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
406  QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
407  QList<QgsWidgetWrapper *> mWidgets;
408  QgsAttributeEditorContext mContext;
409  QDialogButtonBox *mButtonBox = nullptr;
410  QWidget *mSearchButtonBox = nullptr;
411  QList<QgsAttributeFormInterface *> mInterfaces;
412  QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
413  QList< QgsAttributeFormWidget *> mFormWidgets;
414  QgsExpressionContext mExpressionContext;
415  QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
416  bool mValuesInitialized = false;
417  bool mDirty = false;
418  bool mIsSettingFeature = false;
419 
420  struct ContainerInformation
421  {
422  ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
423  : tabWidget( tabWidget )
424  , widget( widget )
425  , expression( expression )
426  , isVisible( true )
427  {}
428 
429  ContainerInformation( QWidget *widget, const QgsExpression &expression )
430  : widget( widget )
431  , expression( expression )
432  , isVisible( true )
433  {}
434 
435  QgsTabWidget *tabWidget = nullptr;
436  QWidget *widget = nullptr;
437  QgsExpression expression;
438  bool isVisible;
439 
440  void apply( QgsExpressionContext *expressionContext );
441  };
442 
443  void registerContainerInformation( ContainerInformation *info );
444 
445  void updateIcon( QgsEditorWidgetWrapper *eww );
446 
447  void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
448 
449  // Contains information about tabs and groupboxes, their visibility state visibility conditions
450  QVector<ContainerInformation *> mContainerVisibilityInformation;
451  QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
452 
453  // Variables below are used for Python
454  static int sFormCounter;
455  int mFormNr;
456  QString mPyFormVarName;
457 
459  bool mIsSaving;
460 
462  bool mPreventFeatureRefresh;
463 
464  bool mIsSettingMultiEditFeatures;
465 
466  QgsFeatureIds mMultiEditFeatureIds;
467  bool mUnsavedMultiEditChanges;
468 
469  QString mEditCommandMessage;
470 
472 
473  QMap<QWidget *, QSvgWidget *> mIconMap;
474 
479  QMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
480 
482  QList<int> mAlreadyUpdatedFields;
483 
484  friend class TestQgsDualView;
485  friend class TestQgsAttributeForm;
486 };
487 
488 #endif // QGSATTRIBUTEFORM_H
489 
QgsAttributeEditorElement
Definition: qgsattributeeditorelement.h:37
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:371
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:36
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsMessageBar
Definition: qgsmessagebar.h:45
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:411
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:386
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