QGIS API Documentation  2.99.0-Master (585a4d3)
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  };
59 
62  {
66  };
67 
68  explicit QgsAttributeForm( QgsVectorLayer *vl,
69  const QgsFeature &feature = QgsFeature(),
71  QWidget *parent SIP_TRANSFERTHIS = nullptr );
72  ~QgsAttributeForm() override;
73 
74  const QgsFeature &feature() { return mFeature; }
75 
80  // TODO QGIS 3.0 - make private
81  void hideButtonBox();
82 
87  // TODO QGIS 3.0 - make private
88  void showButtonBox();
89 
94  // TODO QGIS 3.0 - make private
95  void disconnectButtonBox();
96 
101  void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
102 
108  QgsVectorLayer *layer() { return mLayer; }
109 
115  bool editable();
116 
122  Mode mode() const { return mMode; }
123 
130  void setMode( Mode mode );
131 
137  void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
138 
147  bool eventFilter( QObject *object, QEvent *event ) override;
148 
154  void setMultiEditFeatureIds( const QgsFeatureIds &fids );
155 
162  void setMessageBar( QgsMessageBar *messageBar );
163 
171  QString aggregateFilter() const;
172 
173  signals:
174 
181  void attributeChanged( const QString &attribute, const QVariant &value );
182 
191  void beforeSave( bool &ok ) SIP_SKIP;
192 
196  void featureSaved( const QgsFeature &feature );
197 
204  void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
205 
210  void modeChanged( QgsAttributeForm::Mode mode );
211 
216  void closed();
217 
222  void zoomToFeatures( const QString &filter );
223 
228  void flashFeatures( const QString &filter );
229 
230  public slots:
231 
239  void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
240 
246  void setFeature( const QgsFeature &feature );
247 
253  bool save();
254 
258  void resetValues();
259 
264  void resetSearch();
265 
269  void refreshFeature();
270 
271  private slots:
272  void onAttributeChanged( const QVariant &value );
273  void onAttributeAdded( int idx );
274  void onAttributeDeleted( int idx );
275  void onUpdatedFields();
276  void onConstraintStatusChanged( const QString &constraint,
277  const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
278  void preventFeatureRefresh();
279  void synchronizeEnabledState();
280  void layerSelectionChanged();
281 
283  bool saveMultiEdits();
284  void resetMultiEdit( bool promptToSave = false );
285  void multiEditMessageClicked( const QString &link );
286 
287  void filterAndTriggered();
288  void filterOrTriggered();
289  void filterTriggered();
290 
291  void searchZoomTo();
292  void searchFlash();
293  void searchSetSelection();
294  void searchAddToSelection();
295  void searchRemoveFromSelection();
296  void searchIntersectSelection();
297 
298  private:
299  void init();
300 
301  void cleanPython();
302 
303  void initPython();
304 
305  void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
306 
307  bool fieldIsEditable( int fieldIndex ) const;
308 
309  bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
310 
311  struct WidgetInfo
312  {
313  QWidget *widget = nullptr;
314  QString labelText;
315  QString hint;
316  bool labelOnTop = false;
317  bool labelAlignRight = false;
318  bool showLabel = true;
319  };
320 
321  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
322 
323  void addWidgetWrapper( QgsEditorWidgetWrapper *eww );
324 
329  void createWrappers();
330  void afterWidgetInit();
331 
332  void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
333 
335  bool saveEdits();
336 
337  int messageTimeout();
338  void clearMultiEditMessages();
339  void pushSelectedFeaturesMessage();
340  void runSearchSelect( QgsVectorLayer::SelectBehavior behavior );
341 
342  QString createFilterExpression() const;
343 
345  void updateAllConstraints();
346  void updateConstraints( QgsEditorWidgetWrapper *w );
347  void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
348  bool currentFormFeature( QgsFeature &feature );
349  bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
350  QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
351 
352  QgsVectorLayer *mLayer = nullptr;
353  QgsFeature mFeature;
354  QgsMessageBar *mMessageBar = nullptr;
355  bool mOwnsMessageBar;
356  QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
357  QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
358  QList<QgsWidgetWrapper *> mWidgets;
359  QgsAttributeEditorContext mContext;
360  QDialogButtonBox *mButtonBox = nullptr;
361  QWidget *mSearchButtonBox = nullptr;
362  QList<QgsAttributeFormInterface *> mInterfaces;
363  QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
364  QList< QgsAttributeFormWidget *> mFormWidgets;
365  QgsExpressionContext mExpressionContext;
366  QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
367  bool mValuesInitialized = false;
368  bool mDirty = false;
369 
370  struct ContainerInformation
371  {
372  ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
373  : tabWidget( tabWidget )
374  , widget( widget )
375  , expression( expression )
376  , isVisible( true )
377  {}
378 
379  ContainerInformation( QWidget *widget, const QgsExpression &expression )
380  : widget( widget )
381  , expression( expression )
382  , isVisible( true )
383  {}
384 
385  QgsTabWidget *tabWidget = nullptr;
386  QWidget *widget = nullptr;
387  QgsExpression expression;
388  bool isVisible;
389 
390  void apply( QgsExpressionContext *expressionContext );
391  };
392 
393  void registerContainerInformation( ContainerInformation *info );
394 
395  void updateIcon( QgsEditorWidgetWrapper *eww );
396 
397  void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
398 
399  // Contains information about tabs and groupboxes, their visibility state visibility conditions
400  QVector<ContainerInformation *> mContainerVisibilityInformation;
401  QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
402 
403  // Variables below are used for Python
404  static int sFormCounter;
405  int mFormNr;
406  QString mPyFormVarName;
407 
409  bool mIsSaving;
410 
412  bool mPreventFeatureRefresh;
413 
414  bool mIsSettingMultiEditFeatures;
415 
416  QgsFeatureIds mMultiEditFeatureIds;
417  bool mUnsavedMultiEditChanges;
418 
419  QString mEditCommandMessage;
420 
421  Mode mMode;
422 
423  QMap<QWidget *, QSvgWidget *> mIconMap;
424 
425  friend class TestQgsDualView;
426  friend class TestQgsAttributeForm;
427 };
428 
429 #endif // QGSATTRIBUTEFORM_H
430 
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.
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.