QGIS API Documentation 3.38.0-Grenoble (exported)
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 "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
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
145 QgsAttributeEditorContext::Mode mode() const { return mMode; }
146
152 void setMode( QgsAttributeEditorContext::Mode mode );
153
159 void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
160
169 bool eventFilter( QObject *object, QEvent *event ) override;
170
175 void setMultiEditFeatureIds( const QgsFeatureIds &fids );
176
182 void setMessageBar( QgsMessageBar *messageBar );
183
190 QString aggregateFilter() const;
191
198 void setExtraContextScope( QgsExpressionContextScope *extraScope SIP_TRANSFER );
199
204 bool needsGeometry() const;
205
206 signals:
207
216 Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
217
225 void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
226
235 void beforeSave( bool &ok ) SIP_SKIP;
236
240 void featureSaved( const QgsFeature &feature );
241
247 void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
248
254
258 void closed();
259
263 void zoomToFeatures( const QString &filter );
264
268 void flashFeatures( const QString &filter );
269
274 void openFilteredFeaturesAttributeTable( const QString &filter );
275
276 public slots:
277
285 void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
286
292 void changeGeometry( const QgsGeometry &geometry );
293
299 void setFeature( const QgsFeature &feature );
300
306 bool save();
307
317 bool saveWithDetails( QString *error SIP_OUT = nullptr );
318
322 void resetValues();
323
327 void resetSearch();
328
332 void refreshFeature();
333
343 void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
344
345 private slots:
346 void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
347 void onAttributeAdded( int idx );
348 void onAttributeDeleted( int idx );
349 void onRelatedFeaturesChanged();
350 void onUpdatedFields();
351 void onConstraintStatusChanged( const QString &constraint,
352 const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
353 void preventFeatureRefresh();
354 void synchronizeState();
355 void layerSelectionChanged();
356
358 bool saveMultiEdits();
359 void resetMultiEdit( bool promptToSave = false );
360 void multiEditMessageClicked( const QString &link );
361
362 void filterAndTriggered();
363 void filterOrTriggered();
364 void filterTriggered();
365
366 void searchZoomTo();
367 void searchFlash();
368 void searchSetSelection();
369 void searchAddToSelection();
370 void searchRemoveFromSelection();
371 void searchIntersectSelection();
372
373 private:
374 void init();
375
376 void cleanPython();
377
378 void initPython();
379
380 void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
381
382 bool fieldIsEditable( int fieldIndex ) const;
383
384 bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
385
386 void updateFieldDependencies();
387 void updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww );
388 void updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww );
389 void updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrapper *eww = nullptr );
390
391 void setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids );
392
393 struct WidgetInfo
394 {
395 QWidget *widget = nullptr;
396 QString labelText;
397 QString toolTip;
398 QString hint;
399 bool labelOnTop = false;
400 bool labelAlignRight = false;
401 bool showLabel = true;
403 };
404
405 WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
406
411 void createWrappers();
412 void afterWidgetInit();
413
414 void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
415
417 bool saveEdits( QString *error );
418
419 QgsFeature getUpdatedFeature() const;
420
422 void updateValuesDependencies( const int originIdx );
423 void updateValuesDependenciesDefaultValues( const int originIdx );
424 void updateValuesDependenciesVirtualFields( const int originIdx );
425 void updateRelatedLayerFields();
426
427 void clearMultiEditMessages();
428 void pushSelectedFeaturesMessage();
429 void runSearchSelect( Qgis::SelectBehavior behavior );
430
431 QString createFilterExpression() const;
432
433 QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
434
436 void updateAllConstraints();
437 void updateConstraints( QgsEditorWidgetWrapper *w );
438 void updateContainersVisibility();
439 void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
440 void updateLabels();
441 void updateEditableState();
442 bool currentFormValuesFeature( QgsFeature &feature );
443 bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
444 bool currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
445 QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
446
447 Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
448 QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QString &relationWidgetTypeId, const QgsRelation &rel, const QgsAttributeEditorContext &context );
449
450 QgsVectorLayer *mLayer = nullptr;
451 QgsFeature mFeature;
452 QgsFeature mCurrentFormFeature;
453 QgsMessageBar *mMessageBar = nullptr;
454 bool mOwnsMessageBar;
455 QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
456 QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
457 QList<QgsWidgetWrapper *> mWidgets;
459 std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
460 QDialogButtonBox *mButtonBox = nullptr;
461 QWidget *mSearchButtonBox = nullptr;
462 QList<QgsAttributeFormInterface *> mInterfaces;
463 QMultiMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
464 QList< QgsAttributeFormWidget *> mFormWidgets;
465 QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
466 QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
467 QMap<QWidget *, QgsProperty> mEditableDataDefinedProperties;
468 bool mValuesInitialized = false;
469 bool mDirty = false;
470 bool mIsSettingFeature = false;
471
472 bool mValidConstraints = true;
473 QgsMessageBarItem *mConstraintsFailMessageBarItem = nullptr;
474
475 struct ContainerInformation
476 {
477 ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
478 : tabWidget( tabWidget )
479 , widget( widget )
480 , expression( expression )
481 , isVisible( true )
482 {}
483
484 ContainerInformation( QWidget *widget, const QgsExpression &expression )
485 : widget( widget )
486 , expression( expression )
487 , isVisible( true )
488 {}
489
490 ContainerInformation( QWidget *widget, const QgsExpression &visibilityExpression, bool collapsed, const QgsExpression &collapsedExpression )
491 : widget( widget )
492 , expression( visibilityExpression )
493 , isVisible( true )
494 , isCollapsed( collapsed )
495 , collapsedExpression( collapsedExpression )
496 {}
497
498
499 QgsTabWidget *tabWidget = nullptr;
500 QWidget *widget = nullptr;
501 QgsExpression expression;
502 bool isVisible;
503 bool isCollapsed = false;
504 QgsExpression collapsedExpression;
505
506 void apply( QgsExpressionContext *expressionContext );
507 };
508
509 void registerContainerInformation( ContainerInformation *info );
510
511 void updateIcon( QgsEditorWidgetWrapper *eww );
512
513 void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
514
515 // Contains information about tabs and groupboxes, their visibility/collapsed state conditions
516 QVector<ContainerInformation *> mContainerVisibilityCollapsedInformation;
517 QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
518
519 // Variables below are used for Python
520 static int sFormCounter;
521 int mFormNr;
522 QString mPyFormVarName;
523
525 bool mIsSaving;
526
528 bool mPreventFeatureRefresh;
529
530 bool mIsSettingMultiEditFeatures;
531
532 QgsFeatureIds mMultiEditFeatureIds;
533 bool mUnsavedMultiEditChanges;
534
535 QString mEditCommandMessage;
536
538
539 QMap<QWidget *, QSvgWidget *> mIconMap;
540
545 QMultiMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
546
551 QMultiMap<int, QgsWidgetWrapper *> mVirtualFieldsDependencies;
552
556 QSet<QgsEditorWidgetWrapper *> mRelatedLayerFieldsDependencies;
557
559 QList<int> mAlreadyUpdatedFields;
560
561 bool mNeedsGeometry = false;
562
563 friend class TestQgsDualView;
564 friend class TestQgsAttributeForm;
565};
566
567#endif // QGSATTRIBUTEFORM_H
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1438
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.
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.