QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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"
23
24#include <QWidget>
25#include <QLabel>
26#include <QDialogButtonBox>
27#include <QMultiMap>
28
29#include "qgis_gui.h"
30
31
34class QgsMessageBar;
37class QgsTabWidget;
40class QSvgWidget;
41
46class GUI_EXPORT QgsAttributeForm : public QWidget
47{
48 Q_OBJECT
49
50 public:
51
53 enum Mode
54 {
56 AddFeatureMode,
61 IdentifyMode
62 };
63
66 {
70 };
71
72 explicit QgsAttributeForm( QgsVectorLayer *vl,
73 const QgsFeature &feature = QgsFeature(),
75 QWidget *parent SIP_TRANSFERTHIS = nullptr );
76 ~QgsAttributeForm() override;
77
78 const QgsFeature &feature() { return mFeature; }
79
86 QgsFeature currentFormFeature() const { return mCurrentFormFeature; }
87
94 void displayWarning( const QString &message );
95
96 // TODO QGIS 4.0 - make private
97
102 void hideButtonBox();
103
104 // TODO QGIS 4.0 - make private
105
110 void showButtonBox();
111
112 // TODO QGIS 4.0 - make private
113
118 void disconnectButtonBox();
119
124 void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
125
131 QgsVectorLayer *layer() { return mLayer; }
132
138 bool editable();
139
145 QgsAttributeEditorContext::Mode mode() const { return mMode; }
146
153 void setMode( QgsAttributeEditorContext::Mode mode );
154
160 void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
161
170 bool eventFilter( QObject *object, QEvent *event ) override;
171
177 void setMultiEditFeatureIds( const QgsFeatureIds &fids );
178
185 void setMessageBar( QgsMessageBar *messageBar );
186
194 QString aggregateFilter() const;
195
202 void setExtraContextScope( QgsExpressionContextScope *extraScope SIP_TRANSFER );
203
208 bool needsGeometry() const;
209
210 signals:
211
220 Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
221
230 void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
231
240 void beforeSave( bool &ok ) SIP_SKIP;
241
245 void featureSaved( const QgsFeature &feature );
246
253 void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
254
260
265 void closed();
266
271 void zoomToFeatures( const QString &filter );
272
277 void flashFeatures( const QString &filter );
278
283 void openFilteredFeaturesAttributeTable( const QString &filter );
284
285 public slots:
286
294 void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
295
301 void changeGeometry( const QgsGeometry &geometry );
302
308 void setFeature( const QgsFeature &feature );
309
315 bool save();
316
326 bool saveWithDetails( QString *error SIP_OUT = nullptr );
327
331 void resetValues();
332
337 void resetSearch();
338
342 void refreshFeature();
343
353 void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
354
355 private slots:
356 void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
357 void onAttributeAdded( int idx );
358 void onAttributeDeleted( int idx );
359 void onRelatedFeaturesChanged();
360 void onUpdatedFields();
361 void onConstraintStatusChanged( const QString &constraint,
362 const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
363 void preventFeatureRefresh();
364 void synchronizeState();
365 void layerSelectionChanged();
366
368 bool saveMultiEdits();
369 void resetMultiEdit( bool promptToSave = false );
370 void multiEditMessageClicked( const QString &link );
371
372 void filterAndTriggered();
373 void filterOrTriggered();
374 void filterTriggered();
375
376 void searchZoomTo();
377 void searchFlash();
378 void searchSetSelection();
379 void searchAddToSelection();
380 void searchRemoveFromSelection();
381 void searchIntersectSelection();
382
383 private:
384 void init();
385
386 void cleanPython();
387
388 void initPython();
389
390 void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
391
392 bool fieldIsEditable( int fieldIndex ) const;
393
394 bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
395
396 void updateFieldDependencies();
397 void updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww );
398 void updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww );
399 void updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrapper *eww = nullptr );
400
401 void setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids );
402
403 struct WidgetInfo
404 {
405 QWidget *widget = nullptr;
406 QString labelText;
407 QString toolTip;
408 QString hint;
409 bool labelOnTop = false;
410 bool labelAlignRight = false;
411 bool showLabel = true;
413 };
414
415 WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
416
421 void createWrappers();
422 void afterWidgetInit();
423
424 void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
425
427 bool saveEdits( QString *error );
428
429 QgsFeature getUpdatedFeature() const;
430
432 void updateValuesDependencies( const int originIdx );
433 void updateValuesDependenciesDefaultValues( const int originIdx );
434 void updateValuesDependenciesVirtualFields( const int originIdx );
435 void updateRelatedLayerFields();
436
437 void clearMultiEditMessages();
438 void pushSelectedFeaturesMessage();
439 void runSearchSelect( Qgis::SelectBehavior behavior );
440
441 QString createFilterExpression() const;
442
443 QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
444
446 void updateAllConstraints();
447 void updateConstraints( QgsEditorWidgetWrapper *w );
448 void updateContainersVisibility();
449 void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
450 void updateLabels();
451 void updateEditableState();
452 bool currentFormValuesFeature( QgsFeature &feature );
453 bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
454 bool currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
455 QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
456
457 Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
458 QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QString &relationWidgetTypeId, const QgsRelation &rel, const QgsAttributeEditorContext &context );
459
460 QgsVectorLayer *mLayer = nullptr;
461 QgsFeature mFeature;
462 QgsFeature mCurrentFormFeature;
463 QgsMessageBar *mMessageBar = nullptr;
464 bool mOwnsMessageBar;
465 QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
466 QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
467 QList<QgsWidgetWrapper *> mWidgets;
469 std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
470 QDialogButtonBox *mButtonBox = nullptr;
471 QWidget *mSearchButtonBox = nullptr;
472 QList<QgsAttributeFormInterface *> mInterfaces;
473 QMultiMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
474 QList< QgsAttributeFormWidget *> mFormWidgets;
475 QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
476 QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
477 QMap<QWidget *, QgsProperty> mEditableDataDefinedProperties;
478 bool mValuesInitialized = false;
479 bool mDirty = false;
480 bool mIsSettingFeature = false;
481
482 bool mValidConstraints = true;
483 QgsMessageBarItem *mConstraintsFailMessageBarItem = nullptr;
484
485 struct ContainerInformation
486 {
487 ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
488 : tabWidget( tabWidget )
489 , widget( widget )
490 , expression( expression )
491 , isVisible( true )
492 {}
493
494 ContainerInformation( QWidget *widget, const QgsExpression &expression )
495 : widget( widget )
496 , expression( expression )
497 , isVisible( true )
498 {}
499
500 ContainerInformation( QWidget *widget, const QgsExpression &visibilityExpression, bool collapsed, const QgsExpression &collapsedExpression )
501 : widget( widget )
502 , expression( visibilityExpression )
503 , isVisible( true )
504 , isCollapsed( collapsed )
505 , collapsedExpression( collapsedExpression )
506 {}
507
508
509 QgsTabWidget *tabWidget = nullptr;
510 QWidget *widget = nullptr;
511 QgsExpression expression;
512 bool isVisible;
513 bool isCollapsed = false;
514 QgsExpression collapsedExpression;
515
516 void apply( QgsExpressionContext *expressionContext );
517 };
518
519 void registerContainerInformation( ContainerInformation *info );
520
521 void updateIcon( QgsEditorWidgetWrapper *eww );
522
523 void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
524
525 // Contains information about tabs and groupboxes, their visibility/collapsed state conditions
526 QVector<ContainerInformation *> mContainerVisibilityCollapsedInformation;
527 QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
528
529 // Variables below are used for Python
530 static int sFormCounter;
531 int mFormNr;
532 QString mPyFormVarName;
533
535 bool mIsSaving;
536
538 bool mPreventFeatureRefresh;
539
540 bool mIsSettingMultiEditFeatures;
541
542 QgsFeatureIds mMultiEditFeatureIds;
543 bool mUnsavedMultiEditChanges;
544
545 QString mEditCommandMessage;
546
548
549 QMap<QWidget *, QSvgWidget *> mIconMap;
550
555 QMultiMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
556
561 QMultiMap<int, QgsWidgetWrapper *> mVirtualFieldsDependencies;
562
566 QSet<QgsEditorWidgetWrapper *> mRelatedLayerFieldsDependencies;
567
569 QList<int> mAlreadyUpdatedFields;
570
571 bool mNeedsGeometry = false;
572
573 friend class TestQgsDualView;
574 friend class TestQgsAttributeForm;
575};
576
577#endif // QGSATTRIBUTEFORM_H
SelectBehavior
Specifies how a selection should be applied.
Definition: qgis.h:1009
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:501
The TabStyle struct defines color and font overrides for form fields, tabs and groups labels.