QGIS API Documentation 4.1.0-Master (376402f9aeb)
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
294 public slots:
295
303 void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
304
310 void changeGeometry( const QgsGeometry &geometry );
311
317 void setFeature( const QgsFeature &feature );
318
324 bool save();
325
335 bool saveWithDetails( QString *error SIP_OUT = nullptr );
336
340 void resetValues();
341
345 void resetSearch();
346
350 void refreshFeature();
351
361 void parentFormValueChanged( const QString &attribute, const QVariant &newValue );
362
363 private slots:
364 void onAttributeChanged( const QVariant &value, const QVariantList &additionalFieldValues );
365 void onAttributeAdded( int idx );
366 void onAttributeDeleted( int idx );
367 void onRelatedFeaturesChanged();
368 void onUpdatedFields();
369 void onConstraintStatusChanged( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
370 void preventFeatureRefresh();
371 void synchronizeState();
372 void layerSelectionChanged();
373
375 bool saveMultiEdits();
376 void resetMultiEdit( bool promptToSave = false );
377 void multiEditMessageClicked( const QString &link );
378
379 void filterAndTriggered();
380 void filterOrTriggered();
381 void filterTriggered();
382
383 void searchZoomTo();
384 void searchFlash();
385 void searchSetSelection();
386 void searchAddToSelection();
387 void searchRemoveFromSelection();
388 void searchIntersectSelection();
389
390 private:
391 void init();
392
393 void cleanPython();
394
395 void initPython();
396
397 void updateJoinedFields( const QgsEditorWidgetWrapper &eww );
398
399 bool fieldIsEditable( int fieldIndex ) const;
400
401 bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
402
403 void updateFieldDependencies();
404 void updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww );
405 void updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww );
406 void updateRelatedLayerFieldsDependencies( QgsEditorWidgetWrapper *eww = nullptr );
407 void updateFieldDependenciesParent( QgsEditorWidgetWrapper *eww );
408
409 void setMultiEditFeatureIdsRelations( const QgsFeatureIds &fids );
410
411 struct WidgetInfo
412 {
413 QWidget *widget = nullptr;
414 bool expandingNeeded = false;
415 QString labelText;
416 QString toolTip;
417 QString hint;
418 bool labelOnTop = false;
419 bool labelAlignRight = false;
420 bool showLabel = true;
422 };
423
424 WidgetInfo createWidgetFromDef( const QgsAttributeEditorElement *widgetDef, QWidget *parent, QgsVectorLayer *vl, QgsAttributeEditorContext &context );
425
430 void createWrappers();
431 void afterWidgetInit();
432
433 void scanForEqualAttributes( QgsFeatureIterator &fit, QSet<int> &mixedValueFields, QHash<int, QVariant> &fieldSharedValues ) const;
434
436 bool saveEdits( QString *error );
437
438 QgsFeature getUpdatedFeature() const;
439
441 void updateValuesDependencies( const int originIdx );
442 void updateValuesDependenciesDefaultValues( const int originIdx );
443 void updateValuesDependenciesVirtualFields( const int originIdx );
444 void updateValuesDependenciesParent();
445 void updateRelatedLayerFields();
446
447 void clearMultiEditMessages();
448 void pushSelectedFeaturesMessage();
449 void runSearchSelect( Qgis::SelectBehavior behavior );
450
451 QString createFilterExpression() const;
452
453 QgsExpressionContext createExpressionContext( const QgsFeature &feature ) const;
454
456 void updateAllConstraints();
457 void updateConstraints( QgsEditorWidgetWrapper *w );
458 void updateContainersVisibility();
459 void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
460 void updateLabels();
461 void updateEditableState();
462 bool currentFormValuesFeature( QgsFeature &feature );
463 bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
464 bool currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
465 QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
466
467 Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
468 QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QString &relationWidgetTypeId, const QgsRelation &rel, const QgsAttributeEditorContext &context );
469
470 QToolButton *createCommentInfoButton( QWidget *labelWidget );
471
472 QgsVectorLayer *mLayer = nullptr;
473 QgsFeature mFeature;
474 QgsFeature mCurrentFormFeature;
475 QgsMessageBar *mMessageBar = nullptr;
476 bool mOwnsMessageBar = true;
477 QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
478 QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
479 QList<QgsWidgetWrapper *> mWidgets;
481 std::unique_ptr<QgsExpressionContextScope> mExtraContextScope;
482 QDialogButtonBox *mButtonBox = nullptr;
483 QWidget *mSearchButtonBox = nullptr;
484 QList<QgsAttributeFormInterface *> mInterfaces;
485 QMultiMap<int, QgsAttributeFormEditorWidget *> mFormEditorWidgets;
486 QList<QgsAttributeFormWidget *> mFormWidgets;
487 QMap<const QgsVectorLayerJoinInfo *, QgsFeature> mJoinedFeatures;
488 QMap<QLabel *, QgsProperty> mLabelDataDefinedProperties;
489 QMap<QWidget *, QgsProperty> mEditableDataDefinedProperties;
490 QMap<QWidget *, QgsProperty> mCustomCommentDataDefinedProperties;
491 bool mValuesInitialized = false;
492 bool mDirty = false;
493 bool mIsSettingFeature = false;
494
495 bool mValidConstraints = true;
496 QgsMessageBarItem *mConstraintsFailMessageBarItem = nullptr;
497
498 struct ContainerInformation
499 {
500 ContainerInformation( QgsTabWidget *tabWidget, QWidget *widget, const QgsExpression &expression )
501 : tabWidget( tabWidget )
502 , widget( widget )
503 , expression( expression )
504 , isVisible( true )
505 {}
506
507 ContainerInformation( QWidget *widget, const QgsExpression &expression )
508 : widget( widget )
509 , expression( expression )
510 , isVisible( true )
511 {}
512
513 ContainerInformation( QWidget *widget, const QgsExpression &visibilityExpression, bool collapsed, const QgsExpression &collapsedExpression )
514 : widget( widget )
515 , expression( visibilityExpression )
516 , isVisible( true )
517 , isCollapsed( collapsed )
518 , collapsedExpression( collapsedExpression )
519 {}
520
521
522 QgsTabWidget *tabWidget = nullptr;
523 QWidget *widget = nullptr;
524 QgsExpression expression;
525 bool isVisible;
526 bool isCollapsed = false;
527 QgsExpression collapsedExpression;
528
529 void apply( QgsExpressionContext *expressionContext );
530 };
531
532 void registerContainerInformation( ContainerInformation *info );
533
534 void updateIcon( QgsEditorWidgetWrapper *eww );
535
536 void reloadIcon( const QString &file, const QString &tooltip, QSvgWidget *sw );
537
538 // Contains information about tabs and groupboxes, their visibility/collapsed state conditions
539 QVector<ContainerInformation *> mContainerVisibilityCollapsedInformation;
540 QMap<QString, QVector<ContainerInformation *>> mContainerInformationDependency;
541
542 // Variables below are used for Python
543 static int sFormCounter;
544 int mFormNr;
545 QString mPyFormVarName;
546
548 bool mIsSaving = false;
549
551 bool mPreventFeatureRefresh = false;
552
553 bool mIsSettingMultiEditFeatures = false;
554
555 QgsFeatureIds mMultiEditFeatureIds;
556 bool mUnsavedMultiEditChanges = false;
557
558 QString mEditCommandMessage;
559
561
562 QMap<QWidget *, QSvgWidget *> mIconMap;
563
568 QMultiMap<int, QgsWidgetWrapper *> mDefaultValueDependencies;
569
574 QMultiMap<int, QgsWidgetWrapper *> mVirtualFieldsDependencies;
575
579 QSet<QgsEditorWidgetWrapper *> mRelatedLayerFieldsDependencies;
580
581 QSet<QgsEditorWidgetWrapper *> mParentDependencies;
582
584 QList<int> mAlreadyUpdatedFields;
585
586 bool mNeedsGeometry = false;
587
588 friend class TestQgsDualView;
591};
592
593#endif // QGSATTRIBUTEFORM_H
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1891
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.