QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
18#include "qgsapplication.h"
19
20#include "moc_qgsmaplayerstylecategoriesmodel.cpp"
21
23 : QAbstractListModel( parent )
24 , mLayerType( type )
25{
26 switch ( type )
27 {
30 break;
31
37 break;
38
53 break;
60 // not yet handled by the model
61 break;
62 }
63
64 // move the grouped categories to top
65 int idxCategory = mCategoryList.indexOf( QgsMapLayer::AllAttributeCategories );
66 if ( idxCategory > 0 )
67 {
68 mCategoryList.move( idxCategory, 0 );
69 }
70 idxCategory = mCategoryList.indexOf( QgsMapLayer::AllVisualStyleCategories );
71 if ( idxCategory > 0 )
72 {
73 mCategoryList.move( idxCategory, 0 );
74 }
75 idxCategory = mCategoryList.indexOf( QgsMapLayer::AllStyleCategories );
76 if ( idxCategory > 0 )
77 {
78 mCategoryList.move( idxCategory, 0 );
79 }
80}
81
83{
84 if ( mCategories == categories )
85 return;
86
87 // filter the categories and only preserve the categories supported by the current layer type
88 QgsMapLayer::StyleCategories allowedCategories;
89 for ( QgsMapLayer::StyleCategory category : std::as_const( mCategoryList ) )
90 {
91 allowedCategories |= category;
92 }
93 categories &= allowedCategories;
94
95 beginResetModel();
96 mCategories = categories;
97 endResetModel();
98}
99
104
106{
107 beginResetModel();
108 mShowAllCategories = showAll;
109 endResetModel();
110}
111
112int QgsMapLayerStyleCategoriesModel::rowCount( const QModelIndex & ) const
113{
114 int count = mCategoryList.count();
115
116 if ( count > 0 && !mShowAllCategories )
117 {
118 if ( mCategoryList.contains( QgsMapLayer::AllAttributeCategories ) )
119 {
120 count--;
121 }
122 if ( mCategoryList.contains( QgsMapLayer::AllVisualStyleCategories ) )
123 {
124 count--;
125 }
126 if ( mCategoryList.contains( QgsMapLayer::AllStyleCategories ) )
127 {
128 count--;
129 }
130 }
131
132 return count;
133}
134
135int QgsMapLayerStyleCategoriesModel::columnCount( const QModelIndex & ) const
136{
137 return 1;
138}
139
140QVariant QgsMapLayerStyleCategoriesModel::data( const QModelIndex &index, int role ) const
141{
142 const int filteredRowCount = rowCount();
143
144 if ( !index.isValid() || index.row() >= filteredRowCount )
145 return QVariant();
146
147 const QgsMapLayer::StyleCategory category = mCategoryList.at( index.row() + ( mShowAllCategories ? 0 : mCategoryList.count() - filteredRowCount ) );
148
149 if ( role == Qt::UserRole )
150 {
151 return category;
152 }
153 if ( role == Qt::CheckStateRole )
154 {
155 return mCategories.testFlag( category ) ? Qt::Checked : Qt::Unchecked;
156 }
157
158 QString htmlStylePattern = QStringLiteral( "<p><b>%1</b><br/><span style='color:gray;'>%2</span></p>" );
159 switch ( category )
160 {
162 {
163 QString name = tr( "Layer Configuration" );
164 QString description = tr( "The layers display expression and the datasource flags: identifiable, removable, searchable, read-only and hidden from the project settings" );
165 switch ( role )
166 {
167 case static_cast<int>( Role::NameRole ):
168 return name;
169 case Qt::DisplayRole:
170 return htmlStylePattern.arg( name ).arg( description );
171 case Qt::ToolTipRole:
172 return description;
173 case Qt::DecorationRole:
174 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/layerconfiguration.svg" ) );
175 }
176 break;
177 }
179 {
180 QString name = tr( "Symbology" );
181 QString description = tr( "Everything from the symbology section" );
182 switch ( role )
183 {
184 case static_cast<int>( Role::NameRole ):
185 return name;
186 case Qt::DisplayRole:
187
188 return htmlStylePattern.arg( name ).arg( description );
189 case Qt::ToolTipRole:
190 return description;
191 case Qt::DecorationRole:
192 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/symbology.svg" ) );
193 }
194 break;
195 }
197 {
198 QString name = tr( "3D Symbology" );
199 QString description = tr( "Everything from the 3D symbology section" );
200 switch ( role )
201 {
202 case static_cast<int>( Role::NameRole ):
203 return name;
204 case Qt::DisplayRole:
205
206 return htmlStylePattern.arg( name ).arg( description );
207 case Qt::ToolTipRole:
208 return description;
209 case Qt::DecorationRole:
210 return QgsApplication::getThemeIcon( QStringLiteral( "/3d.svg" ) );
211 }
212 break;
213 }
215 {
216 QString name = tr( "Labels" );
217 QString description = tr( "Everything from the labels section" );
218 switch ( role )
219 {
220 case static_cast<int>( Role::NameRole ):
221 return name;
222 case Qt::DisplayRole:
223
224 return htmlStylePattern.arg( name ).arg( description );
225 case Qt::ToolTipRole:
226 return description;
227 case Qt::DecorationRole:
228 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/labels.svg" ) );
229 }
230 break;
231 }
233 {
234 QString name = tr( "Fields" );
235 QString description = tr( "Virtual fields, aliases, default value expressions and constraints from the form section and WMS/WFS exposure" );
236 switch ( role )
237 {
238 case static_cast<int>( Role::NameRole ):
239 return name;
240 case Qt::DisplayRole:
241
242 return htmlStylePattern.arg( name ).arg( description );
243 case Qt::ToolTipRole:
244 return description;
245 case Qt::DecorationRole:
246 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/sourcefieldsandforms.svg" ) );
247 }
248 break;
249 }
251 {
252 QString name = tr( "Attribute Form" );
253 QString description = tr( "Form layout and widget configuration (no constraints and default value expressions)" );
254 switch ( role )
255 {
256 case static_cast<int>( Role::NameRole ):
257 return name;
258 case Qt::DisplayRole:
259
260 return htmlStylePattern.arg( name ).arg( description );
261 case Qt::ToolTipRole:
262 return description;
263 case Qt::DecorationRole:
264 return QgsApplication::getThemeIcon( QStringLiteral( "/mActionFormView.svg" ) );
265 }
266 break;
267 }
269 {
270 QString name = tr( "Actions" );
271 QString description = tr( "Everything from the actions section" );
272 switch ( role )
273 {
274 case static_cast<int>( Role::NameRole ):
275 return name;
276 case Qt::DisplayRole:
277
278 return htmlStylePattern.arg( name ).arg( description );
279 case Qt::ToolTipRole:
280 return description;
281 case Qt::DecorationRole:
282 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/action.svg" ) );
283 }
284 break;
285 }
287 {
288 QString name = tr( "Map Tips" );
289 QString description = tr( "Map tips settings (no layer display expression)" );
290 switch ( role )
291 {
292 case static_cast<int>( Role::NameRole ):
293 return name;
294 case Qt::DisplayRole:
295
296 return htmlStylePattern.arg( name ).arg( description );
297 case Qt::ToolTipRole:
298 return description;
299 case Qt::DecorationRole:
300 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/display.svg" ) );
301 }
302 break;
303 }
305 {
306 QString name = tr( "Diagrams" );
307 QString description = tr( "Everything from the diagram section" );
308 switch ( role )
309 {
310 case static_cast<int>( Role::NameRole ):
311 return name;
312 case Qt::DisplayRole:
313
314 return htmlStylePattern.arg( name ).arg( description );
315 case Qt::ToolTipRole:
316 return description;
317 case Qt::DecorationRole:
318 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/diagram.svg" ) );
319 }
320 break;
321 }
323 {
324 QString name = tr( "Attribute Table Configuration" );
325 QString description = tr( "Attribute table settings: choice and order of columns and conditional styling" );
326 switch ( role )
327 {
328 case static_cast<int>( Role::NameRole ):
329 return name;
330 case Qt::DisplayRole:
331
332 return htmlStylePattern.arg( name ).arg( description );
333 case Qt::ToolTipRole:
334 return description;
335 case Qt::DecorationRole:
336 return QgsApplication::getThemeIcon( QStringLiteral( "/mActionOpenTable.svg" ) );
337 }
338 break;
339 }
341 {
342 QString name = tr( "Rendering" );
343 QString description = tr( "Everything from the rendering section: Scale visibility, simplify method, opacity, auto refresh etc." );
344 switch ( role )
345 {
346 case static_cast<int>( Role::NameRole ):
347 return name;
348 case Qt::DisplayRole:
349
350 return htmlStylePattern.arg( name ).arg( description );
351 case Qt::ToolTipRole:
352 return description;
353 case Qt::DecorationRole:
354 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/rendering.svg" ) );
355 }
356 break;
357 }
359 {
360 QString name = tr( "Custom Properties" );
361 QString description = tr( "Layer variables and embedded legend widgets as well as all the custom properties (often used by plugins and custom python code)" );
362 switch ( role )
363 {
364 case static_cast<int>( Role::NameRole ):
365 return name;
366 case Qt::DisplayRole:
367
368 return htmlStylePattern.arg( name ).arg( description );
369 case Qt::ToolTipRole:
370 return description;
371 case Qt::DecorationRole:
372 return QgsApplication::getThemeIcon( QStringLiteral( "/mActionOptions.svg" ) );
373 }
374 break;
375 }
377 {
378 QString name = tr( "Geometry Options" );
379 QString description = tr( "Geometry constraints and validity checks" );
380 switch ( role )
381 {
382 case static_cast<int>( Role::NameRole ):
383 return name;
384 case Qt::DisplayRole:
385
386 return htmlStylePattern.arg( name ).arg( description );
387 case Qt::ToolTipRole:
388 return description;
389 case Qt::DecorationRole:
390 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/digitizing.svg" ) );
391 }
392 break;
393 }
395 {
396 QString name = tr( "Relations" );
397 QString description = tr( "The relations this layer has with other layers" );
398 switch ( role )
399 {
400 case static_cast<int>( Role::NameRole ):
401 return name;
402 case Qt::DisplayRole:
403
404 return htmlStylePattern.arg( name ).arg( description );
405 case Qt::ToolTipRole:
406 return description;
407 case Qt::DecorationRole:
408 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/relations.svg" ) );
409 }
410 break;
411 }
413 {
414 QString name = tr( "Temporal Properties" );
415 QString description = tr( "Everything from the temporal section" );
416 switch ( role )
417 {
418 case static_cast<int>( Role::NameRole ):
419 return name;
420 case Qt::DisplayRole:
421
422 return htmlStylePattern.arg( name ).arg( description );
423 case Qt::ToolTipRole:
424 return description;
425 case Qt::DecorationRole:
426 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/temporal.svg" ) );
427 }
428 break;
429 }
431 {
432 QString name = tr( "Legend Settings" );
433 QString description = tr( "Legend settings (no embedded legend widgets)" );
434 switch ( role )
435 {
436 case static_cast<int>( Role::NameRole ):
437 return name;
438 case Qt::DisplayRole:
439
440 return htmlStylePattern.arg( name ).arg( description );
441 case Qt::ToolTipRole:
442 return description;
443 case Qt::DecorationRole:
444 return QgsApplication::getThemeIcon( QStringLiteral( "/legend.svg" ) );
445 }
446 break;
447 }
449 {
450 QString name = tr( "Elevation Properties" );
451 QString description = tr( "Everything from the elevation section" );
452 switch ( role )
453 {
454 case static_cast<int>( Role::NameRole ):
455 return name;
456 case Qt::DisplayRole:
457
458 return htmlStylePattern.arg( name ).arg( description );
459 case Qt::ToolTipRole:
460 return description;
461 case Qt::DecorationRole:
462 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/elevationscale.svg" ) );
463 }
464 break;
465 }
467 {
468 QString name = tr( "Notes" );
469 QString description = tr( "The layer notes" );
470 switch ( role )
471 {
472 case static_cast<int>( Role::NameRole ):
473 return name;
474 case Qt::DisplayRole:
475
476 return htmlStylePattern.arg( name ).arg( description );
477 case Qt::ToolTipRole:
478 return description;
479 case Qt::DecorationRole:
480 return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/notes.svg" ) );
481 }
482 break;
483 }
485 {
486 QString name = tr( "All Fields and Attribute Form Categories" );
487 QString description = tr( "All fields and attribute form categories" );
488 switch ( role )
489 {
490 case static_cast<int>( Role::NameRole ):
491 return name;
492 case Qt::DisplayRole:
493 return htmlStylePattern.arg( name ).arg( description );
494 case Qt::ToolTipRole:
495 return description;
496 case Qt::DecorationRole:
497 return QVariant();
498 }
499 break;
500 }
502 {
503 QString name = tr( "All Symbology and Labeling Categories" );
504 QString description = mLayerType == Qgis::LayerType::Vector ? tr( "All symbology, labeling and diagram categories" ) : tr( "All symbology and labeling categories" );
505 switch ( role )
506 {
507 case static_cast<int>( Role::NameRole ):
508 return name;
509 case Qt::DisplayRole:
510 return htmlStylePattern.arg( name ).arg( description );
511 case Qt::ToolTipRole:
512 return description;
513 case Qt::DecorationRole:
514 return QVariant();
515 }
516 break;
517 }
519 {
520 QString name = tr( "All Style Categories" );
521 QString description = tr( "All style categories" );
522 switch ( role )
523 {
524 case static_cast<int>( Role::NameRole ):
525 return name;
526 case Qt::DisplayRole:
527 return htmlStylePattern.arg( name ).arg( description );
528 case Qt::ToolTipRole:
529 case Qt::DecorationRole:
530 return QVariant();
531 }
532 break;
533 }
534 }
535 return QVariant();
536}
537
538bool QgsMapLayerStyleCategoriesModel::setData( const QModelIndex &index, const QVariant &value, int role )
539{
540 if ( !index.isValid() || index.row() >= rowCount() )
541 return false;
542
543 if ( role == Qt::CheckStateRole )
544 {
545 const QgsMapLayer::StyleCategory category = data( index, Qt::UserRole ).value<QgsMapLayer::StyleCategory>();
546 if ( value.value<Qt::CheckState>() == Qt::Checked )
547 {
548 mCategories |= category;
549 emit dataChanged( index, index );
550 return true;
551 }
552 else if ( value.value<Qt::CheckState>() == Qt::Unchecked )
553 {
554 mCategories &= ~category;
555 emit dataChanged( index, index );
556 return true;
557 }
558 }
559 return false;
560}
561
562
563Qt::ItemFlags QgsMapLayerStyleCategoriesModel::flags( const QModelIndex & ) const
564{
565 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
566}
567
569 : QItemDelegate( parent )
570{
571}
572
573void QgsCategoryDisplayLabelDelegate::drawDisplay( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text ) const
574{
575 QLabel label;
576 label.setText( text );
577 label.setEnabled( option.state & QStyle::State_Enabled );
578 label.setAttribute( Qt::WA_TranslucentBackground );
579 label.setMargin( 3 );
580 label.setWordWrap( true );
581 painter->save();
582 painter->translate( rect.topLeft() );
583 label.resize( rect.size() );
584 label.render( painter );
585 painter->restore();
586}
587
588QSize QgsCategoryDisplayLabelDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
589{
590 Q_UNUSED( option )
591 QLabel label;
592 QString display = index.model()->data( index, Qt::DisplayRole ).toString();
593 label.setText( display );
594 label.setWordWrap( true );
595 label.setFixedWidth( option.widget->size().width() );
596 label.setMargin( 3 );
597 return label.sizeHint();
598}
LayerType
Types of layers that can be added to a map.
Definition qgis.h:190
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:198
@ Plugin
Plugin based layer.
Definition qgis.h:193
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
Definition qgis.h:199
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Definition qgis.h:196
@ Vector
Vector layer.
Definition qgis.h:191
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:195
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:194
@ Raster
Raster layer.
Definition qgis.h:192
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Definition qgis.h:197
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.
@ AllVisualStyleCategories
All categories dealing with map canvas rendering.
@ 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.
@ AllAttributeCategories
All categories dealing with attributes and attribute form.
@ Legend
Legend settings.
@ Diagrams
Diagrams.
@ Labeling
Labeling.
const QList< T > qgsEnumList()
Returns a list all enum entries.
Definition qgis.h:6764