From 3f249a8b3fd2fb5552d900c9d44b81efdd948036 Mon Sep 17 00:00:00 2001 From: hxbb00 Date: Wed, 20 Nov 2024 13:50:36 +0000 Subject: [PATCH 1/2] feat: Enhance the ability to customize icons for QgsPluginLayer --- src/core/qgsiconutils.cpp | 12 ++++++++++-- src/core/qgspluginlayer.cpp | 5 +++++ src/core/qgspluginlayer.h | 6 ++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/core/qgsiconutils.cpp b/src/core/qgsiconutils.cpp index 00a6ccd60f09..d397087af010 100644 --- a/src/core/qgsiconutils.cpp +++ b/src/core/qgsiconutils.cpp @@ -19,6 +19,7 @@ #include "qgsapplication.h" #include "qgsmaplayer.h" #include "qgsvectorlayer.h" +#include "qgspluginlayer.h" #include @@ -111,14 +112,21 @@ QIcon QgsIconUtils::iconForLayer( const QgsMapLayer *layer ) case Qgis::LayerType::Mesh: case Qgis::LayerType::VectorTile: case Qgis::LayerType::PointCloud: - case Qgis::LayerType::Plugin: case Qgis::LayerType::Annotation: case Qgis::LayerType::Group: case Qgis::LayerType::TiledScene: { return QgsIconUtils::iconForLayerType( layer->type() ); } - + case Qgis::LayerType::Plugin: + { + const QgsPluginLayer *vl = qobject_cast( layer ); + if ( !vl ) + { + return QIcon(); + } + return vl->icon(); + } case Qgis::LayerType::Vector: { const QgsVectorLayer *vl = qobject_cast( layer ); diff --git a/src/core/qgspluginlayer.cpp b/src/core/qgspluginlayer.cpp index 958c6bafcabf..685f219b66ee 100644 --- a/src/core/qgspluginlayer.cpp +++ b/src/core/qgspluginlayer.cpp @@ -56,6 +56,11 @@ const QgsDataProvider *QgsPluginLayer::dataProvider() const return mDataProvider; } +QIcon QgsPluginLayer::icon() const +{ + return QIcon(); +} + // // QgsPluginLayerDataProvider // diff --git a/src/core/qgspluginlayer.h b/src/core/qgspluginlayer.h index 51d4d88525dc..505f0fec9455 100644 --- a/src/core/qgspluginlayer.h +++ b/src/core/qgspluginlayer.h @@ -64,6 +64,12 @@ class CORE_EXPORT QgsPluginLayer : public QgsMapLayer QgsDataProvider *dataProvider() override; const QgsDataProvider *dataProvider() const override SIP_SKIP; + /** + * Returns a icon. + * \returns a icon + */ + virtual QIcon icon() const; + protected: QString mPluginLayerType; QgsDataProvider *mDataProvider; From bf41f6bf9b006d28ec529e0f9ce5ef08e5cfc078 Mon Sep 17 00:00:00 2001 From: hxbb00 Date: Thu, 21 Nov 2024 01:52:54 +0000 Subject: [PATCH 2/2] Apply suggestions from code review --- src/core/qgsiconutils.cpp | 10 ++++++---- src/core/qgspluginlayer.cpp | 3 ++- src/core/qgspluginlayer.h | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/core/qgsiconutils.cpp b/src/core/qgsiconutils.cpp index d397087af010..f05fc9e5e8e1 100644 --- a/src/core/qgsiconutils.cpp +++ b/src/core/qgsiconutils.cpp @@ -120,12 +120,14 @@ QIcon QgsIconUtils::iconForLayer( const QgsMapLayer *layer ) } case Qgis::LayerType::Plugin: { - const QgsPluginLayer *vl = qobject_cast( layer ); - if ( !vl ) + if ( const QgsPluginLayer *pl = qobject_cast( layer ) ) { - return QIcon(); + const QIcon icon = pl->icon(); + if ( !icon.isNull() ) + return icon; } - return vl->icon(); + // fallback to default icon if layer did not provide a specific icon + return QgsIconUtils::iconForLayerType( layer->type() ); } case Qgis::LayerType::Vector: { diff --git a/src/core/qgspluginlayer.cpp b/src/core/qgspluginlayer.cpp index 685f219b66ee..b5916ffb4ae7 100644 --- a/src/core/qgspluginlayer.cpp +++ b/src/core/qgspluginlayer.cpp @@ -14,6 +14,7 @@ ***************************************************************************/ #include "qgspluginlayer.h" #include "moc_qgspluginlayer.cpp" +#include "qgsiconutils.h" QgsPluginLayer::QgsPluginLayer( const QString &layerType, const QString &layerName ) : QgsMapLayer( Qgis::LayerType::Plugin, layerName ) @@ -58,7 +59,7 @@ const QgsDataProvider *QgsPluginLayer::dataProvider() const QIcon QgsPluginLayer::icon() const { - return QIcon(); + return QgsIconUtils::iconForLayerType( Qgis::LayerType::Plugin ); } // diff --git a/src/core/qgspluginlayer.h b/src/core/qgspluginlayer.h index 505f0fec9455..ca5af6840c4e 100644 --- a/src/core/qgspluginlayer.h +++ b/src/core/qgspluginlayer.h @@ -65,8 +65,8 @@ class CORE_EXPORT QgsPluginLayer : public QgsMapLayer const QgsDataProvider *dataProvider() const override SIP_SKIP; /** - * Returns a icon. - * \returns a icon + * Returns an icon for the layer. + * \since QGIS 3.42 */ virtual QIcon icon() const;