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