QGIS API Documentation  2.17.0-Master (eef6f05)
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 class QgsTabWidget;
33 
37 class GUI_EXPORT QgsAttributeForm : public QWidget
38 {
39  Q_OBJECT
40 
41  public:
42 
44  enum Mode
45  {
47  AddFeatureMode,
51  };
52 
55  {
59  };
60 
61  explicit QgsAttributeForm( QgsVectorLayer* vl, const QgsFeature &feature = QgsFeature(),
62  const QgsAttributeEditorContext& context = QgsAttributeEditorContext(), QWidget *parent = nullptr );
64 
65  const QgsFeature& feature() { return mFeature; }
66 
71  // TODO QGIS 3.0 - make private
72  void hideButtonBox();
73 
78  // TODO QGIS 3.0 - make private
79  void showButtonBox();
80 
85  // TODO QGIS 3.0 - make private
86  void disconnectButtonBox();
87 
92  void addInterface( QgsAttributeFormInterface* iface );
93 
99  QgsVectorLayer* layer() { return mLayer; }
100 
106  bool editable();
107 
112  Mode mode() const { return mMode; }
113 
119  void setMode( Mode mode );
120 
129  Q_DECL_DEPRECATED void setIsAddDialog( bool isAddDialog );
130 
136  void setEditCommandMessage( const QString& message ) { mEditCommandMessage = message; }
137 
146  bool eventFilter( QObject* object, QEvent* event ) override;
147 
152  void setMultiEditFeatureIds( const QgsFeatureIds& fids );
153 
159  void setMessageBar( QgsMessageBar* messageBar );
160 
161  signals:
168  void attributeChanged( const QString& attribute, const QVariant& value );
169 
178  void beforeSave( bool& ok );
179 
183  void featureSaved( const QgsFeature& feature );
184 
190  void filterExpressionSet( const QString& expression, QgsAttributeForm::FilterType type );
191 
195  void modeChanged( QgsAttributeForm::Mode mode );
196 
200  void closed();
201 
202  public slots:
209  void changeAttribute( const QString& field, const QVariant& value );
210 
216  void setFeature( const QgsFeature& feature );
217 
223  bool save();
224 
230  Q_DECL_DEPRECATED void accept() { save(); }
231 
237  Q_DECL_DEPRECATED void reject() { resetValues(); }
238 
242  void resetValues();
243 
247  void resetSearch();
248 
252  void refreshFeature();
253 
254  private slots:
255  void onAttributeChanged( const QVariant& value );
256  void onAttributeAdded( int idx );
257  void onAttributeDeleted( int idx );
258  void onUpdatedFields();
259  void onConstraintStatusChanged( const QString& constraint,
260  const QString& description, const QString& err, bool ok );
261  void preventFeatureRefresh();
262  void synchronizeEnabledState();
263  void layerSelectionChanged();
264 
266  bool saveMultiEdits();
267  void resetMultiEdit( bool promptToSave = false );
268  void multiEditMessageClicked( const QString& link );
269 
270  void filterAndTriggered();
271  void filterOrTriggered();
272  void filterTriggered();
273 
274  void searchSetSelection();
275  void searchAddToSelection();
276  void searchRemoveFromSelection();
277  void searchIntersectSelection();
278 
279  private:
280  void init();
281 
282  void cleanPython();
283 
284  void initPython();
285 
286  struct WidgetInfo
287  {
288  WidgetInfo()
289  : widget( nullptr )
290  , labelOnTop( false )
291  , labelAlignRight( false )
292  , showLabel( true )
293  {}
294 
295  QWidget* widget;
296  QString labelText;
297  bool labelOnTop;
298  bool labelAlignRight;
299  bool showLabel;
300  };
301 
302  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement* widgetDef, QWidget* parent, QgsVectorLayer* vl, QgsAttributeEditorContext& context );
303 
304  void addWidgetWrapper( QgsEditorWidgetWrapper* eww );
305 
310  void createWrappers();
311  void afterWidgetInit();
312 
313  void scanForEqualAttributes( QgsFeatureIterator& fit, QSet< int >& mixedValueFields, QHash< int, QVariant >& fieldSharedValues ) const;
314 
316  bool saveEdits();
317 
318  int messageTimeout();
319  void clearMultiEditMessages();
320  void pushSelectedFeaturesMessage();
321  void runSearchSelect( QgsVectorLayer::SelectBehaviour behaviour );
322 
323  QString createFilterExpression() const;
324 
326  void updateAllConstaints();
327  void updateConstraints( QgsEditorWidgetWrapper* w );
328  bool currentFormFeature( QgsFeature& feature );
329  bool currentFormValidConstraints( QStringList& invalidFields, QStringList& descriptions );
330  void constraintDependencies( QgsEditorWidgetWrapper* w, QList<QgsEditorWidgetWrapper*>& wDeps );
331  void clearInvalidConstraintsMessage();
332  void displayInvalidConstraintMessage( const QStringList& invalidFields,
333  const QStringList& description );
334 
335  QgsVectorLayer* mLayer;
336  QgsFeature mFeature;
337  QgsMessageBar* mMessageBar;
338  bool mOwnsMessageBar;
339  QgsMessageBarItem* mMultiEditUnsavedMessageBarItem;
340  QgsMessageBarItem* mMultiEditMessageBarItem;
341  QLabel* mInvalidConstraintMessage;
342  QList<QgsWidgetWrapper*> mWidgets;
343  QgsAttributeEditorContext mContext;
344  QDialogButtonBox* mButtonBox;
345  QWidget* mSearchButtonBox;
348  QgsExpressionContext mExpressionContext;
349 
350  struct ContainerInformation
351  {
352  ContainerInformation( QgsTabWidget* tabWidget, QWidget* widget, QgsExpression expression )
353  : tabWidget( tabWidget )
354  , widget( widget )
355  , expression( expression )
356  , isVisible( true )
357  {}
358 
359  ContainerInformation( QWidget* widget, QgsExpression expression )
360  : tabWidget( nullptr )
361  , widget( widget )
362  , expression( expression )
363  , isVisible( true )
364  {}
365 
366  QgsTabWidget* tabWidget;
367  QWidget* widget;
368  QgsExpression expression;
369  bool isVisible;
370 
371  void apply( QgsExpressionContext* expressionContext );
372  };
373 
374  void registerContainerInformation( ContainerInformation* info );
375 
376  // Contains information about tabs and groupboxes, their visibility state visibility conditions
377  QVector<ContainerInformation*> mContainerVisibilityInformation;
378  QMap<QString, QVector<ContainerInformation*> > mContainerInformationDependency;
379 
380  // Variables below are used for python
381  static int sFormCounter;
382  int mFormNr;
383  QString mPyFormVarName;
384 
386  bool mIsSaving;
387 
389  bool mPreventFeatureRefresh;
390 
392  bool mIsSettingFeature;
393  bool mIsSettingMultiEditFeatures;
394 
395  QgsFeatureIds mMultiEditFeatureIds;
396  bool mUnsavedMultiEditChanges;
397 
398  QString mEditCommandMessage;
399 
400  Mode mMode;
401 
403  QMap<QWidget*, QLabel*> mBuddyMap;
404 
405  friend class TestQgsDualView;
406  friend class TestQgsAttributeForm;
407 };
408 
409 #endif // QGSATTRIBUTEFORM_H
410 
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.
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.
A widget consisting of both an editor widget and additional widgets for controlling the behaviour of ...
FilterType
Filter types.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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.
Mode mode() const
Returns the current mode of the form.
const QgsFeature & feature()
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.