59 : QAbstractItemModel( parent )
66 mEntityNames.insert( entity, mStyle->
allNames( entity ) );
100 switch ( entityType )
107 name = mEntityNames[ entityType ].value(
index.row() - offsetForEntity( entityType ) );
113 case Qt::DisplayRole:
114 case Qt::ToolTipRole:
117 switch (
index.column() )
121 const QStringList tags = mStyle->
tagsOfSymbol( entityType, name );
123 if ( role == Qt::ToolTipRole )
125 QString tooltip = QStringLiteral(
"<h3>%1</h3><p><i>%2</i>" ).arg( name,
126 tags.count() > 0 ? tags.join( QLatin1String(
", " ) ) : tr(
"Not tagged" ) );
128 switch ( entityType )
133 std::unique_ptr< QgsSymbol > symbol( mStyle->
symbol( name ) );
136 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
139 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
141 int height =
static_cast< int >( width / 1.61803398875 );
144 QBuffer buffer( &
data );
145 pm.save( &buffer,
"PNG", 100 );
146 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
153 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
156 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
158 int height =
static_cast< int >( width / 1.61803398875 );
162 QBuffer buffer( &
data );
163 pm.save( &buffer,
"PNG", 100 );
164 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
170 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
173 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
175 int height =
static_cast< int >( width / 1.61803398875 );
179 QBuffer buffer( &
data );
180 pm.save( &buffer,
"PNG", 100 );
181 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
187 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
190 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
192 int height =
static_cast< int >( width / 1.61803398875 );
199 QBuffer buffer( &
data );
200 pm.save( &buffer,
"PNG", 100 );
201 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
220 return mStyle->
tagsOfSymbol( entityType, name ).join( QLatin1String(
", " ) );
225 case Qt::DecorationRole:
230 if ( !mExpressionContext )
236 mExpressionContext = qgis::make_unique< QgsExpressionContext >();
240 switch (
index.column() )
243 switch ( entityType )
248 QIcon icon = mIconCache[ entityType ].value( name );
249 if ( !icon.isNull() )
252 std::unique_ptr< QgsSymbol > symbol( mStyle->
symbol( name ) );
255 if ( mAdditionalSizes.isEmpty() )
258 for (
const QSize &s : mAdditionalSizes )
264 mIconCache[ entityType ].insert( name, icon );
270 QIcon icon = mIconCache[ entityType ].value( name );
271 if ( !icon.isNull() )
274 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( name ) );
277 if ( mAdditionalSizes.isEmpty() )
279 for (
const QSize &s : mAdditionalSizes )
285 mIconCache[ entityType ].insert( name, icon );
292 QIcon icon = mIconCache[ entityType ].value( name );
293 if ( !icon.isNull() )
297 if ( mAdditionalSizes.isEmpty() )
299 for (
const QSize &s : mAdditionalSizes )
303 mIconCache[ entityType ].insert( name, icon );
310 QIcon icon = mIconCache[ entityType ].value( name );
311 if ( !icon.isNull() )
315 if ( mAdditionalSizes.isEmpty() )
317 for (
const QSize &s : mAdditionalSizes )
321 mIconCache[ entityType ].insert( name, icon );
328 QIcon icon = mIconCache[ entityType ].value( name );
329 if ( !icon.isNull() )
337 if ( mAdditionalSizes.isEmpty() )
340 for (
const QSize &s : mAdditionalSizes )
346 mIconCache[ entityType ].insert( name, icon );
356 QIcon icon = mIconCache[ entityType ].value( name );
357 if ( !icon.isNull() )
360 if ( sIconGenerator && !mPending3dSymbolIcons.contains( name ) )
362 mPending3dSymbolIcons.insert( name );
367 if ( mAdditionalSizes.isEmpty() )
369 for (
const QSize &s : mAdditionalSizes )
373 mIconCache[ entityType ].insert( name, icon );
396 return mStyle->
isFavorite( entityType, name );
400 switch ( entityType )
405 return symbol ? symbol->
type() : QVariant();
424 switch ( entityType )
443 switch ( entityType )
449 res.reserve( types.size() );
452 res << static_cast< int >( type );
479 if (
index.row() < 0 ||
index.row() >=
rowCount( QModelIndex() ) || role != Qt::EditRole )
482 switch (
index.column() )
488 switch ( entityType )
495 name = mEntityNames[ entityType ].value(
index.row() - offsetForEntity( entityType ) );
499 const QString newName = value.toString();
500 return mStyle->
renameEntity( entityType, name, newName );
512 Qt::ItemFlags
flags = QAbstractItemModel::flags(
index );
515 return flags | Qt::ItemIsEditable;
525 if ( role == Qt::DisplayRole )
527 if ( orientation == Qt::Vertical )
529 return QVariant( section );
536 return QVariant( tr(
"Name" ) );
539 return QVariant( tr(
"Tags" ) );
554 if ( !hasIndex( row, column,
parent ) )
555 return QModelIndex();
559 return createIndex( row, column );
562 return QModelIndex();
568 return QModelIndex();
577 count += mEntityNames[ type ].size();
590 if ( mAdditionalSizes.contains( size ) )
593 mAdditionalSizes << size;
595 if ( sIconGenerator )
603 sIconGenerator = generator;
609 mIconCache[ type ].remove( name );
610 const QStringList oldSymbolNames = mEntityNames[ type ];
611 const QStringList newSymbolNames = mStyle->
allNames( type );
614 const int newNameIndex = newSymbolNames.indexOf( name );
615 if ( newNameIndex < 0 )
618 const int offset = offsetForEntity( type );
619 beginInsertRows( QModelIndex(), newNameIndex + offset, newNameIndex + offset );
620 mEntityNames[ type ] = newSymbolNames;
626 mIconCache[ type ].remove( name );
627 const QStringList oldSymbolNames = mEntityNames[ type ];
628 const QStringList newSymbolNames = mStyle->
allNames( type );
631 const int oldNameIndex = oldSymbolNames.indexOf( name );
632 if ( oldNameIndex < 0 )
635 const int offset = offsetForEntity( type );
636 beginRemoveRows( QModelIndex(), oldNameIndex + offset, oldNameIndex + offset );
637 mEntityNames[ type ] = newSymbolNames;
643 mIconCache[ type ].remove( name );
645 const int offset = offsetForEntity( type );
646 QModelIndex i =
index( offset + mEntityNames[ type ].indexOf( name ),
Tags );
647 emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole );
650 void QgsStyleModel::onEntityRename(
QgsStyle::StyleEntity type,
const QString &oldName,
const QString &newName )
652 mIconCache[ type ].remove( oldName );
653 const QStringList oldSymbolNames = mEntityNames[ type ];
654 const QStringList newSymbolNames = mStyle->
allNames( type );
657 const int oldNameIndex = oldSymbolNames.indexOf( oldName );
658 if ( oldNameIndex < 0 )
662 const int newNameIndex = newSymbolNames.indexOf( newName );
663 if ( newNameIndex < 0 )
666 if ( newNameIndex == oldNameIndex )
668 mEntityNames[ type ] = newSymbolNames;
672 const int offset = offsetForEntity( type );
673 beginMoveRows( QModelIndex(), oldNameIndex + offset, oldNameIndex + offset, QModelIndex(), ( newNameIndex > oldNameIndex ? newNameIndex + 1 : newNameIndex ) + offset );
674 mEntityNames[ type ] = newSymbolNames;
678 void QgsStyleModel::onTagsChanged(
int entity,
const QString &name,
const QStringList & )
682 int row = mEntityNames[type].indexOf( name ) + offsetForEntity( type );
692 emit dataChanged( i, i );
695 void QgsStyleModel::rebuildSymbolIcons()
698 mExpressionContext.reset();
702 void QgsStyleModel::iconGenerated(
QgsStyle::StyleEntity type,
const QString &name,
const QIcon &icon )
704 int row = mEntityNames[type].indexOf( name ) + offsetForEntity( type );
709 mPending3dSymbolIcons.remove( name );
711 emit dataChanged(
index( row, 0 ),
index( row, 0 ) );
727 int maxRowForEntity = 0;
730 maxRowForEntity += mEntityNames[ type ].size();
731 if ( row < maxRowForEntity )
745 if ( type == entity )
748 offset += mEntityNames[ type ].size();
758 : QSortFilterProxyModel( parent )
765 void QgsStyleProxyModel::initialize()
767 setSortCaseSensitivity( Qt::CaseInsensitive );
769 setSourceModel( mModel );
770 setDynamicSortFilter(
true );
778 if ( mSmartGroupId >= 0 )
785 if ( mFavoritesOnly )
801 if ( mSmartGroupId >= 0 )
807 : QSortFilterProxyModel( parent )
809 , mStyle( model->style() )
816 if ( mFilterString.isEmpty() && !mEntityFilterEnabled && !mSymbolTypeFilterEnabled && mTagId < 0 && mSmartGroupId < 0 && !mFavoritesOnly )
819 QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
820 const QString name = sourceModel()->data( index ).toString();
824 if ( mEntityFilterEnabled && ( mEntityFilters.empty() || !mEntityFilters.contains( styleEntityType ) ) )
828 if ( mSymbolTypeFilterEnabled &&
symbolType != mSymbolType )
833 switch ( styleEntityType )
853 if ( !types.empty() && !types.contains( mLayerType ) )
860 if ( mTagId >= 0 && !mTaggedSymbolNames.contains( name ) )
863 if ( mSmartGroupId >= 0 && !mSmartGroupSymbolNames.contains( name ) )
869 if ( !mFilterString.isEmpty() )
873 const QStringList partsToMatch = mFilterString.trimmed().split(
' ' );
875 QStringList partsToSearch = name.split(
' ' );
876 for (
const QString &tag : tags )
878 partsToSearch.append( tag.split(
' ' ) );
881 for (
const QString &part : partsToMatch )
884 for (
const QString &partToSearch : qgis::as_const( partsToSearch ) )
886 if ( partToSearch.contains( part, Qt::CaseInsensitive ) )
902 mFilterString = filter;
909 return mFavoritesOnly;
925 return mSymbolTypeFilterEnabled;
949 mTaggedSymbolNames.clear();
953 mTaggedSymbolNames.append( mStyle->
symbolsWithTag( entity, mTagId ) );
968 mSmartGroupSymbolNames.clear();
969 if ( mSmartGroupId >= 0 )
979 return mSmartGroupId;
995 return mEntityFilterEnabled;
1011 mEntityFilters = QList< QgsStyle::StyleEntity >() <<
entityFilter;
1017 mEntityFilters = filters;
static const double UI_SCALE_FACTOR
UI scaling factor.
An abstract base class for icon generators for a QgsStyleModel.
void setIconSizes(const QList< QSize > &sizes)
Sets the list of icon sizes to generate.
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 ...
QgsAbstractStyleEntityIconGenerator(QObject *parent)
Constructor for QgsAbstractStyleEntityIconGenerator, with the specified parent object.
QList< QSize > iconSizes() const
Returns the list of icon sizes to generate.
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.
static QString defaultThemePath()
Returns the path to the default theme directory.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
static QgsSvgCache * svgCache()
Returns the application's SVG cache, used for caching SVG images and handling parameter replacement w...
static QgsStyleModel * defaultStyleModel()
Returns a shared QgsStyleModel containing the default style library (see QgsStyle::defaultStyle()).
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
void remoteImageFetched(const QString &url)
Emitted when the cache has finished retrieving an image file from a remote url.
Represents a patch shape for use in map legends.
bool isNull() const
Returns true if the patch shape is a null QgsLegendPatchShape, which indicates that the default legen...
Contains settings for how a map layer will be labeled.
static QPixmap labelSettingsPreviewPixmap(const QgsPalLayerSettings &settings, QSize size, const QString &previewText=QString(), int padding=0)
Returns a pixmap preview for label settings.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void projectColorsChanged()
Emitted whenever the project's color scheme has been changed.
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
Qt::ItemFlags flags(const QModelIndex &index) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
static void setIconGenerator(QgsAbstractStyleEntityIconGenerator *generator)
Sets the icon generator to use for deferred style entity icon generation.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QModelIndex parent(const QModelIndex &index) const override
@ LayerTypeRole
Layer type (for label settings entities)
@ TypeRole
Style entity type, see QgsStyle::StyleEntity.
@ 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.
QgsStyleModel(QgsStyle *style, QObject *parent=nullptr)
Constructor for QgsStyleModel, for the specified style and parent object.
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
QgsWkbTypes::GeometryType layerType() const
Returns the layer type filter, or QgsWkbTypes::UnknownGeometry if no layer type filter is present.
void setEntityFilter(QgsStyle::StyleEntity filter)
Sets the style entity type filter.
bool favoritesOnly() const
Returns true if the model is showing only favorited entities.
void setSymbolTypeFilterEnabled(bool enabled)
Sets whether filtering by symbol type is enabled.
void setTagId(int id)
Sets a tag id to filter style entities by.
void setSymbolType(QgsSymbol::SymbolType type)
Sets the symbol type filter.
QgsStyle::StyleEntity entityFilter() const
Returns the style entity type filter.
QgsStyleProxyModel(QgsStyle *style, QObject *parent=nullptr)
Constructor for QgsStyleProxyModel, for the specified style and parent object.
int tagId() const
Returns the tag id used to filter style entities by.
void setEntityFilters(const QList< QgsStyle::StyleEntity > &filters)
Sets the style entity type filters.
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
void setFavoritesOnly(bool favoritesOnly)
Sets whether the model should show only favorited entities.
void setLayerType(QgsWkbTypes::GeometryType type)
Sets the layer type filter.
bool entityFilterEnabled() const
Returns true if filtering by entity type is enabled.
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
void setEntityFilterEnabled(bool enabled)
Sets whether filtering by entity type is enabled.
void setSmartGroupId(int id)
Sets a smart group id to filter style entities by.
QgsSymbol::SymbolType symbolType() const
Returns the symbol type filter.
bool symbolTypeFilterEnabled() const
Returns true if filtering by symbol type is enabled.
int smartGroupId() const
Returns the smart group id used to filter style entities by.
void setFilterString(const QString &filter)
Sets a filter string, such that only symbol entities with names matching the specified string will be...
QgsSymbol::SymbolType legendPatchShapeSymbolType(const QString &name) const
Returns the symbol type corresponding to the legend patch shape with the specified name,...
QgsTextFormat textFormat(const QString &name) const
Returns the text format with the specified name.
QStringList allNames(StyleEntity type) const
Returns a list of the names of all existing entities of the specified type.
void entityChanged(QgsStyle::StyleEntity entity, const QString &name)
Emitted whenever an entity's definition is changed.
QStringList symbolsWithTag(StyleEntity type, int tagid) const
Returns the symbol names with which have the given tag.
QList< QgsWkbTypes::GeometryType > symbol3DCompatibleGeometryTypes(const QString &name) const
Returns the list of the vector layer geometry types which are compatible with the 3D symbol with the ...
QgsSymbol * symbol(const QString &name)
Returns a NEW copy of symbol.
QStringList symbolsOfSmartgroup(StyleEntity type, int id)
Returns the symbols for the smartgroup.
StyleEntity
Enum for Entities involved in a style.
@ LabelSettingsEntity
Label settings.
@ TextFormatEntity
Text formats.
@ SmartgroupEntity
Smart groups.
@ Symbol3DEntity
3D symbol entity (since QGIS 3.14)
@ ColorrampEntity
Color ramps.
@ LegendPatchShapeEntity
Legend patch shape (since QGIS 3.14)
QStringList tagsOfSymbol(StyleEntity type, const QString &symbol)
Returns the tags associated with the symbol.
void entityRenamed(QgsStyle::StyleEntity entity, const QString &oldName, const QString &newName)
Emitted whenever a entity of the specified type has been renamed from oldName to newName.
const QgsSymbol * symbolRef(const QString &name) const
Returns a const pointer to a symbol (doesn't create new instance)
QgsWkbTypes::GeometryType labelSettingsLayerType(const QString &name) const
Returns the layer geometry type corresponding to the label settings with the specified name,...
QgsColorRamp * colorRamp(const QString &name) const
Returns a new copy of the specified color ramp.
bool renameEntity(StyleEntity type, const QString &oldName, const QString &newName)
Renames an entity of the specified type from oldName to newName.
bool isFavorite(StyleEntity type, const QString &name)
Returns true if the symbol with matching type and name is marked as a favorite.
void entityTagsChanged(QgsStyle::StyleEntity entity, const QString &name, const QStringList &newTags)
Emitted whenever an entity's tags are changed.
void favoritedChanged(QgsStyle::StyleEntity entity, const QString &name, bool isFavorite)
Emitted whenever an entity is either favorited or un-favorited.
QgsPalLayerSettings labelSettings(const QString &name) const
Returns the label settings with the specified name.
void entityRemoved(QgsStyle::StyleEntity entity, const QString &name)
Emitted whenever an entity of the specified type is removed from the style and the database has been ...
const QgsSymbol * previewSymbolForPatchShape(const QgsLegendPatchShape &shape) const
Returns a symbol to use for rendering preview icons for a patch shape.
QgsLegendPatchShape legendPatchShape(const QString &name) const
Returns the legend patch shape with the specified name.
void entityAdded(QgsStyle::StyleEntity entity, const QString &name)
Emitted every time a new entity has been added to the database.
void remoteSvgFetched(const QString &url)
Emitted when the cache has finished retrieving an SVG file from a remote url.
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0, Qt::Orientation direction=Qt::Horizontal, bool flipDirection=false, bool drawTransparentBackground=true)
Returns a pixmap preview for a color ramp.
static QPixmap symbolPreviewPixmap(const QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr, bool selected=false, const QgsExpressionContext *expressionContext=nullptr, const QgsLegendPatchShape *shape=nullptr)
Returns a pixmap preview for a color ramp.
Abstract base class for all rendered symbols.
SymbolType type() const
Returns the symbol's type.
SymbolType
Type of the symbol.
Container for all settings relating to text rendering.
static QPixmap textFormatPreviewPixmap(const QgsTextFormat &format, QSize size, const QString &previewText=QString(), int padding=0)
Returns a pixmap preview for a text format.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
const double ICON_PADDING_FACTOR