QGIS API Documentation  2.99.0-Master (8ec3eaf)
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 
27 
30 class QgsMessageBar;
31 class QgsMessageBarItem;
32 class QgsWidgetWrapper;
33 class QgsTabWidget;
34 
38 class GUI_EXPORT QgsAttributeForm : public QWidget
39 {
40  Q_OBJECT
41 
42  public:
43 
45  enum Mode
46  {
48  AddFeatureMode,
52  };
53 
56  {
60  };
61 
62  explicit QgsAttributeForm( QgsVectorLayer* vl, const QgsFeature &feature = QgsFeature(),
63  const QgsAttributeEditorContext& context = QgsAttributeEditorContext(), QWidget *parent = nullptr );
65 
66  const QgsFeature& feature() { return mFeature; }
67 
72  // TODO QGIS 3.0 - make private
73  void hideButtonBox();
74 
79  // TODO QGIS 3.0 - make private
80  void showButtonBox();
81 
86  // TODO QGIS 3.0 - make private
87  void disconnectButtonBox();
88 
93  void addInterface( QgsAttributeFormInterface* iface );
94 
100  QgsVectorLayer* layer() { return mLayer; }
101 
107  bool editable();
108 
113  Mode mode() const { return mMode; }
114 
120  void setMode( Mode mode );
121 
127  void setEditCommandMessage( const QString& message ) { mEditCommandMessage = message; }
128 
137  bool eventFilter( QObject* object, QEvent* event ) override;
138 
143  void setMultiEditFeatureIds( const QgsFeatureIds& fids );
144 
150  void setMessageBar( QgsMessageBar* messageBar );
151 
152  signals:
153 
160  void attributeChanged( const QString& attribute, const QVariant& value );
161 
170  void beforeSave( bool& ok );
171 
175  void featureSaved( const QgsFeature& feature );
176 
182  void filterExpressionSet( const QString& expression, QgsAttributeForm::FilterType type );
183 
187  void modeChanged( QgsAttributeForm::Mode mode );
188 
192  void closed();
193 
194  public slots:
195 
202  void changeAttribute( const QString& field, const QVariant& value );
203 
209  void setFeature( const QgsFeature& feature );
210 
216  bool save();
217 
221  void resetValues();
222 
226  void resetSearch();
227 
231  void refreshFeature();
232 
233  private slots:
234  void onAttributeChanged( const QVariant& value );
235  void onAttributeAdded( int idx );
236  void onAttributeDeleted( int idx );
237  void onUpdatedFields();
238  void onConstraintStatusChanged( const QString& constraint,
239  const QString &description, const QString& err, QgsEditorWidgetWrapper::ConstraintResult result );
240  void preventFeatureRefresh();
241  void synchronizeEnabledState();
242  void layerSelectionChanged();
243 
245  bool saveMultiEdits();
246  void resetMultiEdit( bool promptToSave = false );
247  void multiEditMessageClicked( const QString& link );
248 
249  void filterAndTriggered();
250  void filterOrTriggered();
251  void filterTriggered();
252 
253  void searchSetSelection();
254  void searchAddToSelection();
255  void searchRemoveFromSelection();
256  void searchIntersectSelection();
257 
258  private:
259  void init();
260 
261  void cleanPython();
262 
263  void initPython();
264 
265  struct WidgetInfo
266  {
267  WidgetInfo()
268  : widget( nullptr )
269  , labelOnTop( false )
270  , labelAlignRight( false )
271  , showLabel( true )
272  {}
273 
274  QWidget* widget;
275  QString labelText;
276  bool labelOnTop;
277  bool labelAlignRight;
278  bool showLabel;
279  };
280 
281  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement* widgetDef, QWidget* parent, QgsVectorLayer* vl, QgsAttributeEditorContext& context );
282 
283  void addWidgetWrapper( QgsEditorWidgetWrapper* eww );
284 
289  void createWrappers();
290  void afterWidgetInit();
291 
292  void scanForEqualAttributes( QgsFeatureIterator& fit, QSet< int >& mixedValueFields, QHash< int, QVariant >& fieldSharedValues ) const;
293 
295  bool saveEdits();
296 
297  int messageTimeout();
298  void clearMultiEditMessages();
299  void pushSelectedFeaturesMessage();
300  void runSearchSelect( QgsVectorLayer::SelectBehaviour behaviour );
301 
302  QString createFilterExpression() const;
303 
305  void updateAllConstraints();
306  void updateConstraints( QgsEditorWidgetWrapper* w );
307  bool currentFormFeature( QgsFeature& feature );
308  bool currentFormValidConstraints( QStringList& invalidFields, QStringList& descriptions );
309  QList<QgsEditorWidgetWrapper*> constraintDependencies( QgsEditorWidgetWrapper* w );
310  void clearInvalidConstraintsMessage();
311  void displayInvalidConstraintMessage( const QStringList& invalidFields,
312  const QStringList& description );
313 
314  QgsVectorLayer* mLayer;
315  QgsFeature mFeature;
316  QgsMessageBar* mMessageBar;
317  bool mOwnsMessageBar;
318  QgsMessageBarItem* mMultiEditUnsavedMessageBarItem;
319  QgsMessageBarItem* mMultiEditMessageBarItem;
320  QLabel* mInvalidConstraintMessage;
321  QWidget* mTopMessageWidget;
322  QList<QgsWidgetWrapper*> mWidgets;
323  QgsAttributeEditorContext mContext;
324  QDialogButtonBox* mButtonBox;
325  QWidget* mSearchButtonBox;
326  QList<QgsAttributeFormInterface*> mInterfaces;
327  QMap< int, QgsAttributeFormEditorWidget* > mFormEditorWidgets;
328  QgsExpressionContext mExpressionContext;
329 
330  struct ContainerInformation
331  {
332  ContainerInformation( QgsTabWidget* tabWidget, QWidget* widget, const QgsExpression& expression )
333  : tabWidget( tabWidget )
334  , widget( widget )
335  , expression( expression )
336  , isVisible( true )
337  {}
338 
339  ContainerInformation( QWidget* widget, const QgsExpression& expression )
340  : tabWidget( nullptr )
341  , widget( widget )
342  , expression( expression )
343  , isVisible( true )
344  {}
345 
346  QgsTabWidget* tabWidget;
347  QWidget* widget;
348  QgsExpression expression;
349  bool isVisible;
350 
351  void apply( QgsExpressionContext* expressionContext );
352  };
353 
354  void registerContainerInformation( ContainerInformation* info );
355 
356  // Contains information about tabs and groupboxes, their visibility state visibility conditions
357  QVector<ContainerInformation*> mContainerVisibilityInformation;
358  QMap<QString, QVector<ContainerInformation*> > mContainerInformationDependency;
359 
360  // Variables below are used for python
361  static int sFormCounter;
362  int mFormNr;
363  QString mPyFormVarName;
364 
366  bool mIsSaving;
367 
369  bool mPreventFeatureRefresh;
370 
372  bool mIsSettingFeature;
373  bool mIsSettingMultiEditFeatures;
374 
375  QgsFeatureIds mMultiEditFeatureIds;
376  bool mUnsavedMultiEditChanges;
377 
378  QString mEditCommandMessage;
379 
380  Mode mMode;
381 
383  QMap<QWidget*, QLabel*> mBuddyMap;
384 
385  friend class TestQgsDualView;
386  friend class TestQgsAttributeForm;
387 };
388 
389 #endif // QGSATTRIBUTEFORM_H
390 
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:355
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:42
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
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 behaviour of ...
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.
SelectBehaviour
Selection behaviour.
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:27
Manages an editor widget Widget and wrapper share the same parent.