QGIS API Documentation 3.41.0-Master (57ec4277f5e)
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:
53 enum Mode
54 {
56 AddFeatureMode,
61 IdentifyMode
62 };
63
71
72 explicit QgsAttributeForm( QgsVectorLayer *vl, const QgsFeature &feature = QgsFeature(), const QgsAttributeEditorContext &context = QgsAttributeEditorContext(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
73 ~QgsAttributeForm() override;
74
75 const QgsFeature &feature() { return mFeature; }
76
83 QgsFeature currentFormFeature() const { return mCurrentFormFeature; }
84
91 void displayWarning( const QString &message );
92
93 // TODO QGIS 4.0 - make private
94
99 void hideButtonBox();
100
101 // TODO QGIS 4.0 - make private
102
107 void showButtonBox();
108
109 // TODO QGIS 4.0 - make private
110
115 void disconnectButtonBox();
116
121 void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
122
128 QgsVectorLayer *layer() { return mLayer; }
129
135 bool editable();
136
141 QgsAttributeEditorContext::Mode mode() const { return mMode; }
142
148 void setMode( QgsAttributeEditorContext::Mode mode );
149
155 void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
156
165 bool eventFilter( QObject *object, QEvent *event ) override;
166
171 void setMultiEditFeatureIds( const QgsFeatureIds &fids );
172
178 void setMessageBar( QgsMessageBar *messageBar );
179
186 QString aggregateFilter() const;
187
194 void setExtraContextScope( QgsExpressionContextScope *extraScope SIP_TRANSFER );
195
200 bool needsGeometry() const;
201
202 signals:
203
212 Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
213
221 void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
222
231 void beforeSave( bool &ok ) SIP_SKIP;
232
236 void featureSaved( const QgsFeature &feature );
237
243 void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
244
250
254 void closed();
255
259 void zoomToFeatures( const QString &filter );
260
264 void flashFeatures( const QString &filter );
265
270 void openFilteredFeaturesAttributeTable( const QString &filter );
271
272 public slots:
273
281 void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
282
288 void changeGeometry( const QgsGeometry &geometry );
289
295 void setFeature( const QgsFeature &feature );
296
302 bool save();
303
313 bool saveWithDetails( QString *error SIP_OUT = nullptr );
314
318 void resetValues();
319
323 void resetSearch();
324
328 void refreshFeature();
329
339 void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
340
341 private slots:
342 void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
343 void onAttributeAdded( int idx );
344 void onAttributeDeleted( int idx );
345 void onRelatedFeaturesChanged();
346 void onUpdatedFields();
347 void onConstraintStatusChanged( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
348 void preventFeatureRefresh();
349 void synchronizeState();
350 void layerSelectionChanged();
351
353 bool saveMultiEdits();
354 void resetMultiEdit( bool promptToSave = false );
355 void multiEditMessageClicked( const QString &link );
356
357 void filterAndTriggered();
358 void filterOrTriggered();
359 void filterTriggered();
360
361 void searchZoomTo();
362 void searchFlash();
363 void searchSetSelection();
364 void searchAddToSelection();
365 void searchRemoveFromSelection();
366 void searchIntersectSelection();
367
368 private:
369 void init();
370
371 void cleanPython();
372
373 void initPython();
374
375 void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
376
377 bool fieldIsEditable( int fieldIndex ) const;
378
379 bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
380
381 void updateFieldDependencies();
382 void updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww );
383 void updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww );
384 void updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrapper *eww = nullptr );
385 void updateFieldDependenciesParent( QgsEditorWidgetWrapper *eww );
386
387 void setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids );
388
389 struct WidgetInfo
390 {
391 QWidget *widget = nullptr;
392 QString labelText;
393 QString toolTip;
394 QString hint;
395 bool labelOnTop = false;
396 bool labelAlignRight = false;
397 bool showLabel = true;
399 };
400
401 WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
402
407 void createWrappers();
408 void afterWidgetInit();
409
410 void scanForEqualAttributes( QgsFeatureIterator &fit, QSet<int> &mixedValueFields, QHash<int, QVariant> &fieldSharedValues ) const;
411
413 bool saveEdits( QString *error );
414
415 QgsFeature getUpdatedFeature() const;
416
418 void updateValuesDependencies( const int originIdx );
419 void updateValuesDependenciesDefaultValues( const int originIdx );
420 void updateValuesDependenciesVirtualFields( const int originIdx );
421 void updateValuesDependenciesParent();
422 void updateRelatedLayerFields();
423
424 void clearMultiEditMessages();
425 void pushSelectedFeaturesMessage();
426 void runSearchSelect( Qgis::SelectBehavior behavior );
427
428 QString createFilterExpression() const;
429
430 QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
431
433 void updateAllConstraints();
434 void updateConstraints( QgsEditorWidgetWrapper *w );
435 void updateContainersVisibility();
436 void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
437 void updateLabels();
438 void updateEditableState();
439 bool currentFormValuesFeature( QgsFeature &feature );
440 bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
441 bool currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
442 QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
443
444 Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
445 QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QString &relationWidgetTypeId, const QgsRelation &rel, const QgsAttributeEditorContext &context );
446
447 QgsVectorLayer *mLayer = nullptr;
448 QgsFeature mFeature;
449 QgsFeature mCurrentFormFeature;
450 QgsMessageBar *mMessageBar = nullptr;
451 bool mOwnsMessageBar;
452 QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
453 QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
454 QList<QgsWidgetWrapper *> mWidgets;
456 std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
457 QDialogButtonBox *mButtonBox = nullptr;
458 QWidget *mSearchButtonBox = nullptr;
459 QList<QgsAttributeFormInterface *> mInterfaces;
460 QMultiMap<int, QgsAttributeFormEditorWidget *> mFormEditorWidgets;
461 QList<QgsAttributeFormWidget *> mFormWidgets;
462 QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
463 QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
464 QMap<QWidget *, QgsProperty> mEditableDataDefinedProperties;
465 bool mValuesInitialized = false;
466 bool mDirty = false;
467 bool mIsSettingFeature = false;
468
469 bool mValidConstraints = true;
470 QgsMessageBarItem *mConstraintsFailMessageBarItem = nullptr;
471
472 struct ContainerInformation
473 {
474 ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
475 : tabWidget( tabWidget )
476 , widget( widget )
477 , expression( expression )
478 , isVisible( true )
479 {}
480
481 ContainerInformation( QWidget *widget, const QgsExpression &expression )
482 : widget( widget )
483 , expression( expression )
484 , isVisible( true )
485 {}
486
487 ContainerInformation( QWidget *widget, const QgsExpression &visibilityExpression, bool collapsed, const QgsExpression &collapsedExpression )
488 : widget( widget )
489 , expression( visibilityExpression )
490 , isVisible( true )
491 , isCollapsed( collapsed )
492 , collapsedExpression( collapsedExpression )
493 {}
494
495
496 QgsTabWidget *tabWidget = nullptr;
497 QWidget *widget = nullptr;
498 QgsExpression expression;
499 bool isVisible;
500 bool isCollapsed = false;
501 QgsExpression collapsedExpression;
502
503 void apply( QgsExpressionContext *expressionContext );
504 };
505
506 void registerContainerInformation( ContainerInformation *info );
507
508 void updateIcon( QgsEditorWidgetWrapper *eww );
509
510 void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
511
512 // Contains information about tabs and groupboxes, their visibility/collapsed state conditions
513 QVector<ContainerInformation *> mContainerVisibilityCollapsedInformation;
514 QMap<QString, QVector<ContainerInformation *>> mContainerInformationDependency;
515
516 // Variables below are used for Python
517 static int sFormCounter;
518 int mFormNr;
519 QString mPyFormVarName;
520
522 bool mIsSaving;
523
525 bool mPreventFeatureRefresh;
526
527 bool mIsSettingMultiEditFeatures;
528
529 QgsFeatureIds mMultiEditFeatureIds;
530 bool mUnsavedMultiEditChanges;
531
532 QString mEditCommandMessage;
533
535
536 QMap<QWidget *, QSvgWidget *> mIconMap;
537
542 QMultiMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
543
548 QMultiMap<int, QgsWidgetWrapper *> mVirtualFieldsDependencies;
549
553 QSet<QgsEditorWidgetWrapper *> mRelatedLayerFieldsDependencies;
554
555 QSet<QgsEditorWidgetWrapper *> mParentDependencies;
556
558 QList<int> mAlreadyUpdatedFields;
559
560 bool mNeedsGeometry = false;
561
562 friend class TestQgsDualView;
563 friend class TestQgsAttributeForm;
564 friend class TestQgsValueRelationWidgetWrapper;
565};
566
567#endif // QGSATTRIBUTEFORM_H
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1681
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: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.
Represents a relationship between two vector layers.
Definition qgsrelation.h:44
The QgsTabWidget class is the same as the QTabWidget but with additional methods to temporarily hide/...
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
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.