QGIS API Documentation  2.17.0-Master (6f7b933)
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 "qgsvectorlayer.h"
22 
23 #include <QWidget>
24 #include <QLabel>
25 #include <QDialogButtonBox>
26 
29 class QgsMessageBar;
30 class QgsMessageBarItem;
31 class QgsWidgetWrapper;
32 
36 class GUI_EXPORT QgsAttributeForm : public QWidget
37 {
38  Q_OBJECT
39 
40  public:
41 
43  enum Mode
44  {
46  AddFeatureMode,
50  };
51 
54  {
58  };
59 
60  explicit QgsAttributeForm( QgsVectorLayer* vl, const QgsFeature &feature = QgsFeature(),
61  const QgsAttributeEditorContext& context = QgsAttributeEditorContext(), QWidget *parent = nullptr );
63 
64  const QgsFeature& feature() { return mFeature; }
65 
70  // TODO QGIS 3.0 - make private
71  void hideButtonBox();
72 
77  // TODO QGIS 3.0 - make private
78  void showButtonBox();
79 
84  // TODO QGIS 3.0 - make private
85  void disconnectButtonBox();
86 
91  void addInterface( QgsAttributeFormInterface* iface );
92 
98  QgsVectorLayer* layer() { return mLayer; }
99 
105  bool editable();
106 
111  Mode mode() const { return mMode; }
112 
118  void setMode( Mode mode );
119 
128  Q_DECL_DEPRECATED void setIsAddDialog( bool isAddDialog );
129 
135  void setEditCommandMessage( const QString& message ) { mEditCommandMessage = message; }
136 
145  bool eventFilter( QObject* object, QEvent* event ) override;
146 
151  void setMultiEditFeatureIds( const QgsFeatureIds& fids );
152 
158  void setMessageBar( QgsMessageBar* messageBar );
159 
160  signals:
167  void attributeChanged( const QString& attribute, const QVariant& value );
168 
177  void beforeSave( bool& ok );
178 
182  void featureSaved( const QgsFeature& feature );
183 
189  void filterExpressionSet( const QString& expression, QgsAttributeForm::FilterType type );
190 
194  void modeChanged( QgsAttributeForm::Mode mode );
195 
199  void closed();
200 
201  public slots:
208  void changeAttribute( const QString& field, const QVariant& value );
209 
215  void setFeature( const QgsFeature& feature );
216 
222  bool save();
223 
229  Q_DECL_DEPRECATED void accept() { save(); }
230 
236  Q_DECL_DEPRECATED void reject() { resetValues(); }
237 
241  void resetValues();
242 
246  void resetSearch();
247 
251  void refreshFeature();
252 
253  private slots:
254  void onAttributeChanged( const QVariant& value );
255  void onAttributeAdded( int idx );
256  void onAttributeDeleted( int idx );
257  void onUpdatedFields();
258  void onConstraintStatusChanged( const QString& constraint,
259  const QString& description, const QString& err, bool ok );
260  void preventFeatureRefresh();
261  void synchronizeEnabledState();
262  void layerSelectionChanged();
263 
265  bool saveMultiEdits();
266  void resetMultiEdit( bool promptToSave = false );
267  void multiEditMessageClicked( const QString& link );
268 
269  void filterAndTriggered();
270  void filterOrTriggered();
271  void filterTriggered();
272 
273  void searchSetSelection();
274  void searchAddToSelection();
275  void searchRemoveFromSelection();
276  void searchIntersectSelection();
277 
278  private:
279  void init();
280 
281  void cleanPython();
282 
283  void initPython();
284 
285  struct WidgetInfo
286  {
287  WidgetInfo()
288  : widget( nullptr )
289  , labelOnTop( false )
290  , labelAlignRight( false )
291  , showLabel( true )
292  {}
293 
294  QWidget* widget;
295  QString labelText;
296  bool labelOnTop;
297  bool labelAlignRight;
298  bool showLabel;
299  };
300 
301  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement* widgetDef, QWidget* parent, QgsVectorLayer* vl, QgsAttributeEditorContext& context );
302 
303  void addWidgetWrapper( QgsEditorWidgetWrapper* eww );
304 
309  void createWrappers();
310  void afterWidgetInit();
311 
312  void scanForEqualAttributes( QgsFeatureIterator& fit, QSet< int >& mixedValueFields, QHash< int, QVariant >& fieldSharedValues ) const;
313 
315  bool saveEdits();
316 
317  int messageTimeout();
318  void clearMultiEditMessages();
319  void pushSelectedFeaturesMessage();
320  void runSearchSelect( QgsVectorLayer::SelectBehaviour behaviour );
321 
322  QString createFilterExpression() const;
323 
325  void updateAllConstaints();
326  void updateConstraints( QgsEditorWidgetWrapper* w );
327  bool currentFormFeature( QgsFeature& feature );
328  bool currentFormValidConstraints( QStringList& invalidFields, QStringList& descriptions );
329  void constraintDependencies( QgsEditorWidgetWrapper* w, QList<QgsEditorWidgetWrapper*>& wDeps );
330  void clearInvalidConstraintsMessage();
331  void displayInvalidConstraintMessage( const QStringList& invalidFields,
332  const QStringList& description );
333 
334  QgsVectorLayer* mLayer;
335  QgsFeature mFeature;
336  QgsMessageBar* mMessageBar;
337  bool mOwnsMessageBar;
338  QgsMessageBarItem* mMultiEditUnsavedMessageBarItem;
339  QgsMessageBarItem* mMultiEditMessageBarItem;
340  QLabel* mInvalidConstraintMessage;
341  QList<QgsWidgetWrapper*> mWidgets;
342  QgsAttributeEditorContext mContext;
343  QDialogButtonBox* mButtonBox;
344  QWidget* mSearchButtonBox;
347 
348  // Variables below are used for python
349  static int sFormCounter;
350  int mFormNr;
351  QString mPyFormVarName;
352 
354  bool mIsSaving;
355 
357  bool mPreventFeatureRefresh;
358 
360  bool mIsSettingFeature;
361  bool mIsSettingMultiEditFeatures;
362 
363  QgsFeatureIds mMultiEditFeatureIds;
364  bool mUnsavedMultiEditChanges;
365 
366  QString mEditCommandMessage;
367 
368  Mode mMode;
369 
371  QMap<QWidget*, QLabel*> mBuddyMap;
372 
373  friend class TestQgsDualView;
374  friend class TestQgsAttributeForm;
375 };
376 
377 #endif // QGSATTRIBUTEFORM_H
378 
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.
Q_DECL_DEPRECATED void accept()
Alias for save()
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:187
QgsVectorLayer * layer()
Returns the layer for which this form is shown.
Mode mode() const
Returns the current mode of the form.
A widget consisting of both an editor widget and additional widgets for controlling the behaviour of ...
FilterType
Filter types.
virtual bool eventFilter(QObject *watched, QEvent *event)
Q_DECL_DEPRECATED void reject()
Alias for resetValues()
void setEditCommandMessage(const QString &message)
Sets the edit command message (Undo) that will be used when the dialog is accepted.
SelectBehaviour
Selection behaviour.
const QgsFeature & feature()
Represents a vector layer which manages a vector based data sets.
Manages an editor widget Widget and wrapper share the same parent.