QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
78  // TODO QGIS 4.0 - make private
79 
84  void hideButtonBox();
85 
86  // TODO QGIS 4.0 - make private
87 
92  void showButtonBox();
93 
94  // TODO QGIS 4.0 - make private
95 
100  void disconnectButtonBox();
101 
106  void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
107 
113  QgsVectorLayer *layer() { return mLayer; }
114 
120  bool editable();
121 
127  QgsAttributeEditorContext::Mode mode() const { return mMode; }
128 
135  void setMode( QgsAttributeEditorContext::Mode mode );
136 
142  void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
143 
152  bool eventFilter( QObject *object, QEvent *event ) override;
153 
159  void setMultiEditFeatureIds( const QgsFeatureIds &fids );
160 
167  void setMessageBar( QgsMessageBar *messageBar );
168 
176  QString aggregateFilter() const;
177 
178  signals:
179 
188  Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
189 
198  void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
199 
208  void beforeSave( bool &ok ) SIP_SKIP;
209 
213  void featureSaved( const QgsFeature &feature );
214 
221  void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
222 
227  void modeChanged( QgsAttributeEditorContext::Mode mode );
228 
233  void closed();
234 
239  void zoomToFeatures( const QString &filter );
240 
245  void flashFeatures( const QString &filter );
246 
247  public slots:
248 
256  void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
257 
263  void setFeature( const QgsFeature &feature );
264 
270  bool save();
271 
275  void resetValues();
276 
281  void resetSearch();
282 
286  void refreshFeature();
287 
288  private slots:
289  void onAttributeChanged( const QVariant &value );
290  void onAttributeAdded( int idx );
291  void onAttributeDeleted( int idx );
292  void onUpdatedFields();
293  void onConstraintStatusChanged( const QString &constraint,
294  const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
295  void preventFeatureRefresh();
296  void synchronizeEnabledState();
297  void layerSelectionChanged();
298 
300  bool saveMultiEdits();
301  void resetMultiEdit( bool promptToSave = false );
302  void multiEditMessageClicked( const QString &link );
303 
304  void filterAndTriggered();
305  void filterOrTriggered();
306  void filterTriggered();
307 
308  void searchZoomTo();
309  void searchFlash();
310  void searchSetSelection();
311  void searchAddToSelection();
312  void searchRemoveFromSelection();
313  void searchIntersectSelection();
314 
315  private:
316  void init();
317 
318  void cleanPython();
319 
320  void initPython();
321 
322  void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
323 
324  bool fieldIsEditable( int fieldIndex ) const;
325 
326  bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
327 
328  struct WidgetInfo
329  {
330  QWidget *widget = nullptr;
331  QString labelText;
332  QString toolTip;
333  QString hint;
334  bool labelOnTop = false;
335  bool labelAlignRight = false;
336  bool showLabel = true;
337  };
338 
339  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
340 
341  void addWidgetWrapper( QgsEditorWidgetWrapper *eww );
342 
347  void createWrappers();
348  void afterWidgetInit();
349 
350  void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
351 
353  bool saveEdits();
354 
355  int messageTimeout();
356  void clearMultiEditMessages();
357  void pushSelectedFeaturesMessage();
358  void runSearchSelect( QgsVectorLayer::SelectBehavior behavior );
359 
360  QString createFilterExpression() const;
361 
363  void updateAllConstraints();
364  void updateConstraints( QgsEditorWidgetWrapper *w );
365  void updateContainersVisibility();
366  void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
367  bool currentFormFeature( QgsFeature &feature );
368  bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
369  QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
370 
371  QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context );
372 
373  QgsVectorLayer *mLayer = nullptr;
374  QgsFeature mFeature;
375  QgsMessageBar *mMessageBar = nullptr;
376  bool mOwnsMessageBar;
377  QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
378  QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
379  QList<QgsWidgetWrapper *> mWidgets;
380  QgsAttributeEditorContext mContext;
381  QDialogButtonBox *mButtonBox = nullptr;
382  QWidget *mSearchButtonBox = nullptr;
383  QList<QgsAttributeFormInterface *> mInterfaces;
384  QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
385  QList< QgsAttributeFormWidget *> mFormWidgets;
386  QgsExpressionContext mExpressionContext;
387  QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
388  bool mValuesInitialized = false;
389  bool mDirty = false;
390  bool mIsSettingFeature = false;
391 
392  struct ContainerInformation
393  {
394  ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
395  : tabWidget( tabWidget )
396  , widget( widget )
397  , expression( expression )
398  , isVisible( true )
399  {}
400 
401  ContainerInformation( QWidget *widget, const QgsExpression &expression )
402  : widget( widget )
403  , expression( expression )
404  , isVisible( true )
405  {}
406 
407  QgsTabWidget *tabWidget = nullptr;
408  QWidget *widget = nullptr;
409  QgsExpression expression;
410  bool isVisible;
411 
412  void apply( QgsExpressionContext *expressionContext );
413  };
414 
415  void registerContainerInformation( ContainerInformation *info );
416 
417  void updateIcon( QgsEditorWidgetWrapper *eww );
418 
419  void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
420 
421  // Contains information about tabs and groupboxes, their visibility state visibility conditions
422  QVector<ContainerInformation *> mContainerVisibilityInformation;
423  QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
424 
425  // Variables below are used for Python
426  static int sFormCounter;
427  int mFormNr;
428  QString mPyFormVarName;
429 
431  bool mIsSaving;
432 
434  bool mPreventFeatureRefresh;
435 
436  bool mIsSettingMultiEditFeatures;
437 
438  QgsFeatureIds mMultiEditFeatureIds;
439  bool mUnsavedMultiEditChanges;
440 
441  QString mEditCommandMessage;
442 
444 
445  QMap<QWidget *, QSvgWidget *> mIconMap;
446 
447  friend class TestQgsDualView;
448  friend class TestQgsAttributeForm;
449 };
450 
451 #endif // QGSATTRIBUTEFORM_H
452 
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
This is an abstract base class for any elements of a drag and drop form.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
Base class for all widgets shown on a QgsAttributeForm.
Multi edit mode, for editing fields of multiple features at once.
This class contains context information for attribute editor widgets.
Manages an editor widget Widget and wrapper share the same parent.
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:45
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
QgsVectorLayer * layer()
Returns the layer for which this form is shown.
A widget consisting of both an editor widget and additional widgets for controlling the behavior of t...
FilterType
Filter types.
QgsAttributeEditorContext::Mode mode() const
Returns the current mode of the form.
#define SIP_SKIP
Definition: qgis_sip.h:119
Form values are used for searching/filtering the layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Filter should be combined using "AND".
void setEditCommandMessage(const QString &message)
Sets the edit command message (Undo) that will be used when the dialog is accepted.
#define SIP_DEPRECATED
Definition: qgis_sip.h:99
SelectBehavior
Selection behavior.
ConstraintResult
Result of constraint checks.
Filter should be combined using "OR".
Form is in aggregate search mode, show each widget in this mode.
const QgsFeature & feature()
Single edit mode, for editing a single feature.
Filter should replace any existing filter.
Represents a vector layer which manages a vector based data sets.
The QgsTabWidget class is the same as the QTabWidget but with additional methods to temporarily hide/...
Definition: qgstabwidget.h:29
Manages an editor widget Widget and wrapper share the same parent.