QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
38 break;
39
41 mCategoryList
55 break;
62 // not yet handled by the model
63 break;
64 }
65
66 // move the grouped categories to top
67 int idxCategory = mCategoryList.indexOf( QgsMapLayer::AllAttributeCategories );
68 if ( idxCategory > 0 )
69 {
70 mCategoryList.move( idxCategory, 0 );
71 }
72 idxCategory = mCategoryList.indexOf( QgsMapLayer::AllVisualStyleCategories );
73 if ( idxCategory > 0 )
74 {
75 mCategoryList.move( idxCategory, 0 );
76 }
77 idxCategory = mCategoryList.indexOf( QgsMapLayer::AllStyleCategories );
78 if ( idxCategory > 0 )
79 {
80 mCategoryList.move( idxCategory, 0 );
81 }
82}
83
85{
86 if ( mCategories == categories )
87 return;
88
89 // filter the categories and only preserve the categories supported by the current layer type
90 QgsMapLayer::StyleCategories allowedCategories;
91 for ( QgsMapLayer::StyleCategory category : std::as_const( mCategoryList ) )
92 {
93 allowedCategories |= category;
94 }
95 categories &= allowedCategories;
96
97 beginResetModel();
98 mCategories = categories;
99 endResetModel();
100}
101
106
108{
109 beginResetModel();
110 mShowAllCategories = showAll;
111 endResetModel();
112}
113
114int QgsMapLayerStyleCategoriesModel::rowCount( const QModelIndex & ) const
115{
116 int count = mCategoryList.count();
117
118 if ( count > 0 && !mShowAllCategories )
119 {
120 if ( mCategoryList.contains( QgsMapLayer::AllAttributeCategories ) )
121 {
122 count--;
123 }
124 if ( mCategoryList.contains( QgsMapLayer::AllVisualStyleCategories ) )
125 {
126 count--;
127 }
128 if ( mCategoryList.contains( QgsMapLayer::AllStyleCategories ) )
129 {
130 count--;
131 }
132 }
133
134 return count;
135}
136
137int QgsMapLayerStyleCategoriesModel::columnCount( const QModelIndex & ) const
138{
139 return 1;
140}
141
142QVariant QgsMapLayerStyleCategoriesModel::data( const QModelIndex &index, int role ) const
143{
144 const int filteredRowCount = rowCount();
145
146 if ( !index.isValid() || index.row() >= filteredRowCount )
147 return QVariant();
148
149 const QgsMapLayer::StyleCategory category = mCategoryList.at( index.row() + ( mShowAllCategories ? 0 : mCategoryList.count() - filteredRowCount ) );
150
151 if ( role == Qt::UserRole )
152 {
153 return category;
154 }
155 if ( role == Qt::CheckStateRole )
156 {
157 return mCategories.testFlag( category ) ? Qt::Checked : Qt::Unchecked;
158 }
159
160 QString htmlStylePattern = u"<p><b>%1</b><br/><span style='color:gray;'>%2</span></p>"_s;
161 switch ( category )
162 {
164 {
165 QString name = tr( "Layer Configuration" );
166 QString description = tr( "The layers display expression and the datasource flags: identifiable, removable, searchable, read-only and hidden from the project settings" );
167 switch ( role )
168 {
169 case static_cast<int>( Role::NameRole ):
170 return name;
171 case Qt::DisplayRole:
172 return htmlStylePattern.arg( name ).arg( description );
173 case Qt::ToolTipRole:
174 return description;
175 case Qt::DecorationRole:
176 return QgsApplication::getThemeIcon( u"/propertyicons/layerconfiguration.svg"_s );
177 }
178 break;
179 }
181 {
182 QString name = tr( "Symbology" );
183 QString description = tr( "Everything from the symbology 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( u"/propertyicons/symbology.svg"_s );
195 }
196 break;
197 }
199 {
200 QString name = tr( "3D Symbology" );
201 QString description = tr( "Everything from the 3D symbology section" );
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( u"/3d.svg"_s );
213 }
214 break;
215 }
217 {
218 QString name = tr( "Labels" );
219 QString description = tr( "Everything from the labels section" );
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( u"/propertyicons/labels.svg"_s );
231 }
232 break;
233 }
235 {
236 QString name = tr( "Fields" );
237 QString description = tr( "Virtual fields, aliases, default value expressions and constraints from the form section and WMS/WFS exposure" );
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( u"/propertyicons/sourcefieldsandforms.svg"_s );
249 }
250 break;
251 }
253 {
254 QString name = tr( "Attribute Form" );
255 QString description = tr( "Form layout and widget configuration (no constraints and default value expressions)" );
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( u"/mActionFormView.svg"_s );
267 }
268 break;
269 }
271 {
272 QString name = tr( "Actions" );
273 QString description = tr( "Everything from the actions 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( u"/propertyicons/action.svg"_s );
285 }
286 break;
287 }
289 {
290 QString name = tr( "Map Tips" );
291 QString description = tr( "Map tips settings (no layer display expression)" );
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( u"/propertyicons/display.svg"_s );
303 }
304 break;
305 }
307 {
308 QString name = tr( "Diagrams" );
309 QString description = tr( "Everything from the diagram section" );
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( u"/propertyicons/diagram.svg"_s );
321 }
322 break;
323 }
325 {
326 QString name = tr( "Attribute Table Configuration" );
327 QString description = tr( "Attribute table settings: choice and order of columns and conditional styling" );
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( u"/mActionOpenTable.svg"_s );
339 }
340 break;
341 }
343 {
344 QString name = tr( "Rendering" );
345 QString description = tr( "Everything from the rendering section: Scale visibility, simplify method, opacity, auto refresh etc." );
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( u"/propertyicons/rendering.svg"_s );
357 }
358 break;
359 }
361 {
362 QString name = tr( "Custom Properties" );
363 QString description = tr( "Layer variables and embedded legend widgets as well as all the custom properties (often used by plugins and custom python code)" );
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( u"/mActionOptions.svg"_s );
375 }
376 break;
377 }
379 {
380 QString name = tr( "Geometry Options" );
381 QString description = tr( "Geometry constraints and validity checks" );
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( u"/propertyicons/digitizing.svg"_s );
393 }
394 break;
395 }
397 {
398 QString name = tr( "Relations" );
399 QString description = tr( "The relations this layer has with other layers" );
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( u"/propertyicons/relations.svg"_s );
411 }
412 break;
413 }
415 {
416 QString name = tr( "Temporal Properties" );
417 QString description = tr( "Everything from the temporal 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( u"/propertyicons/temporal.svg"_s );
429 }
430 break;
431 }
433 {
434 QString name = tr( "Legend Settings" );
435 QString description = tr( "Legend settings (no embedded legend widgets)" );
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( u"/legend.svg"_s );
447 }
448 break;
449 }
451 {
452 QString name = tr( "Elevation Properties" );
453 QString description = tr( "Everything from the elevation section" );
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 description;
463 case Qt::DecorationRole:
464 return QgsApplication::getThemeIcon( u"/propertyicons/elevationscale.svg"_s );
465 }
466 break;
467 }
469 {
470 QString name = tr( "Notes" );
471 QString description = tr( "The layer notes" );
472 switch ( role )
473 {
474 case static_cast<int>( Role::NameRole ):
475 return name;
476 case Qt::DisplayRole:
477
478 return htmlStylePattern.arg( name ).arg( description );
479 case Qt::ToolTipRole:
480 return description;
481 case Qt::DecorationRole:
482 return QgsApplication::getThemeIcon( u"/propertyicons/notes.svg"_s );
483 }
484 break;
485 }
487 {
488 QString name = tr( "All Fields and Attribute Form Categories" );
489 QString description = tr( "All fields and attribute form categories" );
490 switch ( role )
491 {
492 case static_cast<int>( Role::NameRole ):
493 return name;
494 case Qt::DisplayRole:
495 return htmlStylePattern.arg( name ).arg( description );
496 case Qt::ToolTipRole:
497 return description;
498 case Qt::DecorationRole:
499 return QVariant();
500 }
501 break;
502 }
504 {
505 QString name = tr( "All Symbology and Labeling Categories" );
506 QString description = mLayerType == Qgis::LayerType::Vector ? tr( "All symbology, labeling and diagram categories" ) : tr( "All symbology and labeling categories" );
507 switch ( role )
508 {
509 case static_cast<int>( Role::NameRole ):
510 return name;
511 case Qt::DisplayRole:
512 return htmlStylePattern.arg( name ).arg( description );
513 case Qt::ToolTipRole:
514 return description;
515 case Qt::DecorationRole:
516 return QVariant();
517 }
518 break;
519 }
521 {
522 QString name = tr( "All Style Categories" );
523 QString description = tr( "All style categories" );
524 switch ( role )
525 {
526 case static_cast<int>( Role::NameRole ):
527 return name;
528 case Qt::DisplayRole:
529 return htmlStylePattern.arg( name ).arg( description );
530 case Qt::ToolTipRole:
531 case Qt::DecorationRole:
532 return QVariant();
533 }
534 break;
535 }
536 }
537 return QVariant();
538}
539
540bool QgsMapLayerStyleCategoriesModel::setData( const QModelIndex &index, const QVariant &value, int role )
541{
542 if ( !index.isValid() || index.row() >= rowCount() )
543 return false;
544
545 if ( role == Qt::CheckStateRole )
546 {
547 const QgsMapLayer::StyleCategory category = data( index, Qt::UserRole ).value<QgsMapLayer::StyleCategory>();
548 if ( value.value<Qt::CheckState>() == Qt::Checked )
549 {
550 mCategories |= category;
551 emit dataChanged( index, index );
552 return true;
553 }
554 else if ( value.value<Qt::CheckState>() == Qt::Unchecked )
555 {
556 mCategories &= ~category;
557 emit dataChanged( index, index );
558 return true;
559 }
560 }
561 return false;
562}
563
564
565Qt::ItemFlags QgsMapLayerStyleCategoriesModel::flags( const QModelIndex & ) const
566{
567 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
568}
569
571 : QItemDelegate( parent )
572{}
573
574void QgsCategoryDisplayLabelDelegate::drawDisplay( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text ) const
575{
576 QLabel label;
577 label.setText( text );
578 label.setEnabled( option.state & QStyle::State_Enabled );
579 label.setAttribute( Qt::WA_TranslucentBackground );
580 label.setMargin( 3 );
581 label.setWordWrap( true );
582 painter->save();
583 painter->translate( rect.topLeft() );
584 label.resize( rect.size() );
585 label.render( painter );
586 painter->restore();
587}
588
589QSize QgsCategoryDisplayLabelDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
590{
591 Q_UNUSED( option )
592 QLabel label;
593 QString display = index.model()->data( index, Qt::DisplayRole ).toString();
594 label.setText( display );
595 label.setWordWrap( true );
596 label.setFixedWidth( option.widget->size().width() );
597 label.setMargin( 3 );
598 return label.sizeHint();
599}
LayerType
Types of layers that can be added to a map.
Definition qgis.h:206
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:214
@ Plugin
Plugin based layer.
Definition qgis.h:209
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
Definition qgis.h:215
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Definition qgis.h:212
@ Vector
Vector layer.
Definition qgis.h:207
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:211
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:210
@ Raster
Raster layer.
Definition qgis.h:208
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Definition qgis.h:213
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:7123