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