QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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 void updateFieldDependenciesParent( QgsEditorWidgetWrapper *eww );
391
392 void setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids );
393
394 struct WidgetInfo
395 {
396 QWidget *widget = nullptr;
397 QString labelText;
398 QString toolTip;
399 QString hint;
400 bool labelOnTop = false;
401 bool labelAlignRight = false;
402 bool showLabel = true;
404 };
405
406 WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
407
412 void createWrappers();
413 void afterWidgetInit();
414
415 void scanForEqualAttributes( QgsFeatureIterator &fit, QSet< int > &mixedValueFields, QHash< int, QVariant > &fieldSharedValues ) const;
416
418 bool saveEdits( QString *error );
419
420 QgsFeature getUpdatedFeature() const;
421
423 void updateValuesDependencies( const int originIdx );
424 void updateValuesDependenciesDefaultValues( const int originIdx );
425 void updateValuesDependenciesVirtualFields( const int originIdx );
426 void updateValuesDependenciesParent();
427 void updateRelatedLayerFields();
428
429 void clearMultiEditMessages();
430 void pushSelectedFeaturesMessage();
431 void runSearchSelect( Qgis::SelectBehavior behavior );
432
433 QString createFilterExpression() const;
434
435 QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
436
438 void updateAllConstraints();
439 void updateConstraints( QgsEditorWidgetWrapper *w );
440 void updateContainersVisibility();
441 void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
442 void updateLabels();
443 void updateEditableState();
444 bool currentFormValuesFeature( QgsFeature &feature );
445 bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
446 bool currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
447 QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
448
449 Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
450 QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QString &relationWidgetTypeId, const QgsRelation &rel, const QgsAttributeEditorContext &context );
451
452 QgsVectorLayer *mLayer = nullptr;
453 QgsFeature mFeature;
454 QgsFeature mCurrentFormFeature;
455 QgsMessageBar *mMessageBar = nullptr;
456 bool mOwnsMessageBar;
457 QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
458 QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
459 QList<QgsWidgetWrapper *> mWidgets;
461 std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
462 QDialogButtonBox *mButtonBox = nullptr;
463 QWidget *mSearchButtonBox = nullptr;
464 QList<QgsAttributeFormInterface *> mInterfaces;
465 QMultiMap< int, QgsAttributeFormEditorWidget * > mFormEditorWidgets;
466 QList< QgsAttributeFormWidget *> mFormWidgets;
467 QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
468 QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
469 QMap<QWidget *, QgsProperty> mEditableDataDefinedProperties;
470 bool mValuesInitialized = false;
471 bool mDirty = false;
472 bool mIsSettingFeature = false;
473
474 bool mValidConstraints = true;
475 QgsMessageBarItem *mConstraintsFailMessageBarItem = nullptr;
476
477 struct ContainerInformation
478 {
479 ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
480 : tabWidget( tabWidget )
481 , widget( widget )
482 , expression( expression )
483 , isVisible( true )
484 {}
485
486 ContainerInformation( QWidget *widget, const QgsExpression &expression )
487 : widget( widget )
488 , expression( expression )
489 , isVisible( true )
490 {}
491
492 ContainerInformation( QWidget *widget, const QgsExpression &visibilityExpression, bool collapsed, const QgsExpression &collapsedExpression )
493 : widget( widget )
494 , expression( visibilityExpression )
495 , isVisible( true )
496 , isCollapsed( collapsed )
497 , collapsedExpression( collapsedExpression )
498 {}
499
500
501 QgsTabWidget *tabWidget = nullptr;
502 QWidget *widget = nullptr;
503 QgsExpression expression;
504 bool isVisible;
505 bool isCollapsed = false;
506 QgsExpression collapsedExpression;
507
508 void apply( QgsExpressionContext *expressionContext );
509 };
510
511 void registerContainerInformation( ContainerInformation *info );
512
513 void updateIcon( QgsEditorWidgetWrapper *eww );
514
515 void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
516
517 // Contains information about tabs and groupboxes, their visibility/collapsed state conditions
518 QVector<ContainerInformation *> mContainerVisibilityCollapsedInformation;
519 QMap<QString, QVector<ContainerInformation *> > mContainerInformationDependency;
520
521 // Variables below are used for Python
522 static int sFormCounter;
523 int mFormNr;
524 QString mPyFormVarName;
525
527 bool mIsSaving;
528
530 bool mPreventFeatureRefresh;
531
532 bool mIsSettingMultiEditFeatures;
533
534 QgsFeatureIds mMultiEditFeatureIds;
535 bool mUnsavedMultiEditChanges;
536
537 QString mEditCommandMessage;
538
540
541 QMap<QWidget *, QSvgWidget *> mIconMap;
542
547 QMultiMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
548
553 QMultiMap<int, QgsWidgetWrapper *> mVirtualFieldsDependencies;
554
558 QSet<QgsEditorWidgetWrapper *> mRelatedLayerFieldsDependencies;
559
560 QSet<QgsEditorWidgetWrapper *> mParentDependencies;
561
563 QList<int> mAlreadyUpdatedFields;
564
565 bool mNeedsGeometry = false;
566
567 friend class TestQgsDualView;
568 friend class TestQgsAttributeForm;
569 friend class TestQgsValueRelationWidgetWrapper;
570};
571
572#endif // QGSATTRIBUTEFORM_H
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1618
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.