QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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;
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,
133  };
134 
140  explicit QgsStyleModel( QgsStyle *style, QObject *parent SIP_TRANSFERTHIS = nullptr );
141 
147  QgsStyle *style() { return mStyle; }
148 
149  QVariant data( const QModelIndex &index, int role ) const override;
150  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
151  Qt::ItemFlags flags( const QModelIndex &index ) const override;
152  QVariant headerData( int section, Qt::Orientation orientation,
153  int role = Qt::DisplayRole ) const override;
154  QModelIndex index( int row, int column,
155  const QModelIndex &parent = QModelIndex() ) const override;
156  QModelIndex parent( const QModelIndex &index ) const override;
157  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
158  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
159 
166  void addDesiredIconSize( QSize size );
167 
176  static void setIconGenerator( QgsAbstractStyleEntityIconGenerator *generator ) SIP_SKIP;
177 
178  private slots:
179 
180  void onEntityAdded( QgsStyle::StyleEntity type, const QString &name );
181  void onEntityRemoved( QgsStyle::StyleEntity type, const QString &name );
182  void onEntityChanged( QgsStyle::StyleEntity type, const QString &name );
183  void onFavoriteChanged( QgsStyle::StyleEntity type, const QString &name, bool isFavorite );
184  void onEntityRename( QgsStyle::StyleEntity type, const QString &oldName, const QString &newName );
185  void onTagsChanged( int entity, const QString &name, const QStringList &tags );
186  void rebuildSymbolIcons();
187  void iconGenerated( QgsStyle::StyleEntity type, const QString &name, const QIcon &icon );
188 
189  private:
190 
191  QgsStyle *mStyle = nullptr;
192 
193  QHash< QgsStyle::StyleEntity, QStringList > mEntityNames;
194 
195  QList< QSize > mAdditionalSizes;
196  mutable std::unique_ptr< QgsExpressionContext > mExpressionContext;
197 
198  mutable QHash< QgsStyle::StyleEntity, QHash< QString, QIcon > > mIconCache;
199 
200  static QgsAbstractStyleEntityIconGenerator *sIconGenerator;
201  mutable QSet< QString > mPending3dSymbolIcons;
202 
203  QgsStyle::StyleEntity entityTypeFromRow( int row ) const;
204 
205  int offsetForEntity( QgsStyle::StyleEntity entity ) const;
206  static QVariant headerDataStatic( int section, Qt::Orientation orientation,
207  int role = Qt::DisplayRole );
208 
209  friend class QgsCombinedStyleModel;
210 };
211 
222 class CORE_EXPORT QgsStyleProxyModel: public QSortFilterProxyModel
223 {
224  Q_OBJECT
225 
226  public:
227 
233  explicit QgsStyleProxyModel( QgsStyle *style, QObject *parent SIP_TRANSFERTHIS = nullptr );
234 
240  explicit QgsStyleProxyModel( QgsStyleModel *model, QObject *parent SIP_TRANSFERTHIS = nullptr );
241 
242 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
243  SIP_IF_FEATURE( CONCATENATED_TABLES_MODEL )
244 
245 
254  explicit QgsStyleProxyModel( QgsCombinedStyleModel *model, QObject *parent SIP_TRANSFERTHIS = nullptr );
255  SIP_END
256 #endif
257 
263  QString filterString() const { return mFilterString; }
264 
271  QgsStyle::StyleEntity entityFilter() const;
272 
280  void setEntityFilter( QgsStyle::StyleEntity filter );
281 
291  void setEntityFilters( const QList<QgsStyle::StyleEntity> &filters ) SIP_SKIP;
292 
299  bool entityFilterEnabled() const;
300 
310  void setEntityFilterEnabled( bool enabled );
311 
320  Qgis::SymbolType symbolType() const;
321 
329  void setSymbolType( Qgis::SymbolType type );
330 
337  bool symbolTypeFilterEnabled() const;
338 
349  void setSymbolTypeFilterEnabled( bool enabled );
350 
359  QgsWkbTypes::GeometryType layerType() const;
360 
367  void setLayerType( QgsWkbTypes::GeometryType type );
368 
379  void setTagId( int id );
380 
390  int tagId() const;
391 
401  void setTagString( const QString &tag );
402 
411  QString tagString() const;
412 
423  void setSmartGroupId( int id );
424 
434  int smartGroupId() const;
435 
436  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
437  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
438 
444  bool favoritesOnly() const;
445 
451  void setFavoritesOnly( bool favoritesOnly );
452 
459  void addDesiredIconSize( QSize size );
460 
461  public slots:
462 
469  void setFilterString( const QString &filter );
470 
471  private:
472 
473  void initialize();
474 
475  QgsStyleModel *mModel = nullptr;
476 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
477  QgsCombinedStyleModel *mCombinedModel = nullptr;
478 #endif
479  QgsStyle *mStyle = nullptr;
480 
481  QString mFilterString;
482 
483  int mTagId = -1;
484  QStringList mTaggedSymbolNames;
485 
486  QString mTagFilter;
487 
488  int mSmartGroupId = -1;
489  QStringList mSmartGroupSymbolNames;
490 
491  bool mFavoritesOnly = false;
492 
493  bool mEntityFilterEnabled = false;
494  QList< QgsStyle::StyleEntity > mEntityFilters = QList< QgsStyle::StyleEntity >() << QgsStyle::SymbolEntity;
495 
496  bool mSymbolTypeFilterEnabled = false;
498 
500 
501 };
502 
503 #endif //QGSSTYLEMODEL_H
QgsStyleModel::StyleName
@ StyleName
Name of associated QgsStyle (QgsStyle::name()) (since QGIS 3.26)
Definition: qgsstylemodel.h:130
QgsStyleModel
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
Definition: qgsstylemodel.h:107
QgsCombinedStyleModel
A model which contains entities from multiple QgsStyle databases.
Definition: qgscombinedstylemodel.h:42
QgsStyleModel::IsFavoriteRole
@ IsFavoriteRole
Whether entity is flagged as a favorite.
Definition: qgsstylemodel.h:127
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
QgsStyleModel::IsTitleRole
@ IsTitleRole
True if the index corresponds to a title item (since QGIS 3.26)
Definition: qgsstylemodel.h:132
QgsStyleModel::Column
Column
Model columns.
Definition: qgsstylemodel.h:114
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
Qgis::SymbolType
SymbolType
Symbol types.
Definition: qgis.h:205
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:147
qgis_sip.h
QgsStyleProxyModel::filterString
QString filterString() const
Returns the current filter string, if set.
Definition: qgsstylemodel.h:263
QgsStyleModel::CompatibleGeometryTypesRole
@ CompatibleGeometryTypesRole
Compatible layer geometry types (for 3D symbols)
Definition: qgsstylemodel.h:129
QgsStyleModel::Role
Role
Custom model roles.
Definition: qgsstylemodel.h:121
qgsstyle.h
QgsStyleModel::SymbolTypeRole
@ SymbolTypeRole
Symbol type (for symbol or legend patch shape entities)
Definition: qgsstylemodel.h:126
SIP_IF_FEATURE
#define SIP_IF_FEATURE(feature)
Definition: qgis_sip.h:176
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:140
QgsStyleModel::LayerTypeRole
@ LayerTypeRole
Layer type (for label settings entities)
Definition: qgsstylemodel.h:128
QgsStyle
Definition: qgsstyle.h:159
QgsStyleModel::EntityName
@ EntityName
Entity name (since QGIS 3.26)
Definition: qgsstylemodel.h:125
QgsWkbTypes::UnknownGeometry
@ UnknownGeometry
Definition: qgswkbtypes.h:145
QgsAbstractStyleEntityIconGenerator
An abstract base class for icon generators for a QgsStyleModel.
Definition: qgsstylemodel.h:44
QgsStyleProxyModel
A QSortFilterProxyModel subclass for showing filtered symbol and color ramps entries from a QgsStyle ...
Definition: qgsstylemodel.h:222
SIP_END
#define SIP_END
Definition: qgis_sip.h:203
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
Qgis::SymbolType::Marker
@ Marker
Marker symbol.
QgsStyleModel::StyleFileName
@ StyleFileName
File name of associated QgsStyle (QgsStyle::fileName()) (since QGIS 3.26)
Definition: qgsstylemodel.h:131
QgsStyle::StyleEntity
StyleEntity
Enum for Entities involved in a style.
Definition: qgsstyle.h:178