QGIS API Documentation  2.17.0-Master (8784312)
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  {}
292 
293  QWidget* widget;
294  QString labelText;
295  bool labelOnTop;
296  bool labelAlignRight;
297  };
298 
299  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement* widgetDef, QWidget* parent, QgsVectorLayer* vl, QgsAttributeEditorContext& context );
300 
301  void addWidgetWrapper( QgsEditorWidgetWrapper* eww );
302 
307  void createWrappers();
308  void afterWidgetInit();
309 
310  void scanForEqualAttributes( QgsFeatureIterator& fit, QSet< int >& mixedValueFields, QHash< int, QVariant >& fieldSharedValues ) const;
311 
313  bool saveEdits();
314 
315  int messageTimeout();
316  void clearMultiEditMessages();
317  void pushSelectedFeaturesMessage();
318  void runSearchSelect( QgsVectorLayer::SelectBehaviour behaviour );
319 
320  QString createFilterExpression() const;
321 
323  void updateAllConstaints();
324  void updateConstraints( QgsEditorWidgetWrapper *w );
325  bool currentFormFeature( QgsFeature &feature );
326  bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
327  void constraintDependencies( QgsEditorWidgetWrapper *w, QList<QgsEditorWidgetWrapper*> &wDeps );
328  void clearInvalidConstraintsMessage();
329  void displayInvalidConstraintMessage( const QStringList &invalidFields,
330  const QStringList &description );
331 
332  QgsVectorLayer* mLayer;
333  QgsFeature mFeature;
334  QgsMessageBar* mMessageBar;
335  bool mOwnsMessageBar;
336  QgsMessageBarItem* mMultiEditUnsavedMessageBarItem;
337  QgsMessageBarItem* mMultiEditMessageBarItem;
338  QLabel* mInvalidConstraintMessage;
339  QList<QgsWidgetWrapper*> mWidgets;
340  QgsAttributeEditorContext mContext;
341  QDialogButtonBox* mButtonBox;
342  QWidget* mSearchButtonBox;
345 
346  // Variables below are used for python
347  static int sFormCounter;
348  int mFormNr;
349  QString mPyFormVarName;
350 
352  bool mIsSaving;
353 
355  bool mPreventFeatureRefresh;
356 
358  bool mIsSettingFeature;
359  bool mIsSettingMultiEditFeatures;
360 
361  QgsFeatureIds mMultiEditFeatureIds;
362  bool mUnsavedMultiEditChanges;
363 
364  QString mEditCommandMessage;
365 
366  Mode mMode;
367 
369  QMap<QWidget*, QLabel*> mBuddyMap;
370 
371  friend class TestQgsDualView;
372  friend class TestQgsAttributeForm;
373 };
374 
375 #endif // QGSATTRIBUTEFORM_H
376 
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.