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