QGIS API Documentation 4.3.0-Master (91d4a333d5d)
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 <QToolButton>
30#include <QWidget>
31
34class QgsMessageBar;
37class QgsTabWidget;
40class QSvgWidget;
41
47class GUI_EXPORT QgsAttributeForm : public QWidget
48{
49 Q_OBJECT
50
51 public:
63
71
72 explicit QgsAttributeForm(
73 QgsVectorLayer *vl, const QgsFeature &feature = QgsFeature(), const QgsAttributeEditorContext &context = QgsAttributeEditorContext(), QWidget *parent SIP_TRANSFERTHIS = nullptr
74 );
75 ~QgsAttributeForm() override;
76
80 const QgsFeature &feature() const { return mFeature; }
81
88 QgsFeature currentFormFeature() const { return mCurrentFormFeature; }
89
96 void displayWarning( const QString &message );
97
98 // TODO QGIS 5.0 - make private
99
104 void hideButtonBox();
105
106 // TODO QGIS 5.0 - make private
107
112 void showButtonBox();
113
114 // TODO QGIS 5.0 - make private
115
120 void disconnectButtonBox();
121
126 void addInterface( QgsAttributeFormInterface *iface SIP_TRANSFER );
127
133 QgsVectorLayer *layer() { return mLayer; }
134
140 bool editable();
141
146 QgsAttributeEditorContext::Mode mode() const { return mMode; }
147
153 void setMode( QgsAttributeEditorContext::Mode mode );
154
160 void setEditCommandMessage( const QString &message ) { mEditCommandMessage = message; }
161
170 bool eventFilter( QObject *object, QEvent *event ) override;
171
176 void setMultiEditFeatureIds( const QgsFeatureIds &fids );
177
183 void setMessageBar( QgsMessageBar *messageBar );
184
191 QString aggregateFilter() const;
192
199 void setExtraContextScope( QgsExpressionContextScope *extraScope SIP_TRANSFER );
200
205 bool needsGeometry() const;
206
212 static QgsFeature createFeature( QgsVectorLayer *layer, const QgsGeometry &geometry, const QgsAttributeMap &attributes, QgsExpressionContext &context );
213
214 signals:
215
224 Q_DECL_DEPRECATED void attributeChanged( const QString &attribute, const QVariant &value ) SIP_DEPRECATED;
225
233 void widgetValueChanged( const QString &attribute, const QVariant &value, bool attributeChanged );
234
243 void rememberLastWidgetValueChanged( const QString &attribute, bool remember );
244
253 void beforeSave( bool &ok ) SIP_SKIP;
254
259
265 void filterExpressionSet( const QString &expression, QgsAttributeForm::FilterType type );
266
272
276 void closed();
277
281 void zoomToFeatures( const QString &filter );
282
286 void flashFeatures( const QString &filter );
287
292 void openFilteredFeaturesAttributeTable( const QString &filter );
293
299
300 public slots:
301
309 void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
310
316 void changeGeometry( const QgsGeometry &geometry );
317
323 void setFeature( const QgsFeature &feature );
324
330 bool save();
331
341 bool saveWithDetails( QString *error SIP_OUT = nullptr );
342
346 void resetValues();
347
351 void resetSearch();
352
356 void refreshFeature();
357
367 void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
368
369 private slots:
370 void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
371 void onAttributeAdded( int idx );
372 void onAttributeDeleted( int idx );
373 void onRelatedFeaturesChanged();
374 void onUpdatedFields();
375 void onConstraintStatusChanged( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
376 void preventFeatureRefresh();
377 void synchronizeState();
378 void layerSelectionChanged();
379
381 bool saveMultiEdits();
382 void resetMultiEdit( bool promptToSave = false );
383 void multiEditMessageClicked( const QString &link );
384
385 void filterAndTriggered();
386 void filterOrTriggered();
387 void filterTriggered();
388
389 void searchZoomTo();
390 void searchFlash();
391 void searchSetSelection();
392 void searchAddToSelection();
393 void searchRemoveFromSelection();
394 void searchIntersectSelection();
395
396 private:
397 void init();
398
399 void cleanPython();
400
401 void initPython();
402
403 void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
404
405 bool fieldIsEditable( int fieldIndex ) const;
406
407 bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
408
409 void updateFieldDependencies();
410 void updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww );
411 void updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww );
412 void updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrapper *eww = nullptr );
413 void updateFieldDependenciesParent( QgsEditorWidgetWrapper *eww );
414
415 void setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids );
416
417 struct WidgetInfo
418 {
419 QWidget *widget = nullptr;
420 bool expandingNeeded = false;
421 QString labelText;
422 QString toolTip;
423 QString hint;
424 bool labelOnTop = false;
425 bool labelAlignRight = false;
426 bool showLabel = true;
428 };
429
430 WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
431
436 void createWrappers();
437 void afterWidgetInit();
438
439 void scanForEqualAttributes( QgsFeatureIterator &fit, QSet<int> &mixedValueFields, QHash<int, QVariant> &fieldSharedValues ) const;
440
442 bool saveEdits( QString *error );
443
444 QgsFeature getUpdatedFeature() const;
445
447 void updateValuesDependencies( const int originIdx );
448 void updateValuesDependenciesDefaultValues( const int originIdx );
449 void updateValuesDependenciesVirtualFields( const int originIdx );
450 void updateValuesDependenciesParent();
451 void updateRelatedLayerFields();
452
453 void clearMultiEditMessages();
454 void pushSelectedFeaturesMessage();
455 void runSearchSelect( Qgis::SelectBehavior behavior );
456
457 QString createFilterExpression() const;
458
459 QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
460
462 void updateAllConstraints();
463 void updateConstraints( QgsEditorWidgetWrapper *w );
464 void updateContainersVisibility();
465 void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
466 void updateLabels();
467 void updateEditableState();
468 bool currentFormValuesFeature( QgsFeature &feature );
469 bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
470 bool currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
471 QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
472
473 Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
474 QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QString &relationWidgetTypeId, const QgsRelation &rel, const QgsAttributeEditorContext &context );
475
476 QToolButton *createCommentInfoButton( QWidget *labelWidget );
477
478 QgsVectorLayer *mLayer = nullptr;
479 QgsFeature mFeature;
480 QgsFeature mCurrentFormFeature;
481 QgsMessageBar *mMessageBar = nullptr;
482 bool mOwnsMessageBar = true;
483 QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
484 QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
485 QList<QgsWidgetWrapper *> mWidgets;
487 std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
488 QDialogButtonBox *mButtonBox = nullptr;
489 QWidget *mSearchButtonBox = nullptr;
490 QList<QgsAttributeFormInterface *> mInterfaces;
491 QMultiMap<int, QgsAttributeFormEditorWidget *> mFormEditorWidgets;
492 QList<QgsAttributeFormWidget *> mFormWidgets;
493 QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
494 QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
495 QMap<QWidget *, QgsProperty> mEditableDataDefinedProperties;
496 QMap<QWidget *, QgsProperty> mCustomCommentDataDefinedProperties;
497 bool mValuesInitialized = false;
498 bool mDirty = false;
499 bool mIsSettingFeature = false;
500
501 bool mValidConstraints = true;
502 QgsMessageBarItem *mConstraintsFailMessageBarItem = nullptr;
503
504 struct ContainerInformation
505 {
506 ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
507 : tabWidget( tabWidget )
508 , widget( widget )
509 , expression( expression )
510 , isVisible( true )
511 {}
512
513 ContainerInformation( QWidget *widget, const QgsExpression &expression )
514 : widget( widget )
515 , expression( expression )
516 , isVisible( true )
517 {}
518
519 ContainerInformation( QWidget *widget, const QgsExpression &visibilityExpression, bool collapsed, const QgsExpression &collapsedExpression )
520 : widget( widget )
521 , expression( visibilityExpression )
522 , isVisible( true )
523 , isCollapsed( collapsed )
524 , collapsedExpression( collapsedExpression )
525 {}
526
527
528 QgsTabWidget *tabWidget = nullptr;
529 QWidget *widget = nullptr;
530 QgsExpression expression;
531 bool isVisible;
532 bool isCollapsed = false;
533 QgsExpression collapsedExpression;
534
535 void apply( QgsExpressionContext *expressionContext );
536 };
537
538 void registerContainerInformation( ContainerInformation *info );
539
540 void updateIcon( QgsEditorWidgetWrapper *eww );
541
542 void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
543
544 // Contains information about tabs and groupboxes, their visibility/collapsed state conditions
545 QVector<ContainerInformation *> mContainerVisibilityCollapsedInformation;
546 QMap<QString, QVector<ContainerInformation *>> mContainerInformationDependency;
547
548 // Variables below are used for Python
549 static int sFormCounter;
550 int mFormNr;
551 QString mPyFormVarName;
552
554 bool mIsSaving = false;
555
557 bool mPreventFeatureRefresh = false;
558
559 bool mIsSettingMultiEditFeatures = false;
560
561 QgsFeatureIds mMultiEditFeatureIds;
562 bool mUnsavedMultiEditChanges = false;
563
564 QString mEditCommandMessage;
565
567
568 QMap<QWidget *, QSvgWidget *> mIconMap;
569
574 QMultiMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
575
580 QMultiMap<int, QgsWidgetWrapper *> mVirtualFieldsDependencies;
581
585 QSet<QgsEditorWidgetWrapper *> mRelatedLayerFieldsDependencies;
586
587 QSet<QgsEditorWidgetWrapper *> mParentDependencies;
588
590 QList<int> mAlreadyUpdatedFields;
591
592 bool mNeedsGeometry = false;
593
594 friend class TestQgsDualView;
598};
599
600#endif // QGSATTRIBUTEFORM_H
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1941
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 editableChanged()
Emitted when the editable state of the attribute form has changed.
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)
friend class TestQgsQmlWidgetWrapper
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:138
#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.