61 : QAbstractItemModel( parent )
68 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 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
137 int height =
static_cast< int >( width / 1.61803398875 );
140 QBuffer buffer( &
data );
141 pm.save( &buffer,
"PNG", 100 );
142 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
149 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
150 int height =
static_cast< int >( width / 1.61803398875 );
154 QBuffer buffer( &
data );
155 pm.save( &buffer,
"PNG", 100 );
156 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
162 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
163 int height =
static_cast< int >( width / 1.61803398875 );
167 QBuffer buffer( &
data );
168 pm.save( &buffer,
"PNG", 100 );
169 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
175 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
176 int height =
static_cast< int >( width / 1.61803398875 );
183 QBuffer buffer( &
data );
184 pm.save( &buffer,
"PNG", 100 );
185 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
204 return mStyle->
tagsOfSymbol( entityType, name ).join( QLatin1String(
", " ) );
209 case Qt::DecorationRole:
214 if ( !mExpressionContext )
220 mExpressionContext = std::make_unique< QgsExpressionContext >();
224 switch (
index.column() )
227 switch ( entityType )
232 QIcon icon = mIconCache[ entityType ].value( name );
233 if ( !icon.isNull() )
236 std::unique_ptr< QgsSymbol > symbol( mStyle->
symbol( name ) );
239 if ( mAdditionalSizes.isEmpty() )
242 for (
const QSize &s : mAdditionalSizes )
248 mIconCache[ entityType ].insert( name, icon );
254 QIcon icon = mIconCache[ entityType ].value( name );
255 if ( !icon.isNull() )
258 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( name ) );
261 if ( mAdditionalSizes.isEmpty() )
263 for (
const QSize &s : mAdditionalSizes )
269 mIconCache[ entityType ].insert( name, icon );
276 QIcon icon = mIconCache[ entityType ].value( name );
277 if ( !icon.isNull() )
281 if ( mAdditionalSizes.isEmpty() )
283 for (
const QSize &s : mAdditionalSizes )
287 mIconCache[ entityType ].insert( name, icon );
294 QIcon icon = mIconCache[ entityType ].value( name );
295 if ( !icon.isNull() )
299 if ( mAdditionalSizes.isEmpty() )
301 for (
const QSize &s : mAdditionalSizes )
305 mIconCache[ entityType ].insert( name, icon );
312 QIcon icon = mIconCache[ entityType ].value( name );
313 if ( !icon.isNull() )
321 if ( mAdditionalSizes.isEmpty() )
324 for (
const QSize &s : mAdditionalSizes )
330 mIconCache[ entityType ].insert( name, icon );
340 QIcon icon = mIconCache[ entityType ].value( name );
341 if ( !icon.isNull() )
344 if ( sIconGenerator && !mPending3dSymbolIcons.contains( name ) )
346 mPending3dSymbolIcons.insert( name );
351 if ( mAdditionalSizes.isEmpty() )
353 for (
const QSize &s : mAdditionalSizes )
357 mIconCache[ entityType ].insert( name, icon );
380 return mStyle->
isFavorite( entityType, name );
384 switch ( entityType )
389 return symbol ?
static_cast< int >( symbol->
type() ) : QVariant();
408 switch ( entityType )
427 switch ( entityType )
433 res.reserve( types.size() );
436 res << static_cast< int >( type );
457 return mStyle->
name();
472 if (
index.row() < 0 ||
index.row() >=
rowCount( QModelIndex() ) || role != Qt::EditRole )
475 switch (
index.column() )
481 switch ( entityType )
488 name = mEntityNames[ entityType ].value(
index.row() - offsetForEntity( entityType ) );
492 const QString newName = value.toString();
493 return mStyle->
renameEntity( entityType, name, newName );
505 Qt::ItemFlags
flags = QAbstractItemModel::flags(
index );
508 return flags | Qt::ItemIsEditable;
518 return headerDataStatic( section, orientation, role );
521QVariant QgsStyleModel::headerDataStatic(
int section, Qt::Orientation orientation,
int role )
523 if ( role == Qt::DisplayRole )
525 if ( orientation == Qt::Vertical )
527 return QVariant( section );
534 return QVariant( tr(
"Name" ) );
537 return QVariant( tr(
"Tags" ) );
552 if ( !hasIndex( row, column,
parent ) )
553 return QModelIndex();
557 return createIndex( row, column );
560 return QModelIndex();
566 return QModelIndex();
575 count += mEntityNames[ type ].size();
588 if ( mAdditionalSizes.contains( size ) )
591 mAdditionalSizes << size;
593 if ( sIconGenerator )
601 sIconGenerator = generator;
607 mIconCache[ type ].remove( name );
608 const QStringList oldSymbolNames = mEntityNames[ type ];
609 const QStringList newSymbolNames = mStyle->
allNames( type );
612 const int newNameIndex = newSymbolNames.indexOf( name );
613 if ( newNameIndex < 0 )
616 const int offset = offsetForEntity( type );
617 beginInsertRows( QModelIndex(), newNameIndex + offset, newNameIndex + offset );
618 mEntityNames[ type ] = newSymbolNames;
624 mIconCache[ type ].remove( name );
625 const QStringList oldSymbolNames = mEntityNames[ type ];
626 const QStringList newSymbolNames = mStyle->
allNames( type );
629 const int oldNameIndex = oldSymbolNames.indexOf( name );
630 if ( oldNameIndex < 0 )
633 const int offset = offsetForEntity( type );
634 beginRemoveRows( QModelIndex(), oldNameIndex + offset, oldNameIndex + offset );
635 mEntityNames[ type ] = newSymbolNames;
641 mIconCache[ type ].remove( name );
643 const int offset = offsetForEntity( type );
644 QModelIndex i =
index( offset + mEntityNames[ type ].indexOf( name ),
Tags );
645 emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole );
650 const int offset = offsetForEntity( type );
651 QModelIndex i =
index( offset + mEntityNames[ type ].indexOf( name ),
Name );
652 emit dataChanged( i, i, QVector< int >() << Role::IsFavoriteRole );
655void QgsStyleModel::onEntityRename(
QgsStyle::StyleEntity type,
const QString &oldName,
const QString &newName )
657 mIconCache[ type ].remove( oldName );
658 const QStringList oldSymbolNames = mEntityNames[ type ];
659 const QStringList newSymbolNames = mStyle->
allNames( type );
662 const int oldNameIndex = oldSymbolNames.indexOf( oldName );
663 if ( oldNameIndex < 0 )
667 const int newNameIndex = newSymbolNames.indexOf( newName );
668 if ( newNameIndex < 0 )
671 if ( newNameIndex == oldNameIndex )
673 mEntityNames[ type ] = newSymbolNames;
677 const int offset = offsetForEntity( type );
678 beginMoveRows( QModelIndex(), oldNameIndex + offset, oldNameIndex + offset, QModelIndex(), ( newNameIndex > oldNameIndex ? newNameIndex + 1 : newNameIndex ) + offset );
679 mEntityNames[ type ] = newSymbolNames;
683void QgsStyleModel::onTagsChanged(
int entity,
const QString &name,
const QStringList & )
686 int row = mEntityNames[type].indexOf( name ) + offsetForEntity( type );
699void QgsStyleModel::rebuildSymbolIcons()
702 mExpressionContext.reset();
706void QgsStyleModel::iconGenerated(
QgsStyle::StyleEntity type,
const QString &name,
const QIcon &icon )
708 int row = mEntityNames[type].indexOf( name ) + offsetForEntity( type );
713 mPending3dSymbolIcons.remove( name );
715 emit dataChanged(
index( row, 0 ),
index( row, 0 ) );
731 int maxRowForEntity = 0;
734 maxRowForEntity += mEntityNames[ type ].size();
735 if ( row < maxRowForEntity )
749 if ( type == entity )
752 offset += mEntityNames[ type ].size();
762 : QSortFilterProxyModel( parent )
766 setSourceModel( mModel );
770void QgsStyleProxyModel::initialize()
772 setSortCaseSensitivity( Qt::CaseInsensitive );
774 setDynamicSortFilter(
true );
784 if ( mSmartGroupId >= 0 )
800 if ( mSmartGroupId >= 0 )
807 : QSortFilterProxyModel( parent )
809 , mStyle( model->style() )
811 setSourceModel( mModel );
816 : QSortFilterProxyModel( parent )
817 , mCombinedModel( model )
819 setSourceModel( mCombinedModel );
825 if ( mFilterString.isEmpty() && !mEntityFilterEnabled && !mSymbolTypeFilterEnabled && mTagId < 0 && mSmartGroupId < 0 && !mFavoritesOnly && mTagFilter.isEmpty() )
828 const QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
833 const QString name = sourceModel()->data( index ).toString();
837 if ( mEntityFilterEnabled && ( mEntityFilters.empty() || !mEntityFilters.contains( styleEntityType ) ) )
841 if ( mSymbolTypeFilterEnabled &&
symbolType != mSymbolType )
846 switch ( styleEntityType )
866 if ( !types.empty() && !types.contains( mLayerType ) )
873 if ( mTagId >= 0 && !mTaggedSymbolNames.contains( name ) )
876 if ( mSmartGroupId >= 0 && !mSmartGroupSymbolNames.contains( name ) )
879 if ( !mTagFilter.isEmpty() && !tags.contains( mTagFilter, Qt::CaseInsensitive ) )
885 if ( !mFilterString.isEmpty() )
889 const QStringList partsToMatch = mFilterString.trimmed().split(
' ' );
891 QStringList partsToSearch = name.split(
' ' );
892 for (
const QString &tag : tags )
894 partsToSearch.append( tag.split(
' ' ) );
897 for (
const QString &part : partsToMatch )
900 for (
const QString &partToSearch : std::as_const( partsToSearch ) )
902 if ( partToSearch.contains( part, Qt::CaseInsensitive ) )
920 if ( leftSource != rightSource )
921 return QString::localeAwareCompare( leftSource, rightSource ) < 0;
925 return QString::localeAwareCompare( leftName, rightName ) < 0;
930 mFilterString = filter;
937 return mFavoritesOnly;
950 if ( mCombinedModel )
956 return mSymbolTypeFilterEnabled;
961 mSymbolTypeFilterEnabled = enabled;
982 mTaggedSymbolNames.clear();
986 mTaggedSymbolNames.append( mStyle->
symbolsWithTag( entity, mTagId ) );
1016 mSmartGroupSymbolNames.clear();
1017 if ( mSmartGroupId >= 0 )
1028 return mSmartGroupId;
1044 return mEntityFilterEnabled;
1060 mEntityFilters = QList< QgsStyle::StyleEntity >() <<
entityFilter;
1066 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()).
A model which contains entities from multiple QgsStyle databases.
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
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.
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
@ StyleName
Name of associated QgsStyle (QgsStyle::name()) (since QGIS 3.26)
@ LayerTypeRole
Layer type (for label settings entities)
@ StyleFileName
File name of associated QgsStyle (QgsStyle::fileName()) (since QGIS 3.26)
@ EntityName
Entity name (since QGIS 3.26)
@ 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)
@ IsTitleRole
True if the index corresponds to a title item (since QGIS 3.26)
@ 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 lessThan(const QModelIndex &left, const QModelIndex &right) const override
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.
QString tagString() const
Returns the tag string used to filter style entities by.
Qgis::SymbolType symbolType() const
Returns the symbol type filter.
void setTagString(const QString &tag)
Sets a tag to filter style entities by.
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.
void setSymbolType(Qgis::SymbolType type)
Sets the symbol 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.
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...
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.
void rebuildIconPreviews()
Emitted whenever icon previews for entities in the style must be rebuilt.
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)
QString fileName() const
Returns the current file name of the style database.
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.
QString name() const
Returns the name of the style.
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 ...
Qgis::SymbolType legendPatchShapeSymbolType(const QString &name) const
Returns the symbol type corresponding to the legend patch shape with the specified name,...
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.
Qgis::SymbolType type() const
Returns the symbol's type.
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