QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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 "qgis_gui.h"
20#include "qgis_sip.h"
24#include "qgsfeature.h"
25
26#include <QDialogButtonBox>
27#include <QLabel>
28#include <QMultiMap>
29#include <QWidget>
30
33class QgsMessageBar;
36class QgsTabWidget;
39class QSvgWidget;
40
46class GUI_EXPORT QgsAttributeForm : public QWidget
47{
48 Q_OBJECT
49
50 public:
62
70
71 explicit QgsAttributeForm( QgsVectorLayer *vl, const QgsFeature &feature = QgsFeature(), const QgsAttributeEditorContext &context = QgsAttributeEditorContext(), QWidget *parent SIP_TRANSFERTHIS = nullptr );
72 ~QgsAttributeForm() override;
73
77 const QgsFeature &feature() const { return mFeature; }
78
85 QgsFeature currentFormFeature() const { return mCurrentFormFeature; }
86
93 void displayWarning( const QString &message );
94
95 // TODO QGIS 4.0 - make private
96
101 void hideButtonBox();
102
103 // TODO QGIS 4.0 - make private
104
109 void showButtonBox();
110
111 // TODO QGIS 4.0 - make private
112
117 void disconnectButtonBox();
118
123 void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
124
130 QgsVectorLayer *layer() { return mLayer; }
131
137 bool editable();
138
143 QgsAttributeEditorContext::Mode mode() const { return mMode; }
144
150 void setMode( QgsAttributeEditorContext::Mode mode );
151
157 void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
158
167 bool eventFilter( QObject *object, QEvent *event ) override;
168
173 void setMultiEditFeatureIds( const QgsFeatureIds &fids );
174
180 void setMessageBar( QgsMessageBar *messageBar );
181
188 QString aggregateFilter() const;
189
196 void setExtraContextScope( QgsExpressionContextScope *extraScope SIP_TRANSFER );
197
202 bool needsGeometry() const;
203
209 static QgsFeature createFeature( QgsVectorLayer *layer, const QgsGeometry &geometry, const QgsAttributeMap &attributes, QgsExpressionContext &context );
210
211 signals:
212
221 Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
222
230 void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
231
240 void rememberLastWidgetValueChanged( const QString &attribute, bool remember );
241
250 void beforeSave( bool &ok ) SIP_SKIP;
251
256
262 void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
263
269
273 void closed();
274
278 void zoomToFeatures( const QString &filter );
279
283 void flashFeatures( const QString &filter );
284
289 void openFilteredFeaturesAttributeTable( const QString &filter );
290
291 public slots:
292
300 void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
301
307 void changeGeometry( const QgsGeometry &geometry );
308
314 void setFeature( const QgsFeature &feature );
315
321 bool save();
322
332 bool saveWithDetails( QString *error SIP_OUT = nullptr );
333
337 void resetValues();
338
342 void resetSearch();
343
347 void refreshFeature();
348
358 void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
359
360 private slots:
361 void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
362 void onAttributeAdded( int idx );
363 void onAttributeDeleted( int idx );
364 void onRelatedFeaturesChanged();
365 void onUpdatedFields();
366 void onConstraintStatusChanged( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
367 void preventFeatureRefresh();
368 void synchronizeState();
369 void layerSelectionChanged();
370
372 bool saveMultiEdits();
373 void resetMultiEdit( bool promptToSave = false );
374 void multiEditMessageClicked( const QString &link );
375
376 void filterAndTriggered();
377 void filterOrTriggered();
378 void filterTriggered();
379
380 void searchZoomTo();
381 void searchFlash();
382 void searchSetSelection();
383 void searchAddToSelection();
384 void searchRemoveFromSelection();
385 void searchIntersectSelection();
386
387 private:
388 void init();
389
390 void cleanPython();
391
392 void initPython();
393
394 void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
395
396 bool fieldIsEditable( int fieldIndex ) const;
397
398 bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
399
400 void updateFieldDependencies();
401 void updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww );
402 void updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww );
403 void updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrapper *eww = nullptr );
404 void updateFieldDependenciesParent( QgsEditorWidgetWrapper *eww );
405
406 void setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids );
407
408 struct WidgetInfo
409 {
410 QWidget *widget = nullptr;
411 QString labelText;
412 QString toolTip;
413 QString hint;
414 bool labelOnTop = false;
415 bool labelAlignRight = false;
416 bool showLabel = true;
418 };
419
420 WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
421
426 void createWrappers();
427 void afterWidgetInit();
428
429 void scanForEqualAttributes( QgsFeatureIterator &fit, QSet<int> &mixedValueFields, QHash<int, QVariant> &fieldSharedValues ) const;
430
432 bool saveEdits( QString *error );
433
434 QgsFeature getUpdatedFeature() const;
435
437 void updateValuesDependencies( const int originIdx );
438 void updateValuesDependenciesDefaultValues( const int originIdx );
439 void updateValuesDependenciesVirtualFields( const int originIdx );
440 void updateValuesDependenciesParent();
441 void updateRelatedLayerFields();
442
443 void clearMultiEditMessages();
444 void pushSelectedFeaturesMessage();
445 void runSearchSelect( Qgis::SelectBehavior behavior );
446
447 QString createFilterExpression() const;
448
449 QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
450
452 void updateAllConstraints();
453 void updateConstraints( QgsEditorWidgetWrapper *w );
454 void updateContainersVisibility();
455 void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
456 void updateLabels();
457 void updateEditableState();
458 bool currentFormValuesFeature( QgsFeature &feature );
459 bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
460 bool currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
461 QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
462
463 Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
464 QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QString &relationWidgetTypeId, const QgsRelation &rel, const QgsAttributeEditorContext &context );
465
466 QgsVectorLayer *mLayer = nullptr;
467 QgsFeature mFeature;
468 QgsFeature mCurrentFormFeature;
469 QgsMessageBar *mMessageBar = nullptr;
470 bool mOwnsMessageBar;
471 QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
472 QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
473 QList<QgsWidgetWrapper *> mWidgets;
475 std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
476 QDialogButtonBox *mButtonBox = nullptr;
477 QWidget *mSearchButtonBox = nullptr;
478 QList<QgsAttributeFormInterface *> mInterfaces;
479 QMultiMap<int, QgsAttributeFormEditorWidget *> mFormEditorWidgets;
480 QList<QgsAttributeFormWidget *> mFormWidgets;
481 QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
482 QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
483 QMap<QWidget *, QgsProperty> mEditableDataDefinedProperties;
484 bool mValuesInitialized = false;
485 bool mDirty = false;
486 bool mIsSettingFeature = false;
487
488 bool mValidConstraints = true;
489 QgsMessageBarItem *mConstraintsFailMessageBarItem = nullptr;
490
491 struct ContainerInformation
492 {
493 ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
494 : tabWidget( tabWidget )
495 , widget( widget )
496 , expression( expression )
497 , isVisible( true )
498 {}
499
500 ContainerInformation( QWidget *widget, const QgsExpression &expression )
501 : widget( widget )
502 , expression( expression )
503 , isVisible( true )
504 {}
505
506 ContainerInformation( QWidget *widget, const QgsExpression &visibilityExpression, bool collapsed, const QgsExpression &collapsedExpression )
507 : widget( widget )
508 , expression( visibilityExpression )
509 , isVisible( true )
510 , isCollapsed( collapsed )
511 , collapsedExpression( collapsedExpression )
512 {}
513
514
515 QgsTabWidget *tabWidget = nullptr;
516 QWidget *widget = nullptr;
517 QgsExpression expression;
518 bool isVisible;
519 bool isCollapsed = false;
520 QgsExpression collapsedExpression;
521
522 void apply( QgsExpressionContext *expressionContext );
523 };
524
525 void registerContainerInformation( ContainerInformation *info );
526
527 void updateIcon( QgsEditorWidgetWrapper *eww );
528
529 void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
530
531 // Contains information about tabs and groupboxes, their visibility/collapsed state conditions
532 QVector<ContainerInformation *> mContainerVisibilityCollapsedInformation;
533 QMap<QString, QVector<ContainerInformation *>> mContainerInformationDependency;
534
535 // Variables below are used for Python
536 static int sFormCounter;
537 int mFormNr;
538 QString mPyFormVarName;
539
541 bool mIsSaving;
542
544 bool mPreventFeatureRefresh;
545
546 bool mIsSettingMultiEditFeatures;
547
548 QgsFeatureIds mMultiEditFeatureIds;
549 bool mUnsavedMultiEditChanges;
550
551 QString mEditCommandMessage;
552
554
555 QMap<QWidget *, QSvgWidget *> mIconMap;
556
561 QMultiMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
562
567 QMultiMap<int, QgsWidgetWrapper *> mVirtualFieldsDependencies;
568
572 QSet<QgsEditorWidgetWrapper *> mRelatedLayerFieldsDependencies;
573
574 QSet<QgsEditorWidgetWrapper *> mParentDependencies;
575
577 QList<int> mAlreadyUpdatedFields;
578
579 bool mNeedsGeometry = false;
580
581 friend class TestQgsDualView;
584};
585
586#endif // QGSATTRIBUTEFORM_H
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1771
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.
void parentFormValueChanged(const QString &attribute, const QVariant &newValue)
Is called in embedded forms when an attribute value in the parent form has changed to newValue.
QgsVectorLayer * layer()
Returns the layer for which this form is shown.
void setFeature(const QgsFeature &feature)
Update all editors to correspond to a different feature.
void refreshFeature()
reload current feature
void beforeSave(bool &ok)
Will be emitted before the feature is saved.
friend class TestQgsValueRelationWidgetWrapper
void changeGeometry(const QgsGeometry &geometry)
Changes the geometry of the feature attached to the form.
friend class TestQgsAttributeForm
void rememberLastWidgetValueChanged(const QString &attribute, bool remember)
Notifies about changes to remembrance of attributes' last value.
@ 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.
@ IdentifyMode
Identify the feature.
@ SingleEditMode
Single edit mode, for editing a single feature.
bool saveWithDetails(QString *error=nullptr)
Save all the values from the editors to the layer.
void changeAttribute(const QString &field, const QVariant &value, const QString &hintText=QString())
Call this to change the content of a given attribute.
bool save()
Save all the values from the editors to the layer.
void filterExpressionSet(const QString &expression, QgsAttributeForm::FilterType type)
Emitted when a filter expression is set using the form.
friend class TestQgsDualView
void closed()
Emitted when the user selects the close option from the form's button bar.
void resetSearch()
Resets the search/filter form values.
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.
void resetValues()
Sets all values to the values of the current feature.
QgsAttributeForm(QgsVectorLayer *vl, const QgsFeature &feature=QgsFeature(), const QgsAttributeEditorContext &context=QgsAttributeEditorContext(), QWidget *parent=nullptr)
void modeChanged(QgsAttributeEditorContext::Mode mode)
Emitted when the form changes mode.
const QgsFeature & feature() const
Returns feature of attribute form.
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:42
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:114
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
QMap< int, QVariant > QgsAttributeMap
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.