QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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
47class GUI_EXPORT QgsAttributeForm : public QWidget
48{
49 Q_OBJECT
50
51 public:
52
54 enum Mode
55 {
57 AddFeatureMode,
62 IdentifyMode
63 };
64
67 {
71 };
72
73 explicit QgsAttributeForm( QgsVectorLayer *vl,
74 const QgsFeature &feature = QgsFeature(),
76 QWidget *parent SIP_TRANSFERTHIS = nullptr );
77 ~QgsAttributeForm() override;
78
79 const QgsFeature &feature() { return mFeature; }
80
87 QgsFeature currentFormFeature() const { return mCurrentFormFeature; }
88
95 void displayWarning( const QString &message );
96
97 // TODO QGIS 4.0 - make private
98
103 void hideButtonBox();
104
105 // TODO QGIS 4.0 - make private
106
111 void showButtonBox();
112
113 // TODO QGIS 4.0 - make private
114
119 void disconnectButtonBox();
120
125 void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
126
132 QgsVectorLayer *layer() { return mLayer; }
133
139 bool editable();
140
146 QgsAttributeEditorContext::Mode mode() const { return mMode; }
147
154 void setMode( QgsAttributeEditorContext::Mode mode );
155
161 void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
162
171 bool eventFilter( QObject *object, QEvent *event ) override;
172
178 void setMultiEditFeatureIds( const QgsFeatureIds &fids );
179
186 void setMessageBar( QgsMessageBar *messageBar );
187
195 QString aggregateFilter() const;
196
203 void setExtraContextScope( QgsExpressionContextScope *extraScope SIP_TRANSFER );
204
209 bool needsGeometry() const;
210
211 signals:
212
221 Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
222
231 void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
232
241 void beforeSave( bool &ok ) SIP_SKIP;
242
246 void featureSaved( const QgsFeature &feature );
247
254 void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
255
261
266 void closed();
267
272 void zoomToFeatures( const QString &filter );
273
278 void flashFeatures( const QString &filter );
279
284 void openFilteredFeaturesAttributeTable( const QString &filter );
285
286 public slots:
287
295 void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
296
302 void changeGeometry( const QgsGeometry &geometry );
303
309 void setFeature( const QgsFeature &feature );
310
316 bool save();
317
327 bool saveWithDetails( QString *error SIP_OUT = nullptr );
328
332 void resetValues();
333
338 void resetSearch();
339
343 void refreshFeature();
344
354 void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
355
356 private slots:
357 void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
358 void onAttributeAdded( int idx );
359 void onAttributeDeleted( int idx );
360 void onRelatedFeaturesChanged();
361 void onUpdatedFields();
362 void onConstraintStatusChanged( const QString &constraint,
363 const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
364 void preventFeatureRefresh();
365 void synchronizeState();
366 void layerSelectionChanged();
367
369 bool saveMultiEdits();
370 void resetMultiEdit( bool promptToSave = false );
371 void multiEditMessageClicked( const QString &link );
372
373 void filterAndTriggered();
374 void filterOrTriggered();
375 void filterTriggered();
376
377 void searchZoomTo();
378 void searchFlash();
379 void searchSetSelection();
380 void searchAddToSelection();
381 void searchRemoveFromSelection();
382 void searchIntersectSelection();
383
384 private:
385 void init();
386
387 void cleanPython();
388
389 void initPython();
390
391 void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
392
393 bool fieldIsEditable( int fieldIndex ) const;
394
395 bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
396
397 void updateFieldDependencies();
398 void updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww );
399 void updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww );
400 void updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrapper *eww = nullptr );
401
402 void setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids );
403
404 struct WidgetInfo
405 {
406 QWidget *widget = nullptr;
407 QString labelText;
408 QString toolTip;
409 QString hint;
410 bool labelOnTop = false;
411 bool labelAlignRight = false;
412 bool showLabel = true;
414 };
415
416 WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
417
422 void createWrappers();
423 void afterWidgetInit();
424
425 void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
426
428 bool saveEdits( QString *error );
429
430 QgsFeature getUpdatedFeature() const;
431
433 void updateValuesDependencies( const int originIdx );
434 void updateValuesDependenciesDefaultValues( const int originIdx );
435 void updateValuesDependenciesVirtualFields( const int originIdx );
436 void updateRelatedLayerFields();
437
438 void clearMultiEditMessages();
439 void pushSelectedFeaturesMessage();
440 void runSearchSelect( Qgis::SelectBehavior behavior );
441
442 QString createFilterExpression() const;
443
444 QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
445
447 void updateAllConstraints();
448 void updateConstraints( QgsEditorWidgetWrapper *w );
449 void updateContainersVisibility();
450 void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
451 void updateLabels();
452 void updateEditableState();
453 bool currentFormValuesFeature( QgsFeature &feature );
454 bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
455 bool currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
456 QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
457
458 Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
459 QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QString &relationWidgetTypeId, const QgsRelation &rel, const QgsAttributeEditorContext &context );
460
461 QgsVectorLayer *mLayer = nullptr;
462 QgsFeature mFeature;
463 QgsFeature mCurrentFormFeature;
464 QgsMessageBar *mMessageBar = nullptr;
465 bool mOwnsMessageBar;
466 QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
467 QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
468 QList<QgsWidgetWrapper *> mWidgets;
470 std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
471 QDialogButtonBox *mButtonBox = nullptr;
472 QWidget *mSearchButtonBox = nullptr;
473 QList<QgsAttributeFormInterface *> mInterfaces;
474 QMultiMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
475 QList< QgsAttributeFormWidget *> mFormWidgets;
476 QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
477 QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
478 QMap<QWidget *, QgsProperty> mEditableDataDefinedProperties;
479 bool mValuesInitialized = false;
480 bool mDirty = false;
481 bool mIsSettingFeature = false;
482
483 bool mValidConstraints = true;
484 QgsMessageBarItem *mConstraintsFailMessageBarItem = nullptr;
485
486 struct ContainerInformation
487 {
488 ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
489 : tabWidget( tabWidget )
490 , widget( widget )
491 , expression( expression )
492 , isVisible( true )
493 {}
494
495 ContainerInformation( QWidget *widget, const QgsExpression &expression )
496 : widget( widget )
497 , expression( expression )
498 , isVisible( true )
499 {}
500
501 ContainerInformation( QWidget *widget, const QgsExpression &visibilityExpression, bool collapsed, const QgsExpression &collapsedExpression )
502 : widget( widget )
503 , expression( visibilityExpression )
504 , isVisible( true )
505 , isCollapsed( collapsed )
506 , collapsedExpression( collapsedExpression )
507 {}
508
509
510 QgsTabWidget *tabWidget = nullptr;
511 QWidget *widget = nullptr;
512 QgsExpression expression;
513 bool isVisible;
514 bool isCollapsed = false;
515 QgsExpression collapsedExpression;
516
517 void apply( QgsExpressionContext *expressionContext );
518 };
519
520 void registerContainerInformation( ContainerInformation *info );
521
522 void updateIcon( QgsEditorWidgetWrapper *eww );
523
524 void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
525
526 // Contains information about tabs and groupboxes, their visibility/collapsed state conditions
527 QVector<ContainerInformation *> mContainerVisibilityCollapsedInformation;
528 QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
529
530 // Variables below are used for Python
531 static int sFormCounter;
532 int mFormNr;
533 QString mPyFormVarName;
534
536 bool mIsSaving;
537
539 bool mPreventFeatureRefresh;
540
541 bool mIsSettingMultiEditFeatures;
542
543 QgsFeatureIds mMultiEditFeatureIds;
544 bool mUnsavedMultiEditChanges;
545
546 QString mEditCommandMessage;
547
549
550 QMap<QWidget *, QSvgWidget *> mIconMap;
551
556 QMultiMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
557
562 QMultiMap<int, QgsWidgetWrapper *> mVirtualFieldsDependencies;
563
567 QSet<QgsEditorWidgetWrapper *> mRelatedLayerFieldsDependencies;
568
570 QList<int> mAlreadyUpdatedFields;
571
572 bool mNeedsGeometry = false;
573
574 friend class TestQgsDualView;
575 friend class TestQgsAttributeForm;
576};
577
578#endif // QGSATTRIBUTEFORM_H
SelectBehavior
Specifies how a selection should be applied.
Definition: qgis.h:1167
This class contains context information for attribute editor widgets.
This is 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...
Base class for all widgets shown on a QgsAttributeForm.
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 Widget and wrapper share the same parent.
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...
Class for 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:56
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
Represents an item shown within a QgsMessageBar widget.
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
The QgsTabWidget class is the same as the QTabWidget but with additional methods to temporarily hide/...
Definition: qgstabwidget.h:33
Represents a vector layer which manages a vector based data sets.
Manages an editor widget Widget and wrapper share the same parent.
#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
Definition: qgsfeatureid.h:37
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
const QgsField & field
Definition: qgsfield.h:554
The TabStyle struct defines color and font overrides for form fields, tabs and groups labels.