QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsmaplayerstylecategoriesmodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayerstylecategoriesmodel.cpp
3 --------------------------------------
4 Date : September 2018
5 Copyright : (C) 2018 by Denis Rouzaud
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 "qgsapplication.h"
18
20 : QAbstractListModel( parent )
21{
22 switch ( type )
23 {
25 mCategoryList = qgsEnumList<QgsMapLayer::StyleCategory>();
26 break;
27
29 mCategoryList << QgsMapLayer::StyleCategory::Symbology << QgsMapLayer::StyleCategory::Labeling << QgsMapLayer::StyleCategory::AllStyleCategories;
30 break;
31
33 mCategoryList << QgsMapLayer::StyleCategory::Symbology << QgsMapLayer::StyleCategory::AllStyleCategories;
34 break;
41 // not yet handled by the model
42 break;
43 }
44
45 // move All categories to top
46 int idxAllStyleCategories = mCategoryList.indexOf( QgsMapLayer::AllStyleCategories );
47 if ( idxAllStyleCategories > 0 )
48 {
49 mCategoryList.move( idxAllStyleCategories, 0 );
50 }
51}
52
54{
55 if ( mCategories == categories )
56 return;
57
58 beginResetModel();
59 mCategories = categories;
60 endResetModel();
61}
62
64{
65 return mCategories;
66}
67
69{
70 beginResetModel();
71 mShowAllCategories = showAll;
72 endResetModel();
73}
74
75int QgsMapLayerStyleCategoriesModel::rowCount( const QModelIndex & ) const
76{
77 int count = mCategoryList.count();
78 if ( count > 0 && !mShowAllCategories )
79 count--;
80 return count;
81}
82
83int QgsMapLayerStyleCategoriesModel::columnCount( const QModelIndex & ) const
84{
85 return 1;
86}
87
88QVariant QgsMapLayerStyleCategoriesModel::data( const QModelIndex &index, int role ) const
89{
90 if ( !index.isValid() || index.row() >= rowCount() )
91 return QVariant();
92
93 const QgsMapLayer::StyleCategory category = mCategoryList.at( index.row() + ( mShowAllCategories ? 0 : 1 ) );
94
95 if ( role == Qt::UserRole )
96 {
97 return category;
98 }
99 if ( role == Qt::CheckStateRole )
100 {
101 return mCategories.testFlag( category ) ? Qt::Checked : Qt::Unchecked;
102 }
103
104 switch ( category )
105 {
106 case QgsMapLayer::StyleCategory::LayerConfiguration:
107 switch ( role )
108 {
109 case Qt::DisplayRole:
110 return tr( "Layer Configuration" );
111 case Qt::ToolTipRole:
112 return tr( "Identifiable, removable, searchable, display expression, read-only, hidden" );
113 case Qt::DecorationRole:
114 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/system.svg" ) );
115 }
116 break;
117 case QgsMapLayer::StyleCategory::Symbology:
118 switch ( role )
119 {
120 case Qt::DisplayRole:
121 case Qt::ToolTipRole:
122 return tr( "Symbology" );
123 case Qt::DecorationRole:
124 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/symbology.svg" ) );
125 }
126 break;
127 case QgsMapLayer::StyleCategory::Symbology3D:
128 switch ( role )
129 {
130 case Qt::DisplayRole:
131 return tr( "3D Symbology" );
132 case Qt::ToolTipRole:
133 return tr( "3D symbology" );
134 case Qt::DecorationRole:
135 return QgsApplication::getThemeIcon( QStringLiteral( "/3d.svg" ) );
136 }
137 break;
138 case QgsMapLayer::StyleCategory::Labeling:
139 switch ( role )
140 {
141 case Qt::DisplayRole:
142 case Qt::ToolTipRole:
143 return tr( "Labels" );
144 case Qt::DecorationRole:
145 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/labels.svg" ) );
146 }
147 break;
148 case QgsMapLayer::StyleCategory::Fields:
149 switch ( role )
150 {
151 case Qt::DisplayRole:
152 return tr( "Fields" );
153 case Qt::ToolTipRole:
154 return tr( "Aliases, widgets, WMS/WFS, expressions, constraints, virtual fields" );
155 case Qt::DecorationRole:
156 return QgsApplication::getThemeIcon( QStringLiteral( "/mSourceFields.svg" ) );
157 }
158 break;
159 case QgsMapLayer::StyleCategory::Forms:
160 switch ( role )
161 {
162 case Qt::DisplayRole:
163 return tr( "Attribute Form" );
164 case Qt::ToolTipRole:
165 return tr( "Attribute form settings, widget configuration" );
166 case Qt::DecorationRole:
167 return QgsApplication::getThemeIcon( QStringLiteral( "/mActionFormView.svg" ) );
168 }
169 break;
170 case QgsMapLayer::StyleCategory::Actions:
171 switch ( role )
172 {
173 case Qt::DisplayRole:
174 return tr( "Actions" );
175 case Qt::ToolTipRole:
176 return tr( "Map layer actions" );
177 case Qt::DecorationRole:
178 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/action.svg" ) );
179 }
180 break;
181 case QgsMapLayer::StyleCategory::MapTips:
182 switch ( role )
183 {
184 case Qt::DisplayRole:
185 return tr( "Map Tips" );
186 case Qt::ToolTipRole:
187 return tr( "Map tips" );
188 case Qt::DecorationRole:
189 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/display.svg" ) );
190 }
191 break;
192 case QgsMapLayer::StyleCategory::Diagrams:
193 switch ( role )
194 {
195 case Qt::DisplayRole:
196 case Qt::ToolTipRole:
197 return tr( "Diagrams" );
198 case Qt::DecorationRole:
199 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/diagram.svg" ) );
200 }
201 break;
202 case QgsMapLayer::StyleCategory::AttributeTable:
203 switch ( role )
204 {
205 case Qt::DisplayRole:
206 return tr( "Attribute Table Configuration" );
207 case Qt::ToolTipRole:
208 return tr( "Choice and order of columns, conditional styling" );
209 case Qt::DecorationRole:
210 return QgsApplication::getThemeIcon( QStringLiteral( "/mActionOpenTable.svg" ) );
211 }
212 break;
213 case QgsMapLayer::StyleCategory::Rendering:
214 switch ( role )
215 {
216 case Qt::DisplayRole:
217 return tr( "Rendering" );
218 case Qt::ToolTipRole:
219 return tr( "Scale visibility, simplify method, opacity" );
220 case Qt::DecorationRole:
221 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/rendering.svg" ) );
222 }
223 break;
224 case QgsMapLayer::StyleCategory::CustomProperties:
225 switch ( role )
226 {
227 case Qt::DisplayRole:
228 return tr( "Custom Properties" );
229 case Qt::ToolTipRole:
230 return tr( "Variables, custom properties (often used by plugins and custom python code)" );
231 case Qt::DecorationRole:
232 return QgsApplication::getThemeIcon( QStringLiteral( "/mActionOptions.svg" ) );
233 }
234 break;
235 case QgsMapLayer::StyleCategory::GeometryOptions:
236 switch ( role )
237 {
238 case Qt::DisplayRole:
239 return tr( "Geometry Options" );
240 case Qt::ToolTipRole:
241 return tr( "Geometry constraints and validity checks" );
242 case Qt::DecorationRole:
243 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/digitizing.svg" ) );
244 }
245 break;
246 case QgsMapLayer::StyleCategory::Relations:
247 switch ( role )
248 {
249 case Qt::DisplayRole:
250 return tr( "Relations" );
251 case Qt::ToolTipRole:
252 return tr( "Relations with other layers" );
253 case Qt::DecorationRole:
254 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/relations.svg" ) );
255 }
256 break;
257
258 case QgsMapLayer::StyleCategory::Temporal:
259 switch ( role )
260 {
261 case Qt::DisplayRole:
262 return tr( "Temporal Properties" );
263 case Qt::ToolTipRole:
264 return tr( "Temporal properties" );
265 case Qt::DecorationRole:
266 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/temporal.svg" ) );
267 }
268 break;
269
270 case QgsMapLayer::StyleCategory::Legend:
271 switch ( role )
272 {
273 case Qt::DisplayRole:
274 return tr( "Legend Settings" );
275 case Qt::ToolTipRole:
276 return tr( "Legend settings" );
277 case Qt::DecorationRole:
278 return QgsApplication::getThemeIcon( QStringLiteral( "/legend.svg" ) );
279 }
280 break;
281
282 case QgsMapLayer::StyleCategory::Elevation:
283 switch ( role )
284 {
285 case Qt::DisplayRole:
286 return tr( "Elevation Properties" );
287 case Qt::ToolTipRole:
288 return tr( "Elevation properties" );
289 case Qt::DecorationRole:
290 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/elevationscale.svg" ) );
291 }
292 break;
293
294 case QgsMapLayer::StyleCategory::Notes:
295 switch ( role )
296 {
297 case Qt::DisplayRole:
298 case Qt::ToolTipRole:
299 return tr( "Notes" );
300 case Qt::DecorationRole:
301 return QIcon(); // TODO
302 }
303 break;
304
305 case QgsMapLayer::StyleCategory::AllStyleCategories:
306 switch ( role )
307 {
308 case Qt::DisplayRole:
309 return tr( "All Style Categories" );
310 case Qt::ToolTipRole:
311 return QVariant();
312 case Qt::DecorationRole:
313 return QVariant();
314 }
315 break;
316
317 }
318 return QVariant();
319}
320
321bool QgsMapLayerStyleCategoriesModel::setData( const QModelIndex &index, const QVariant &value, int role )
322{
323 if ( !index.isValid() || index.row() >= rowCount() )
324 return false;
325
326 if ( role == Qt::CheckStateRole )
327 {
328 const QgsMapLayer::StyleCategory category = data( index, Qt::UserRole ).value<QgsMapLayer::StyleCategory>();
329 if ( value.value<Qt::CheckState>() == Qt::Checked )
330 {
331 mCategories |= category;
332 emit dataChanged( index, index );
333 return true;
334 }
335 else if ( value.value<Qt::CheckState>() == Qt::Unchecked )
336 {
337 mCategories &= ~category;
338 emit dataChanged( index, index );
339 return true;
340 }
341 }
342 return false;
343}
344
345
346Qt::ItemFlags QgsMapLayerStyleCategoriesModel::flags( const QModelIndex & ) const
347{
348 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
349}
LayerType
Types of layers that can be added to a map.
Definition: qgis.h:114
@ Group
Composite group layer. Added in QGIS 3.24.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ Vector
Vector layer.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ Raster
Raster layer.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
void setCategories(QgsMapLayer::StyleCategories categories)
Reset the model data.
int columnCount(const QModelIndex &=QModelIndex()) const override
QgsMapLayerStyleCategoriesModel(Qgis::LayerType type, QObject *parent=nullptr)
Constructor for QgsMapLayerStyleCategoriesModel, for the specified layer type.
Qt::ItemFlags flags(const QModelIndex &) const override
QVariant data(const QModelIndex &index, int role) const override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QgsMapLayer::StyleCategories categories() const
Returns the categories as defined in the model.
void setShowAllCategories(bool showAll)
Defines if the model should list the AllStyleCategories entry.
int rowCount(const QModelIndex &=QModelIndex()) const override
QFlags< StyleCategory > StyleCategories
Definition: qgsmaplayer.h:188
StyleCategory
Categories of style to distinguish appropriate sections for import/export.
Definition: qgsmaplayer.h:165
@ AllStyleCategories
Definition: qgsmaplayer.h:184