QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
qgsattributesformtreeviewindicatorprovider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributesformtreeviewindicatorprovider.cpp
3 ---------------------
4 begin : June 2025
5 copyright : (C) 2025 by Germán Carrillo
6 email : german 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
17
18#include "qgsapplication.h"
19#include "qgsfieldconstraints.h"
20
21#include "moc_qgsattributesformtreeviewindicatorprovider.cpp"
22
28
30{
31 // recursively populate indicators
32 for ( int i = indexFrom; i <= indexTo; ++i )
33 {
34 QgsAttributesFormItem *childItem = item->child( i );
35
36 if ( QgsAttributesFormItem::isGroup( childItem ) )
37 {
38 if ( childItem->childCount() > 0 )
39 {
40 onAddedChildren( childItem, 0, childItem->childCount() - 1 );
41 }
42 }
43 else
44 {
45 updateItemIndicator( childItem );
46 }
47 }
48}
49
50std::unique_ptr<QgsAttributesFormTreeViewIndicator> QgsAttributesFormTreeViewIndicatorProvider::newIndicator( QgsAttributesFormItem *item )
51{
52 auto indicator = std::make_unique<QgsAttributesFormTreeViewIndicator>( this );
53 indicator->setIcon( QgsApplication::getThemeIcon( iconName( item ) ) );
54 indicator->setToolTip( tooltipText( item ) );
55 mIndicators.insert( indicator.get() );
56
57 return indicator;
58}
59
61{
62 if ( acceptsItem( item ) )
63 {
64 const QList<QgsAttributesFormTreeViewIndicator *> itemIndicators = mAttributesFormTreeView->indicators( item );
65
66 // maybe the indicator exists already
67 for ( QgsAttributesFormTreeViewIndicator *indicator : itemIndicators )
68 {
69 if ( mIndicators.contains( indicator ) )
70 {
71 indicator->setToolTip( tooltipText( item ) );
72 indicator->setIcon( QgsApplication::getThemeIcon( iconName( item ) ) );
73 return;
74 }
75 }
76
77 // it does not exist: need to create a new one
78 mAttributesFormTreeView->addIndicator( item, newIndicator( item ).release() );
79 }
80 else
81 {
82 removeItemIndicator( item );
83 }
84}
85
86void QgsAttributesFormTreeViewIndicatorProvider::removeItemIndicator( QgsAttributesFormItem *item )
87{
88 const QList<QgsAttributesFormTreeViewIndicator *> itemIndicators = mAttributesFormTreeView->indicators( item );
89
90 // Get rid of the existing indicator
91 for ( QgsAttributesFormTreeViewIndicator *indicator : itemIndicators )
92 {
93 if ( mIndicators.contains( indicator ) )
94 {
95 mIndicators.remove( indicator );
96 mAttributesFormTreeView->removeIndicator( item, indicator );
97 indicator->deleteLater();
98 return;
99 }
100 }
101}
102
107
109{
110 QgsAttributesFormItem *item = mAttributesFormTreeView->sourceModel()->rootItem();
111
112 if ( enabled )
113 {
114 if ( mEnabled )
115 {
116 return; // Already done
117 }
118
119 // Draw indicators for all existing items
120 if ( item->childCount() > 0 )
121 {
122 onAddedChildren( item, 0, item->childCount() - 1 );
123 }
124
125 // Connect
127 mEnabled = true;
128 }
129 else
130 {
131 if ( !mEnabled )
132 {
133 return; // Already done
134 }
135
136 // Disconnect
138
139 // Get rid of all item indicators in the view and in the provider
140 mAttributesFormTreeView->removeAllIndicators();
141 mIndicators.clear();
142 mEnabled = false;
143 }
144}
145
146
151
152bool QgsFieldConstraintIndicatorProvider::acceptsItem( QgsAttributesFormItem *item )
153{
154 if ( item->type() == QgsAttributesFormData::Field )
155 {
157 const QgsFieldConstraints constraints = config.mFieldConstraints;
158
160 return true;
161
163 return true;
164
166 return true;
167 }
168 return false;
169}
170
171QString QgsFieldConstraintIndicatorProvider::iconName( QgsAttributesFormItem *item )
172{
173 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
174 const QgsFieldConstraints constraints = config.mFieldConstraints;
175
176 bool hardConstraint = false;
177
179 {
181 {
182 hardConstraint = true;
183 }
184 }
185
186 if ( !hardConstraint )
187 {
189 {
191 {
192 hardConstraint = true;
193 }
194 }
195 }
196
197 if ( !hardConstraint )
198 {
200 {
202 {
203 hardConstraint = true;
204 }
205 }
206 }
207
208 return hardConstraint ? QStringLiteral( "/field_indicators/mIndicatorConstraintHard.svg" ) : QStringLiteral( "/field_indicators/mIndicatorConstraint.svg" );
209}
210
211QString QgsFieldConstraintIndicatorProvider::tooltipText( QgsAttributesFormItem *item )
212{
213 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
214 const QgsFieldConstraints constraints = config.mFieldConstraints;
215
216 auto addOriginAndStrengthText = [=]( QString name, QgsFieldConstraints::Constraint constraint ) {
217 return QStringLiteral( "%1 (%2, %3)" ).arg( name, constraints.constraintOrigin( constraint ) == QgsFieldConstraints::ConstraintOriginProvider ? tr( "provider" ) : tr( "layer" ), constraints.constraintStrength( constraint ) == QgsFieldConstraints::ConstraintStrengthHard ? tr( "enforced" ) : tr( "unenforced" ) );
218 };
219
220 QString tooltipText;
222 {
223 tooltipText += addOriginAndStrengthText( tr( "Not Null" ), QgsFieldConstraints::ConstraintNotNull );
224 }
225
227 {
228 tooltipText += "\n" + addOriginAndStrengthText( tr( "Unique" ), QgsFieldConstraints::ConstraintUnique );
229 }
230
232 {
233 tooltipText += "\n" + addOriginAndStrengthText( tr( "Expression" ), QgsFieldConstraints::ConstraintExpression );
234 tooltipText += !constraints.constraintDescription().isEmpty() ? QStringLiteral( "\n " ) + constraints.constraintDescription() : QString();
235 }
236
237 return tooltipText;
238}
239
240
245
246bool QgsFieldDefaultValueIndicatorProvider::acceptsItem( QgsAttributesFormItem *item )
247{
248 if ( item->type() == QgsAttributesFormData::Field )
249 {
251 return !config.mDefaultValueExpression.isEmpty();
252 }
253 return false;
254}
255
256QString QgsFieldDefaultValueIndicatorProvider::iconName( QgsAttributesFormItem *item )
257{
258 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
259
260 QString iconName = QStringLiteral( "/field_indicators/mIndicatorDefaultValue.svg" );
261
262 if ( !config.mDefaultValueExpression.isEmpty() )
263 {
264 if ( config.mApplyDefaultValueOnUpdate )
265 {
266 iconName = QStringLiteral( "/field_indicators/mIndicatorDefaultValueApplyOnUpdate.svg" );
267 }
268 }
269 return iconName;
270}
271
272QString QgsFieldDefaultValueIndicatorProvider::tooltipText( QgsAttributesFormItem *item )
273{
274 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
275 QString text;
276 if ( !config.mDefaultValueExpression.isEmpty() )
277 {
278 text += config.mDefaultValueExpression;
279 text += QStringLiteral( "\n(%1)" ).arg( config.mApplyDefaultValueOnUpdate ? tr( "Apply on update" ) : tr( "Do not apply on update" ) );
280 }
281 return text;
282}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Graphical representation for the attribute drag and drop editor.
void removeIndicator(QgsAttributesFormItem *item, QgsAttributesFormTreeViewIndicator *indicator)
Removes the indicator from the given item.
const QList< QgsAttributesFormTreeViewIndicator * > indicators(const QModelIndex &index) const
Returns the list of indicators associated with a given index.
@ Field
Vector layer field.
Holds parent-child relations as well as item data contained in a QgsAttributesFormModel.
int childCount() const
Returns the number of children items for the given item.
QgsAttributesFormItem * child(int row)
Access the child item located at row position.
static bool isGroup(QgsAttributesFormItem *item)
Returns whether the item is a group.
QgsAttributesFormData::AttributesFormItemType type() const
Returns the type of the item.
QVariant data(int role) const
Returns the data stored in the item, corresponding to the given role.
void addedChildren(QgsAttributesFormItem *item, int indexFrom, int indexTo)
Notifies other objects when children have been added to the item, informing the indices where added c...
@ ItemFieldConfigRole
Prior to QGIS 3.44, this was available as FieldConfigRole.
void setEnabled(bool enabled)
Enables or disables the provider.
void updateItemIndicator(QgsAttributesFormItem *item)
Updates the state of a the indicator for the given item.
void onAddedChildren(QgsAttributesFormItem *item, int indexFrom, int indexTo)
Connects to signals of new items added to the tree.
bool isEnabled() const
Returns whether the provider is enabled or not.
QgsAttributesFormTreeViewIndicatorProvider(QgsAttributesFormBaseView *view)
Constructor for QgsAttributesFormTreeViewIndicatorProvider.
Indicator that can be used in an Attributes Form tree view to display icons next to field items.
QgsFieldConstraintIndicatorProvider(QgsAttributesFormBaseView *view)
Constructor for QgsFieldConstraintIndicatorProvider.
Stores information about constraints which may be present on a field.
@ ConstraintStrengthHard
Constraint must be honored before feature can be accepted.
@ ConstraintOriginNotSet
Constraint is not set.
@ ConstraintOriginProvider
Constraint was set at data provider.
Constraint
Constraints which may be present on a field.
@ ConstraintNotNull
Field may not be null.
@ ConstraintUnique
Field must have a unique value.
@ ConstraintExpression
Field has an expression constraint set. See constraintExpression().
QgsFieldDefaultValueIndicatorProvider(QgsAttributesFormBaseView *view)
Constructor for QgsFieldDefaultValueIndicatorProvider.
Holds the configuration for a field.