QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 onSymbolAdded( const QString &name, QgsSymbol *symbol );
104  void onSymbolRemoved( const QString &name );
105  void onSymbolChanged( const QString &name );
106  void onSymbolRename( const QString &oldName, const QString &newName );
107  void onRampAdded( const QString &name );
108  void onRampRemoved( const QString &name );
109  void onRampChanged( const QString &name );
110  void onRampRename( const QString &oldName, const QString &newName );
111 
112  void onTextFormatAdded( const QString &name );
113  void onTextFormatRemoved( const QString &name );
114  void onTextFormatChanged( const QString &name );
115  void onTextFormatRename( const QString &oldName, const QString &newName );
116 
117  void onLabelSettingsAdded( const QString &name );
118  void onLabelSettingsRemoved( const QString &name );
119  void onLabelSettingsChanged( const QString &name );
120  void onLabelSettingsRename( const QString &oldName, const QString &newName );
121 
122  void onTagsChanged( int entity, const QString &name, const QStringList &tags );
123  void rebuildSymbolIcons();
124 
125  private:
126 
127  QgsStyle *mStyle = nullptr;
128  QStringList mSymbolNames;
129  QStringList mRampNames;
130  QStringList mTextFormatNames;
131  QStringList mLabelSettingsNames;
132  QList< QSize > mAdditionalSizes;
133  mutable std::unique_ptr< QgsExpressionContext > mExpressionContext;
134 
135  mutable QHash< QString, QIcon > mSymbolIconCache;
136  mutable QHash< QString, QIcon > mColorRampIconCache;
137  mutable QHash< QString, QIcon > mTextFormatIconCache;
138  mutable QHash< QString, QIcon > mLabelSettingsIconCache;
139 
140  QgsStyle::StyleEntity entityTypeFromRow( int row ) const;
141 
142 };
143 
154 class CORE_EXPORT QgsStyleProxyModel: public QSortFilterProxyModel
155 {
156  Q_OBJECT
157 
158  public:
159 
165  explicit QgsStyleProxyModel( QgsStyle *style, QObject *parent SIP_TRANSFERTHIS = nullptr );
166 
172  explicit QgsStyleProxyModel( QgsStyleModel *model, QObject *parent SIP_TRANSFERTHIS = nullptr );
173 
179  QString filterString() const { return mFilterString; }
180 
187  QgsStyle::StyleEntity entityFilter() const;
188 
196  void setEntityFilter( QgsStyle::StyleEntity filter );
197 
207  void setEntityFilters( const QList<QgsStyle::StyleEntity> &filters ) SIP_SKIP;
208 
215  bool entityFilterEnabled() const;
216 
226  void setEntityFilterEnabled( bool enabled );
227 
236  QgsSymbol::SymbolType symbolType() const;
237 
245  void setSymbolType( QgsSymbol::SymbolType type );
246 
253  bool symbolTypeFilterEnabled() const;
254 
265  void setSymbolTypeFilterEnabled( bool enabled );
266 
275  QgsWkbTypes::GeometryType layerType() const;
276 
283  void setLayerType( QgsWkbTypes::GeometryType type );
284 
293  void setTagId( int id );
294 
302  int tagId() const;
303 
312  void setSmartGroupId( int id );
313 
321  int smartGroupId() const;
322 
323  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
324 
330  bool favoritesOnly() const;
331 
337  void setFavoritesOnly( bool favoritesOnly );
338 
345  void addDesiredIconSize( QSize size );
346 
347  public slots:
348 
355  void setFilterString( const QString &filter );
356 
357  private:
358 
359  void initialize();
360 
361  QgsStyleModel *mModel = nullptr;
362  QgsStyle *mStyle = nullptr;
363 
364  QString mFilterString;
365 
366  int mTagId = -1;
367  QStringList mTaggedSymbolNames;
368 
369  int mSmartGroupId = -1;
370  QStringList mSmartGroupSymbolNames;
371 
372  bool mFavoritesOnly = false;
373 
374  bool mEntityFilterEnabled = false;
375  QList< QgsStyle::StyleEntity > mEntityFilters = QList< QgsStyle::StyleEntity >() << QgsStyle::SymbolEntity;
376 
377  bool mSymbolTypeFilterEnabled = false;
379 
381 
382 };
383 
384 #endif //QGSSTYLEMODEL_H
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
Definition: qgsstylemodel.h:45
QgsStyle * style()
Returns the style managed by the model.
Definition: qgsstylemodel.h:80
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
Layer type (for label settings entities)
Definition: qgsstylemodel.h:65
Role
Custom model roles.
Definition: qgsstylemodel.h:59
String list of tags.
Definition: qgsstylemodel.h:62
StyleEntity
Enum for Entities involved in a style.
Definition: qgsstyle.h:177
SymbolType
Type of the symbol.
Definition: qgssymbol.h:83
#define SIP_SKIP
Definition: qgis_sip.h:126
QString filterString() const
Returns the current filter string, if set.
Tags column.
Definition: qgsstylemodel.h:55
A QSortFilterProxyModel subclass for showing filtered symbol and color ramps entries from a QgsStyle ...
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:139
Marker symbol.
Definition: qgssymbol.h:85
Whether entity is flagged as a favorite.
Definition: qgsstylemodel.h:64
Column
Model columns.
Definition: qgsstylemodel.h:52
Symbol type (for symbol entities)
Definition: qgsstylemodel.h:63