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