31 : QAbstractItemModel( parent )
38 mEntityNames.insert( entity, mStyle->
allNames( entity ) );
76 name = mEntityNames[ entityType ].value(
index.row() - offsetForEntity( entityType ) );
86 switch (
index.column() )
90 const QStringList tags = mStyle->
tagsOfSymbol( entityType, name );
92 if ( role == Qt::ToolTipRole )
94 QString tooltip = QStringLiteral(
"<h3>%1</h3><p><i>%2</i>" ).arg( name,
95 tags.count() > 0 ? tags.join( QStringLiteral(
", " ) ) : tr(
"Not tagged" ) );
102 std::unique_ptr< QgsSymbol > symbol( mStyle->
symbol( name ) );
105 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
108 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
110 int height =
static_cast< int >( width / 1.61803398875 );
113 QBuffer buffer( &
data );
114 pm.save( &buffer,
"PNG", 100 );
115 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
122 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
125 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
127 int height =
static_cast< int >( width / 1.61803398875 );
131 QBuffer buffer( &
data );
132 pm.save( &buffer,
"PNG", 100 );
133 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
139 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
142 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
144 int height =
static_cast< int >( width / 1.61803398875 );
148 QBuffer buffer( &
data );
149 pm.save( &buffer,
"PNG", 100 );
150 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
156 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
159 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
161 int height =
static_cast< int >( width / 1.61803398875 );
168 QBuffer buffer( &
data );
169 pm.save( &buffer,
"PNG", 100 );
170 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
188 return mStyle->
tagsOfSymbol( entityType, name ).join( QStringLiteral(
", " ) );
193 case Qt::DecorationRole:
198 if ( !mExpressionContext )
204 mExpressionContext = qgis::make_unique< QgsExpressionContext >();
208 switch (
index.column() )
211 switch ( entityType )
216 QIcon icon = mIconCache[ entityType ].value( name );
217 if ( !icon.isNull() )
220 std::unique_ptr< QgsSymbol > symbol( mStyle->
symbol( name ) );
223 if ( mAdditionalSizes.isEmpty() )
226 for (
const QSize &s : mAdditionalSizes )
232 mIconCache[ entityType ].insert( name, icon );
238 QIcon icon = mIconCache[ entityType ].value( name );
239 if ( !icon.isNull() )
242 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( name ) );
245 if ( mAdditionalSizes.isEmpty() )
247 for (
const QSize &s : mAdditionalSizes )
253 mIconCache[ entityType ].insert( name, icon );
260 QIcon icon = mIconCache[ entityType ].value( name );
261 if ( !icon.isNull() )
265 if ( mAdditionalSizes.isEmpty() )
267 for (
const QSize &s : mAdditionalSizes )
271 mIconCache[ entityType ].insert( name, icon );
278 QIcon icon = mIconCache[ entityType ].value( name );
279 if ( !icon.isNull() )
283 if ( mAdditionalSizes.isEmpty() )
285 for (
const QSize &s : mAdditionalSizes )
289 mIconCache[ entityType ].insert( name, icon );
296 QIcon icon = mIconCache[ entityType ].value( name );
297 if ( !icon.isNull() )
305 if ( mAdditionalSizes.isEmpty() )
308 for (
const QSize &s : mAdditionalSizes )
314 mIconCache[ entityType ].insert( name, icon );
337 return mStyle->
isFavorite( entityType, name );
341 switch ( entityType )
346 return symbol ? symbol->
type() : QVariant();
373 #ifndef _MSC_VER // avoid warning
380 if (
index.row() < 0 ||
index.row() >=
rowCount( QModelIndex() ) || role != Qt::EditRole )
383 switch (
index.column() )
389 switch ( entityType )
396 name = mEntityNames[ entityType ].value(
index.row() - offsetForEntity( entityType ) );
400 const QString newName = value.toString();
401 return mStyle->
renameEntity( entityType, name, newName );
413 Qt::ItemFlags
flags = QAbstractItemModel::flags(
index );
416 return flags | Qt::ItemIsEditable;
426 if ( role == Qt::DisplayRole )
428 if ( orientation == Qt::Vertical )
430 return QVariant( section );
437 return QVariant( tr(
"Name" ) );
440 return QVariant( tr(
"Tags" ) );
455 if ( !hasIndex( row, column,
parent ) )
456 return QModelIndex();
460 return createIndex( row, column );
463 return QModelIndex();
469 return QModelIndex();
478 count += mEntityNames[ type ].size();
491 if ( mAdditionalSizes.contains( size ) )
494 mAdditionalSizes << size;
500 mIconCache[ type ].remove( name );
501 const QStringList oldSymbolNames = mEntityNames[ type ];
502 const QStringList newSymbolNames = mStyle->
allNames( type );
505 const int newNameIndex = newSymbolNames.indexOf( name );
506 if ( newNameIndex < 0 )
509 const int offset = offsetForEntity( type );
510 beginInsertRows( QModelIndex(), newNameIndex + offset, newNameIndex + offset );
511 mEntityNames[ type ] = newSymbolNames;
517 mIconCache[ type ].remove( name );
518 const QStringList oldSymbolNames = mEntityNames[ type ];
519 const QStringList newSymbolNames = mStyle->
allNames( type );
522 const int oldNameIndex = oldSymbolNames.indexOf( name );
523 if ( oldNameIndex < 0 )
526 const int offset = offsetForEntity( type );
527 beginRemoveRows( QModelIndex(), oldNameIndex + offset, oldNameIndex + offset );
528 mEntityNames[ type ] = newSymbolNames;
534 mIconCache[ type ].remove( name );
536 const int offset = offsetForEntity( type );
537 QModelIndex i =
index( offset + mEntityNames[ type ].indexOf( name ),
Tags );
538 emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole );
541 void QgsStyleModel::onEntityRename(
QgsStyle::StyleEntity type,
const QString &oldName,
const QString &newName )
543 mIconCache[ type ].remove( oldName );
544 const QStringList oldSymbolNames = mEntityNames[ type ];
545 const QStringList newSymbolNames = mStyle->
allNames( type );
548 const int oldNameIndex = oldSymbolNames.indexOf( oldName );
549 if ( oldNameIndex < 0 )
553 const int newNameIndex = newSymbolNames.indexOf( newName );
554 if ( newNameIndex < 0 )
557 if ( newNameIndex == oldNameIndex )
559 mEntityNames[ type ] = newSymbolNames;
563 const int offset = offsetForEntity( type );
564 beginMoveRows( QModelIndex(), oldNameIndex + offset, oldNameIndex + offset, QModelIndex(), ( newNameIndex > oldNameIndex ? newNameIndex + 1 : newNameIndex ) + offset );
565 mEntityNames[ type ] = newSymbolNames;
569 void QgsStyleModel::onTagsChanged(
int entity,
const QString &name,
const QStringList & )
573 int row = mEntityNames[type].indexOf( name ) + offsetForEntity( type );
583 emit dataChanged( i, i );
586 void QgsStyleModel::rebuildSymbolIcons()
589 mExpressionContext.reset();
595 int maxRowForEntity = 0;
598 maxRowForEntity += mEntityNames[ type ].size();
599 if ( row < maxRowForEntity )
613 if ( type == entity )
616 offset += mEntityNames[ type ].size();
626 : QSortFilterProxyModel( parent )
633 void QgsStyleProxyModel::initialize()
635 setSortCaseSensitivity( Qt::CaseInsensitive );
637 setSourceModel( mModel );
638 setDynamicSortFilter(
true );
646 if ( mSmartGroupId >= 0 )
653 if ( mFavoritesOnly )
669 if ( mSmartGroupId >= 0 )
675 : QSortFilterProxyModel( parent )
677 , mStyle( model->style() )
684 if ( mFilterString.isEmpty() && !mEntityFilterEnabled && !mSymbolTypeFilterEnabled && mTagId < 0 && mSmartGroupId < 0 && !mFavoritesOnly )
687 QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
688 const QString name = sourceModel()->data( index ).toString();
692 if ( mEntityFilterEnabled && ( mEntityFilters.empty() || !mEntityFilters.contains( styleEntityType ) ) )
696 if ( mSymbolTypeFilterEnabled &&
symbolType != mSymbolType )
703 if ( mTagId >= 0 && !mTaggedSymbolNames.contains( name ) )
706 if ( mSmartGroupId >= 0 && !mSmartGroupSymbolNames.contains( name ) )
712 if ( !mFilterString.isEmpty() )
716 const QStringList partsToMatch = mFilterString.trimmed().split(
' ' );
718 QStringList partsToSearch = name.split(
' ' );
719 for (
const QString &tag : tags )
721 partsToSearch.append( tag.split(
' ' ) );
724 for (
const QString &part : partsToMatch )
727 for (
const QString &partToSearch : qgis::as_const( partsToSearch ) )
729 if ( partToSearch.contains( part, Qt::CaseInsensitive ) )
745 mFilterString = filter;
752 return mFavoritesOnly;
768 return mSymbolTypeFilterEnabled;
792 mTaggedSymbolNames.clear();
796 mTaggedSymbolNames.append( mStyle->
symbolsWithTag( entity, mTagId ) );
811 mSmartGroupSymbolNames.clear();
812 if ( mSmartGroupId >= 0 )
822 return mSmartGroupId;
838 return mEntityFilterEnabled;
854 mEntityFilters = QList< QgsStyle::StyleEntity >() <<
entityFilter;
860 mEntityFilters = filters;