QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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 "moc_qgsmaplayerstylecategoriesmodel.cpp"
18#include "qgsapplication.h"
19
21 : QAbstractListModel( parent )
22{
23 switch ( type )
24 {
26 mCategoryList = qgsEnumList<QgsMapLayer::StyleCategory>();
27 break;
28
31 break;
32
44 break;
51 // not yet handled by the model
52 break;
53 }
54
55 // move All categories to top
56 int idxAllStyleCategories = mCategoryList.indexOf( QgsMapLayer::AllStyleCategories );
57 if ( idxAllStyleCategories > 0 )
58 {
59 mCategoryList.move( idxAllStyleCategories, 0 );
60 }
61}
62
64{
65 if ( mCategories == categories )
66 return;
67
68 // filter the categories and only preserve the categories supported by the current layer type
69 QgsMapLayer::StyleCategories allowedCategories;
70 for ( QgsMapLayer::StyleCategory category : std::as_const( mCategoryList ) )
71 {
72 if ( category == QgsMapLayer::AllStyleCategories )
73 continue;
74 allowedCategories |= category;
75 }
76 categories &= allowedCategories;
77
78 beginResetModel();
79 mCategories = categories;
80 endResetModel();
81}
82
87
89{
90 beginResetModel();
91 mShowAllCategories = showAll;
92 endResetModel();
93}
94
95int QgsMapLayerStyleCategoriesModel::rowCount( const QModelIndex & ) const
96{
97 int count = mCategoryList.count();
98 if ( count > 0 && !mShowAllCategories )
99 count--;
100 return count;
101}
102
103int QgsMapLayerStyleCategoriesModel::columnCount( const QModelIndex & ) const
104{
105 return 1;
106}
107
108QVariant QgsMapLayerStyleCategoriesModel::data( const QModelIndex &index, int role ) const
109{
110 if ( !index.isValid() || index.row() >= rowCount() )
111 return QVariant();
112
113 const QgsMapLayer::StyleCategory category = mCategoryList.at( index.row() + ( mShowAllCategories ? 0 : 1 ) );
114
115 if ( role == Qt::UserRole )
116 {
117 return category;
118 }
119 if ( role == Qt::CheckStateRole )
120 {
121 return mCategories.testFlag( category ) ? Qt::Checked : Qt::Unchecked;
122 }
123
124 QString htmlStylePattern = QStringLiteral( "<p><b>%1</b><br/><span style='color:gray;'>%2</span></p>" );
125 switch ( category )
126 {
128 {
129 QString name = tr( "Layer Configuration" );
130 QString description = tr( "The layers display expression and the datasource flags: identifiable, removable, searchable, read-only and hidden from the project settings" );
131 switch ( role )
132 {
133 case static_cast<int>( Role::NameRole ):
134 return name;
135 case Qt::DisplayRole:
136 return htmlStylePattern.arg( name ).arg( description );
137 case Qt::ToolTipRole:
138 return description;
139 case Qt::DecorationRole:
140 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/layerconfiguration.svg" ) );
141 }
142 break;
143 }
145 {
146 QString name = tr( "Symbology" );
147 QString description = tr( "Everything from the symbology section" );
148 switch ( role )
149 {
150 case static_cast<int>( Role::NameRole ):
151 return name;
152 case Qt::DisplayRole:
153
154 return htmlStylePattern.arg( name ).arg( description );
155 case Qt::ToolTipRole:
156 return description;
157 case Qt::DecorationRole:
158 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/symbology.svg" ) );
159 }
160 break;
161 }
163 {
164 QString name = tr( "3D Symbology" );
165 QString description = tr( "Everything from the 3D symbology section" );
166 switch ( role )
167 {
168 case static_cast<int>( Role::NameRole ):
169 return name;
170 case Qt::DisplayRole:
171
172 return htmlStylePattern.arg( name ).arg( description );
173 case Qt::ToolTipRole:
174 return description;
175 case Qt::DecorationRole:
176 return QgsApplication::getThemeIcon( QStringLiteral( "/3d.svg" ) );
177 }
178 break;
179 }
181 {
182 QString name = tr( "Labels" );
183 QString description = tr( "Everything from the labels section" );
184 switch ( role )
185 {
186 case static_cast<int>( Role::NameRole ):
187 return name;
188 case Qt::DisplayRole:
189
190 return htmlStylePattern.arg( name ).arg( description );
191 case Qt::ToolTipRole:
192 return description;
193 case Qt::DecorationRole:
194 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/labels.svg" ) );
195 }
196 break;
197 }
199 {
200 QString name = tr( "Fields" );
201 QString description = tr( "Virtual fields, aliases, default value expressions and constraints from the form section and WMS/WFS exposure" );
202 switch ( role )
203 {
204 case static_cast<int>( Role::NameRole ):
205 return name;
206 case Qt::DisplayRole:
207
208 return htmlStylePattern.arg( name ).arg( description );
209 case Qt::ToolTipRole:
210 return description;
211 case Qt::DecorationRole:
212 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/sourcefieldsandforms.svg" ) );
213 }
214 break;
215 }
217 {
218 QString name = tr( "Attribute Form" );
219 QString description = tr( "Form layout and widget configuration (no constraints and default value expressions)" );
220 switch ( role )
221 {
222 case static_cast<int>( Role::NameRole ):
223 return name;
224 case Qt::DisplayRole:
225
226 return htmlStylePattern.arg( name ).arg( description );
227 case Qt::ToolTipRole:
228 return description;
229 case Qt::DecorationRole:
230 return QgsApplication::getThemeIcon( QStringLiteral( "/mActionFormView.svg" ) );
231 }
232 break;
233 }
235 {
236 QString name = tr( "Actions" );
237 QString description = tr( "Everything from the actions section" );
238 switch ( role )
239 {
240 case static_cast<int>( Role::NameRole ):
241 return name;
242 case Qt::DisplayRole:
243
244 return htmlStylePattern.arg( name ).arg( description );
245 case Qt::ToolTipRole:
246 return description;
247 case Qt::DecorationRole:
248 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/action.svg" ) );
249 }
250 break;
251 }
253 {
254 QString name = tr( "Map Tips" );
255 QString description = tr( "Map tips settings (no layer display expression)" );
256 switch ( role )
257 {
258 case static_cast<int>( Role::NameRole ):
259 return name;
260 case Qt::DisplayRole:
261
262 return htmlStylePattern.arg( name ).arg( description );
263 case Qt::ToolTipRole:
264 return description;
265 case Qt::DecorationRole:
266 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/display.svg" ) );
267 }
268 break;
269 }
271 {
272 QString name = tr( "Diagrams" );
273 QString description = tr( "Everything from the diagram section" );
274 switch ( role )
275 {
276 case static_cast<int>( Role::NameRole ):
277 return name;
278 case Qt::DisplayRole:
279
280 return htmlStylePattern.arg( name ).arg( description );
281 case Qt::ToolTipRole:
282 return description;
283 case Qt::DecorationRole:
284 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/diagram.svg" ) );
285 }
286 break;
287 }
289 {
290 QString name = tr( "Attribute Table Configuration" );
291 QString description = tr( "Attribute table settings: choice and order of columns and conditional styling" );
292 switch ( role )
293 {
294 case static_cast<int>( Role::NameRole ):
295 return name;
296 case Qt::DisplayRole:
297
298 return htmlStylePattern.arg( name ).arg( description );
299 case Qt::ToolTipRole:
300 return description;
301 case Qt::DecorationRole:
302 return QgsApplication::getThemeIcon( QStringLiteral( "/mActionOpenTable.svg" ) );
303 }
304 break;
305 }
307 {
308 QString name = tr( "Rendering" );
309 QString description = tr( "Everything from the rendering section: Scale visibility, simplify method, opacity, auto refresh etc." );
310 switch ( role )
311 {
312 case static_cast<int>( Role::NameRole ):
313 return name;
314 case Qt::DisplayRole:
315
316 return htmlStylePattern.arg( name ).arg( description );
317 case Qt::ToolTipRole:
318 return description;
319 case Qt::DecorationRole:
320 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/rendering.svg" ) );
321 }
322 break;
323 }
325 {
326 QString name = tr( "Custom Properties" );
327 QString description = tr( "Layer variables and embedded legend widgets as well as all the custom properties (often used by plugins and custom python code)" );
328 switch ( role )
329 {
330 case static_cast<int>( Role::NameRole ):
331 return name;
332 case Qt::DisplayRole:
333
334 return htmlStylePattern.arg( name ).arg( description );
335 case Qt::ToolTipRole:
336 return description;
337 case Qt::DecorationRole:
338 return QgsApplication::getThemeIcon( QStringLiteral( "/mActionOptions.svg" ) );
339 }
340 break;
341 }
343 {
344 QString name = tr( "Geometry Options" );
345 QString description = tr( "Geometry constraints and validity checks" );
346 switch ( role )
347 {
348 case static_cast<int>( Role::NameRole ):
349 return name;
350 case Qt::DisplayRole:
351
352 return htmlStylePattern.arg( name ).arg( description );
353 case Qt::ToolTipRole:
354 return description;
355 case Qt::DecorationRole:
356 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/digitizing.svg" ) );
357 }
358 break;
359 }
361 {
362 QString name = tr( "Relations" );
363 QString description = tr( "The relations this layer has with other layers" );
364 switch ( role )
365 {
366 case static_cast<int>( Role::NameRole ):
367 return name;
368 case Qt::DisplayRole:
369
370 return htmlStylePattern.arg( name ).arg( description );
371 case Qt::ToolTipRole:
372 return description;
373 case Qt::DecorationRole:
374 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/relations.svg" ) );
375 }
376 break;
377 }
379 {
380 QString name = tr( "Temporal Properties" );
381 QString description = tr( "Everything from the temporal section" );
382 switch ( role )
383 {
384 case static_cast<int>( Role::NameRole ):
385 return name;
386 case Qt::DisplayRole:
387
388 return htmlStylePattern.arg( name ).arg( description );
389 case Qt::ToolTipRole:
390 return description;
391 case Qt::DecorationRole:
392 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/temporal.svg" ) );
393 }
394 break;
395 }
397 {
398 QString name = tr( "Legend Settings" );
399 QString description = tr( "Legend settings (no embedded legend widgets)" );
400 switch ( role )
401 {
402 case static_cast<int>( Role::NameRole ):
403 return name;
404 case Qt::DisplayRole:
405
406 return htmlStylePattern.arg( name ).arg( description );
407 case Qt::ToolTipRole:
408 return description;
409 case Qt::DecorationRole:
410 return QgsApplication::getThemeIcon( QStringLiteral( "/legend.svg" ) );
411 }
412 break;
413 }
415 {
416 QString name = tr( "Elevation Properties" );
417 QString description = tr( "Everything from the elevation section" );
418 switch ( role )
419 {
420 case static_cast<int>( Role::NameRole ):
421 return name;
422 case Qt::DisplayRole:
423
424 return htmlStylePattern.arg( name ).arg( description );
425 case Qt::ToolTipRole:
426 return description;
427 case Qt::DecorationRole:
428 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/elevationscale.svg" ) );
429 }
430 break;
431 }
433 {
434 QString name = tr( "Notes" );
435 QString description = tr( "The layer notes" );
436 switch ( role )
437 {
438 case static_cast<int>( Role::NameRole ):
439 return name;
440 case Qt::DisplayRole:
441
442 return htmlStylePattern.arg( name ).arg( description );
443 case Qt::ToolTipRole:
444 return description;
445 case Qt::DecorationRole:
446 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/notes.svg" ) );
447 }
448 break;
449 }
451 {
452 QString name = tr( "All Style Categories" );
453 QString description = tr( "All style categories" );
454 switch ( role )
455 {
456 case static_cast<int>( Role::NameRole ):
457 return name;
458 case Qt::DisplayRole:
459
460 return htmlStylePattern.arg( name ).arg( description );
461 case Qt::ToolTipRole:
462 return QVariant();
463 case Qt::DecorationRole:
464 return QVariant();
465 }
466 break;
467 }
468 }
469 return QVariant();
470}
471
472bool QgsMapLayerStyleCategoriesModel::setData( const QModelIndex &index, const QVariant &value, int role )
473{
474 if ( !index.isValid() || index.row() >= rowCount() )
475 return false;
476
477 if ( role == Qt::CheckStateRole )
478 {
479 const QgsMapLayer::StyleCategory category = data( index, Qt::UserRole ).value<QgsMapLayer::StyleCategory>();
480 if ( value.value<Qt::CheckState>() == Qt::Checked )
481 {
482 mCategories |= category;
483 emit dataChanged( index, index );
484 return true;
485 }
486 else if ( value.value<Qt::CheckState>() == Qt::Unchecked )
487 {
488 mCategories &= ~category;
489 emit dataChanged( index, index );
490 return true;
491 }
492 }
493 return false;
494}
495
496
497Qt::ItemFlags QgsMapLayerStyleCategoriesModel::flags( const QModelIndex & ) const
498{
499 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
500}
501
503 : QItemDelegate( parent )
504{
505}
506
507void QgsCategoryDisplayLabelDelegate::drawDisplay( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text ) const
508{
509 QLabel label;
510 label.setText( text );
511 label.setEnabled( option.state & QStyle::State_Enabled );
512 label.setAttribute( Qt::WA_TranslucentBackground );
513 label.setMargin( 3 );
514 label.setWordWrap( true );
515 painter->save();
516 painter->translate( rect.topLeft() );
517 label.resize( rect.size() );
518 label.render( painter );
519 painter->restore();
520}
521
522QSize QgsCategoryDisplayLabelDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
523{
524 Q_UNUSED( option )
525 QLabel label;
526 QString display = index.model()->data( index, Qt::DisplayRole ).toString();
527 label.setText( display );
528 label.setWordWrap( true );
529 label.setFixedWidth( option.widget->size().width() );
530 label.setMargin( 3 );
531 return label.sizeHint();
532}
LayerType
Types of layers that can be added to a map.
Definition qgis.h:169
@ 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 drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const override
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
QgsCategoryDisplayLabelDelegate(QObject *parent=nullptr)
constructor
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
StyleCategory
Categories of style to distinguish appropriate sections for import/export.
@ GeometryOptions
Geometry validation configuration.
@ AttributeTable
Attribute table settings: choice and order of columns, conditional styling.
@ LayerConfiguration
General configuration: identifiable, removable, searchable, display expression, read-only.
@ Symbology
Symbology.
@ MapTips
Map tips.
@ Notes
Layer user notes.
@ Temporal
Temporal properties.
@ Rendering
Rendering: scale visibility, simplify method, opacity.
@ Relations
Relations.
@ Elevation
Elevation settings.
@ Symbology3D
3D symbology
@ CustomProperties
Custom properties (by plugins for instance)
@ Actions
Actions.
@ Forms
Feature form.
@ Fields
Aliases, widgets, WMS/WFS, expressions, constraints, virtual fields.
@ Legend
Legend settings.
@ Diagrams
Diagrams.
@ Labeling
Labeling.