28 : QAbstractItemModel( parent )
61 if ( index.row() < 0 || index.row() >=
rowCount( QModelIndex() ) )
65 const bool isColorRamp = index.row() >= mStyle->
symbolCount();
66 const QString name = !isColorRamp
67 ? mSymbolNames.value( index.row() )
68 : mRampNames.value( index.row() - mSymbolNames.size() );
76 switch ( index.column() )
82 if ( role == Qt::ToolTipRole )
84 QString tooltip = QStringLiteral(
"<h3>%1</h3><p><i>%2</i>" ).arg( name,
85 tags.count() > 0 ? tags.join( QStringLiteral(
", " ) ) : tr(
"Not tagged" ) );
88 std::unique_ptr< QgsSymbol > symbol( mStyle->
symbol( name ) );
91 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data( index, Qt::FontRole ).value< QFont >() ).width(
'X' ) * 23 );
92 int height =
static_cast< int >( width / 1.61803398875 );
95 QBuffer buffer( &data );
96 pm.save( &buffer,
"PNG", 100 );
97 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString( data.toBase64() ) );
112 case Qt::DecorationRole:
116 switch ( index.column() )
122 QIcon icon = mSymbolIconCache.value( name );
123 if ( !icon.isNull() )
126 std::unique_ptr< QgsSymbol > symbol( mStyle->
symbol( name ) );
129 if ( mAdditionalSizes.isEmpty() )
132 for (
const QVariant &size : mAdditionalSizes )
134 QSize s = size.toSize();
139 mSymbolIconCache.insert( name, icon );
145 QIcon icon = mColorRampIconCache.value( name );
146 if ( !icon.isNull() )
149 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( name ) );
152 if ( mAdditionalSizes.isEmpty() )
154 for (
const QVariant &size : mAdditionalSizes )
156 QSize s = size.toSize();
161 mColorRampIconCache.insert( name, icon );
182 return symbol ? symbol->
type() : QVariant();
194 if ( index.row() < 0 || index.row() >=
rowCount( QModelIndex() ) || role != Qt::EditRole )
197 switch ( index.column() )
201 const bool isColorRamp = index.row() >= mStyle->
symbolCount();
202 const QString name = !isColorRamp
203 ? mSymbolNames.value( index.row() )
204 : mRampNames.value( index.row() - mSymbolNames.size() );
205 const QString newName = value.toString();
221 Qt::ItemFlags
flags = QAbstractItemModel::flags( index );
222 if ( index.isValid() && index.column() ==
Name )
224 return flags | Qt::ItemIsEditable;
234 if ( role == Qt::DisplayRole )
236 if ( orientation == Qt::Vertical )
238 return QVariant( section );
245 return QVariant( tr(
"Name" ) );
248 return QVariant( tr(
"Tags" ) );
263 if ( !hasIndex( row, column, parent ) )
264 return QModelIndex();
266 if ( !parent.isValid() )
268 return createIndex( row, column );
271 return QModelIndex();
277 return QModelIndex();
282 if ( !parent.isValid() )
284 return mSymbolNames.count() + mRampNames.count();
296 mAdditionalSizes << size;
297 mSymbolIconCache.clear();
298 mColorRampIconCache.clear();
301 void QgsStyleModel::onSymbolAdded(
const QString &name,
QgsSymbol * )
303 mSymbolIconCache.remove( name );
304 const QStringList oldSymbolNames = mSymbolNames;
305 const QStringList newSymbolNames = mStyle->
symbolNames();
308 const int newNameIndex = newSymbolNames.indexOf( name );
309 if ( newNameIndex < 0 )
312 beginInsertRows( QModelIndex(), newNameIndex, newNameIndex );
313 mSymbolNames = newSymbolNames;
317 void QgsStyleModel::onSymbolRemoved(
const QString &name )
319 mSymbolIconCache.remove( name );
320 const QStringList oldSymbolNames = mSymbolNames;
321 const QStringList newSymbolNames = mStyle->
symbolNames();
324 const int oldNameIndex = oldSymbolNames.indexOf( name );
325 if ( oldNameIndex < 0 )
328 beginRemoveRows( QModelIndex(), oldNameIndex, oldNameIndex );
329 mSymbolNames = newSymbolNames;
333 void QgsStyleModel::onSymbolChanged(
const QString &name )
335 mSymbolIconCache.remove( name );
337 QModelIndex i =
index( mSymbolNames.indexOf( name ),
Tags );
338 emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole );
341 void QgsStyleModel::onSymbolRename(
const QString &oldName,
const QString &newName )
343 mSymbolIconCache.remove( oldName );
344 const QStringList oldSymbolNames = mSymbolNames;
345 const QStringList newSymbolNames = mStyle->
symbolNames();
348 const int oldNameIndex = oldSymbolNames.indexOf( oldName );
349 if ( oldNameIndex < 0 )
353 const int newNameIndex = newSymbolNames.indexOf( newName );
354 if ( newNameIndex < 0 )
357 if ( newNameIndex == oldNameIndex )
359 mSymbolNames = newSymbolNames;
363 beginMoveRows( QModelIndex(), oldNameIndex, oldNameIndex, QModelIndex(), newNameIndex > oldNameIndex ? newNameIndex + 1 : newNameIndex );
364 mSymbolNames = newSymbolNames;
368 void QgsStyleModel::onRampAdded(
const QString &name )
370 mColorRampIconCache.remove( name );
371 const QStringList oldRampNames = mRampNames;
375 const int newNameIndex = newRampNames.indexOf( name );
376 if ( newNameIndex < 0 )
379 beginInsertRows( QModelIndex(), newNameIndex + mSymbolNames.count(), newNameIndex + mSymbolNames.count() );
380 mRampNames = newRampNames;
384 void QgsStyleModel::onRampRemoved(
const QString &name )
386 mColorRampIconCache.remove( name );
387 const QStringList oldRampNames = mRampNames;
391 const int oldNameIndex = oldRampNames.indexOf( name );
392 if ( oldNameIndex < 0 )
395 beginRemoveRows( QModelIndex(), oldNameIndex + mSymbolNames.count(), oldNameIndex + mSymbolNames.count() );
396 mRampNames = newRampNames;
400 void QgsStyleModel::onRampChanged(
const QString &name )
402 mColorRampIconCache.remove( name );
404 QModelIndex i =
index( mSymbolNames.count() + mRampNames.indexOf( name ),
Tags );
405 emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole );
408 void QgsStyleModel::onRampRename(
const QString &oldName,
const QString &newName )
410 mColorRampIconCache.remove( oldName );
411 const QStringList oldRampNames = mRampNames;
415 const int oldNameIndex = oldRampNames.indexOf( oldName );
416 if ( oldNameIndex < 0 )
420 const int newNameIndex = newRampNames.indexOf( newName );
421 if ( newNameIndex < 0 )
424 if ( newNameIndex == oldNameIndex )
426 mRampNames = newRampNames;
430 beginMoveRows( QModelIndex(), oldNameIndex + mSymbolNames.count(), oldNameIndex + mSymbolNames.count(),
431 QModelIndex(), ( newNameIndex > oldNameIndex ? newNameIndex + 1 : newNameIndex ) + mSymbolNames.count() );
432 mRampNames = newRampNames;
436 void QgsStyleModel::onTagsChanged(
int entity,
const QString &name,
const QStringList & )
441 i =
index( mSymbolNames.indexOf( name ),
Tags );
445 i =
index( mSymbolNames.count() + mRampNames.indexOf( name ),
Tags );
447 emit dataChanged( i, i );
450 void QgsStyleModel::rebuildSymbolIcons()
452 mSymbolIconCache.clear();
453 emit dataChanged(
index( 0, 0 ),
index( mSymbolNames.count() - 1, 0 ), QVector<int>() << Qt::DecorationRole );
461 : QSortFilterProxyModel( parent )
465 setSortCaseSensitivity( Qt::CaseInsensitive );
467 setSourceModel( mModel );
468 setDynamicSortFilter(
true );
476 if ( mSmartGroupId >= 0 )
483 if ( mFavoritesOnly )
489 if ( mSmartGroupId >= 0 )
494 if ( mSmartGroupId >= 0 )
501 if ( mFilterString.isEmpty() && !mEntityFilterEnabled && !mSymbolTypeFilterEnabled && mTagId < 0 && mSmartGroupId < 0 && !mFavoritesOnly )
504 QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
505 const QString name = sourceModel()->data( index ).toString();
509 if ( mEntityFilterEnabled && styleEntityType != mEntityFilter )
513 if ( mSymbolTypeFilterEnabled && symbolType != mSymbolType )
516 if ( mTagId >= 0 && !mTaggedSymbolNames.contains( name ) )
519 if ( mSmartGroupId >= 0 && !mSmartGroupSymbolNames.contains( name ) )
522 if ( mFavoritesOnly && !mFavoritedSymbolNames.contains( name ) )
525 if ( !mFilterString.isEmpty() )
529 const QStringList partsToMatch = mFilterString.trimmed().split(
' ' );
531 QStringList partsToSearch = name.split(
' ' );
532 for (
const QString &tag : tags )
534 partsToSearch.append( tag.split(
' ' ) );
537 for (
const QString &part : partsToMatch )
540 for (
const QString &partToSearch : qgis::as_const( partsToSearch ) )
542 if ( partToSearch.contains( part, Qt::CaseInsensitive ) )
558 mFilterString = filter;
564 return mFavoritesOnly;
571 if ( mFavoritesOnly )
578 mFavoritedSymbolNames.clear();
590 return mSymbolTypeFilterEnabled;
610 mTaggedSymbolNames.clear();
625 if ( mSmartGroupId >= 0 )
632 mSmartGroupSymbolNames.clear();
640 return mSmartGroupId;
656 return mEntityFilterEnabled;
667 return mEntityFilter;
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
static QgsSvgCache * svgCache()
Returns the application's SVG cache, used for caching SVG images and handling parameter replacement w...
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setFilterString(const QString &filter)
Sets a filter string, such that only symbol entities with names matching the specified string will be...
void symbolSaved(const QString &name, QgsSymbol *symbol)
Emitted every time a new symbol has been added to the database.
void setSmartGroupId(int id)
Sets a smart group id to filter style entities by.
Abstract base class for all rendered symbols.
static const double UI_SCALE_FACTOR
UI scaling factor.
QStringList symbolsWithTag(StyleEntity type, int tagid) const
Returns the symbol names with which have the given tag.
void entityTagsChanged(QgsStyle::StyleEntity entity, const QString &name, const QStringList &newTags)
Emitted whenever an entity's tags are changed.
QgsStyleModel(QgsStyle *style, QObject *parent=nullptr)
Constructor for QgsStyleModel, for the specified style and parent object.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images...
void setTagId(int id)
Sets a tag id to filter style entities by.
QStringList tagsOfSymbol(StyleEntity type, const QString &symbol)
Returns the tags associated with the symbol.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void setSymbolType(QgsSymbol::SymbolType type)
Sets the symbol type filter.
void projectColorsChanged()
Emitted whenever the project's color scheme has been changed.
void rampChanged(const QString &name)
Emitted whenever a color ramp's definition is changed.
QgsColorRamp * colorRamp(const QString &name) const
Returns a new copy of the specified color ramp.
void setFavoritesOnly(bool favoritesOnly)
Sets whether the model should show only favorited entities.
void rampAdded(const QString &name)
Emitted whenever a color ramp has been added to the style and the database has been updated as a resu...
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
int tagId() const
Returns the tag id used to filter style entities by.
QModelIndex parent(const QModelIndex &index) const override
StyleEntity
Enum for Entities involved in a style.
void setSymbolTypeFilterEnabled(bool enabled)
Sets whether filtering by symbol type is enabled.
SymbolType
Type of the symbol.
int symbolCount()
Returns count of symbols in style.
void setEntityFilterEnabled(bool enabled)
Sets whether filtering by entity type is enabled.
bool renameSymbol(const QString &oldName, const QString &newName)
Renames a symbol from oldName to newName.
const double ICON_PADDING_FACTOR
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
void rampRemoved(const QString &name)
Emitted whenever a color ramp has been removed from the style and the database has been updated as a ...
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
const QgsSymbol * symbolRef(const QString &name) const
Returns a const pointer to a symbol (doesn't create new instance)
QStringList symbolNames()
Returns a list of names of symbols.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
bool favoritesOnly() const
Returns true if the model is showing only favorited entities.
void symbolRenamed(const QString &oldName, const QString &newName)
Emitted whenever a symbol has been renamed from oldName to newName.
void favoritedChanged(QgsStyle::StyleEntity entity, const QString &name, bool isFavorite)
Emitted whenever an entity is either favorited or un-favorited.
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0)
Returns a pixmap preview for a color ramp.
QStringList colorRampNames()
Returns a list of names of color ramps.
void symbolRemoved(const QString &name)
Emitted whenever a symbol has been removed from the style and the database has been updated as a resu...
QStringList symbolsOfSmartgroup(StyleEntity type, int id)
Returns the symbols for the smartgroup.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
void remoteImageFetched(const QString &url)
Emitted when the cache has finished retrieving an image file from a remote url.
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
void rampRenamed(const QString &oldName, const QString &newName)
Emitted whenever a color ramp has been renamed from oldName to newName.
QgsStyleProxyModel(QgsStyle *style, QObject *parent=nullptr)
Constructor for QgsStyleProxyModel, for the specified style and parent object.
int smartGroupId() const
Returns the smart group id used to filter style entities by.
void symbolChanged(const QString &name)
Emitted whenever a symbol's definition is changed.
SymbolType type() const
Returns the symbol's type.
QgsSymbol::SymbolType symbolType() const
Returns the symbol type filter.
bool entityFilterEnabled() const
Returns true if filtering by entity type is enabled.
static QgsProject * instance()
Returns the QgsProject singleton instance.
bool renameColorRamp(const QString &oldName, const QString &newName)
Changes ramp's name.
void remoteSvgFetched(const QString &url)
Emitted when the cache has finished retrieving an SVG file from a remote url.
QgsSymbol * symbol(const QString &name)
Returns a NEW copy of symbol.
QVariant data(const QModelIndex &index, int role) const override
void setEntityFilter(QgsStyle::StyleEntity filter)
Sets the style entity type filter.
Style entity type, see QgsStyle::StyleEntity.
QgsStyle::StyleEntity entityFilter() const
Returns the style entity type filter.
bool symbolTypeFilterEnabled() const
Returns true if filtering by symbol type is enabled.
QStringList symbolsOfFavorite(StyleEntity type) const
Returns the symbol names which are flagged as favorite.
Qt::ItemFlags flags(const QModelIndex &index) const override
Symbol type (for symbol entities)
static QPixmap symbolPreviewPixmap(const QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr)
Returns a pixmap preview for a color ramp.