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