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