QGIS API Documentation  3.2.0-Bonn (bc43194)
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 "qgis.h"
22 #include "qgseditorwidgetwrapper.h"
23 
24 #include <QWidget>
25 #include <QSvgWidget>
26 #include <QLabel>
27 #include <QDialogButtonBox>
28 #include "qgis_gui.h"
29 
30 
33 class QgsMessageBar;
34 class QgsMessageBarItem;
35 class QgsWidgetWrapper;
36 class QgsTabWidget;
38 
43 class GUI_EXPORT QgsAttributeForm : public QWidget
44 {
45  Q_OBJECT
46 
47  public:
48 
50  enum Mode
51  {
53  AddFeatureMode,
58  IdentifyMode
59  };
60 
63  {
67  };
68 
69  explicit QgsAttributeForm( QgsVectorLayer *vl,
70  const QgsFeature &feature = QgsFeature(),
72  QWidget *parent SIP_TRANSFERTHIS = nullptr );
73  ~QgsAttributeForm() override;
74 
75  const QgsFeature &feature() { return mFeature; }
76 
81  // TODO QGIS 3.0 - make private
82  void hideButtonBox();
83 
88  // TODO QGIS 3.0 - make private
89  void showButtonBox();
90 
95  // TODO QGIS 3.0 - make private
96  void disconnectButtonBox();
97 
102  void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
103 
109  QgsVectorLayer *layer() { return mLayer; }
110 
116  bool editable();
117 
123  Mode mode() const { return mMode; }
124 
131  void setMode( Mode mode );
132 
138  void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
139 
148  bool eventFilter( QObject *object, QEvent *event ) override;
149 
155  void setMultiEditFeatureIds( const QgsFeatureIds &fids );
156 
163  void setMessageBar( QgsMessageBar *messageBar );
164 
172  QString aggregateFilter() const;
173 
174  signals:
175 
184  Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
185 
194  void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
195 
204  void beforeSave( bool &ok ) SIP_SKIP;
205 
209  void featureSaved( const QgsFeature &feature );
210 
217  void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
218 
223  void modeChanged( QgsAttributeForm::Mode mode );
224 
229  void closed();
230 
235  void zoomToFeatures( const QString &filter );
236 
241  void flashFeatures( const QString &filter );
242 
243  public slots:
244 
252  void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
253 
259  void setFeature( const QgsFeature &feature );
260 
266  bool save();
267 
271  void resetValues();
272 
277  void resetSearch();
278 
282  void refreshFeature();
283 
284  private slots:
285  void onAttributeChanged( const QVariant &value );
286  void onAttributeAdded( int idx );
287  void onAttributeDeleted( int idx );
288  void onUpdatedFields();
289  void onConstraintStatusChanged( const QString &constraint,
290  const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
291  void preventFeatureRefresh();
292  void synchronizeEnabledState();
293  void layerSelectionChanged();
294 
296  bool saveMultiEdits();
297  void resetMultiEdit( bool promptToSave = false );
298  void multiEditMessageClicked( const QString &link );
299 
300  void filterAndTriggered();
301  void filterOrTriggered();
302  void filterTriggered();
303 
304  void searchZoomTo();
305  void searchFlash();
306  void searchSetSelection();
307  void searchAddToSelection();
308  void searchRemoveFromSelection();
309  void searchIntersectSelection();
310 
311  private:
312  void init();
313 
314  void cleanPython();
315 
316  void initPython();
317 
318  void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
319 
320  bool fieldIsEditable( int fieldIndex ) const;
321 
322  bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
323 
324  struct WidgetInfo
325  {
326  QWidget *widget = nullptr;
327  QString labelText;
328  QString hint;
329  bool labelOnTop = false;
330  bool labelAlignRight = false;
331  bool showLabel = true;
332  };
333 
334  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
335 
336  void addWidgetWrapper( QgsEditorWidgetWrapper *eww );
337 
342  void createWrappers();
343  void afterWidgetInit();
344 
345  void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
346 
348  bool saveEdits();
349 
350  int messageTimeout();
351  void clearMultiEditMessages();
352  void pushSelectedFeaturesMessage();
353  void runSearchSelect( QgsVectorLayer::SelectBehavior behavior );
354 
355  QString createFilterExpression() const;
356 
358  void updateAllConstraints();
359  void updateConstraints( QgsEditorWidgetWrapper *w );
360  void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
361  bool currentFormFeature( QgsFeature &feature );
362  bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
363  QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
364 
365  QgsVectorLayer *mLayer = nullptr;
366  QgsFeature mFeature;
367  QgsMessageBar *mMessageBar = nullptr;
368  bool mOwnsMessageBar;
369  QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
370  QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
371  QList<QgsWidgetWrapper *> mWidgets;
372  QgsAttributeEditorContext mContext;
373  QDialogButtonBox *mButtonBox = nullptr;
374  QWidget *mSearchButtonBox = nullptr;
375  QList<QgsAttributeFormInterface *> mInterfaces;
376  QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
377  QList< QgsAttributeFormWidget *> mFormWidgets;
378  QgsExpressionContext mExpressionContext;
379  QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
380  bool mValuesInitialized = false;
381  bool mDirty = false;
382  bool mIsSettingFeature = false;
383 
384  struct ContainerInformation
385  {
386  ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
387  : tabWidget( tabWidget )
388  , widget( widget )
389  , expression( expression )
390  , isVisible( true )
391  {}
392 
393  ContainerInformation( QWidget *widget, const QgsExpression &expression )
394  : widget( widget )
395  , expression( expression )
396  , isVisible( true )
397  {}
398 
399  QgsTabWidget *tabWidget = nullptr;
400  QWidget *widget = nullptr;
401  QgsExpression expression;
402  bool isVisible;
403 
404  void apply( QgsExpressionContext *expressionContext );
405  };
406 
407  void registerContainerInformation( ContainerInformation *info );
408 
409  void updateIcon( QgsEditorWidgetWrapper *eww );
410 
411  void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
412 
413  // Contains information about tabs and groupboxes, their visibility state visibility conditions
414  QVector<ContainerInformation *> mContainerVisibilityInformation;
415  QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
416 
417  // Variables below are used for Python
418  static int sFormCounter;
419  int mFormNr;
420  QString mPyFormVarName;
421 
423  bool mIsSaving;
424 
426  bool mPreventFeatureRefresh;
427 
428  bool mIsSettingMultiEditFeatures;
429 
430  QgsFeatureIds mMultiEditFeatureIds;
431  bool mUnsavedMultiEditChanges;
432 
433  QString mEditCommandMessage;
434 
435  Mode mMode;
436 
437  QMap<QWidget *, QSvgWidget *> mIconMap;
438 
439  friend class TestQgsDualView;
440  friend class TestQgsAttributeForm;
441 };
442 
443 #endif // QGSATTRIBUTEFORM_H
444 
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.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
Base class for all widgets shown on a QgsAttributeForm.
Multi edit mode, for editing fields of multiple features at once.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:544
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:45
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
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 behavior of t...
FilterType
Filter types.
#define SIP_SKIP
Definition: qgis_sip.h:119
Form values are used for searching/filtering the layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
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.
#define SIP_DEPRECATED
Definition: qgis_sip.h:99
SelectBehavior
Selection behavior.
ConstraintResult
Result of constraint checks.
Filter should be combined using "OR".
Form is in aggregate search mode, show each widget in this mode.
Mode mode() const
Returns the current mode of the form.
qint64 QgsFeatureId
Definition: qgsfeature.h:37
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:29
Manages an editor widget Widget and wrapper share the same parent.