QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
19#include "qgsapplication.h"
21#include "qgsattributeform.h"
24#include "qgsgui.h"
27#include "qgsvectorlayerutils.h"
28
29#include <QLabel>
30#include <QLayout>
31#include <QStackedWidget>
32
33#include "moc_qgsattributeformeditorwidget.cpp"
34
37 , mWidgetType( widgetType )
38 , mEditorWidget( editorWidget )
39 , mForm( form )
40 , mMultiEditButton( new QgsMultiEditToolButton() )
41{
42 mRememberLastValueButton = new QToolButton();
43 mRememberLastValueButton->setAutoRaise( true );
44 mRememberLastValueButton->setCheckable( true );
45 mRememberLastValueButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconRememberDisabled.svg" ) ) );
46 mRememberLastValueButton->setToolTip( tr( "When enabled, the entered value will be remembered and reused for the next feature additions" ) );
47 updateRememberWidget();
48
49 connect( mRememberLastValueButton, &QAbstractButton::toggled, this, [this]( bool checked ) {
50 mRememberLastValueButton->setIcon( QgsApplication::getThemeIcon( checked ? QStringLiteral( "/mIconRememberEnabled.svg" ) : QStringLiteral( "/mIconRememberDisabled.svg" ) ) );
51 emit rememberLastValueChanged( mEditorWidget->fieldIdx(), checked );
52 } );
53 connect( mForm, &QgsAttributeForm::modeChanged, this, [this]( QgsAttributeEditorContext::Mode ) {
54 updateRememberWidget();
55 } );
56
57 mConstraintResultLabel = new QLabel();
58 mConstraintResultLabel->setObjectName( QStringLiteral( "ConstraintStatus" ) );
59 mConstraintResultLabel->setSizePolicy( QSizePolicy::Fixed, mConstraintResultLabel->sizePolicy().verticalPolicy() );
60 mConstraintResultLabel->setAlignment( Qt::AlignCenter );
61 mConstraintResultLabel->setFixedWidth( 24 );
62
63 mAggregateButton = new QgsAggregateToolButton();
64 mAggregateButton->setType( mEditorWidget->field().type() );
65 connect( mAggregateButton, &QgsAggregateToolButton::aggregateChanged, this, &QgsAttributeFormEditorWidget::onAggregateChanged );
66
67 if ( mEditorWidget->widget() )
68 {
69 mEditorWidget->widget()->setObjectName( mEditorWidget->field().name() );
70 }
71
72 connect( mEditorWidget, &QgsEditorWidgetWrapper::valuesChanged, this, &QgsAttributeFormEditorWidget::editorWidgetValuesChanged );
73
74 mMultiEditButton->setField( mEditorWidget->field() );
75 connect( mMultiEditButton, &QgsMultiEditToolButton::resetFieldValueTriggered, this, &QgsAttributeFormEditorWidget::resetValue );
76 connect( mMultiEditButton, &QgsMultiEditToolButton::setFieldValueTriggered, this, &QgsAttributeFormEditorWidget::setFieldTriggered );
77
78 updateWidgets();
79}
80
82{
83 //there's a chance these widgets are not currently added to the layout, so have no parent set
84 delete mMultiEditButton;
85 delete mRememberLastValueButton;
86 delete mConstraintResultLabel;
87}
88
90{
91 Q_ASSERT( !mWidgetType.isEmpty() );
92 const QVariantMap config = mEditorWidget->config();
93 const int fieldIdx = mEditorWidget->fieldIdx();
94
95 QgsSearchWidgetWrapper *sww = QgsGui::editorWidgetRegistry()->createSearchWidget( mWidgetType, layer(), fieldIdx, config, searchWidgetFrame(), context );
97 searchWidgetFrame()->layout()->addWidget( mAggregateButton );
99 {
100 // create secondary widget for between type searches
101 QgsSearchWidgetWrapper *sww2 = QgsGui::editorWidgetRegistry()->createSearchWidget( mWidgetType, layer(), fieldIdx, config, searchWidgetFrame(), context );
103 }
104}
105
106void QgsAttributeFormEditorWidget::setConstraintStatus( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result )
107{
108 switch ( result )
109 {
111 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#FF9800\">%1</font>" ).arg( QChar( 0x2718 ) ) );
112 mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
113 break;
114
116 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#FFC107\">%1</font>" ).arg( QChar( 0x2718 ) ) );
117 mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
118 break;
119
121 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#259B24\">%1</font>" ).arg( QChar( 0x2714 ) ) );
122 mConstraintResultLabel->setToolTip( description );
123 break;
124 }
125}
126
128{
129 mIsConstraintResultVisible = editable;
130
131 switch ( mode() )
132 {
133 case SearchMode:
135 return;
136 case DefaultMode:
137 case MultiEditMode:
138 break;
139 }
140
141 if ( !layer() || QgsVectorLayerUtils::attributeHasConstraints( layer(), mEditorWidget->fieldIdx() ) )
142 {
143 const bool hasConstraintResultLabel = ( editPage()->layout()->indexOf( mConstraintResultLabel ) >= 0 );
144 if ( editable && !hasConstraintResultLabel )
145 {
146 editPage()->layout()->addWidget( mConstraintResultLabel );
147 }
148 else if ( !editable && hasConstraintResultLabel )
149 {
150 editPage()->layout()->removeWidget( mConstraintResultLabel );
151 mConstraintResultLabel->setParent( nullptr );
152 }
153 }
154}
155
157{
158 return mEditorWidget;
159}
160
162{
163 if ( mEditorWidget && mixed )
164 mEditorWidget->showIndeterminateState();
165 mMultiEditButton->setIsMixed( mixed );
166 mIsMixed = mixed;
167}
168
170{
171 mRememberLastValueButton->setChecked( remember );
172 mRememberLastValueButton->setIcon( QgsApplication::getThemeIcon( remember ? QStringLiteral( "/mIconRememberEnabled.svg" ) : QStringLiteral( "/mIconRememberDisabled.svg" ) ) );
173}
174
176{
177 if ( mEditorWidget )
178 {
179 mPreviousValue = mEditorWidget->value();
180 mPreviousAdditionalValues = mEditorWidget->additionalFieldValues();
181 }
182
183 setIsMixed( false );
184 mMultiEditButton->changesCommitted();
185 mIsChanged = false;
186}
187
188
189void QgsAttributeFormEditorWidget::initialize( const QVariant &initialValue, bool mixedValues, const QVariantList &additionalFieldValues )
190{
191 if ( mEditorWidget )
192 {
193 mBlockValueUpdate = true;
194 mEditorWidget->setValues( initialValue, additionalFieldValues );
195 mBlockValueUpdate = false;
196 }
197 mPreviousValue = initialValue;
198 mPreviousAdditionalValues = additionalFieldValues;
199 setIsMixed( mixedValues );
200 mMultiEditButton->setIsChanged( false );
201 mIsChanged = false;
202 updateWidgets();
203}
204
206{
207 return mEditorWidget->value();
208}
209
210
211void QgsAttributeFormEditorWidget::editorWidgetValuesChanged( const QVariant &value, const QVariantList &additionalFieldValues )
212{
213 if ( mBlockValueUpdate )
214 return;
215
216 mIsChanged = true;
217
218 switch ( mode() )
219 {
220 case DefaultMode:
221 case SearchMode:
223 break;
224 case MultiEditMode:
225 mMultiEditButton->setIsChanged( true );
226 }
227
229 emit valueChanged( value );
231 emit valuesChanged( value, additionalFieldValues );
232}
233
234void QgsAttributeFormEditorWidget::resetValue()
235{
236 mIsChanged = false;
237 mBlockValueUpdate = true;
238 if ( mEditorWidget )
239 mEditorWidget->setValues( mPreviousValue, mPreviousAdditionalValues );
240 mBlockValueUpdate = false;
241
242 switch ( mode() )
243 {
244 case DefaultMode:
245 case SearchMode:
247 break;
248 case MultiEditMode:
249 {
250 mMultiEditButton->setIsChanged( false );
251 if ( mEditorWidget && mIsMixed )
252 mEditorWidget->showIndeterminateState();
253 break;
254 }
255 }
256}
257
258void QgsAttributeFormEditorWidget::setFieldTriggered()
259{
260 mIsChanged = true;
261}
262
263void QgsAttributeFormEditorWidget::onAggregateChanged()
264{
265 const auto constWigets( searchWidgetWrappers() );
266 for ( QgsSearchWidgetWrapper *searchWidget : constWigets )
267 searchWidget->setAggregate( mAggregateButton->aggregate() );
268}
269
270void QgsAttributeFormEditorWidget::updateRememberWidget()
271{
272 const bool hasRememberButton = ( editPage()->layout()->indexOf( mRememberLastValueButton ) >= 0 );
273 const int idx = mEditorWidget->fieldIdx();
274 if ( !hasRememberButton && form() && form()->mode() == QgsAttributeEditorContext::AddFeatureMode )
275 {
276 if ( layer() && layer()->editFormConfig().reuseLastValuePolicy( idx ) != Qgis::AttributeFormReuseLastValuePolicy::NotAllowed )
277 {
278 editPage()->layout()->addWidget( mRememberLastValueButton );
279 }
280 }
281 else if ( hasRememberButton )
282 {
283 editPage()->layout()->removeWidget( mRememberLastValueButton );
284 mRememberLastValueButton->setParent( nullptr );
285 }
286}
287
288void QgsAttributeFormEditorWidget::updateWidgets()
289{
290 //first update the tool buttons
291 const bool hasMultiEditButton = ( editPage()->layout()->indexOf( mMultiEditButton ) >= 0 );
292
293 bool shouldShowMultiEditButton = false;
294 switch ( mode() )
295 {
299 // in these modes we don't show the multi edit button
300 shouldShowMultiEditButton = false;
301 break;
302
304 {
305 // in multi-edit mode we need to know upfront whether or not to allow add the multiedit buttons
306 // for this field.
307 // if the field is always read only regardless of the feature, no need to dig further. But otherwise
308 // we may need to test editability for the actual selected features...
309 if ( mEditorWidget )
310 {
311 const int fieldIndex = mEditorWidget->fieldIdx();
312 shouldShowMultiEditButton = !QgsVectorLayerUtils::fieldIsReadOnly( layer(), fieldIndex );
313 if ( shouldShowMultiEditButton )
314 {
315 // depending on the field type, the editability of the field may vary feature by feature (e.g. for joined
316 // fields coming from joins without the upsert on edit capabilities).
317 // But this feature-by-feature check is EXPENSIVE!!! (see https://github.com/qgis/QGIS/issues/41366), so
318 // avoid it whenever we can...
319 const bool fieldEditabilityDependsOnFeature = QgsVectorLayerUtils::fieldEditabilityDependsOnFeature( layer(), fieldIndex );
320 if ( fieldEditabilityDependsOnFeature )
321 {
322 QgsFeature feature;
323 QgsFeatureIterator it = layer()->getSelectedFeatures();
324 while ( it.nextFeature( feature ) )
325 {
326 const bool isEditable = QgsVectorLayerUtils::fieldIsEditable( layer(), fieldIndex, feature );
327 if ( !isEditable )
328 {
329 // as soon as we find one read-only feature for the field, we can break early...
330 shouldShowMultiEditButton = false;
331 break;
332 }
333 }
334 }
335 }
336 }
337 }
338 break;
339 }
340
341 if ( hasMultiEditButton && !shouldShowMultiEditButton )
342 {
343 editPage()->layout()->removeWidget( mMultiEditButton );
344 mMultiEditButton->setParent( nullptr );
345 }
346 else if ( !hasMultiEditButton && shouldShowMultiEditButton )
347 {
348 editPage()->layout()->addWidget( mMultiEditButton );
349 }
350
352
353 switch ( mode() )
354 {
355 case DefaultMode:
356 case MultiEditMode:
357 {
358 if ( mIsConstraintResultVisible && editPage()->layout()->indexOf( mConstraintResultLabel ) == -1 )
359 {
360 if ( !layer() || ( mEditorWidget && QgsVectorLayerUtils::attributeHasConstraints( layer(), mEditorWidget->fieldIdx() ) ) )
361 {
362 editPage()->layout()->addWidget( mConstraintResultLabel );
363 }
364 }
365 break;
366 }
367
369 {
370 mAggregateButton->setVisible( true );
371 break;
372 }
373
374 case SearchMode:
375 {
376 mAggregateButton->setVisible( false );
377 break;
378 }
379 }
380}
@ NotAllowed
Reuse of last values not allowed.
Definition qgis.h:5530
Offers a toolbutton to choose between different aggregate functions.
void aggregateChanged()
The function name of the selected aggregate has changed.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
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;.
void setRememberLastValue(bool remember)
Sets whether the widget value will be remembered for potential reuse when creating new features.
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.
void rememberLastValueChanged(int index, bool remember)
Emitted when the widget's remember last value toggle changes.
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.
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.
QgsAttributeForm * form() const
The form on which this widget is shown.
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.
QgsAttributeFormWidget(QgsWidgetWrapper *widget, QgsAttributeForm *form)
A new form widget for the wrapper widget on form.
The attribute form widget for vector layer features.
void modeChanged(QgsAttributeEditorContext::Mode mode)
Emitted when the form changes mode.
QgsSearchWidgetWrapper * createSearchWidget(const QString &widgetId, QgsVectorLayer *vl, int fieldIdx, const QVariantMap &config, QWidget *parent, const QgsAttributeEditorContext &context=QgsAttributeEditorContext())
Manages an editor widget.
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.
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.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories.
Definition qgsgui.cpp:106
A tool button for controlling how edits to multiple features are applied.
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.
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 attributeHasConstraints(const QgsVectorLayer *layer, int attributeIndex)
Returns true if a feature attribute has active constraints.
static bool fieldIsReadOnly(const QgsVectorLayer *layer, int fieldIndex)
Returns true if the field at index fieldIndex from layer is editable, false if the field is read only...
QgsFeatureIterator getSelectedFeatures(QgsFeatureRequest request=QgsFeatureRequest()) const
Returns an iterator of the selected features.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7170
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169