Skip to content

Commit ab2589b

Browse files
committed
Added rasterLayer method
1 parent ef89a62 commit ab2589b

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

python/core/qgsmimedatautils.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ Returns encoded representation of the object
4848
:rtype: QgsVectorLayer
4949
%End
5050

51+
QgsRasterLayer *rasterLayer( bool &owner, QString &error ) const;
52+
%Docstring
53+
Get raster layer from uri if possible, otherwise returns 0 and error is set
54+
\param owner set to true if caller becomes owner
55+
\param error set to error message if cannot get raster
56+
:rtype: QgsRasterLayer
57+
%End
58+
5159
QString layerType;
5260
%Docstring
5361
Type of URI. Recognized types: "vector" / "raster" / "plugin" / "custom"

src/core/qgsmimedatautils.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ QgsVectorLayer *QgsMimeDataUtils::Uri::vectorLayer( bool &owner, QString &error
100100
return new QgsVectorLayer( uri, name, providerKey );
101101
}
102102

103+
QgsRasterLayer *QgsMimeDataUtils::Uri::rasterLayer( bool &owner, QString &error ) const
104+
{
105+
owner = false;
106+
if ( layerType != QLatin1String( "raster" ) )
107+
{
108+
error = QObject::tr( "%1: Not a raster layer." ).arg( name );
109+
return nullptr;
110+
}
111+
owner = true;
112+
return new QgsRasterLayer( uri, name, providerKey );
113+
}
114+
103115
// -----
104116

105117
bool QgsMimeDataUtils::isUriList( const QMimeData *data )

src/core/qgsmimedatautils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
class QgsLayerItem;
2424
class QgsLayerTreeNode;
2525
class QgsVectorLayer;
26+
class QgsRasterLayer;
2627

2728
/** \ingroup core
2829
* \class QgsMimeDataUtils
@@ -51,6 +52,12 @@ class CORE_EXPORT QgsMimeDataUtils
5152
*/
5253
QgsVectorLayer *vectorLayer( bool &owner, QString &error ) const;
5354

55+
/** Get raster layer from uri if possible, otherwise returns 0 and error is set
56+
* \param owner set to true if caller becomes owner
57+
* \param error set to error message if cannot get raster
58+
*/
59+
QgsRasterLayer *rasterLayer( bool &owner, QString &error ) const;
60+
5461
//! Type of URI. Recognized types: "vector" / "raster" / "plugin" / "custom"
5562
QString layerType;
5663
//! For "vector" / "raster" type: provider id.

0 commit comments

Comments
 (0)