27 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
63 : QAbstractItemModel( parent )
70 mEntityNames.insert( entity, mStyle->
allNames( entity ) );
102 switch ( entityType )
109 name = mEntityNames[ entityType ].value(
index.row() - offsetForEntity( entityType ) );
115 case Qt::DisplayRole:
116 case Qt::ToolTipRole:
119 switch (
index.column() )
123 const QStringList tags = mStyle->
tagsOfSymbol( entityType, name );
125 if ( role == Qt::ToolTipRole )
127 QString tooltip = QStringLiteral(
"<h3>%1</h3><p><i>%2</i>" ).arg( name,
128 tags.count() > 0 ? tags.join( QLatin1String(
", " ) ) : tr(
"Not tagged" ) );
130 switch ( entityType )
135 std::unique_ptr< QgsSymbol > symbol( mStyle->
symbol( name ) );
138 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
139 int height =
static_cast< int >( width / 1.61803398875 );
142 QBuffer buffer( &
data );
143 pm.save( &buffer,
"PNG", 100 );
144 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
151 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
152 int height =
static_cast< int >( width / 1.61803398875 );
156 QBuffer buffer( &
data );
157 pm.save( &buffer,
"PNG", 100 );
158 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
164 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
165 int height =
static_cast< int >( width / 1.61803398875 );
169 QBuffer buffer( &
data );
170 pm.save( &buffer,
"PNG", 100 );
171 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
177 int width =
static_cast< int >(
Qgis::UI_SCALE_FACTOR * QFontMetrics(
data(
index, Qt::FontRole ).value< QFont >() ).horizontalAdvance(
'X' ) * 23 );
178 int height =
static_cast< int >( width / 1.61803398875 );
185 QBuffer buffer( &
data );
186 pm.save( &buffer,
"PNG", 100 );
187 tooltip += QStringLiteral(
"<p><img src='data:image/png;base64, %3'>" ).arg( QString(
data.toBase64() ) );
206 return mStyle->
tagsOfSymbol( entityType, name ).join( QLatin1String(
", " ) );
211 case Qt::DecorationRole:
216 if ( !mExpressionContext )
222 mExpressionContext = std::make_unique< QgsExpressionContext >();
226 switch (
index.column() )
229 switch ( entityType )
234 QIcon icon = mIconCache[ entityType ].value( name );
235 if ( !icon.isNull() )
238 std::unique_ptr< QgsSymbol > symbol( mStyle->
symbol( name ) );
241 if ( mAdditionalSizes.isEmpty() )
244 for (
const QSize &s : mAdditionalSizes )
250 mIconCache[ entityType ].insert( name, icon );
256 QIcon icon = mIconCache[ entityType ].value( name );
257 if ( !icon.isNull() )
260 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( name ) );
263 if ( mAdditionalSizes.isEmpty() )
265 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() )
301 if ( mAdditionalSizes.isEmpty() )
303 for (
const QSize &s : mAdditionalSizes )
307 mIconCache[ entityType ].insert( name, icon );
314 QIcon icon = mIconCache[ entityType ].value( name );
315 if ( !icon.isNull() )
323 if ( mAdditionalSizes.isEmpty() )
326 for (
const QSize &s : mAdditionalSizes )
332 mIconCache[ entityType ].insert( name, icon );
342 QIcon icon = mIconCache[ entityType ].value( name );
343 if ( !icon.isNull() )
346 if ( sIconGenerator && !mPending3dSymbolIcons.contains( name ) )
348 mPending3dSymbolIcons.insert( name );
353 if ( mAdditionalSizes.isEmpty() )
355 for (
const QSize &s : mAdditionalSizes )
359 mIconCache[ entityType ].insert( name, icon );
382 return mStyle->
isFavorite( entityType, name );
386 switch ( entityType )
391 return symbol ?
static_cast< int >( symbol->
type() ) : QVariant();
410 switch ( entityType )
429 switch ( entityType )
435 res.reserve( types.size() );
438 res << static_cast< int >( type );
459 return mStyle->
name();
467 #ifndef _MSC_VER // avoid warning
474 if (
index.row() < 0 ||
index.row() >=
rowCount( QModelIndex() ) || role != Qt::EditRole )
477 switch (
index.column() )
483 switch ( entityType )
490 name = mEntityNames[ entityType ].value(
index.row() - offsetForEntity( entityType ) );
494 const QString newName = value.toString();
495 return mStyle->
renameEntity( entityType, name, newName );
507 Qt::ItemFlags
flags = QAbstractItemModel::flags(
index );
510 return flags | Qt::ItemIsEditable;
520 return headerDataStatic( section, orientation, role );
523 QVariant QgsStyleModel::headerDataStatic(
int section, Qt::Orientation orientation,
int role )
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 );
652 const int offset = offsetForEntity( type );
653 QModelIndex i =
index( offset + mEntityNames[ type ].indexOf( name ),
Name );
654 emit dataChanged( i, i, QVector< int >() << Role::IsFavoriteRole );
657 void QgsStyleModel::onEntityRename(
QgsStyle::StyleEntity type,
const QString &oldName,
const QString &newName )
659 mIconCache[ type ].remove( oldName );
660 const QStringList oldSymbolNames = mEntityNames[ type ];
661 const QStringList newSymbolNames = mStyle->
allNames( type );
664 const int oldNameIndex = oldSymbolNames.indexOf( oldName );
665 if ( oldNameIndex < 0 )
669 const int newNameIndex = newSymbolNames.indexOf( newName );
670 if ( newNameIndex < 0 )
673 if ( newNameIndex == oldNameIndex )
675 mEntityNames[ type ] = newSymbolNames;
679 const int offset = offsetForEntity( type );
680 beginMoveRows( QModelIndex(), oldNameIndex + offset, oldNameIndex + offset, QModelIndex(), ( newNameIndex > oldNameIndex ? newNameIndex + 1 : newNameIndex ) + offset );
681 mEntityNames[ type ] = newSymbolNames;
685 void QgsStyleModel::onTagsChanged(
int entity,
const QString &name,
const QStringList & )
688 int row = mEntityNames[type].indexOf( name ) + offsetForEntity( type );
701 void QgsStyleModel::rebuildSymbolIcons()
704 mExpressionContext.reset();
708 void QgsStyleModel::iconGenerated(
QgsStyle::StyleEntity type,
const QString &name,
const QIcon &icon )
710 int row = mEntityNames[type].indexOf( name ) + offsetForEntity( type );
715 mPending3dSymbolIcons.remove( name );
717 emit dataChanged(
index( row, 0 ),
index( row, 0 ) );
733 int maxRowForEntity = 0;
736 maxRowForEntity += mEntityNames[ type ].size();
737 if ( row < maxRowForEntity )
751 if ( type == entity )
754 offset += mEntityNames[ type ].size();
764 : QSortFilterProxyModel( parent )
768 setSourceModel( mModel );
772 void QgsStyleProxyModel::initialize()
774 setSortCaseSensitivity( Qt::CaseInsensitive );
776 setDynamicSortFilter(
true );
786 if ( mSmartGroupId >= 0 )
802 if ( mSmartGroupId >= 0 )
809 : QSortFilterProxyModel( parent )
811 , mStyle( model->style() )
813 setSourceModel( mModel );
817 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
819 : QSortFilterProxyModel( parent )
820 , mCombinedModel( model )
822 setSourceModel( mCombinedModel );
829 if ( mFilterString.isEmpty() && !mEntityFilterEnabled && !mSymbolTypeFilterEnabled && mTagId < 0 && mSmartGroupId < 0 && !mFavoritesOnly && mTagFilter.isEmpty() )
832 const QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
837 const QString name = sourceModel()->data( index ).toString();
841 if ( mEntityFilterEnabled && ( mEntityFilters.empty() || !mEntityFilters.contains( styleEntityType ) ) )
845 if ( mSymbolTypeFilterEnabled &&
symbolType != mSymbolType )
850 switch ( styleEntityType )
870 if ( !types.empty() && !types.contains( mLayerType ) )
877 if ( mTagId >= 0 && !mTaggedSymbolNames.contains( name ) )
880 if ( mSmartGroupId >= 0 && !mSmartGroupSymbolNames.contains( name ) )
883 if ( !mTagFilter.isEmpty() && !tags.contains( mTagFilter, Qt::CaseInsensitive ) )
889 if ( !mFilterString.isEmpty() )
893 const QStringList partsToMatch = mFilterString.trimmed().split(
' ' );
895 QStringList partsToSearch = name.split(
' ' );
896 for (
const QString &tag : tags )
898 partsToSearch.append( tag.split(
' ' ) );
901 for (
const QString &part : partsToMatch )
904 for (
const QString &partToSearch : std::as_const( partsToSearch ) )
906 if ( partToSearch.contains( part, Qt::CaseInsensitive ) )
924 if ( leftSource != rightSource )
925 return QString::localeAwareCompare( leftSource, rightSource ) < 0;
929 return QString::localeAwareCompare( leftName, rightName ) < 0;
934 mFilterString = filter;
941 return mFavoritesOnly;
954 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
955 if ( mCombinedModel )
962 return mSymbolTypeFilterEnabled;
967 mSymbolTypeFilterEnabled = enabled;
988 mTaggedSymbolNames.clear();
992 mTaggedSymbolNames.append( mStyle->
symbolsWithTag( entity, mTagId ) );
1022 mSmartGroupSymbolNames.clear();
1023 if ( mSmartGroupId >= 0 )
1034 return mSmartGroupId;
1050 return mEntityFilterEnabled;
1066 mEntityFilters = QList< QgsStyle::StyleEntity >() <<
entityFilter;
1072 mEntityFilters = filters;