QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 : [email protected]
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 "qgsapplication.h"
18 
20  : QAbstractListModel( parent )
21 {
22  switch ( type )
23  {
25  mCategoryList = qgsEnumMap<QgsMapLayer::StyleCategory>().keys();
26  break;
27 
29  mCategoryList << QgsMapLayer::StyleCategory::Symbology << QgsMapLayer::StyleCategory::Labeling << QgsMapLayer::StyleCategory::AllStyleCategories;
30  break;
31 
38  // not yet handled by the model
39  break;
40  }
41 
42  // move All categories to top
43  mCategoryList.move( mCategoryList.indexOf( QgsMapLayer::AllStyleCategories ), 0 );
44 }
45 
46 void QgsMapLayerStyleCategoriesModel::setCategories( QgsMapLayer::StyleCategories categories )
47 {
48  if ( mCategories == categories )
49  return;
50 
51  beginResetModel();
52  mCategories = categories;
53  endResetModel();
54 }
55 
56 QgsMapLayer::StyleCategories QgsMapLayerStyleCategoriesModel::categories() const
57 {
58  return mCategories;
59 }
60 
62 {
63  beginResetModel();
64  mShowAllCategories = showAll;
65  endResetModel();
66 }
67 
68 int QgsMapLayerStyleCategoriesModel::rowCount( const QModelIndex & ) const
69 {
70  int count = mCategoryList.count();
71  if ( !mShowAllCategories )
72  count--;
73  return count;
74 }
75 
76 int QgsMapLayerStyleCategoriesModel::columnCount( const QModelIndex & ) const
77 {
78  return 1;
79 }
80 
81 QVariant QgsMapLayerStyleCategoriesModel::data( const QModelIndex &index, int role ) const
82 {
83  if ( !index.isValid() || index.row() >= rowCount() )
84  return QVariant();
85 
86  const QgsMapLayer::StyleCategory category = mCategoryList.at( index.row() + ( mShowAllCategories ? 0 : 1 ) );
87 
88  if ( role == Qt::UserRole )
89  {
90  return category;
91  }
92  if ( role == Qt::CheckStateRole )
93  {
94  return mCategories.testFlag( category ) ? Qt::Checked : Qt::Unchecked;
95  }
96 
97  switch ( category )
98  {
99  case QgsMapLayer::StyleCategory::LayerConfiguration:
100  switch ( role )
101  {
102  case Qt::DisplayRole:
103  return tr( "Layer Configuration" );
104  case Qt::ToolTipRole:
105  return tr( "Identifiable, removable, searchable, display expression, read-only, hidden" );
106  case Qt::DecorationRole:
107  return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/system.svg" ) );
108  }
109  break;
110  case QgsMapLayer::StyleCategory::Symbology:
111  switch ( role )
112  {
113  case Qt::DisplayRole:
114  return tr( "Symbology" );
115  case Qt::ToolTipRole:
116  return QVariant();
117  case Qt::DecorationRole:
118  return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/symbology.svg" ) );
119  }
120  break;
121  case QgsMapLayer::StyleCategory::Symbology3D:
122  switch ( role )
123  {
124  case Qt::DisplayRole:
125  return tr( "3D Symbology" );
126  case Qt::ToolTipRole:
127  return QVariant();
128  case Qt::DecorationRole:
129  return QgsApplication::getThemeIcon( QStringLiteral( "/3d.svg" ) );
130  }
131  break;
132  case QgsMapLayer::StyleCategory::Labeling:
133  switch ( role )
134  {
135  case Qt::DisplayRole:
136  return tr( "Labels" );
137  case Qt::ToolTipRole:
138  return QVariant();
139  case Qt::DecorationRole:
140  return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/labels.svg" ) );
141  }
142  break;
143  case QgsMapLayer::StyleCategory::Fields:
144  switch ( role )
145  {
146  case Qt::DisplayRole:
147  return tr( "Fields" );
148  case Qt::ToolTipRole:
149  return tr( "Aliases, widgets, WMS/WFS, expressions, constraints, virtual fields" );
150  case Qt::DecorationRole:
151  return QgsApplication::getThemeIcon( QStringLiteral( "/mSourceFields.svg" ) );
152  }
153  break;
154  case QgsMapLayer::StyleCategory::Forms:
155  switch ( role )
156  {
157  case Qt::DisplayRole:
158  return tr( "Forms" );
159  case Qt::ToolTipRole:
160  return QVariant();
161  case Qt::DecorationRole:
162  return QgsApplication::getThemeIcon( QStringLiteral( "/mActionFormView.svg" ) );
163  }
164  break;
165  case QgsMapLayer::StyleCategory::Actions:
166  switch ( role )
167  {
168  case Qt::DisplayRole:
169  return tr( "Actions" );
170  case Qt::ToolTipRole:
171  return QVariant();
172  case Qt::DecorationRole:
173  return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/action.svg" ) );
174  }
175  break;
176  case QgsMapLayer::StyleCategory::MapTips:
177  switch ( role )
178  {
179  case Qt::DisplayRole:
180  return tr( "Map Tips" );
181  case Qt::ToolTipRole:
182  return QVariant();
183  case Qt::DecorationRole:
184  return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/display.svg" ) );
185  }
186  break;
187  case QgsMapLayer::StyleCategory::Diagrams:
188  switch ( role )
189  {
190  case Qt::DisplayRole:
191  return tr( "Diagrams" );
192  case Qt::ToolTipRole:
193  return QVariant();
194  case Qt::DecorationRole:
195  return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/diagram.svg" ) );
196  }
197  break;
198  case QgsMapLayer::StyleCategory::AttributeTable:
199  switch ( role )
200  {
201  case Qt::DisplayRole:
202  return tr( "Attribute Table Settings" );
203  case Qt::ToolTipRole:
204  return tr( "Choice and order of columns, conditional styling" );
205  case Qt::DecorationRole:
206  return QgsApplication::getThemeIcon( QStringLiteral( "/mActionOpenTable.svg" ) );
207  }
208  break;
209  case QgsMapLayer::StyleCategory::Rendering:
210  switch ( role )
211  {
212  case Qt::DisplayRole:
213  return tr( "Rendering" );
214  case Qt::ToolTipRole:
215  return tr( "Scale visibility, simplify method, opacity" );
216  case Qt::DecorationRole:
217  return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/rendering.svg" ) );
218  }
219  break;
220  case QgsMapLayer::StyleCategory::CustomProperties:
221  switch ( role )
222  {
223  case Qt::DisplayRole:
224  return tr( "Custom Properties" );
225  case Qt::ToolTipRole:
226  return QVariant();
227  case Qt::DecorationRole:
228  return QgsApplication::getThemeIcon( QStringLiteral( "/mActionOptions.svg" ) );
229  }
230  break;
231  case QgsMapLayer::StyleCategory::GeometryOptions:
232  switch ( role )
233  {
234  case Qt::DisplayRole:
235  return tr( "Geometry Options" );
236  case Qt::ToolTipRole:
237  return tr( "Geometry constraints and validity checks" );
238  case Qt::DecorationRole:
239  return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/digitizing.svg" ) );
240  }
241  break;
242  case QgsMapLayer::StyleCategory::Relations:
243  switch ( role )
244  {
245  case Qt::DisplayRole:
246  return tr( "Relations" );
247  case Qt::ToolTipRole:
248  return tr( "Relations with other layers" );
249  case Qt::DecorationRole:
250  return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/relations.svg" ) );
251  }
252  break;
253 
254  case QgsMapLayer::StyleCategory::Temporal:
255  switch ( role )
256  {
257  case Qt::DisplayRole:
258  return tr( "Temporal Properties" );
259  case Qt::ToolTipRole:
260  return tr( "Temporal properties" );
261  case Qt::DecorationRole:
262  return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/temporal.svg" ) );
263  }
264  break;
265 
266  case QgsMapLayer::StyleCategory::Legend:
267  switch ( role )
268  {
269  case Qt::DisplayRole:
270  return tr( "Legend Settings" );
271  case Qt::ToolTipRole:
272  return tr( "Legend settings" );
273  case Qt::DecorationRole:
274  return QgsApplication::getThemeIcon( QStringLiteral( "/legend.svg" ) );
275  }
276  break;
277 
278  case QgsMapLayer::StyleCategory::Elevation:
279  switch ( role )
280  {
281  case Qt::DisplayRole:
282  return tr( "Elevation Properties" );
283  case Qt::ToolTipRole:
284  return tr( "Elevation properties" );
285  case Qt::DecorationRole:
286  return QIcon(); // TODO
287  }
288  break;
289 
290  case QgsMapLayer::StyleCategory::Notes:
291  switch ( role )
292  {
293  case Qt::DisplayRole:
294  case Qt::ToolTipRole:
295  return tr( "Notes" );
296  case Qt::DecorationRole:
297  return QIcon(); // TODO
298  }
299  break;
300 
301  case QgsMapLayer::StyleCategory::AllStyleCategories:
302  switch ( role )
303  {
304  case Qt::DisplayRole:
305  return tr( "All Style Categories" );
306  case Qt::ToolTipRole:
307  return QVariant();
308  case Qt::DecorationRole:
309  return QVariant();
310  }
311  break;
312 
313  }
314  return QVariant();
315 }
316 
317 bool QgsMapLayerStyleCategoriesModel::setData( const QModelIndex &index, const QVariant &value, int role )
318 {
319  if ( !index.isValid() || index.row() >= rowCount() )
320  return false;
321 
322  if ( role == Qt::CheckStateRole )
323  {
324  const QgsMapLayer::StyleCategory category = data( index, Qt::UserRole ).value<QgsMapLayer::StyleCategory>();
325  if ( value.value<Qt::CheckState>() == Qt::Checked )
326  {
327  mCategories |= category;
328  emit dataChanged( index, index );
329  return true;
330  }
331  else if ( value.value<Qt::CheckState>() == Qt::Unchecked )
332  {
333  mCategories &= ~category;
334  emit dataChanged( index, index );
335  return true;
336  }
337  }
338  return false;
339 }
340 
341 
342 Qt::ItemFlags QgsMapLayerStyleCategoriesModel::flags( const QModelIndex & ) const
343 {
344  return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
345 }
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
void setCategories(QgsMapLayer::StyleCategories categories)
Reset the model data.
int columnCount(const QModelIndex &=QModelIndex()) const override
Qt::ItemFlags flags(const QModelIndex &) const override
QVariant data(const QModelIndex &index, int role) const override
QgsMapLayerStyleCategoriesModel(QgsMapLayerType type, QObject *parent=nullptr)
Constructor for QgsMapLayerStyleCategoriesModel, for the specified layer type.
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
StyleCategory
Categories of style to distinguish appropriate sections for import/export.
Definition: qgsmaplayer.h:159
@ AllStyleCategories
Definition: qgsmaplayer.h:178
QgsMapLayerType
Types of layers that can be added to a map.
Definition: qgis.h:47
@ PointCloudLayer
Point cloud layer. Added in QGIS 3.18.
@ MeshLayer
Mesh layer. Added in QGIS 3.2.
@ VectorLayer
Vector layer.
@ RasterLayer
Raster layer.
@ GroupLayer
Composite group layer. Added in QGIS 3.24.
@ VectorTileLayer
Vector tile layer. Added in QGIS 3.14.
@ AnnotationLayer
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ PluginLayer
Plugin based layer.