QGIS API Documentation  2.99.0-Master (cd0ba91)
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 );
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 
368  struct ContainerInformation
369  {
370  ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
371  : tabWidget( tabWidget )
372  , widget( widget )
373  , expression( expression )
374  , isVisible( true )
375  {}
376 
377  ContainerInformation( QWidget *widget, const QgsExpression &expression )
378  : widget( widget )
379  , expression( expression )
380  , isVisible( true )
381  {}
382 
383  QgsTabWidget *tabWidget = nullptr;
384  QWidget *widget = nullptr;
385  QgsExpression expression;
386  bool isVisible;
387 
388  void apply( QgsExpressionContext *expressionContext );
389  };
390 
391  void registerContainerInformation( ContainerInformation *info );
392 
393  void updateIcon( QgsEditorWidgetWrapper *eww );
394 
395  void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
396 
397  // Contains information about tabs and groupboxes, their visibility state visibility conditions
398  QVector<ContainerInformation *> mContainerVisibilityInformation;
399  QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
400 
401  // Variables below are used for Python
402  static int sFormCounter;
403  int mFormNr;
404  QString mPyFormVarName;
405 
407  bool mIsSaving;
408 
410  bool mPreventFeatureRefresh;
411 
412  bool mIsSettingMultiEditFeatures;
413 
414  QgsFeatureIds mMultiEditFeatureIds;
415  bool mUnsavedMultiEditChanges;
416 
417  QString mEditCommandMessage;
418 
419  Mode mMode;
420 
421  QMap<QWidget *, QSvgWidget *> mIconMap;
422 
423  friend class TestQgsDualView;
424  friend class TestQgsAttributeForm;
425 };
426 
427 #endif // QGSATTRIBUTEFORM_H
428 
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.