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