diff --git a/python/core/qgsmimedatautils.sip b/python/core/qgsmimedatautils.sip index 6f9c062d79c2..7ab7b35dfb05 100644 --- a/python/core/qgsmimedatautils.sip +++ b/python/core/qgsmimedatautils.sip @@ -48,6 +48,14 @@ Returns encoded representation of the object :rtype: QgsVectorLayer %End + QgsRasterLayer *rasterLayer( bool &owner, QString &error ) const; +%Docstring + Get raster layer from uri if possible, otherwise returns 0 and error is set + \param owner set to true if caller becomes owner + \param error set to error message if cannot get raster + :rtype: QgsRasterLayer +%End + QString layerType; %Docstring Type of URI. Recognized types: "vector" / "raster" / "plugin" / "custom" diff --git a/src/core/qgsmimedatautils.cpp b/src/core/qgsmimedatautils.cpp index 9d1b6cc9a421..7c625efc0032 100644 --- a/src/core/qgsmimedatautils.cpp +++ b/src/core/qgsmimedatautils.cpp @@ -100,6 +100,18 @@ QgsVectorLayer *QgsMimeDataUtils::Uri::vectorLayer( bool &owner, QString &error return new QgsVectorLayer( uri, name, providerKey ); } +QgsRasterLayer *QgsMimeDataUtils::Uri::rasterLayer( bool &owner, QString &error ) const +{ + owner = false; + if ( layerType != QLatin1String( "raster" ) ) + { + error = QObject::tr( "%1: Not a raster layer." ).arg( name ); + return nullptr; + } + owner = true; + return new QgsRasterLayer( uri, name, providerKey ); +} + // ----- bool QgsMimeDataUtils::isUriList( const QMimeData *data ) diff --git a/src/core/qgsmimedatautils.h b/src/core/qgsmimedatautils.h index d7edcb8ae2c2..93eaeaf47366 100644 --- a/src/core/qgsmimedatautils.h +++ b/src/core/qgsmimedatautils.h @@ -23,6 +23,7 @@ class QgsLayerItem; class QgsLayerTreeNode; class QgsVectorLayer; +class QgsRasterLayer; /** \ingroup core * \class QgsMimeDataUtils @@ -51,6 +52,12 @@ class CORE_EXPORT QgsMimeDataUtils */ QgsVectorLayer *vectorLayer( bool &owner, QString &error ) const; + /** Get raster layer from uri if possible, otherwise returns 0 and error is set + * \param owner set to true if caller becomes owner + * \param error set to error message if cannot get raster + */ + QgsRasterLayer *rasterLayer( bool &owner, QString &error ) const; + //! Type of URI. Recognized types: "vector" / "raster" / "plugin" / "custom" QString layerType; //! For "vector" / "raster" type: provider id.