QGIS API Documentation  2.99.0-Master (bdf46d7)
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"
21 #include "qgseditorwidgetwrapper.h"
22 
23 #include <QWidget>
24 #include <QLabel>
25 #include <QDialogButtonBox>
26 #include "qgis_gui.h"
27 
28 
31 class QgsMessageBar;
32 class QgsMessageBarItem;
33 class QgsWidgetWrapper;
34 class QgsTabWidget;
35 
39 class GUI_EXPORT QgsAttributeForm : public QWidget
40 {
41  Q_OBJECT
42 
43  public:
44 
46  enum Mode
47  {
49  AddFeatureMode,
53  };
54 
57  {
61  };
62 
63  explicit QgsAttributeForm( QgsVectorLayer *vl, const QgsFeature &feature = QgsFeature(),
64  const QgsAttributeEditorContext &context = QgsAttributeEditorContext(), QWidget *parent = nullptr );
66 
67  const QgsFeature &feature() { return mFeature; }
68 
73  // TODO QGIS 3.0 - make private
74  void hideButtonBox();
75 
80  // TODO QGIS 3.0 - make private
81  void showButtonBox();
82 
87  // TODO QGIS 3.0 - make private
88  void disconnectButtonBox();
89 
94  void addInterface( QgsAttributeFormInterface *iface );
95 
101  QgsVectorLayer *layer() { return mLayer; }
102 
108  bool editable();
109 
114  Mode mode() const { return mMode; }
115 
121  void setMode( Mode mode );
122 
128  void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
129 
138  bool eventFilter( QObject *object, QEvent *event ) override;
139 
144  void setMultiEditFeatureIds( const QgsFeatureIds &fids );
145 
151  void setMessageBar( QgsMessageBar *messageBar );
152 
153  signals:
154 
161  void attributeChanged( const QString &attribute, const QVariant &value );
162 
171  void beforeSave( bool &ok );
172 
176  void featureSaved( const QgsFeature &feature );
177 
183  void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
184 
188  void modeChanged( QgsAttributeForm::Mode mode );
189 
193  void closed();
194 
199  void zoomToFeatures( const QString &filter );
200 
201  public slots:
202 
209  void changeAttribute( const QString &field, const QVariant &value );
210 
216  void setFeature( const QgsFeature &feature );
217 
223  bool save();
224 
228  void resetValues();
229 
233  void resetSearch();
234 
238  void refreshFeature();
239 
240  private slots:
241  void onAttributeChanged( const QVariant &value );
242  void onAttributeAdded( int idx );
243  void onAttributeDeleted( int idx );
244  void onUpdatedFields();
245  void onConstraintStatusChanged( const QString &constraint,
246  const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
247  void preventFeatureRefresh();
248  void synchronizeEnabledState();
249  void layerSelectionChanged();
250 
252  bool saveMultiEdits();
253  void resetMultiEdit( bool promptToSave = false );
254  void multiEditMessageClicked( const QString &link );
255 
256  void filterAndTriggered();
257  void filterOrTriggered();
258  void filterTriggered();
259 
260  void searchZoomTo();
261  void searchSetSelection();
262  void searchAddToSelection();
263  void searchRemoveFromSelection();
264  void searchIntersectSelection();
265 
266  private:
267  void init();
268 
269  void cleanPython();
270 
271  void initPython();
272 
273  struct WidgetInfo
274  {
275  WidgetInfo()
276  : widget( nullptr )
277  , labelOnTop( false )
278  , labelAlignRight( false )
279  , showLabel( true )
280  {}
281 
282  QWidget *widget = nullptr;
283  QString labelText;
284  bool labelOnTop;
285  bool labelAlignRight;
286  bool showLabel;
287  };
288 
289  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
290 
291  void addWidgetWrapper( QgsEditorWidgetWrapper *eww );
292 
297  void createWrappers();
298  void afterWidgetInit();
299 
300  void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
301 
303  bool saveEdits();
304 
305  int messageTimeout();
306  void clearMultiEditMessages();
307  void pushSelectedFeaturesMessage();
308  void runSearchSelect( QgsVectorLayer::SelectBehavior behavior );
309 
310  QString createFilterExpression() const;
311 
313  void updateAllConstraints();
314  void updateConstraints( QgsEditorWidgetWrapper *w );
315  bool currentFormFeature( QgsFeature &feature );
316  bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
317  QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
318  void clearInvalidConstraintsMessage();
319  void displayInvalidConstraintMessage( const QStringList &invalidFields,
320  const QStringList &description );
321 
322  QgsVectorLayer *mLayer = nullptr;
323  QgsFeature mFeature;
324  QgsMessageBar *mMessageBar = nullptr;
325  bool mOwnsMessageBar;
326  QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
327  QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
328  QLabel *mInvalidConstraintMessage = nullptr;
329  QWidget *mTopMessageWidget = nullptr;
330  QList<QgsWidgetWrapper *> mWidgets;
331  QgsAttributeEditorContext mContext;
332  QDialogButtonBox *mButtonBox = nullptr;
333  QWidget *mSearchButtonBox = nullptr;
334  QList<QgsAttributeFormInterface *> mInterfaces;
335  QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
336  QgsExpressionContext mExpressionContext;
337 
338  struct ContainerInformation
339  {
340  ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
341  : tabWidget( tabWidget )
342  , widget( widget )
343  , expression( expression )
344  , isVisible( true )
345  {}
346 
347  ContainerInformation( QWidget *widget, const QgsExpression &expression )
348  : tabWidget( nullptr )
349  , widget( widget )
350  , expression( expression )
351  , isVisible( true )
352  {}
353 
354  QgsTabWidget *tabWidget = nullptr;
355  QWidget *widget = nullptr;
356  QgsExpression expression;
357  bool isVisible;
358 
359  void apply( QgsExpressionContext *expressionContext );
360  };
361 
362  void registerContainerInformation( ContainerInformation *info );
363 
364  // Contains information about tabs and groupboxes, their visibility state visibility conditions
365  QVector<ContainerInformation *> mContainerVisibilityInformation;
366  QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
367 
368  // Variables below are used for Python
369  static int sFormCounter;
370  int mFormNr;
371  QString mPyFormVarName;
372 
374  bool mIsSaving;
375 
377  bool mPreventFeatureRefresh;
378 
380  bool mIsSettingFeature;
381  bool mIsSettingMultiEditFeatures;
382 
383  QgsFeatureIds mMultiEditFeatureIds;
384  bool mUnsavedMultiEditChanges;
385 
386  QString mEditCommandMessage;
387 
388  Mode mMode;
389 
391  QMap<QWidget *, QLabel *> mBuddyMap;
392 
393  friend class TestQgsDualView;
394  friend class TestQgsAttributeForm;
395 };
396 
397 #endif // QGSATTRIBUTEFORM_H
398 
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
This is an abstract base class for any elements of a drag and drop form.
Multi edit mode, for editing fields of multiple features at once.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:517
This class contains context information for attribute editor widgets.
Manages an editor widget Widget and wrapper share the same parent.
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:43
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
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.
Form values are used for searching/filtering the layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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.
SelectBehavior
Selection behavior.
ConstraintResult
Result of constraint checks.
Filter should be combined using "OR".
Mode mode() const
Returns the current mode of the form.
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:28
Manages an editor widget Widget and wrapper share the same parent.