QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 <QAbstractItemModel>
23 #include <QSortFilterProxyModel>
24 #include <QIcon>
25 #include <QHash>
26 
27 class QgsSymbol;
28 
29 #ifndef SIP_RUN
30 
43 class CORE_EXPORT QgsAbstractStyleEntityIconGenerator : public QObject
44 {
45  Q_OBJECT
46 
47  public:
48 
53  QgsAbstractStyleEntityIconGenerator( QObject *parent );
54 
59  virtual void generateIcon( QgsStyle *style, QgsStyle::StyleEntity type, const QString &name ) = 0;
60 
66  void setIconSizes( const QList< QSize > &sizes );
67 
73  QList< QSize > iconSizes() const;
74 
75  signals:
76 
81  void iconGenerated( QgsStyle::StyleEntity type, const QString &name, const QIcon &icon );
82 
83  private:
84 
85  QList< QSize > mIconSizes;
86 
87 };
88 
89 #endif
90 
106 class CORE_EXPORT QgsStyleModel: public QAbstractItemModel
107 {
108  Q_OBJECT
109 
110  public:
111 
113  enum Column
114  {
115  Name = 0,
117  };
118 
120  enum Role
121  {
122  TypeRole = Qt::UserRole + 1,
128  };
129 
135  explicit QgsStyleModel( QgsStyle *style, QObject *parent SIP_TRANSFERTHIS = nullptr );
136 
142  QgsStyle *style() { return mStyle; }
143 
144  QVariant data( const QModelIndex &index, int role ) const override;
145  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
146  Qt::ItemFlags flags( const QModelIndex &index ) const override;
147  QVariant headerData( int section, Qt::Orientation orientation,
148  int role = Qt::DisplayRole ) const override;
149  QModelIndex index( int row, int column,
150  const QModelIndex &parent = QModelIndex() ) const override;
151  QModelIndex parent( const QModelIndex &index ) const override;
152  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
153  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
154 
161  void addDesiredIconSize( QSize size );
162 
171  static void setIconGenerator( QgsAbstractStyleEntityIconGenerator *generator ) SIP_SKIP;
172 
173  private slots:
174 
175  void onEntityAdded( QgsStyle::StyleEntity type, const QString &name );
176  void onEntityRemoved( QgsStyle::StyleEntity type, const QString &name );
177  void onEntityChanged( QgsStyle::StyleEntity type, const QString &name );
178  void onEntityRename( QgsStyle::StyleEntity type, const QString &oldName, const QString &newName );
179  void onTagsChanged( int entity, const QString &name, const QStringList &tags );
180  void rebuildSymbolIcons();
181  void iconGenerated( QgsStyle::StyleEntity type, const QString &name, const QIcon &icon );
182 
183  private:
184 
185  QgsStyle *mStyle = nullptr;
186 
187  QHash< QgsStyle::StyleEntity, QStringList > mEntityNames;
188 
189  QList< QSize > mAdditionalSizes;
190  mutable std::unique_ptr< QgsExpressionContext > mExpressionContext;
191 
192  mutable QHash< QgsStyle::StyleEntity, QHash< QString, QIcon > > mIconCache;
193 
194  static QgsAbstractStyleEntityIconGenerator *sIconGenerator;
195  mutable QSet< QString > mPending3dSymbolIcons;
196 
197  QgsStyle::StyleEntity entityTypeFromRow( int row ) const;
198 
199  int offsetForEntity( QgsStyle::StyleEntity entity ) const;
200 
201 };
202 
213 class CORE_EXPORT QgsStyleProxyModel: public QSortFilterProxyModel
214 {
215  Q_OBJECT
216 
217  public:
218 
224  explicit QgsStyleProxyModel( QgsStyle *style, QObject *parent SIP_TRANSFERTHIS = nullptr );
225 
231  explicit QgsStyleProxyModel( QgsStyleModel *model, QObject *parent SIP_TRANSFERTHIS = nullptr );
232 
238  QString filterString() const { return mFilterString; }
239 
246  QgsStyle::StyleEntity entityFilter() const;
247 
255  void setEntityFilter( QgsStyle::StyleEntity filter );
256 
266  void setEntityFilters( const QList<QgsStyle::StyleEntity> &filters ) SIP_SKIP;
267 
274  bool entityFilterEnabled() const;
275 
285  void setEntityFilterEnabled( bool enabled );
286 
295  Qgis::SymbolType symbolType() const;
296 
304  void setSymbolType( Qgis::SymbolType type );
305 
312  bool symbolTypeFilterEnabled() const;
313 
324  void setSymbolTypeFilterEnabled( bool enabled );
325 
334  QgsWkbTypes::GeometryType layerType() const;
335 
342  void setLayerType( QgsWkbTypes::GeometryType type );
343 
352  void setTagId( int id );
353 
361  int tagId() const;
362 
371  void setSmartGroupId( int id );
372 
380  int smartGroupId() const;
381 
382  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
383 
389  bool favoritesOnly() const;
390 
396  void setFavoritesOnly( bool favoritesOnly );
397 
404  void addDesiredIconSize( QSize size );
405 
406  public slots:
407 
414  void setFilterString( const QString &filter );
415 
416  private:
417 
418  void initialize();
419 
420  QgsStyleModel *mModel = nullptr;
421  QgsStyle *mStyle = nullptr;
422 
423  QString mFilterString;
424 
425  int mTagId = -1;
426  QStringList mTaggedSymbolNames;
427 
428  int mSmartGroupId = -1;
429  QStringList mSmartGroupSymbolNames;
430 
431  bool mFavoritesOnly = false;
432 
433  bool mEntityFilterEnabled = false;
434  QList< QgsStyle::StyleEntity > mEntityFilters = QList< QgsStyle::StyleEntity >() << QgsStyle::SymbolEntity;
435 
436  bool mSymbolTypeFilterEnabled = false;
438 
440 
441 };
442 
443 #endif //QGSSTYLEMODEL_H
SymbolType
Symbol types.
Definition: qgis.h:168
@ Marker
Marker symbol.
An abstract base class for icon generators for a QgsStyleModel.
Definition: qgsstylemodel.h:44
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 ...
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.
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
QgsStyle * style()
Returns the style managed by the model.
Role
Custom model roles.
@ LayerTypeRole
Layer type (for label settings entities)
@ CompatibleGeometryTypesRole
Compatible layer geometry types (for 3D symbols)
@ IsFavoriteRole
Whether entity is flagged as a favorite.
@ SymbolTypeRole
Symbol type (for symbol or legend patch shape entities)
@ TagRole
String list of tags.
Column
Model columns.
@ Tags
Tags column.
A QSortFilterProxyModel subclass for showing filtered symbol and color ramps entries from a QgsStyle ...
QString filterString() const
Returns the current filter string, if set.
StyleEntity
Enum for Entities involved in a style.
Definition: qgsstyle.h:179
@ SymbolEntity
Symbols.
Definition: qgsstyle.h:180
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126