QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgsattributeformeditorwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributeformeditorwidget.cpp
3 -------------------------------
4 Date : March 2016
5 Copyright : (C) 2016 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
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
17#include "qgsattributeform.h"
24#include "qgsgui.h"
25#include "qgsvectorlayerutils.h"
26
27#include <QLayout>
28#include <QLabel>
29#include <QStackedWidget>
30
32 : QgsAttributeFormWidget( editorWidget, form )
33 , mWidgetType( widgetType )
34 , mEditorWidget( editorWidget )
35 , mForm( form )
36 , mMultiEditButton( new QgsMultiEditToolButton() )
37 , mBlockValueUpdate( false )
38 , mIsMixed( false )
39 , mIsChanged( false )
40{
41 mConstraintResultLabel = new QLabel( this );
42 mConstraintResultLabel->setObjectName( QStringLiteral( "ConstraintStatus" ) );
43 mConstraintResultLabel->setSizePolicy( QSizePolicy::Fixed, mConstraintResultLabel->sizePolicy().verticalPolicy() );
44
45 mMultiEditButton->setField( mEditorWidget->field() );
46 mAggregateButton = new QgsAggregateToolButton();
47 mAggregateButton->setType( mEditorWidget->field().type() );
48 connect( mAggregateButton, &QgsAggregateToolButton::aggregateChanged, this, &QgsAttributeFormEditorWidget::onAggregateChanged );
49
50 if ( mEditorWidget->widget() )
51 {
52 mEditorWidget->widget()->setObjectName( mEditorWidget->field().name() );
53 }
54
55 connect( mEditorWidget, &QgsEditorWidgetWrapper::valuesChanged, this, &QgsAttributeFormEditorWidget::editorWidgetValuesChanged );
56
57 connect( mMultiEditButton, &QgsMultiEditToolButton::resetFieldValueTriggered, this, &QgsAttributeFormEditorWidget::resetValue );
58 connect( mMultiEditButton, &QgsMultiEditToolButton::setFieldValueTriggered, this, &QgsAttributeFormEditorWidget::setFieldTriggered );
59
60 mMultiEditButton->setField( mEditorWidget->field() );
61
62 updateWidgets();
63}
64
66{
67 //there's a chance these widgets are not currently added to the layout, so have no parent set
68 delete mMultiEditButton;
69}
70
72{
73 Q_ASSERT( !mWidgetType.isEmpty() );
74 const QVariantMap config = mEditorWidget->config();
75 const int fieldIdx = mEditorWidget->fieldIdx();
76
77 QgsSearchWidgetWrapper *sww = QgsGui::editorWidgetRegistry()->createSearchWidget( mWidgetType, layer(), fieldIdx, config,
78 searchWidgetFrame(), context );
80 searchWidgetFrame()->layout()->addWidget( mAggregateButton );
83 {
84 // create secondary widget for between type searches
85 QgsSearchWidgetWrapper *sww2 = QgsGui::editorWidgetRegistry()->createSearchWidget( mWidgetType, layer(), fieldIdx, config,
86 searchWidgetFrame(), context );
88 }
89}
90
91void QgsAttributeFormEditorWidget::setConstraintStatus( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result )
92{
93 switch ( result )
94 {
96 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#FF9800\">%1</font>" ).arg( QChar( 0x2718 ) ) );
97 mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
98 break;
99
101 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#FFC107\">%1</font>" ).arg( QChar( 0x2718 ) ) );
102 mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
103 break;
104
106 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#259B24\">%1</font>" ).arg( QChar( 0x2714 ) ) );
107 mConstraintResultLabel->setToolTip( description );
108 break;
109 }
110}
111
113{
114 mConstraintResultLabel->setHidden( !editable );
115}
116
118{
119 return mEditorWidget;
120}
121
123{
124 if ( mEditorWidget && mixed )
125 mEditorWidget->showIndeterminateState();
126 mMultiEditButton->setIsMixed( mixed );
127 mIsMixed = mixed;
128}
129
131{
132 if ( mEditorWidget )
133 {
134 mPreviousValue = mEditorWidget->value();
135 mPreviousAdditionalValues = mEditorWidget->additionalFieldValues();
136 }
137
138 setIsMixed( false );
139 mMultiEditButton->changesCommitted();
140 mIsChanged = false;
141}
142
143
144
145void QgsAttributeFormEditorWidget::initialize( const QVariant &initialValue, bool mixedValues, const QVariantList &additionalFieldValues )
146{
147 if ( mEditorWidget )
148 {
149 mBlockValueUpdate = true;
150 mEditorWidget->setValues( initialValue, additionalFieldValues );
151 mBlockValueUpdate = false;
152 }
153 mPreviousValue = initialValue;
154 mPreviousAdditionalValues = additionalFieldValues;
155 setIsMixed( mixedValues );
156 mMultiEditButton->setIsChanged( false );
157 mIsChanged = false;
158 updateWidgets();
159}
160
162{
163 return mEditorWidget->value();
164}
165
166
167
168void QgsAttributeFormEditorWidget::editorWidgetValuesChanged( const QVariant &value, const QVariantList &additionalFieldValues )
169{
170 if ( mBlockValueUpdate )
171 return;
172
173 mIsChanged = true;
174
175 switch ( mode() )
176 {
177 case DefaultMode:
178 case SearchMode:
180 break;
181 case MultiEditMode:
182 mMultiEditButton->setIsChanged( true );
183 }
184
186 emit valueChanged( value );
188 emit valuesChanged( value, additionalFieldValues );
189}
190
191void QgsAttributeFormEditorWidget::resetValue()
192{
193 mIsChanged = false;
194 mBlockValueUpdate = true;
195 if ( mEditorWidget )
196 mEditorWidget->setValues( mPreviousValue, mPreviousAdditionalValues );
197 mBlockValueUpdate = false;
198
199 switch ( mode() )
200 {
201 case DefaultMode:
202 case SearchMode:
204 break;
205 case MultiEditMode:
206 {
207 mMultiEditButton->setIsChanged( false );
208 if ( mEditorWidget && mIsMixed )
209 mEditorWidget->showIndeterminateState();
210 break;
211 }
212 }
213}
214
215void QgsAttributeFormEditorWidget::setFieldTriggered()
216{
217 mIsChanged = true;
218}
219
220void QgsAttributeFormEditorWidget::onAggregateChanged()
221{
222 const auto constWigets( searchWidgetWrappers() );
223 for ( QgsSearchWidgetWrapper *searchWidget : constWigets )
224 searchWidget->setAggregate( mAggregateButton->aggregate() );
225}
226
227void QgsAttributeFormEditorWidget::updateWidgets()
228{
229 //first update the tool buttons
230 const bool hasMultiEditButton = ( editPage()->layout()->indexOf( mMultiEditButton ) >= 0 );
231
232 bool shouldShowMultiEditButton = false;
233 switch ( mode() )
234 {
238 // in these modes we don't show the multi edit button
239 shouldShowMultiEditButton = false;
240 break;
241
243 {
244 // in multi-edit mode we need to know upfront whether or not to allow add the multiedit buttons
245 // for this field.
246 // if the field is always read only regardless of the feature, no need to dig further. But otherwise
247 // we may need to test editability for the actual selected features...
248 const int fieldIndex = mEditorWidget->fieldIdx();
249 shouldShowMultiEditButton = !QgsVectorLayerUtils::fieldIsReadOnly( layer(), fieldIndex );
250 if ( shouldShowMultiEditButton )
251 {
252 // depending on the field type, the editability of the field may vary feature by feature (e.g. for joined
253 // fields coming from joins without the upsert on edit capabilities).
254 // But this feature-by-feature check is EXPENSIVE!!! (see https://github.com/qgis/QGIS/issues/41366), so
255 // avoid it whenever we can...
256 const bool fieldEditabilityDependsOnFeature = QgsVectorLayerUtils::fieldEditabilityDependsOnFeature( layer(), fieldIndex );
257 if ( fieldEditabilityDependsOnFeature )
258 {
259 QgsFeature feature;
261 while ( it.nextFeature( feature ) )
262 {
263 const bool isEditable = QgsVectorLayerUtils::fieldIsEditable( layer(), fieldIndex, feature );
264 if ( !isEditable )
265 {
266 // as soon as we find one read-only feature for the field, we can break early...
267 shouldShowMultiEditButton = false;
268 break;
269 }
270 }
271 }
272 }
273 }
274 break;
275 }
276
277 if ( hasMultiEditButton && !shouldShowMultiEditButton )
278 {
279 editPage()->layout()->removeWidget( mMultiEditButton );
280 mMultiEditButton->setParent( nullptr );
281 }
282 else if ( !hasMultiEditButton && shouldShowMultiEditButton )
283 {
284 editPage()->layout()->addWidget( mMultiEditButton );
285 }
286
288
289 switch ( mode() )
290 {
291 case DefaultMode:
292 case MultiEditMode:
293 {
294 editPage()->layout()->addWidget( mConstraintResultLabel );
295 break;
296 }
297
299 {
300 mAggregateButton->setVisible( true );
301 break;
302 }
303
304 case SearchMode:
305 {
306 mAggregateButton->setVisible( false );
307 break;
308 }
309 }
310}
Offers a toolbutton to choose between different aggregate functions.
void setType(QMetaType::Type type)
Based on the type of underlying data, some aggregates will be available or not.
void aggregateChanged()
The function name of the selected aggregate has changed.
This class contains context information for attribute editor widgets.
QgsAttributeFormEditorWidget(QgsEditorWidgetWrapper *editorWidget, const QString &widgetType, QgsAttributeForm *form)
Constructor for QgsAttributeFormEditorWidget.
void initialize(const QVariant &initialValue, bool mixedValues=false, const QVariantList &additionalFieldValues=QVariantList())
Resets the widget to an initial value.
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues)
Emitted when the widget's value changes.
void changesCommitted()
Called when field values have been committed;.
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emitted when the widget's value changes.
QVariant currentValue() const
Returns the current value of the attached editor widget.
QgsEditorWidgetWrapper * editorWidget() const
Returns the editor widget wrapper.
void setConstraintStatus(const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result)
Set the constraint status for this widget.
void setIsMixed(bool mixed)
Sets whether the widget should be displayed in a "mixed values" mode.
void createSearchWidgetWrappers(const QgsAttributeEditorContext &context=QgsAttributeEditorContext()) override
Creates the search widget wrappers for the widget used when the form is in search mode.
void setConstraintResultVisible(bool editable)
Set the constraint result label visible or invisible according to the layer editable status.
Base class for all widgets shown on a QgsAttributeForm.
void setSearchWidgetWrapper(QgsSearchWidgetWrapper *wrapper)
Sets the search widget wrapper for the widget used when the form is in search mode.
QList< QgsSearchWidgetWrapper * > searchWidgetWrappers()
Returns the search widget wrapper used in this widget.
void setVisiblePageForMode(QgsAttributeFormWidget::Mode mode)
Sets the visible page in the widget to the page matching the specified mode.
void addAdditionalSearchWidgetWrapper(QgsSearchWidgetWrapper *wrapper)
Adds an additional search widget wrapper.
QWidget * searchWidgetFrame()
Returns the widget which should be used as a parent during construction of the search widget wrapper.
QWidget * editPage() const
Returns a pointer to the EDIT page widget.
QgsVectorLayer * layer()
The layer for which this widget and its form is shown.
Mode mode() const
Returns the current mode for the widget.
@ SearchMode
Layer search/filter mode.
@ MultiEditMode
Multi edit mode, both the editor widget and a QgsMultiEditToolButton is shown.
@ DefaultMode
Default mode, only the editor widget is shown.
@ AggregateSearchMode
Embedded in a search form, show additional aggregate function toolbutton.
QgsSearchWidgetWrapper * createSearchWidget(const QString &widgetId, QgsVectorLayer *vl, int fieldIdx, const QVariantMap &config, QWidget *parent, const QgsAttributeEditorContext &context=QgsAttributeEditorContext())
Manages an editor widget Widget and wrapper share the same parent.
virtual QVariant value() const =0
Will be used to access the widget's value.
virtual QVariantList additionalFieldValues() const
Will be used to access the widget's values for potential additional fields handled by the widget.
int fieldIdx() const
Access the field index.
virtual void showIndeterminateState()
Sets the widget to display in an indeterminate "mixed value" state.
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues=QVariantList())
Emit this signal, whenever the value changed.
void setValues(const QVariant &value, const QVariantList &additionalValues)
Is called when the value of the widget or additional field values needs to be changed.
QgsField field() const
Access the field.
ConstraintResult
Result of constraint checks.
@ ConstraintResultFailSoft
Widget failed at least one soft (non-enforced) constraint.
@ ConstraintResultPass
Widget passed constraints successfully.
@ ConstraintResultFailHard
Widget failed at least one hard (enforced) constraint.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QMetaType::Type type
Definition qgsfield.h:60
QString name
Definition qgsfield.h:62
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories.
Definition qgsgui.cpp:93
A tool button widget which is displayed next to editor widgets in attribute forms,...
void setIsMixed(bool mixed)
Sets whether the associated field contains mixed values.
void changesCommitted()
Called when field values have been changed and field now contains all the same values.
void setIsChanged(bool changed)
Sets whether the associated field has changed.
void resetFieldValueTriggered()
Emitted when the "reset to original values" option is selected.
void setFieldValueTriggered()
Emitted when the "set field value for all features" option is selected.
void setField(const QgsField &field)
Sets the field associated with this button.
Shows a search widget on a filter form.
@ IsNotBetween
Supports searching for values outside of a set range.
@ Between
Supports searches between two values.
virtual FilterFlags supportedFlags() const
Returns filter flags supported by the search widget.
static bool fieldEditabilityDependsOnFeature(const QgsVectorLayer *layer, int fieldIndex)
Returns true if the editability of the field at index fieldIndex from layer may vary feature by featu...
static bool fieldIsEditable(const QgsVectorLayer *layer, int fieldIndex, const QgsFeature &feature)
Tests whether a field is editable for a particular feature.
static bool fieldIsReadOnly(const QgsVectorLayer *layer, int fieldIndex)
QgsFeatureIterator getSelectedFeatures(QgsFeatureRequest request=QgsFeatureRequest()) const
Returns an iterator of the selected features.
QWidget * widget()
Access the widget managed by this wrapper.
QVariant config(const QString &key, const QVariant &defaultVal=QVariant()) const
Use this inside your overridden classes to access the configuration.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6494
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6493