QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsstylemodel.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstylemodel.h
3  ---------------
4  begin : September 2018
5  copyright : (C) 2018 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
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 
16 #ifndef QGSSTYLEMODEL_H
17 #define QGSSTYLEMODEL_H
18 
19 #include "qgis_core.h"
20 #include "qgis_sip.h"
21 #include "qgsstyle.h"
22 #include "qgssymbol.h"
23 #include <QAbstractItemModel>
24 #include <QSortFilterProxyModel>
25 #include <QIcon>
26 #include <QHash>
27 
28 class QgsSymbol;
29 
45 class CORE_EXPORT QgsStyleModel: public QAbstractItemModel
46 {
47  Q_OBJECT
48 
49  public:
50 
52  enum Column
53  {
54  Name = 0,
55  Tags,
56  };
57 
59  enum Role
60  {
61  TypeRole = Qt::UserRole + 1,
66  };
67 
73  explicit QgsStyleModel( QgsStyle *style, QObject *parent SIP_TRANSFERTHIS = nullptr );
74 
80  QgsStyle *style() { return mStyle; }
81 
82  QVariant data( const QModelIndex &index, int role ) const override;
83  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
84  Qt::ItemFlags flags( const QModelIndex &index ) const override;
85  QVariant headerData( int section, Qt::Orientation orientation,
86  int role = Qt::DisplayRole ) const override;
87  QModelIndex index( int row, int column,
88  const QModelIndex &parent = QModelIndex() ) const override;
89  QModelIndex parent( const QModelIndex &index ) const override;
90  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
91  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
92 
99  void addDesiredIconSize( QSize size );
100 
101  private slots:
102 
103  void onEntityAdded( QgsStyle::StyleEntity type, const QString &name );
104  void onEntityRemoved( QgsStyle::StyleEntity type, const QString &name );
105  void onEntityChanged( QgsStyle::StyleEntity type, const QString &name );
106  void onEntityRename( QgsStyle::StyleEntity type, const QString &oldName, const QString &newName );
107  void onTagsChanged( int entity, const QString &name, const QStringList &tags );
108  void rebuildSymbolIcons();
109 
110  private:
111 
112  QgsStyle *mStyle = nullptr;
113 
114  QHash< QgsStyle::StyleEntity, QStringList > mEntityNames;
115 
116  QList< QSize > mAdditionalSizes;
117  mutable std::unique_ptr< QgsExpressionContext > mExpressionContext;
118 
119  mutable QHash< QgsStyle::StyleEntity, QHash< QString, QIcon > > mIconCache;
120 
121  QgsStyle::StyleEntity entityTypeFromRow( int row ) const;
122 
123  int offsetForEntity( QgsStyle::StyleEntity entity ) const;
124 
125 };
126 
137 class CORE_EXPORT QgsStyleProxyModel: public QSortFilterProxyModel
138 {
139  Q_OBJECT
140 
141  public:
142 
148  explicit QgsStyleProxyModel( QgsStyle *style, QObject *parent SIP_TRANSFERTHIS = nullptr );
149 
155  explicit QgsStyleProxyModel( QgsStyleModel *model, QObject *parent SIP_TRANSFERTHIS = nullptr );
156 
162  QString filterString() const { return mFilterString; }
163 
170  QgsStyle::StyleEntity entityFilter() const;
171 
179  void setEntityFilter( QgsStyle::StyleEntity filter );
180 
190  void setEntityFilters( const QList<QgsStyle::StyleEntity> &filters ) SIP_SKIP;
191 
198  bool entityFilterEnabled() const;
199 
209  void setEntityFilterEnabled( bool enabled );
210 
219  QgsSymbol::SymbolType symbolType() const;
220 
228  void setSymbolType( QgsSymbol::SymbolType type );
229 
236  bool symbolTypeFilterEnabled() const;
237 
248  void setSymbolTypeFilterEnabled( bool enabled );
249 
258  QgsWkbTypes::GeometryType layerType() const;
259 
266  void setLayerType( QgsWkbTypes::GeometryType type );
267 
276  void setTagId( int id );
277 
285  int tagId() const;
286 
295  void setSmartGroupId( int id );
296 
304  int smartGroupId() const;
305 
306  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
307 
313  bool favoritesOnly() const;
314 
320  void setFavoritesOnly( bool favoritesOnly );
321 
328  void addDesiredIconSize( QSize size );
329 
330  public slots:
331 
338  void setFilterString( const QString &filter );
339 
340  private:
341 
342  void initialize();
343 
344  QgsStyleModel *mModel = nullptr;
345  QgsStyle *mStyle = nullptr;
346 
347  QString mFilterString;
348 
349  int mTagId = -1;
350  QStringList mTaggedSymbolNames;
351 
352  int mSmartGroupId = -1;
353  QStringList mSmartGroupSymbolNames;
354 
355  bool mFavoritesOnly = false;
356 
357  bool mEntityFilterEnabled = false;
358  QList< QgsStyle::StyleEntity > mEntityFilters = QList< QgsStyle::StyleEntity >() << QgsStyle::SymbolEntity;
359 
360  bool mSymbolTypeFilterEnabled = false;
362 
364 
365 };
366 
367 #endif //QGSSTYLEMODEL_H
QgsStyleModel
Definition: qgsstylemodel.h:45
QgsStyleModel::IsFavoriteRole
@ IsFavoriteRole
Whether entity is flagged as a favorite.
Definition: qgsstylemodel.h:64
QgsSymbol
Definition: qgssymbol.h:63
QgsStyleModel::Column
Column
Model columns.
Definition: qgsstylemodel.h:52
QgsStyle::SymbolEntity
@ SymbolEntity
Symbols.
Definition: qgsstyle.h:180
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsStyleModel::Tags
@ Tags
Tags column.
Definition: qgsstylemodel.h:55
QgsStyleModel::TagRole
@ TagRole
String list of tags.
Definition: qgsstylemodel.h:62
QgsStyleModel::style
QgsStyle * style()
Returns the style managed by the model.
Definition: qgsstylemodel.h:80
qgis_sip.h
QgsStyleProxyModel::filterString
QString filterString() const
Returns the current filter string, if set.
Definition: qgsstylemodel.h:162
QgsStyleModel::Role
Role
Custom model roles.
Definition: qgsstylemodel.h:59
qgsstyle.h
QgsStyleModel::SymbolTypeRole
@ SymbolTypeRole
Symbol type (for symbol or legend patch shape entities)
Definition: qgsstylemodel.h:63
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:139
QgsStyleModel::LayerTypeRole
@ LayerTypeRole
Layer type (for label settings entities)
Definition: qgsstylemodel.h:65
QgsStyle
Definition: qgsstyle.h:159
QgsWkbTypes::UnknownGeometry
@ UnknownGeometry
Definition: qgswkbtypes.h:144
QgsSymbol::Marker
@ Marker
Marker symbol.
Definition: qgssymbol.h:87
QgsSymbol::SymbolType
SymbolType
Type of the symbol.
Definition: qgssymbol.h:85
QgsStyleProxyModel
Definition: qgsstylemodel.h:137
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
qgssymbol.h
QgsStyle::StyleEntity
StyleEntity
Enum for Entities involved in a style.
Definition: qgsstyle.h:178