From 0e8be09fd0d71889dc12bb006b6df882111784d3 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 9 Apr 2020 20:33:17 +1000 Subject: [PATCH] Remove a bunch of duplicate code and simplify interface for QgsStyle/QgsStyleModel --- .../auto_generated/symbology/qgsstyle.sip.in | 9 + src/core/symbology/qgsstyle.cpp | 241 +++------ src/core/symbology/qgsstyle.h | 20 +- src/core/symbology/qgsstylemodel.cpp | 466 +++++------------- src/core/symbology/qgsstylemodel.h | 37 +- 5 files changed, 222 insertions(+), 551 deletions(-) diff --git a/python/core/auto_generated/symbology/qgsstyle.sip.in b/python/core/auto_generated/symbology/qgsstyle.sip.in index 97223e0dea5..c905e9860cd 100644 --- a/python/core/auto_generated/symbology/qgsstyle.sip.in +++ b/python/core/auto_generated/symbology/qgsstyle.sip.in @@ -353,6 +353,15 @@ Removes all tags for the specified symbol or colorramp bool removeSymbol( const QString &name ); %Docstring Removes symbol from style (and delete it) +%End + + bool renameEntity( StyleEntity type, const QString &oldName, const QString &newName ); +%Docstring +Renames an entity of the specified ``type`` from ``oldName`` to ``newName``. + +Returns ``True`` if the entity was successfully renamed. + +.. versionadded:: 3.14 %End bool renameSymbol( const QString &oldName, const QString &newName ); diff --git a/src/core/symbology/qgsstyle.cpp b/src/core/symbology/qgsstyle.cpp index 864a6cf4869..e9dc7b86fed 100644 --- a/src/core/symbology/qgsstyle.cpp +++ b/src/core/symbology/qgsstyle.cpp @@ -110,15 +110,9 @@ void QgsStyle::clear() mSymbols.clear(); mColorRamps.clear(); mTextFormats.clear(); - mCachedColorRampTags.clear(); - mCachedSymbolTags.clear(); - mCachedTextFormatTags.clear(); - mCachedLabelSettingsTags.clear(); - mCachedSymbolFavorites.clear(); - mCachedColorRampFavorites.clear(); - mCachedTextFormatFavorites.clear(); - mCachedLabelSettingsFavorites.clear(); + mCachedTags.clear(); + mCachedFavorites.clear(); } bool QgsStyle::addSymbol( const QString &name, QgsSymbol *symbol, bool update ) @@ -169,7 +163,7 @@ bool QgsStyle::saveSymbol( const QString &name, QgsSymbol *symbol, bool favorite return false; } - mCachedSymbolFavorites[ name ] = favorite; + mCachedFavorites[ SymbolEntity ].insert( name, favorite ); tagSymbol( SymbolEntity, name, tags ); @@ -204,13 +198,36 @@ bool QgsStyle::removeSymbol( const QString &name ) const bool result = remove( SymbolEntity, symbolid ); if ( result ) { - mCachedSymbolTags.remove( name ); - mCachedSymbolFavorites.remove( name ); + mCachedTags[ SymbolEntity ].remove( name ); + mCachedFavorites[ SymbolEntity ].remove( name ); emit symbolRemoved( name ); } return result; } +bool QgsStyle::renameEntity( QgsStyle::StyleEntity type, const QString &oldName, const QString &newName ) +{ + switch ( type ) + { + case SymbolEntity: + return renameSymbol( oldName, newName ); + + case ColorrampEntity: + return renameColorRamp( oldName, newName ); + + case TextFormatEntity: + return renameTextFormat( oldName, newName ); + + case LabelSettingsEntity: + return renameLabelSettings( oldName, newName ); + + case TagEntity: + case SmartgroupEntity: + return false; + } + return false; +} + QgsSymbol *QgsStyle::symbol( const QString &name ) { const QgsSymbol *symbol = symbolRef( name ); @@ -323,7 +340,7 @@ bool QgsStyle::saveColorRamp( const QString &name, QgsColorRamp *ramp, bool favo return false; } - mCachedColorRampFavorites[ name ] = favorite; + mCachedFavorites[ ColorrampEntity ].insert( name, favorite ); tagSymbol( ColorrampEntity, name, tags ); @@ -345,8 +362,8 @@ bool QgsStyle::removeColorRamp( const QString &name ) return false; } - mCachedColorRampTags.remove( name ); - mCachedColorRampFavorites.remove( name ); + mCachedTags[ ColorrampEntity ].remove( name ); + mCachedFavorites[ ColorrampEntity ].remove( name ); emit rampRemoved( name ); @@ -667,8 +684,8 @@ bool QgsStyle::renameSymbol( const QString &oldName, const QString &newName ) return false; } - mCachedSymbolTags.remove( oldName ); - mCachedSymbolFavorites.remove( oldName ); + mCachedTags[ SymbolEntity ].remove( oldName ); + mCachedFavorites[ SymbolEntity ].remove( oldName ); const bool result = rename( SymbolEntity, symbolid, newName ); if ( result ) @@ -690,8 +707,8 @@ bool QgsStyle::renameColorRamp( const QString &oldName, const QString &newName ) return false; mColorRamps.insert( newName, ramp ); - mCachedColorRampTags.remove( oldName ); - mCachedColorRampFavorites.remove( oldName ); + mCachedTags[ ColorrampEntity ].remove( oldName ); + mCachedFavorites[ ColorrampEntity ].remove( oldName ); int rampid = 0; sqlite3_statement_unique_ptr statement; @@ -733,7 +750,7 @@ bool QgsStyle::saveTextFormat( const QString &name, const QgsTextFormat &format, return false; } - mCachedTextFormatFavorites[ name ] = favorite; + mCachedFavorites[ TextFormatEntity ].insert( name, favorite ); tagSymbol( TextFormatEntity, name, tags ); @@ -756,8 +773,8 @@ bool QgsStyle::removeTextFormat( const QString &name ) return false; } - mCachedTextFormatTags.remove( name ); - mCachedTextFormatFavorites.remove( name ); + mCachedTags[ TextFormatEntity ].remove( name ); + mCachedFavorites[ TextFormatEntity ].remove( name ); emit textFormatRemoved( name ); @@ -778,8 +795,8 @@ bool QgsStyle::renameTextFormat( const QString &oldName, const QString &newName QgsTextFormat format = mTextFormats.take( oldName ); mTextFormats.insert( newName, format ); - mCachedTextFormatTags.remove( oldName ); - mCachedTextFormatFavorites.remove( oldName ); + mCachedTags[ TextFormatEntity ].remove( oldName ); + mCachedFavorites[ TextFormatEntity ].remove( oldName ); int textFormatId = 0; sqlite3_statement_unique_ptr statement; @@ -821,7 +838,7 @@ bool QgsStyle::saveLabelSettings( const QString &name, const QgsPalLayerSettings return false; } - mCachedLabelSettingsFavorites[ name ] = favorite; + mCachedFavorites[ LabelSettingsEntity ].insert( name, favorite ); tagSymbol( LabelSettingsEntity, name, tags ); @@ -844,8 +861,8 @@ bool QgsStyle::removeLabelSettings( const QString &name ) return false; } - mCachedLabelSettingsTags.remove( name ); - mCachedLabelSettingsFavorites.remove( name ); + mCachedTags[ LabelSettingsEntity ].remove( name ); + mCachedFavorites[ LabelSettingsEntity ].remove( name ); emit labelSettingsRemoved( name ); @@ -865,8 +882,8 @@ bool QgsStyle::renameLabelSettings( const QString &oldName, const QString &newNa QgsPalLayerSettings settings = mLabelSettings.take( oldName ); mLabelSettings.insert( newName, settings ); - mCachedLabelSettingsTags.remove( oldName ); - mCachedLabelSettingsFavorites.remove( oldName ); + mCachedTags[ LabelSettingsEntity ].remove( oldName ); + mCachedFavorites[ LabelSettingsEntity ].remove( oldName ); int labelSettingsId = 0; sqlite3_statement_unique_ptr statement; @@ -1080,14 +1097,8 @@ bool QgsStyle::rename( StyleEntity type, int id, const QString &newName ) } else { - mCachedColorRampTags.clear(); - mCachedSymbolTags.clear(); - mCachedTextFormatTags.clear(); - mCachedLabelSettingsTags.clear(); - mCachedSymbolFavorites.clear(); - mCachedColorRampFavorites.clear(); - mCachedTextFormatFavorites.clear(); - mCachedLabelSettingsFavorites.clear(); + mCachedTags.clear(); + mCachedFavorites.clear(); switch ( type ) { @@ -1148,14 +1159,8 @@ bool QgsStyle::remove( StyleEntity type, int id ) } else { - mCachedColorRampTags.clear(); - mCachedSymbolTags.clear(); - mCachedTextFormatTags.clear(); - mCachedLabelSettingsTags.clear(); - mCachedSymbolFavorites.clear(); - mCachedColorRampFavorites.clear(); - mCachedTextFormatFavorites.clear(); - mCachedLabelSettingsFavorites.clear(); + mCachedTags.clear(); + mCachedFavorites.clear(); if ( groupRemoved ) { @@ -1216,21 +1221,13 @@ bool QgsStyle::addFavorite( StyleEntity type, const QString &name ) { switch ( type ) { - case SymbolEntity: - mCachedSymbolFavorites[ name ] = true; - break; - case ColorrampEntity: - mCachedColorRampFavorites[ name ] = true; - break; - case TextFormatEntity: - mCachedTextFormatFavorites[ name ] = true; - break; - case LabelSettingsEntity: - mCachedLabelSettingsFavorites[ name ] = true; - break; case TagEntity: case SmartgroupEntity: break; + + default: + mCachedFavorites[ type ].insert( name, true ); + break; } emit favoritedChanged( type, name, true ); } @@ -1268,21 +1265,13 @@ bool QgsStyle::removeFavorite( StyleEntity type, const QString &name ) { switch ( type ) { - case SymbolEntity: - mCachedSymbolFavorites[ name ] = false; - break; - case ColorrampEntity: - mCachedColorRampFavorites[ name ] = false; - break; - case TextFormatEntity: - mCachedTextFormatFavorites[ name ] = false; - break; - case LabelSettingsEntity: - mCachedLabelSettingsFavorites[ name ] = false; - break; case TagEntity: case SmartgroupEntity: break; + + default: + mCachedFavorites[ type ].insert( name, false ); + break; } emit favoritedChanged( type, name, false ); } @@ -1675,28 +1664,13 @@ QStringList QgsStyle::tagsOfSymbol( StyleEntity type, const QString &symbol ) { switch ( type ) { - case SymbolEntity: - if ( mCachedSymbolTags.contains( symbol ) ) - return mCachedSymbolTags.value( symbol ); - break; - - case ColorrampEntity: - if ( mCachedColorRampTags.contains( symbol ) ) - return mCachedColorRampTags.value( symbol ); - break; - - case TextFormatEntity: - if ( mCachedTextFormatTags.contains( symbol ) ) - return mCachedTextFormatTags.value( symbol ); - break; - - case LabelSettingsEntity: - if ( mCachedLabelSettingsTags.contains( symbol ) ) - return mCachedLabelSettingsTags.value( symbol ); - break; - case TagEntity: case SmartgroupEntity: + return QStringList(); + + default: + if ( mCachedTags[ type ].contains( symbol ) ) + return mCachedTags[ type ].value( symbol ); break; } @@ -1779,25 +1753,13 @@ QStringList QgsStyle::tagsOfSymbol( StyleEntity type, const QString &symbol ) // update cache switch ( type ) { - case SymbolEntity: - mCachedSymbolTags[ symbol ] = tagList; - break; - - case ColorrampEntity: - mCachedColorRampTags[ symbol ] = tagList; - break; - - case TextFormatEntity: - mCachedTextFormatTags[ symbol ] = tagList; - break; - - case LabelSettingsEntity: - mCachedLabelSettingsTags[ symbol ] = tagList; - break; - case TagEntity: case SmartgroupEntity: break; + + default: + mCachedTags[ type ].insert( symbol, tagList ); + break; } return tagList; @@ -1813,29 +1775,14 @@ bool QgsStyle::isFavorite( QgsStyle::StyleEntity type, const QString &name ) switch ( type ) { - case SymbolEntity: - if ( mCachedSymbolFavorites.contains( name ) ) - return mCachedSymbolFavorites.value( name ); - break; - - case ColorrampEntity: - if ( mCachedColorRampFavorites.contains( name ) ) - return mCachedColorRampFavorites.value( name ); - break; - - case TextFormatEntity: - if ( mCachedTextFormatFavorites.contains( name ) ) - return mCachedTextFormatFavorites.value( name ); - break; - - case LabelSettingsEntity: - if ( mCachedLabelSettingsFavorites.contains( name ) ) - return mCachedLabelSettingsFavorites.value( name ); - break; - case TagEntity: case SmartgroupEntity: return false; + + default: + if ( mCachedFavorites[ type ].contains( name ) ) + return mCachedFavorites[ type ].value( name ); + break; } const QStringList names = allNames( type ); @@ -1851,28 +1798,7 @@ bool QgsStyle::isFavorite( QgsStyle::StyleEntity type, const QString &name ) if ( n == name ) res = isFav; - switch ( type ) - { - case SymbolEntity: - mCachedSymbolFavorites[n] = isFav; - break; - - case ColorrampEntity: - mCachedColorRampFavorites[ n ] = isFav; - break; - - case TextFormatEntity: - mCachedTextFormatFavorites[ n ] = isFav; - break; - - case LabelSettingsEntity: - mCachedLabelSettingsFavorites[ n ] = isFav; - break; - - case TagEntity: - case SmartgroupEntity: - return false; - } + mCachedFavorites[ type ].insert( n, isFav ); } return res; } @@ -2858,28 +2784,7 @@ bool QgsStyle::updateSymbol( StyleEntity type, const QString &name ) void QgsStyle::clearCachedTags( QgsStyle::StyleEntity type, const QString &name ) { - switch ( type ) - { - case SymbolEntity: - mCachedSymbolTags.remove( name ); - break; - - case ColorrampEntity: - mCachedColorRampTags.remove( name ); - break; - - case TextFormatEntity: - mCachedTextFormatTags.remove( name ); - break; - - case LabelSettingsEntity: - mCachedLabelSettingsTags.remove( name ); - break; - - case TagEntity: - case SmartgroupEntity: - break; - } + mCachedTags[ type ].remove( name ); } QgsStyle::StyleEntity QgsStyleSymbolEntity::type() const diff --git a/src/core/symbology/qgsstyle.h b/src/core/symbology/qgsstyle.h index 9960361f6dc..48903472fbb 100644 --- a/src/core/symbology/qgsstyle.h +++ b/src/core/symbology/qgsstyle.h @@ -412,6 +412,15 @@ class CORE_EXPORT QgsStyle : public QObject //! Removes symbol from style (and delete it) bool removeSymbol( const QString &name ); + /** + * Renames an entity of the specified \a type from \a oldName to \a newName. + * + * Returns TRUE if the entity was successfully renamed. + * + * \since QGIS 3.14 + */ + bool renameEntity( StyleEntity type, const QString &oldName, const QString &newName ); + /** * Renames a symbol from \a oldName to \a newName. * @@ -865,15 +874,8 @@ class CORE_EXPORT QgsStyle : public QObject QgsTextFormatMap mTextFormats; QgsLabelSettingsMap mLabelSettings; - QHash< QString, QStringList > mCachedSymbolTags; - QHash< QString, QStringList > mCachedColorRampTags; - QHash< QString, QStringList > mCachedTextFormatTags; - QHash< QString, QStringList > mCachedLabelSettingsTags; - - QHash< QString, bool > mCachedSymbolFavorites; - QHash< QString, bool > mCachedColorRampFavorites; - QHash< QString, bool > mCachedTextFormatFavorites; - QHash< QString, bool > mCachedLabelSettingsFavorites; + QHash< QgsStyle::StyleEntity, QHash< QString, QStringList > > mCachedTags; + QHash< QgsStyle::StyleEntity, QHash< QString, bool > > mCachedFavorites; QString mErrorString; QString mFileName; diff --git a/src/core/symbology/qgsstylemodel.cpp b/src/core/symbology/qgsstylemodel.cpp index d73e77f4f0d..17068e37360 100644 --- a/src/core/symbology/qgsstylemodel.cpp +++ b/src/core/symbology/qgsstylemodel.cpp @@ -25,35 +25,38 @@ const double ICON_PADDING_FACTOR = 0.16; +const auto ENTITIES = { QgsStyle::SymbolEntity, QgsStyle::ColorrampEntity, QgsStyle::TextFormatEntity, QgsStyle::LabelSettingsEntity }; + QgsStyleModel::QgsStyleModel( QgsStyle *style, QObject *parent ) : QAbstractItemModel( parent ) , mStyle( style ) { Q_ASSERT( mStyle ); - mSymbolNames = mStyle->symbolNames(); - mRampNames = mStyle->colorRampNames(); - mTextFormatNames = mStyle->textFormatNames(); - mLabelSettingsNames = mStyle->labelSettingsNames(); - connect( mStyle, &QgsStyle::symbolSaved, this, &QgsStyleModel::onSymbolAdded ); - connect( mStyle, &QgsStyle::symbolRemoved, this, &QgsStyleModel::onSymbolRemoved ); - connect( mStyle, &QgsStyle::symbolRenamed, this, &QgsStyleModel::onSymbolRename ); - connect( mStyle, &QgsStyle::symbolChanged, this, &QgsStyleModel::onSymbolChanged ); + for ( QgsStyle::StyleEntity entity : ENTITIES ) + { + mEntityNames.insert( entity, mStyle->allNames( entity ) ); + } - connect( mStyle, &QgsStyle::rampAdded, this, &QgsStyleModel::onRampAdded ); - connect( mStyle, &QgsStyle::rampChanged, this, &QgsStyleModel::onRampChanged ); - connect( mStyle, &QgsStyle::rampRemoved, this, &QgsStyleModel::onRampRemoved ); - connect( mStyle, &QgsStyle::rampRenamed, this, &QgsStyleModel::onRampRename ); + connect( mStyle, &QgsStyle::symbolSaved, this, [ = ]( const QString & name ) { onEntityAdded( QgsStyle::SymbolEntity, name ); } ); + connect( mStyle, &QgsStyle::symbolRemoved, this, [ = ]( const QString & name ) { onEntityRemoved( QgsStyle::SymbolEntity, name ); } ); + connect( mStyle, &QgsStyle::symbolRenamed, this, [ = ]( const QString & oldName, const QString & newName ) { onEntityRename( QgsStyle::SymbolEntity, oldName, newName ); } ); + connect( mStyle, &QgsStyle::symbolChanged, this, [ = ]( const QString & name ) { onEntityChanged( QgsStyle::SymbolEntity, name ); } ); - connect( mStyle, &QgsStyle::textFormatAdded, this, &QgsStyleModel::onTextFormatAdded ); - connect( mStyle, &QgsStyle::textFormatChanged, this, &QgsStyleModel::onTextFormatChanged ); - connect( mStyle, &QgsStyle::textFormatRemoved, this, &QgsStyleModel::onTextFormatRemoved ); - connect( mStyle, &QgsStyle::textFormatRenamed, this, &QgsStyleModel::onTextFormatRename ); + connect( mStyle, &QgsStyle::rampAdded, this, [ = ]( const QString & name ) { onEntityAdded( QgsStyle::ColorrampEntity, name ); } ); + connect( mStyle, &QgsStyle::rampChanged, this, [ = ]( const QString & name ) { onEntityChanged( QgsStyle::ColorrampEntity, name ); } ); + connect( mStyle, &QgsStyle::rampRemoved, this, [ = ]( const QString & name ) { onEntityRemoved( QgsStyle::ColorrampEntity, name ); } ); + connect( mStyle, &QgsStyle::rampRenamed, this, [ = ]( const QString & oldName, const QString & newName ) { onEntityRename( QgsStyle::ColorrampEntity, oldName, newName ); } ); - connect( mStyle, &QgsStyle::labelSettingsAdded, this, &QgsStyleModel::onLabelSettingsAdded ); - connect( mStyle, &QgsStyle::labelSettingsChanged, this, &QgsStyleModel::onLabelSettingsChanged ); - connect( mStyle, &QgsStyle::labelSettingsRemoved, this, &QgsStyleModel::onLabelSettingsRemoved ); - connect( mStyle, &QgsStyle::labelSettingsRenamed, this, &QgsStyleModel::onLabelSettingsRename ); + connect( mStyle, &QgsStyle::textFormatAdded, this, [ = ]( const QString & name ) { onEntityAdded( QgsStyle::TextFormatEntity, name ); } ); + connect( mStyle, &QgsStyle::textFormatChanged, this, [ = ]( const QString & name ) { onEntityChanged( QgsStyle::TextFormatEntity, name ); } ); + connect( mStyle, &QgsStyle::textFormatRemoved, this, [ = ]( const QString & name ) { onEntityRemoved( QgsStyle::TextFormatEntity, name ); } ); + connect( mStyle, &QgsStyle::textFormatRenamed, this, [ = ]( const QString & oldName, const QString & newName ) { onEntityRename( QgsStyle::TextFormatEntity, oldName, newName ); } ); + + connect( mStyle, &QgsStyle::labelSettingsAdded, this, [ = ]( const QString & name ) { onEntityAdded( QgsStyle::LabelSettingsEntity, name ); } ); + connect( mStyle, &QgsStyle::labelSettingsChanged, this, [ = ]( const QString & name ) { onEntityChanged( QgsStyle::LabelSettingsEntity, name ); } ); + connect( mStyle, &QgsStyle::labelSettingsRemoved, this, [ = ]( const QString & name ) { onEntityRemoved( QgsStyle::LabelSettingsEntity, name ); } ); + connect( mStyle, &QgsStyle::labelSettingsRenamed, this, [ = ]( const QString & oldName, const QString & newName ) { onEntityRename( QgsStyle::LabelSettingsEntity, oldName, newName ); } ); connect( mStyle, &QgsStyle::entityTagsChanged, this, &QgsStyleModel::onTagsChanged ); @@ -81,25 +84,13 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const QString name; switch ( entityType ) { - case QgsStyle::SymbolEntity: - name = mSymbolNames.value( index.row() ); - break; - - case QgsStyle::ColorrampEntity: - name = mRampNames.value( index.row() - mSymbolNames.size() ); - break; - - case QgsStyle::TextFormatEntity: - name = mTextFormatNames.value( index.row() - mSymbolNames.size() - mRampNames.size() ); - break; - - case QgsStyle::LabelSettingsEntity: - name = mLabelSettingsNames.value( index.row() - mSymbolNames.size() - mRampNames.size() - mTextFormatNames.size() ); - break; - case QgsStyle::TagEntity: case QgsStyle::SmartgroupEntity: break; + + default: + name = mEntityNames[ entityType ].value( index.row() - offsetForEntity( entityType ) ); + break; } switch ( role ) @@ -217,7 +208,7 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const case QgsStyle::SymbolEntity: { // use cached icon if possible - QIcon icon = mSymbolIconCache.value( name ); + QIcon icon = mIconCache[ entityType ].value( name ); if ( !icon.isNull() ) return icon; @@ -233,13 +224,13 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const } } - mSymbolIconCache.insert( name, icon ); + mIconCache[ entityType ].insert( name, icon ); return icon; } case QgsStyle::ColorrampEntity: { // use cached icon if possible - QIcon icon = mColorRampIconCache.value( name ); + QIcon icon = mIconCache[ entityType ].value( name ); if ( !icon.isNull() ) return icon; @@ -254,14 +245,14 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const } } - mColorRampIconCache.insert( name, icon ); + mIconCache[ entityType ].insert( name, icon ); return icon; } case QgsStyle::TextFormatEntity: { // use cached icon if possible - QIcon icon = mTextFormatIconCache.value( name ); + QIcon icon = mIconCache[ entityType ].value( name ); if ( !icon.isNull() ) return icon; @@ -272,14 +263,14 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const { icon.addPixmap( QgsTextFormat::textFormatPreviewPixmap( format, s, QString(), static_cast< int >( s.width() * ICON_PADDING_FACTOR ) ) ); } - mTextFormatIconCache.insert( name, icon ); + mIconCache[ entityType ].insert( name, icon ); return icon; } case QgsStyle::LabelSettingsEntity: { // use cached icon if possible - QIcon icon = mLabelSettingsIconCache.value( name ); + QIcon icon = mIconCache[ entityType ].value( name ); if ( !icon.isNull() ) return icon; @@ -290,7 +281,7 @@ QVariant QgsStyleModel::data( const QModelIndex &index, int role ) const { icon.addPixmap( QgsPalLayerSettings::labelSettingsPreviewPixmap( settings, s, QString(), static_cast< int >( s.width() * ICON_PADDING_FACTOR ) ) ); } - mLabelSettingsIconCache.insert( name, icon ); + mIconCache[ entityType ].insert( name, icon ); return icon; } @@ -353,48 +344,17 @@ bool QgsStyleModel::setData( const QModelIndex &index, const QVariant &value, in QString name; switch ( entityType ) { - case QgsStyle::SymbolEntity: - name = mSymbolNames.value( index.row() ); - break; - - case QgsStyle::ColorrampEntity: - name = mRampNames.value( index.row() - mSymbolNames.size() ); - break; - - case QgsStyle::TextFormatEntity: - name = mTextFormatNames.value( index.row() - mSymbolNames.size() - mRampNames.size() ); - break; - - case QgsStyle::LabelSettingsEntity: - name = mLabelSettingsNames.value( index.row() - mSymbolNames.size() - mRampNames.size() - mTextFormatNames.size() ); - break; - case QgsStyle::TagEntity: case QgsStyle::SmartgroupEntity: + return false; + + default: + name = mEntityNames[ entityType ].value( index.row() - offsetForEntity( entityType ) ); break; } const QString newName = value.toString(); - - switch ( entityType ) - { - case QgsStyle::SymbolEntity: - return mStyle->renameSymbol( name, newName ); - - case QgsStyle::ColorrampEntity: - return mStyle->renameColorRamp( name, newName ); - - case QgsStyle::TextFormatEntity: - return mStyle->renameTextFormat( name, newName ); - - case QgsStyle::LabelSettingsEntity: - return mStyle->renameLabelSettings( name, newName ); - - case QgsStyle::TagEntity: - case QgsStyle::SmartgroupEntity: - return false; - } - break; + return mStyle->renameEntity( entityType, name, newName ); } case Tags: @@ -469,7 +429,10 @@ int QgsStyleModel::rowCount( const QModelIndex &parent ) const { if ( !parent.isValid() ) { - return mSymbolNames.count() + mRampNames.count() + mTextFormatNames.count() + mLabelSettingsNames.count(); + int count = 0; + for ( QgsStyle::StyleEntity type : ENTITIES ) + count += mEntityNames[ type ].size(); + return count; } return 0; } @@ -485,57 +448,57 @@ void QgsStyleModel::addDesiredIconSize( QSize size ) return; mAdditionalSizes << size; - mSymbolIconCache.clear(); - mColorRampIconCache.clear(); - mTextFormatIconCache.clear(); - mLabelSettingsIconCache.clear(); + mIconCache.clear(); } -void QgsStyleModel::onSymbolAdded( const QString &name, QgsSymbol * ) +void QgsStyleModel::onEntityAdded( QgsStyle::StyleEntity type, const QString &name ) { - mSymbolIconCache.remove( name ); - const QStringList oldSymbolNames = mSymbolNames; - const QStringList newSymbolNames = mStyle->symbolNames(); + mIconCache[ type ].remove( name ); + const QStringList oldSymbolNames = mEntityNames[ type ]; + const QStringList newSymbolNames = mStyle->allNames( type ); // find index of newly added symbol const int newNameIndex = newSymbolNames.indexOf( name ); if ( newNameIndex < 0 ) return; // shouldn't happen - beginInsertRows( QModelIndex(), newNameIndex, newNameIndex ); - mSymbolNames = newSymbolNames; + const int offset = offsetForEntity( type ); + beginInsertRows( QModelIndex(), newNameIndex + offset, newNameIndex + offset ); + mEntityNames[ type ] = newSymbolNames; endInsertRows(); } -void QgsStyleModel::onSymbolRemoved( const QString &name ) +void QgsStyleModel::onEntityRemoved( QgsStyle::StyleEntity type, const QString &name ) { - mSymbolIconCache.remove( name ); - const QStringList oldSymbolNames = mSymbolNames; - const QStringList newSymbolNames = mStyle->symbolNames(); + mIconCache[ type ].remove( name ); + const QStringList oldSymbolNames = mEntityNames[ type ]; + const QStringList newSymbolNames = mStyle->allNames( type ); // find index of removed symbol const int oldNameIndex = oldSymbolNames.indexOf( name ); if ( oldNameIndex < 0 ) return; // shouldn't happen - beginRemoveRows( QModelIndex(), oldNameIndex, oldNameIndex ); - mSymbolNames = newSymbolNames; + const int offset = offsetForEntity( type ); + beginRemoveRows( QModelIndex(), oldNameIndex + offset, oldNameIndex + offset ); + mEntityNames[ type ] = newSymbolNames; endRemoveRows(); } -void QgsStyleModel::onSymbolChanged( const QString &name ) +void QgsStyleModel::onEntityChanged( QgsStyle::StyleEntity type, const QString &name ) { - mSymbolIconCache.remove( name ); + mIconCache[ type ].remove( name ); - QModelIndex i = index( mSymbolNames.indexOf( name ), Tags ); + const int offset = offsetForEntity( type ); + QModelIndex i = index( offset + mEntityNames[ type ].indexOf( name ), Tags ); emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole ); } -void QgsStyleModel::onSymbolRename( const QString &oldName, const QString &newName ) +void QgsStyleModel::onEntityRename( QgsStyle::StyleEntity type, const QString &oldName, const QString &newName ) { - mSymbolIconCache.remove( oldName ); - const QStringList oldSymbolNames = mSymbolNames; - const QStringList newSymbolNames = mStyle->symbolNames(); + mIconCache[ type ].remove( oldName ); + const QStringList oldSymbolNames = mEntityNames[ type ]; + const QStringList newSymbolNames = mStyle->allNames( type ); // find index of removed symbol const int oldNameIndex = oldSymbolNames.indexOf( oldName ); @@ -549,257 +512,55 @@ void QgsStyleModel::onSymbolRename( const QString &oldName, const QString &newNa if ( newNameIndex == oldNameIndex ) { - mSymbolNames = newSymbolNames; + mEntityNames[ type ] = newSymbolNames; return; } - beginMoveRows( QModelIndex(), oldNameIndex, oldNameIndex, QModelIndex(), newNameIndex > oldNameIndex ? newNameIndex + 1 : newNameIndex ); - mSymbolNames = newSymbolNames; - endMoveRows(); -} - -void QgsStyleModel::onRampAdded( const QString &name ) -{ - mColorRampIconCache.remove( name ); - const QStringList oldRampNames = mRampNames; - const QStringList newRampNames = mStyle->colorRampNames(); - - // find index of newly added symbol - const int newNameIndex = newRampNames.indexOf( name ); - if ( newNameIndex < 0 ) - return; // shouldn't happen - - beginInsertRows( QModelIndex(), newNameIndex + mSymbolNames.count(), newNameIndex + mSymbolNames.count() ); - mRampNames = newRampNames; - endInsertRows(); -} - -void QgsStyleModel::onRampRemoved( const QString &name ) -{ - mColorRampIconCache.remove( name ); - const QStringList oldRampNames = mRampNames; - const QStringList newRampNames = mStyle->colorRampNames(); - - // find index of removed symbol - const int oldNameIndex = oldRampNames.indexOf( name ); - if ( oldNameIndex < 0 ) - return; // shouldn't happen - - beginRemoveRows( QModelIndex(), oldNameIndex + mSymbolNames.count(), oldNameIndex + mSymbolNames.count() ); - mRampNames = newRampNames; - endRemoveRows(); -} - -void QgsStyleModel::onRampChanged( const QString &name ) -{ - mColorRampIconCache.remove( name ); - - QModelIndex i = index( mSymbolNames.count() + mRampNames.indexOf( name ), Tags ); - emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole ); -} - -void QgsStyleModel::onRampRename( const QString &oldName, const QString &newName ) -{ - mColorRampIconCache.remove( oldName ); - const QStringList oldRampNames = mRampNames; - const QStringList newRampNames = mStyle->colorRampNames(); - - // find index of removed ramp - const int oldNameIndex = oldRampNames.indexOf( oldName ); - if ( oldNameIndex < 0 ) - return; // shouldn't happen - - // find index of newly added ramp - const int newNameIndex = newRampNames.indexOf( newName ); - if ( newNameIndex < 0 ) - return; // shouldn't happen - - if ( newNameIndex == oldNameIndex ) - { - mRampNames = newRampNames; - return; - } - - beginMoveRows( QModelIndex(), oldNameIndex + mSymbolNames.count(), oldNameIndex + mSymbolNames.count(), - QModelIndex(), ( newNameIndex > oldNameIndex ? newNameIndex + 1 : newNameIndex ) + mSymbolNames.count() ); - mRampNames = newRampNames; - endMoveRows(); -} - -void QgsStyleModel::onTextFormatAdded( const QString &name ) -{ - mTextFormatIconCache.remove( name ); - const QStringList oldTextFormatNames = mTextFormatNames; - const QStringList newTextFormatNames = mStyle->textFormatNames(); - - // find index of newly added symbol - const int newNameIndex = newTextFormatNames.indexOf( name ); - if ( newNameIndex < 0 ) - return; // shouldn't happen - - beginInsertRows( QModelIndex(), newNameIndex + mSymbolNames.count() + mRampNames.count(), newNameIndex + mSymbolNames.count() + mRampNames.count() ); - mTextFormatNames = newTextFormatNames; - endInsertRows(); -} - -void QgsStyleModel::onTextFormatRemoved( const QString &name ) -{ - mTextFormatIconCache.remove( name ); - const QStringList oldTextFormatNames = mTextFormatNames; - const QStringList newTextFormatNames = mStyle->textFormatNames(); - - // find index of removed symbol - const int oldNameIndex = oldTextFormatNames.indexOf( name ); - if ( oldNameIndex < 0 ) - return; // shouldn't happen - - beginRemoveRows( QModelIndex(), oldNameIndex + mSymbolNames.count() + mRampNames.count(), oldNameIndex + mSymbolNames.count() + mRampNames.count() ); - mTextFormatNames = newTextFormatNames; - endRemoveRows(); -} - -void QgsStyleModel::onTextFormatChanged( const QString &name ) -{ - mTextFormatIconCache.remove( name ); - - QModelIndex i = index( mSymbolNames.count() + mRampNames.count() + mTextFormatNames.indexOf( name ), Tags ); - emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole ); -} - -void QgsStyleModel::onTextFormatRename( const QString &oldName, const QString &newName ) -{ - mTextFormatIconCache.remove( oldName ); - const QStringList oldTextFormatNames = mTextFormatNames; - const QStringList newTextFormatNames = mStyle->textFormatNames(); - - // find index of removed format - const int oldNameIndex = oldTextFormatNames.indexOf( oldName ); - if ( oldNameIndex < 0 ) - return; // shouldn't happen - - // find index of newly added format - const int newNameIndex = newTextFormatNames.indexOf( newName ); - if ( newNameIndex < 0 ) - return; // shouldn't happen - - if ( newNameIndex == oldNameIndex ) - { - mTextFormatNames = newTextFormatNames; - return; - } - - beginMoveRows( QModelIndex(), oldNameIndex + mSymbolNames.count() + mRampNames.count(), oldNameIndex + mSymbolNames.count() + mRampNames.count(), - QModelIndex(), ( newNameIndex > oldNameIndex ? newNameIndex + 1 : newNameIndex ) + mSymbolNames.count() + mRampNames.count() ); - mTextFormatNames = newTextFormatNames; - endMoveRows(); -} - -void QgsStyleModel::onLabelSettingsAdded( const QString &name ) -{ - mLabelSettingsIconCache.remove( name ); - const QStringList oldLabelSettingsNames = mLabelSettingsNames; - const QStringList newLabelSettingsNames = mStyle->labelSettingsNames(); - - // find index of newly added symbol - const int newNameIndex = newLabelSettingsNames.indexOf( name ); - if ( newNameIndex < 0 ) - return; // shouldn't happen - - beginInsertRows( QModelIndex(), newNameIndex + mSymbolNames.count() + mRampNames.count() + mTextFormatNames.count(), newNameIndex + mSymbolNames.count() + mRampNames.count() + mTextFormatNames.count() ); - mLabelSettingsNames = newLabelSettingsNames; - endInsertRows(); -} - -void QgsStyleModel::onLabelSettingsRemoved( const QString &name ) -{ - mLabelSettingsIconCache.remove( name ); - const QStringList oldLabelSettingsNames = mLabelSettingsNames; - const QStringList newLabelSettingsNames = mStyle->labelSettingsNames(); - - // find index of removed symbol - const int oldNameIndex = oldLabelSettingsNames.indexOf( name ); - if ( oldNameIndex < 0 ) - return; // shouldn't happen - - beginRemoveRows( QModelIndex(), oldNameIndex + mSymbolNames.count() + mRampNames.count() + mTextFormatNames.count(), oldNameIndex + mSymbolNames.count() + mRampNames.count() + mTextFormatNames.count() ); - mLabelSettingsNames = newLabelSettingsNames; - endRemoveRows(); -} - -void QgsStyleModel::onLabelSettingsChanged( const QString &name ) -{ - mLabelSettingsIconCache.remove( name ); - - QModelIndex i = index( mSymbolNames.count() + mRampNames.count() + mTextFormatNames.count() + mLabelSettingsNames.indexOf( name ), Tags ); - emit dataChanged( i, i, QVector< int >() << Qt::DecorationRole ); -} - -void QgsStyleModel::onLabelSettingsRename( const QString &oldName, const QString &newName ) -{ - mLabelSettingsIconCache.remove( oldName ); - const QStringList oldLabelSettingsNames = mLabelSettingsNames; - const QStringList newLabelSettingsNames = mStyle->labelSettingsNames(); - - // find index of removed format - const int oldNameIndex = oldLabelSettingsNames.indexOf( oldName ); - if ( oldNameIndex < 0 ) - return; // shouldn't happen - - // find index of newly added format - const int newNameIndex = newLabelSettingsNames.indexOf( newName ); - if ( newNameIndex < 0 ) - return; // shouldn't happen - - if ( newNameIndex == oldNameIndex ) - { - mLabelSettingsNames = newLabelSettingsNames; - return; - } - - beginMoveRows( QModelIndex(), oldNameIndex + mSymbolNames.count() + mRampNames.count() + mTextFormatNames.count(), oldNameIndex + mSymbolNames.count() + mRampNames.count() + mTextFormatNames.count(), - QModelIndex(), ( newNameIndex > oldNameIndex ? newNameIndex + 1 : newNameIndex ) + mSymbolNames.count() + mRampNames.count() + mTextFormatNames.count() ); - mLabelSettingsNames = newLabelSettingsNames; + const int offset = offsetForEntity( type ); + beginMoveRows( QModelIndex(), oldNameIndex + offset, oldNameIndex + offset, QModelIndex(), ( newNameIndex > oldNameIndex ? newNameIndex + 1 : newNameIndex ) + offset ); + mEntityNames[ type ] = newSymbolNames; endMoveRows(); } void QgsStyleModel::onTagsChanged( int entity, const QString &name, const QStringList & ) { + QgsStyle::StyleEntity type = static_cast< QgsStyle::StyleEntity >( entity ); QModelIndex i; + int row = mEntityNames[type].indexOf( name ) + offsetForEntity( type ); switch ( static_cast< QgsStyle::StyleEntity >( entity ) ) { - case QgsStyle::SymbolEntity: - i = index( mSymbolNames.indexOf( name ), Tags ); - break; - - case QgsStyle::ColorrampEntity: - i = index( mSymbolNames.count() + mRampNames.indexOf( name ), Tags ); - break; - - case QgsStyle::TextFormatEntity: - i = index( mSymbolNames.count() + mRampNames.count() + mTextFormatNames.indexOf( name ), Tags ); - break; - - case QgsStyle::LabelSettingsEntity: - i = index( mSymbolNames.count() + mRampNames.count() + mTextFormatNames.count() + mLabelSettingsNames.indexOf( name ), Tags ); - break; - case QgsStyle::TagEntity: case QgsStyle::SmartgroupEntity: return; + + default: + i = index( row, Tags ); } emit dataChanged( i, i ); } void QgsStyleModel::rebuildSymbolIcons() { - mSymbolIconCache.clear(); + mIconCache[ QgsStyle::SymbolEntity ].clear(); mExpressionContext.reset(); - emit dataChanged( index( 0, 0 ), index( mSymbolNames.count() - 1, 0 ), QVector() << Qt::DecorationRole ); + emit dataChanged( index( 0, 0 ), index( mEntityNames[ QgsStyle::SymbolEntity ].count() - 1, 0 ), QVector() << Qt::DecorationRole ); } QgsStyle::StyleEntity QgsStyleModel::entityTypeFromRow( int row ) const { - if ( row >= mStyle->symbolCount() + mStyle->colorRampCount() + + mTextFormatNames.count() ) + // NOTE -- this is just here to throw warnings when new entity types are added, ensuring that this logic gets upgraded!! + switch ( 0 ) + { + case QgsStyle::LabelSettingsEntity: + case QgsStyle::TextFormatEntity: + case QgsStyle::ColorrampEntity: + case QgsStyle::SymbolEntity: + case QgsStyle::TagEntity: + case QgsStyle::SmartgroupEntity: + break; + } + + if ( row >= mStyle->symbolCount() + mStyle->colorRampCount() + mEntityNames[ QgsStyle::TextFormatEntity ].count() ) return QgsStyle::LabelSettingsEntity; else if ( row >= mStyle->symbolCount() + mStyle->colorRampCount() ) return QgsStyle::TextFormatEntity; @@ -808,6 +569,28 @@ QgsStyle::StyleEntity QgsStyleModel::entityTypeFromRow( int row ) const return QgsStyle::SymbolEntity; } +int QgsStyleModel::offsetForEntity( QgsStyle::StyleEntity entity ) const +{ + int offset = 0; + switch ( entity ) + { + case QgsStyle::LabelSettingsEntity: + offset += mEntityNames[ QgsStyle::TextFormatEntity ].size(); + FALLTHROUGH + case QgsStyle::TextFormatEntity: + offset += mEntityNames[ QgsStyle::ColorrampEntity ].size(); + FALLTHROUGH + case QgsStyle::ColorrampEntity: + offset += mEntityNames[ QgsStyle::SymbolEntity ].size(); + FALLTHROUGH + case QgsStyle::SymbolEntity: + case QgsStyle::TagEntity: + case QgsStyle::SmartgroupEntity: + break; + } + return offset; +} + // // QgsStyleProxyModel // @@ -984,16 +767,11 @@ void QgsStyleProxyModel::setTagId( int id ) { mTagId = id; + mTaggedSymbolNames.clear(); if ( mTagId >= 0 ) { - mTaggedSymbolNames = mStyle->symbolsWithTag( QgsStyle::SymbolEntity, mTagId ); - mTaggedSymbolNames.append( mStyle->symbolsWithTag( QgsStyle::ColorrampEntity, mTagId ) ); - mTaggedSymbolNames.append( mStyle->symbolsWithTag( QgsStyle::TextFormatEntity, mTagId ) ); - mTaggedSymbolNames.append( mStyle->symbolsWithTag( QgsStyle::LabelSettingsEntity, mTagId ) ); - } - else - { - mTaggedSymbolNames.clear(); + for ( QgsStyle::StyleEntity entity : ENTITIES ) + mTaggedSymbolNames.append( mStyle->symbolsWithTag( entity, mTagId ) ); } invalidateFilter(); @@ -1008,18 +786,12 @@ void QgsStyleProxyModel::setSmartGroupId( int id ) { mSmartGroupId = id; + mSmartGroupSymbolNames.clear(); if ( mSmartGroupId >= 0 ) { - mSmartGroupSymbolNames = mStyle->symbolsOfSmartgroup( QgsStyle::SymbolEntity, mSmartGroupId ); - mSmartGroupSymbolNames.append( mStyle->symbolsOfSmartgroup( QgsStyle::ColorrampEntity, mSmartGroupId ) ); - mSmartGroupSymbolNames.append( mStyle->symbolsOfSmartgroup( QgsStyle::TextFormatEntity, mSmartGroupId ) ); - mSmartGroupSymbolNames.append( mStyle->symbolsOfSmartgroup( QgsStyle::LabelSettingsEntity, mSmartGroupId ) ); + for ( QgsStyle::StyleEntity entity : ENTITIES ) + mSmartGroupSymbolNames.append( mStyle->symbolsOfSmartgroup( entity, mSmartGroupId ) ); } - else - { - mSmartGroupSymbolNames.clear(); - } - invalidateFilter(); } diff --git a/src/core/symbology/qgsstylemodel.h b/src/core/symbology/qgsstylemodel.h index a6e5eb36c89..ca23ed5cd53 100644 --- a/src/core/symbology/qgsstylemodel.h +++ b/src/core/symbology/qgsstylemodel.h @@ -100,45 +100,28 @@ class CORE_EXPORT QgsStyleModel: public QAbstractItemModel private slots: - void onSymbolAdded( const QString &name, QgsSymbol *symbol ); - void onSymbolRemoved( const QString &name ); - void onSymbolChanged( const QString &name ); - void onSymbolRename( const QString &oldName, const QString &newName ); - void onRampAdded( const QString &name ); - void onRampRemoved( const QString &name ); - void onRampChanged( const QString &name ); - void onRampRename( const QString &oldName, const QString &newName ); - - void onTextFormatAdded( const QString &name ); - void onTextFormatRemoved( const QString &name ); - void onTextFormatChanged( const QString &name ); - void onTextFormatRename( const QString &oldName, const QString &newName ); - - void onLabelSettingsAdded( const QString &name ); - void onLabelSettingsRemoved( const QString &name ); - void onLabelSettingsChanged( const QString &name ); - void onLabelSettingsRename( const QString &oldName, const QString &newName ); - + void onEntityAdded( QgsStyle::StyleEntity type, const QString &name ); + void onEntityRemoved( QgsStyle::StyleEntity type, const QString &name ); + void onEntityChanged( QgsStyle::StyleEntity type, const QString &name ); + void onEntityRename( QgsStyle::StyleEntity type, const QString &oldName, const QString &newName ); void onTagsChanged( int entity, const QString &name, const QStringList &tags ); void rebuildSymbolIcons(); private: QgsStyle *mStyle = nullptr; - QStringList mSymbolNames; - QStringList mRampNames; - QStringList mTextFormatNames; - QStringList mLabelSettingsNames; + + QHash< QgsStyle::StyleEntity, QStringList > mEntityNames; + QList< QSize > mAdditionalSizes; mutable std::unique_ptr< QgsExpressionContext > mExpressionContext; - mutable QHash< QString, QIcon > mSymbolIconCache; - mutable QHash< QString, QIcon > mColorRampIconCache; - mutable QHash< QString, QIcon > mTextFormatIconCache; - mutable QHash< QString, QIcon > mLabelSettingsIconCache; + mutable QHash< QgsStyle::StyleEntity, QHash< QString, QIcon > > mIconCache; QgsStyle::StyleEntity entityTypeFromRow( int row ) const; + int offsetForEntity( QgsStyle::StyleEntity entity ) const; + }; /**