QGIS API Documentation 3.43.0-Master (b60ef06885e)
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"
24
25#include <QWidget>
26#include <QLabel>
27#include <QDialogButtonBox>
28#include <QMultiMap>
29
30#include "qgis_gui.h"
31
32
35class QgsMessageBar;
38class QgsTabWidget;
41class QSvgWidget;
42
48class GUI_EXPORT QgsAttributeForm : public QWidget
49{
50 Q_OBJECT
51
52 public:
54 enum Mode
55 {
57 AddFeatureMode,
62 IdentifyMode
63 };
64
72
73 explicit QgsAttributeForm( QgsVectorLayer *vl, const QgsFeature &feature = QgsFeature(), const QgsAttributeEditorContext &context = QgsAttributeEditorContext(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
74 ~QgsAttributeForm() override;
75
76 const QgsFeature &feature() { return mFeature; }
77
84 QgsFeature currentFormFeature() const { return mCurrentFormFeature; }
85
92 void displayWarning( const QString &message );
93
94 // TODO QGIS 4.0 - make private
95
100 void hideButtonBox();
101
102 // TODO QGIS 4.0 - make private
103
108 void showButtonBox();
109
110 // TODO QGIS 4.0 - make private
111
116 void disconnectButtonBox();
117
122 void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
123
129 QgsVectorLayer *layer() { return mLayer; }
130
136 bool editable();
137
142 QgsAttributeEditorContext::Mode mode() const { return mMode; }
143
149 void setMode( QgsAttributeEditorContext::Mode mode );
150
156 void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
157
166 bool eventFilter( QObject *object, QEvent *event ) override;
167
172 void setMultiEditFeatureIds( const QgsFeatureIds &fids );
173
179 void setMessageBar( QgsMessageBar *messageBar );
180
187 QString aggregateFilter() const;
188
195 void setExtraContextScope( QgsExpressionContextScope *extraScope SIP_TRANSFER );
196
201 bool needsGeometry() const;
202
203 signals:
204
213 Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
214
222 void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
223
232 void beforeSave( bool &ok ) SIP_SKIP;
233
237 void featureSaved( const QgsFeature &feature );
238
244 void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
245
251
255 void closed();
256
260 void zoomToFeatures( const QString &filter );
261
265 void flashFeatures( const QString &filter );
266
271 void openFilteredFeaturesAttributeTable( const QString &filter );
272
273 public slots:
274
282 void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
283
289 void changeGeometry( const QgsGeometry &geometry );
290
296 void setFeature( const QgsFeature &feature );
297
303 bool save();
304
314 bool saveWithDetails( QString *error SIP_OUT = nullptr );
315
319 void resetValues();
320
324 void resetSearch();
325
329 void refreshFeature();
330
340 void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
341
342 private slots:
343 void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
344 void onAttributeAdded( int idx );
345 void onAttributeDeleted( int idx );
346 void onRelatedFeaturesChanged();
347 void onUpdatedFields();
348 void onConstraintStatusChanged( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
349 void preventFeatureRefresh();
350 void synchronizeState();
351 void layerSelectionChanged();
352
354 bool saveMultiEdits();
355 void resetMultiEdit( bool promptToSave = false );
356 void multiEditMessageClicked( const QString &link );
357
358 void filterAndTriggered();
359 void filterOrTriggered();
360 void filterTriggered();
361
362 void searchZoomTo();
363 void searchFlash();
364 void searchSetSelection();
365 void searchAddToSelection();
366 void searchRemoveFromSelection();
367 void searchIntersectSelection();
368
369 private:
370 void init();
371
372 void cleanPython();
373
374 void initPython();
375
376 void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
377
378 bool fieldIsEditable( int fieldIndex ) const;
379
380 bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
381
382 void updateFieldDependencies();
383 void updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww );
384 void updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww );
385 void updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrapper *eww = nullptr );
386 void updateFieldDependenciesParent( QgsEditorWidgetWrapper *eww );
387
388 void setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids );
389
390 struct WidgetInfo
391 {
392 QWidget *widget = nullptr;
393 QString labelText;
394 QString toolTip;
395 QString hint;
396 bool labelOnTop = false;
397 bool labelAlignRight = false;
398 bool showLabel = true;
400 };
401
402 WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
403
408 void createWrappers();
409 void afterWidgetInit();
410
411 void scanForEqualAttributes( QgsFeatureIterator &fit, QSet<int> &mixedValueFields, QHash<int, QVariant> &fieldSharedValues ) const;
412
414 bool saveEdits( QString *error );
415
416 QgsFeature getUpdatedFeature() const;
417
419 void updateValuesDependencies( const int originIdx );
420 void updateValuesDependenciesDefaultValues( const int originIdx );
421 void updateValuesDependenciesVirtualFields( const int originIdx );
422 void updateValuesDependenciesParent();
423 void updateRelatedLayerFields();
424
425 void clearMultiEditMessages();
426 void pushSelectedFeaturesMessage();
427 void runSearchSelect( Qgis::SelectBehavior behavior );
428
429 QString createFilterExpression() const;
430
431 QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
432
434 void updateAllConstraints();
435 void updateConstraints( QgsEditorWidgetWrapper *w );
436 void updateContainersVisibility();
437 void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
438 void updateLabels();
439 void updateEditableState();
440 bool currentFormValuesFeature( QgsFeature &feature );
441 bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
442 bool currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
443 QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
444
445 Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
446 QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QString &relationWidgetTypeId, const QgsRelation &rel, const QgsAttributeEditorContext &context );
447
448 QgsVectorLayer *mLayer = nullptr;
449 QgsFeature mFeature;
450 QgsFeature mCurrentFormFeature;
451 QgsMessageBar *mMessageBar = nullptr;
452 bool mOwnsMessageBar;
453 QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
454 QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
455 QList<QgsWidgetWrapper *> mWidgets;
457 std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
458 QDialogButtonBox *mButtonBox = nullptr;
459 QWidget *mSearchButtonBox = nullptr;
460 QList<QgsAttributeFormInterface *> mInterfaces;
461 QMultiMap<int, QgsAttributeFormEditorWidget *> mFormEditorWidgets;
462 QList<QgsAttributeFormWidget *> mFormWidgets;
463 QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
464 QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
465 QMap<QWidget *, QgsProperty> mEditableDataDefinedProperties;
466 bool mValuesInitialized = false;
467 bool mDirty = false;
468 bool mIsSettingFeature = false;
469
470 bool mValidConstraints = true;
471 QgsMessageBarItem *mConstraintsFailMessageBarItem = nullptr;
472
473 struct ContainerInformation
474 {
475 ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
476 : tabWidget( tabWidget )
477 , widget( widget )
478 , expression( expression )
479 , isVisible( true )
480 {}
481
482 ContainerInformation( QWidget *widget, const QgsExpression &expression )
483 : widget( widget )
484 , expression( expression )
485 , isVisible( true )
486 {}
487
488 ContainerInformation( QWidget *widget, const QgsExpression &visibilityExpression, bool collapsed, const QgsExpression &collapsedExpression )
489 : widget( widget )
490 , expression( visibilityExpression )
491 , isVisible( true )
492 , isCollapsed( collapsed )
493 , collapsedExpression( collapsedExpression )
494 {}
495
496
497 QgsTabWidget *tabWidget = nullptr;
498 QWidget *widget = nullptr;
499 QgsExpression expression;
500 bool isVisible;
501 bool isCollapsed = false;
502 QgsExpression collapsedExpression;
503
504 void apply( QgsExpressionContext *expressionContext );
505 };
506
507 void registerContainerInformation( ContainerInformation *info );
508
509 void updateIcon( QgsEditorWidgetWrapper *eww );
510
511 void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
512
513 // Contains information about tabs and groupboxes, their visibility/collapsed state conditions
514 QVector<ContainerInformation *> mContainerVisibilityCollapsedInformation;
515 QMap<QString, QVector<ContainerInformation *>> mContainerInformationDependency;
516
517 // Variables below are used for Python
518 static int sFormCounter;
519 int mFormNr;
520 QString mPyFormVarName;
521
523 bool mIsSaving;
524
526 bool mPreventFeatureRefresh;
527
528 bool mIsSettingMultiEditFeatures;
529
530 QgsFeatureIds mMultiEditFeatureIds;
531 bool mUnsavedMultiEditChanges;
532
533 QString mEditCommandMessage;
534
536
537 QMap<QWidget *, QSvgWidget *> mIconMap;
538
543 QMultiMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
544
549 QMultiMap<int, QgsWidgetWrapper *> mVirtualFieldsDependencies;
550
554 QSet<QgsEditorWidgetWrapper *> mRelatedLayerFieldsDependencies;
555
556 QSet<QgsEditorWidgetWrapper *> mParentDependencies;
557
559 QList<int> mAlreadyUpdatedFields;
560
561 bool mNeedsGeometry = false;
562
563 friend class TestQgsDualView;
564 friend class TestQgsAttributeForm;
565 friend class TestQgsValueRelationWidgetWrapper;
566};
567
568#endif // QGSATTRIBUTEFORM_H
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1734
Contains context information for attribute editor widgets.
An abstract base class for any elements of a drag and drop form.
A widget consisting of both an editor widget and additional widgets for controlling the behavior of t...
Interface class for custom attribute forms.
Base class for all widgets shown on a QgsAttributeForm.
The attribute form widget for vector layer features.
QgsVectorLayer * layer()
Returns the layer for which this form is shown.
void beforeSave(bool &ok)
Will be emitted before the feature is saved.
@ SearchMode
Form values are used for searching/filtering the layer.
@ MultiEditMode
Multi edit mode, for editing fields of multiple features at once.
@ AggregateSearchMode
Form is in aggregate search mode, show each widget in this mode.
@ SingleEditMode
Single edit mode, for editing a single feature.
void filterExpressionSet(const QString &expression, QgsAttributeForm::FilterType type)
Emitted when a filter expression is set using the form.
void closed()
Emitted when the user selects the close option from the form's button bar.
void setEditCommandMessage(const QString &message)
Sets the edit command message (Undo) that will be used when the dialog is accepted.
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
void openFilteredFeaturesAttributeTable(const QString &filter)
Emitted when the user chooses to open the attribute table dialog with a filtered set of features.
void flashFeatures(const QString &filter)
Emitted when the user chooses to flash a filtered set of features.
const QgsFeature & feature()
void modeChanged(QgsAttributeEditorContext::Mode mode)
Emitted when the form changes mode.
void featureSaved(const QgsFeature &feature)
Emitted when a feature is changed or added.
void zoomToFeatures(const QString &filter)
Emitted when the user chooses to zoom to a filtered set of features.
QgsFeature currentFormFeature() const
Returns the feature that is currently displayed in the form with all the changes received on editing ...
Q_DECL_DEPRECATED void attributeChanged(const QString &attribute, const QVariant &value)
Notifies about changes of attributes, this signal is not emitted when the value is set back to the or...
FilterType
Filter types.
@ ReplaceFilter
Filter should replace any existing filter.
@ FilterOr
Filter should be combined using "OR".
@ FilterAnd
Filter should be combined using "AND".
QgsAttributeEditorContext::Mode mode() const
Returns the current mode of the form.
Manages an editor widget.
ConstraintResult
Result of constraint checks.
Single scope for storing variables and functions for use within a QgsExpressionContext.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Handles parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
A geometry is the spatial representation of a feature.
Represents an item shown within a QgsMessageBar widget.
A bar for displaying non-blocking messages to the user.
A widget wrapper for relation widgets.
Represents a relationship between two vector layers.
Definition qgsrelation.h:44
A QTabWidget with additional methods to temporarily hide/show tabs.
Represents a vector layer which manages a vector based dataset.
Manages an editor widget.
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
The TabStyle struct defines color and font overrides for form fields, tabs and groups labels.