QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
72 QgsVectorLayer *vl, const QgsFeature &feature = QgsFeature(), const QgsAttributeEditorContext &context = QgsAttributeEditorContext(), QWidget *parent SIP_TRANSFERTHIS = nullptr
73 );
74 ~QgsAttributeForm() override;
75
79 const QgsFeature &feature() const { return mFeature; }
80
87 QgsFeature currentFormFeature() const { return mCurrentFormFeature; }
88
95 void displayWarning( const QString &message );
96
97 // TODO QGIS 5.0 - make private
98
103 void hideButtonBox();
104
105 // TODO QGIS 5.0 - make private
106
111 void showButtonBox();
112
113 // TODO QGIS 5.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
211 static QgsFeature createFeature( QgsVectorLayer *layer, const QgsGeometry &geometry, const QgsAttributeMap &attributes, QgsExpressionContext &context );
212
213 signals:
214
223 Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
224
232 void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
233
242 void rememberLastWidgetValueChanged( const QString &attribute, bool remember );
243
252 void beforeSave( bool &ok ) SIP_SKIP;
253
258
264 void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
265
271
275 void closed();
276
280 void zoomToFeatures( const QString &filter );
281
285 void flashFeatures( const QString &filter );
286
291 void openFilteredFeaturesAttributeTable( const QString &filter );
292
293 public slots:
294
302 void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
303
309 void changeGeometry( const QgsGeometry &geometry );
310
316 void setFeature( const QgsFeature &feature );
317
323 bool save();
324
334 bool saveWithDetails( QString *error SIP_OUT = nullptr );
335
339 void resetValues();
340
344 void resetSearch();
345
349 void refreshFeature();
350
360 void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
361
362 private slots:
363 void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
364 void onAttributeAdded( int idx );
365 void onAttributeDeleted( int idx );
366 void onRelatedFeaturesChanged();
367 void onUpdatedFields();
368 void onConstraintStatusChanged( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
369 void preventFeatureRefresh();
370 void synchronizeState();
371 void layerSelectionChanged();
372
374 bool saveMultiEdits();
375 void resetMultiEdit( bool promptToSave = false );
376 void multiEditMessageClicked( const QString &link );
377
378 void filterAndTriggered();
379 void filterOrTriggered();
380 void filterTriggered();
381
382 void searchZoomTo();
383 void searchFlash();
384 void searchSetSelection();
385 void searchAddToSelection();
386 void searchRemoveFromSelection();
387 void searchIntersectSelection();
388
389 private:
390 void init();
391
392 void cleanPython();
393
394 void initPython();
395
396 void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
397
398 bool fieldIsEditable( int fieldIndex ) const;
399
400 bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
401
402 void updateFieldDependencies();
403 void updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww );
404 void updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww );
405 void updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrapper *eww = nullptr );
406 void updateFieldDependenciesParent( QgsEditorWidgetWrapper *eww );
407
408 void setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids );
409
410 struct WidgetInfo
411 {
412 QWidget *widget = nullptr;
413 bool expandingNeeded = false;
414 QString labelText;
415 QString toolTip;
416 QString hint;
417 bool labelOnTop = false;
418 bool labelAlignRight = false;
419 bool showLabel = true;
421 };
422
423 WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
424
429 void createWrappers();
430 void afterWidgetInit();
431
432 void scanForEqualAttributes( QgsFeatureIterator &fit, QSet<int> &mixedValueFields, QHash<int, QVariant> &fieldSharedValues ) const;
433
435 bool saveEdits( QString *error );
436
437 QgsFeature getUpdatedFeature() const;
438
440 void updateValuesDependencies( const int originIdx );
441 void updateValuesDependenciesDefaultValues( const int originIdx );
442 void updateValuesDependenciesVirtualFields( const int originIdx );
443 void updateValuesDependenciesParent();
444 void updateRelatedLayerFields();
445
446 void clearMultiEditMessages();
447 void pushSelectedFeaturesMessage();
448 void runSearchSelect( Qgis::SelectBehavior behavior );
449
450 QString createFilterExpression() const;
451
452 QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
453
455 void updateAllConstraints();
456 void updateConstraints( QgsEditorWidgetWrapper *w );
457 void updateContainersVisibility();
458 void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
459 void updateLabels();
460 void updateEditableState();
461 bool currentFormValuesFeature( QgsFeature &feature );
462 bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
463 bool currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
464 QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
465
466 Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
467 QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QString &relationWidgetTypeId, const QgsRelation &rel, const QgsAttributeEditorContext &context );
468
469 QgsVectorLayer *mLayer = nullptr;
470 QgsFeature mFeature;
471 QgsFeature mCurrentFormFeature;
472 QgsMessageBar *mMessageBar = nullptr;
473 bool mOwnsMessageBar = true;
474 QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
475 QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
476 QList<QgsWidgetWrapper *> mWidgets;
478 std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
479 QDialogButtonBox *mButtonBox = nullptr;
480 QWidget *mSearchButtonBox = nullptr;
481 QList<QgsAttributeFormInterface *> mInterfaces;
482 QMultiMap<int, QgsAttributeFormEditorWidget *> mFormEditorWidgets;
483 QList<QgsAttributeFormWidget *> mFormWidgets;
484 QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
485 QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
486 QMap<QWidget *, QgsProperty> mEditableDataDefinedProperties;
487 bool mValuesInitialized = false;
488 bool mDirty = false;
489 bool mIsSettingFeature = false;
490
491 bool mValidConstraints = true;
492 QgsMessageBarItem *mConstraintsFailMessageBarItem = nullptr;
493
494 struct ContainerInformation
495 {
496 ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
497 : tabWidget( tabWidget )
498 , widget( widget )
499 , expression( expression )
500 , isVisible( true )
501 {}
502
503 ContainerInformation( QWidget *widget, const QgsExpression &expression )
504 : widget( widget )
505 , expression( expression )
506 , isVisible( true )
507 {}
508
509 ContainerInformation( QWidget *widget, const QgsExpression &visibilityExpression, bool collapsed, const QgsExpression &collapsedExpression )
510 : widget( widget )
511 , expression( visibilityExpression )
512 , isVisible( true )
513 , isCollapsed( collapsed )
514 , collapsedExpression( collapsedExpression )
515 {}
516
517
518 QgsTabWidget *tabWidget = nullptr;
519 QWidget *widget = nullptr;
520 QgsExpression expression;
521 bool isVisible;
522 bool isCollapsed = false;
523 QgsExpression collapsedExpression;
524
525 void apply( QgsExpressionContext *expressionContext );
526 };
527
528 void registerContainerInformation( ContainerInformation *info );
529
530 void updateIcon( QgsEditorWidgetWrapper *eww );
531
532 void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
533
534 // Contains information about tabs and groupboxes, their visibility/collapsed state conditions
535 QVector<ContainerInformation *> mContainerVisibilityCollapsedInformation;
536 QMap<QString, QVector<ContainerInformation *>> mContainerInformationDependency;
537
538 // Variables below are used for Python
539 static int sFormCounter;
540 int mFormNr;
541 QString mPyFormVarName;
542
544 bool mIsSaving = false;
545
547 bool mPreventFeatureRefresh = false;
548
549 bool mIsSettingMultiEditFeatures = false;
550
551 QgsFeatureIds mMultiEditFeatureIds;
552 bool mUnsavedMultiEditChanges = false;
553
554 QString mEditCommandMessage;
555
557
558 QMap<QWidget *, QSvgWidget *> mIconMap;
559
564 QMultiMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
565
570 QMultiMap<int, QgsWidgetWrapper *> mVirtualFieldsDependencies;
571
575 QSet<QgsEditorWidgetWrapper *> mRelatedLayerFieldsDependencies;
576
577 QSet<QgsEditorWidgetWrapper *> mParentDependencies;
578
580 QList<int> mAlreadyUpdatedFields;
581
582 bool mNeedsGeometry = false;
583
584 friend class TestQgsDualView;
587};
588
589#endif // QGSATTRIBUTEFORM_H
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1850
Contains context information for attribute editor widgets.
@ SingleEditMode
Single edit mode, for editing a single feature.
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:60
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:113
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_OUT
Definition qgis_sip.h:57
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.