QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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_sip.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;
39 
44 class GUI_EXPORT QgsAttributeForm : public QWidget
45 {
46  Q_OBJECT
47 
48  public:
49 
51  enum Mode
52  {
54  AddFeatureMode,
59  IdentifyMode
60  };
61 
64  {
68  };
69 
70  explicit QgsAttributeForm( QgsVectorLayer *vl,
71  const QgsFeature &feature = QgsFeature(),
73  QWidget *parent SIP_TRANSFERTHIS = nullptr );
74  ~QgsAttributeForm() override;
75 
76  const QgsFeature &feature() { return mFeature; }
77 
78  // TODO QGIS 4.0 - make private
79 
84  void hideButtonBox();
85 
86  // TODO QGIS 4.0 - make private
87 
92  void showButtonBox();
93 
94  // TODO QGIS 4.0 - make private
95 
100  void disconnectButtonBox();
101 
106  void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
107 
113  QgsVectorLayer *layer() { return mLayer; }
114 
120  bool editable();
121 
127  QgsAttributeEditorContext::Mode mode() const { return mMode; }
128 
135  void setMode( QgsAttributeEditorContext::Mode mode );
136 
142  void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
143 
152  bool eventFilter( QObject *object, QEvent *event ) override;
153 
159  void setMultiEditFeatureIds( const QgsFeatureIds &fids );
160 
167  void setMessageBar( QgsMessageBar *messageBar );
168 
176  QString aggregateFilter() const;
177 
178  signals:
179 
188  Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
189 
198  void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
199 
208  void beforeSave( bool &ok ) SIP_SKIP;
209 
213  void featureSaved( const QgsFeature &feature );
214 
221  void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
222 
227  void modeChanged( QgsAttributeEditorContext::Mode mode );
228 
233  void closed();
234 
239  void zoomToFeatures( const QString &filter );
240 
245  void flashFeatures( const QString &filter );
246 
247  public slots:
248 
256  void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
257 
263  void setFeature( const QgsFeature &feature );
264 
270  bool save();
271 
275  void resetValues();
276 
281  void resetSearch();
282 
286  void refreshFeature();
287 
288  private slots:
289  void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
290  void onAttributeAdded( int idx );
291  void onAttributeDeleted( int idx );
292  void onUpdatedFields();
293  void onConstraintStatusChanged( const QString &constraint,
294  const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
295  void preventFeatureRefresh();
296  void synchronizeEnabledState();
297  void layerSelectionChanged();
298 
300  bool saveMultiEdits();
301  void resetMultiEdit( bool promptToSave = false );
302  void multiEditMessageClicked( const QString &link );
303 
304  void filterAndTriggered();
305  void filterOrTriggered();
306  void filterTriggered();
307 
308  void searchZoomTo();
309  void searchFlash();
310  void searchSetSelection();
311  void searchAddToSelection();
312  void searchRemoveFromSelection();
313  void searchIntersectSelection();
314 
315  private:
316  void init();
317 
318  void cleanPython();
319 
320  void initPython();
321 
322  void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
323 
324  bool fieldIsEditable( int fieldIndex ) const;
325 
326  bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
327 
328  struct WidgetInfo
329  {
330  QWidget *widget = nullptr;
331  QString labelText;
332  QString toolTip;
333  QString hint;
334  bool labelOnTop = false;
335  bool labelAlignRight = false;
336  bool showLabel = true;
337  };
338 
339  WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
340 
341  void addWidgetWrapper( QgsEditorWidgetWrapper *eww );
342 
347  void createWrappers();
348  void afterWidgetInit();
349 
350  void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
351 
353  bool saveEdits();
354 
356  void createDefaultValueDependencies();
357 
359  bool updateDefaultValues( const int originIdx );
360 
361  int messageTimeout();
362  void clearMultiEditMessages();
363  void pushSelectedFeaturesMessage();
364  void runSearchSelect( QgsVectorLayer::SelectBehavior behavior );
365 
366  QString createFilterExpression() const;
367 
369  void updateAllConstraints();
370  void updateConstraints( QgsEditorWidgetWrapper *w );
371  void updateContainersVisibility();
372  void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
373  bool currentFormFeature( QgsFeature &feature );
374  bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
375  QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
376 
377  QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context );
378 
379  QgsVectorLayer *mLayer = nullptr;
380  QgsFeature mFeature;
381  QgsMessageBar *mMessageBar = nullptr;
382  bool mOwnsMessageBar;
383  QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
384  QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
385  QList<QgsWidgetWrapper *> mWidgets;
386  QgsAttributeEditorContext mContext;
387  QDialogButtonBox *mButtonBox = nullptr;
388  QWidget *mSearchButtonBox = nullptr;
389  QList<QgsAttributeFormInterface *> mInterfaces;
390  QMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
391  QList< QgsAttributeFormWidget *> mFormWidgets;
392  QgsExpressionContext mExpressionContext;
393  QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
394  bool mValuesInitialized = false;
395  bool mDirty = false;
396  bool mIsSettingFeature = false;
397 
398  struct ContainerInformation
399  {
400  ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
401  : tabWidget( tabWidget )
402  , widget( widget )
403  , expression( expression )
404  , isVisible( true )
405  {}
406 
407  ContainerInformation( QWidget *widget, const QgsExpression &expression )
408  : widget( widget )
409  , expression( expression )
410  , isVisible( true )
411  {}
412 
413  QgsTabWidget *tabWidget = nullptr;
414  QWidget *widget = nullptr;
415  QgsExpression expression;
416  bool isVisible;
417 
418  void apply( QgsExpressionContext *expressionContext );
419  };
420 
421  void registerContainerInformation( ContainerInformation *info );
422 
423  void updateIcon( QgsEditorWidgetWrapper *eww );
424 
425  void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
426 
427  // Contains information about tabs and groupboxes, their visibility state visibility conditions
428  QVector<ContainerInformation *> mContainerVisibilityInformation;
429  QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
430 
431  // Variables below are used for Python
432  static int sFormCounter;
433  int mFormNr;
434  QString mPyFormVarName;
435 
437  bool mIsSaving;
438 
440  bool mPreventFeatureRefresh;
441 
442  bool mIsSettingMultiEditFeatures;
443 
444  QgsFeatureIds mMultiEditFeatureIds;
445  bool mUnsavedMultiEditChanges;
446 
447  QString mEditCommandMessage;
448 
450 
451  QMap<QWidget *, QSvgWidget *> mIconMap;
452 
457  QMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
458 
460  QList<int> mAlreadyUpdatedFields;
461 
462  friend class TestQgsDualView;
463  friend class TestQgsAttributeForm;
464 };
465 
466 #endif // QGSATTRIBUTEFORM_H
467 
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
This is an abstract base class for any elements of a drag and drop form.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
Base class for all widgets shown on a QgsAttributeForm.
Multi edit mode, for editing fields of multiple features at once.
This class contains context information for attribute editor widgets.
Manages an editor widget Widget and wrapper share the same parent.
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
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:55
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.
QgsAttributeEditorContext::Mode mode() const
Returns the current mode of the form.
#define SIP_SKIP
Definition: qgis_sip.h:126
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:106
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.
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.