QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
30 #ifndef SIP_RUN
31 
44 class CORE_EXPORT QgsAbstractStyleEntityIconGenerator : public QObject
45 {
46  Q_OBJECT
47 
48  public:
49 
54  QgsAbstractStyleEntityIconGenerator( QObject *parent );
55 
60  virtual void generateIcon( QgsStyle *style, QgsStyle::StyleEntity type, const QString &name ) = 0;
61 
67  void setIconSizes( const QList< QSize > &sizes );
68 
74  QList< QSize > iconSizes() const;
75 
76  signals:
77 
82  void iconGenerated( QgsStyle::StyleEntity type, const QString &name, const QIcon &icon );
83 
84  private:
85 
86  QList< QSize > mIconSizes;
87 
88 };
89 
90 #endif
91 
107 class CORE_EXPORT QgsStyleModel: public QAbstractItemModel
108 {
109  Q_OBJECT
110 
111  public:
112 
114  enum Column
115  {
116  Name = 0,
118  };
119 
121  enum Role
122  {
123  TypeRole = Qt::UserRole + 1,
129  };
130 
136  explicit QgsStyleModel( QgsStyle *style, QObject *parent SIP_TRANSFERTHIS = nullptr );
137 
143  QgsStyle *style() { return mStyle; }
144 
145  QVariant data( const QModelIndex &index, int role ) const override;
146  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
147  Qt::ItemFlags flags( const QModelIndex &index ) const override;
148  QVariant headerData( int section, Qt::Orientation orientation,
149  int role = Qt::DisplayRole ) const override;
150  QModelIndex index( int row, int column,
151  const QModelIndex &parent = QModelIndex() ) const override;
152  QModelIndex parent( const QModelIndex &index ) const override;
153  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
154  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
155 
162  void addDesiredIconSize( QSize size );
163 
172  static void setIconGenerator( QgsAbstractStyleEntityIconGenerator *generator ) SIP_SKIP;
173 
174  private slots:
175 
176  void onEntityAdded( QgsStyle::StyleEntity type, const QString &name );
177  void onEntityRemoved( QgsStyle::StyleEntity type, const QString &name );
178  void onEntityChanged( QgsStyle::StyleEntity type, const QString &name );
179  void onEntityRename( QgsStyle::StyleEntity type, const QString &oldName, const QString &newName );
180  void onTagsChanged( int entity, const QString &name, const QStringList &tags );
181  void rebuildSymbolIcons();
182  void iconGenerated( QgsStyle::StyleEntity type, const QString &name, const QIcon &icon );
183 
184  private:
185 
186  QgsStyle *mStyle = nullptr;
187 
188  QHash< QgsStyle::StyleEntity, QStringList > mEntityNames;
189 
190  QList< QSize > mAdditionalSizes;
191  mutable std::unique_ptr< QgsExpressionContext > mExpressionContext;
192 
193  mutable QHash< QgsStyle::StyleEntity, QHash< QString, QIcon > > mIconCache;
194 
195  static QgsAbstractStyleEntityIconGenerator *sIconGenerator;
196  mutable QSet< QString > mPending3dSymbolIcons;
197 
198  QgsStyle::StyleEntity entityTypeFromRow( int row ) const;
199 
200  int offsetForEntity( QgsStyle::StyleEntity entity ) const;
201 
202 };
203 
214 class CORE_EXPORT QgsStyleProxyModel: public QSortFilterProxyModel
215 {
216  Q_OBJECT
217 
218  public:
219 
225  explicit QgsStyleProxyModel( QgsStyle *style, QObject *parent SIP_TRANSFERTHIS = nullptr );
226 
232  explicit QgsStyleProxyModel( QgsStyleModel *model, QObject *parent SIP_TRANSFERTHIS = nullptr );
233 
239  QString filterString() const { return mFilterString; }
240 
247  QgsStyle::StyleEntity entityFilter() const;
248 
256  void setEntityFilter( QgsStyle::StyleEntity filter );
257 
267  void setEntityFilters( const QList<QgsStyle::StyleEntity> &filters ) SIP_SKIP;
268 
275  bool entityFilterEnabled() const;
276 
286  void setEntityFilterEnabled( bool enabled );
287 
296  QgsSymbol::SymbolType symbolType() const;
297 
305  void setSymbolType( QgsSymbol::SymbolType type );
306 
313  bool symbolTypeFilterEnabled() const;
314 
325  void setSymbolTypeFilterEnabled( bool enabled );
326 
335  QgsWkbTypes::GeometryType layerType() const;
336 
343  void setLayerType( QgsWkbTypes::GeometryType type );
344 
353  void setTagId( int id );
354 
362  int tagId() const;
363 
372  void setSmartGroupId( int id );
373 
381  int smartGroupId() const;
382 
383  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
384 
390  bool favoritesOnly() const;
391 
397  void setFavoritesOnly( bool favoritesOnly );
398 
405  void addDesiredIconSize( QSize size );
406 
407  public slots:
408 
415  void setFilterString( const QString &filter );
416 
417  private:
418 
419  void initialize();
420 
421  QgsStyleModel *mModel = nullptr;
422  QgsStyle *mStyle = nullptr;
423 
424  QString mFilterString;
425 
426  int mTagId = -1;
427  QStringList mTaggedSymbolNames;
428 
429  int mSmartGroupId = -1;
430  QStringList mSmartGroupSymbolNames;
431 
432  bool mFavoritesOnly = false;
433 
434  bool mEntityFilterEnabled = false;
435  QList< QgsStyle::StyleEntity > mEntityFilters = QList< QgsStyle::StyleEntity >() << QgsStyle::SymbolEntity;
436 
437  bool mSymbolTypeFilterEnabled = false;
439 
441 
442 };
443 
444 #endif //QGSSTYLEMODEL_H
QgsStyleModel
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
Definition: qgsstylemodel.h:108
QgsStyleModel::IsFavoriteRole
@ IsFavoriteRole
Whether entity is flagged as a favorite.
Definition: qgsstylemodel.h:126
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
QgsStyleModel::Column
Column
Model columns.
Definition: qgsstylemodel.h:115
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:117
QgsStyleModel::TagRole
@ TagRole
String list of tags.
Definition: qgsstylemodel.h:124
QgsStyleModel::style
QgsStyle * style()
Returns the style managed by the model.
Definition: qgsstylemodel.h:143
qgis_sip.h
QgsStyleProxyModel::filterString
QString filterString() const
Returns the current filter string, if set.
Definition: qgsstylemodel.h:239
QgsStyleModel::CompatibleGeometryTypesRole
@ CompatibleGeometryTypesRole
Compatible layer geometry types (for 3D symbols)
Definition: qgsstylemodel.h:128
QgsStyleModel::Role
Role
Custom model roles.
Definition: qgsstylemodel.h:122
qgsstyle.h
QgsStyleModel::SymbolTypeRole
@ SymbolTypeRole
Symbol type (for symbol or legend patch shape entities)
Definition: qgsstylemodel.h:125
QgsAbstractStyleEntityIconGenerator::generateIcon
virtual void generateIcon(QgsStyle *style, QgsStyle::StyleEntity type, const QString &name)=0
Triggers generation of an icon for an entity from the specified style database, with matching entity ...
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
QgsStyleModel::LayerTypeRole
@ LayerTypeRole
Layer type (for label settings entities)
Definition: qgsstylemodel.h:127
QgsAbstractStyleEntityIconGenerator::iconGenerated
void iconGenerated(QgsStyle::StyleEntity type, const QString &name, const QIcon &icon)
Emitted when the icon for the style entity with matching type and name has been generated.
QgsStyle
Definition: qgsstyle.h:160
QgsWkbTypes::UnknownGeometry
@ UnknownGeometry
Definition: qgswkbtypes.h:145
QgsSymbol::Marker
@ Marker
Marker symbol.
Definition: qgssymbol.h:87
QgsAbstractStyleEntityIconGenerator
An abstract base class for icon generators for a QgsStyleModel.
Definition: qgsstylemodel.h:45
QgsSymbol::SymbolType
SymbolType
Type of the symbol.
Definition: qgssymbol.h:86
QgsStyleProxyModel
A QSortFilterProxyModel subclass for showing filtered symbol and color ramps entries from a QgsStyle ...
Definition: qgsstylemodel.h:215
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:179