QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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( bool synchronizeWidgetWrapper = true );
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  QWidget* mTopMessageWidget;
343  QList<QgsWidgetWrapper*> mWidgets;
344  QgsAttributeEditorContext mContext;
345  QDialogButtonBox* mButtonBox;
346  QWidget* mSearchButtonBox;
349  QgsExpressionContext mExpressionContext;
350 
351  struct ContainerInformation
352  {
353  ContainerInformation( QgsTabWidget* tabWidget, QWidget* widget, QgsExpression expression )
354  : tabWidget( tabWidget )
355  , widget( widget )
356  , expression( expression )
357  , isVisible( true )
358  {}
359 
360  ContainerInformation( QWidget* widget, QgsExpression expression )
361  : tabWidget( nullptr )
362  , widget( widget )
363  , expression( expression )
364  , isVisible( true )
365  {}
366 
367  QgsTabWidget* tabWidget;
368  QWidget* widget;
369  QgsExpression expression;
370  bool isVisible;
371 
372  void apply( QgsExpressionContext* expressionContext );
373  };
374 
375  void registerContainerInformation( ContainerInformation* info );
376 
377  // Contains information about tabs and groupboxes, their visibility state visibility conditions
378  QVector<ContainerInformation*> mContainerVisibilityInformation;
379  QMap<QString, QVector<ContainerInformation*> > mContainerInformationDependency;
380 
381  // Variables below are used for python
382  static int sFormCounter;
383  int mFormNr;
384  QString mPyFormVarName;
385 
387  bool mIsSaving;
388 
390  bool mPreventFeatureRefresh;
391 
393  bool mIsSettingFeature;
394  bool mIsSettingMultiEditFeatures;
395 
396  QgsFeatureIds mMultiEditFeatureIds;
397  bool mUnsavedMultiEditChanges;
398 
399  QString mEditCommandMessage;
400 
401  Mode mMode;
402 
404  QMap<QWidget*, QLabel*> mBuddyMap;
405 
406  friend class TestQgsDualView;
407  friend class TestQgsAttributeForm;
408 };
409 
410 #endif // QGSATTRIBUTEFORM_H
411 
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.