25#include <QRegularExpression>
27static const char *QGIS_URILIST_MIMETYPE =
"application/x-vnd.qgis.qgis.uri";
32 const QStringList decoded = decode( encData );
33 if ( decoded.size() < 4 )
41 if (
layerType == QLatin1String(
"raster" ) && decoded.size() >= 6 )
52 if ( decoded.size() > 6 )
54 if ( decoded.size() > 7 )
55 pId = decoded.at( 7 );
56 if ( decoded.size() > 8 )
58 if ( decoded.size() > 9 )
61 QgsDebugMsgLevel( QStringLiteral(
"type:%1 key:%2 name:%3 uri:%4 supportedCRS:%5 supportedFormats:%6" )
68 : providerKey( layer->providerType() )
69 , name( layer->name() )
70 , uri( layer->dataProvider() ? layer->dataProvider()->dataSourceUri() : layer->source() )
71 , layerId( layer->id() )
72 , pId( QString::number( QCoreApplication::applicationPid() ) )
74 switch ( layer->
type() )
79 wkbType = qobject_cast< QgsVectorLayer *>( layer )->wkbType();
95 layerType = QStringLiteral(
"pointcloud" );
100 layerType = QStringLiteral(
"vector-tile" );
105 layerType = QStringLiteral(
"tiled-scene" );
121 return encode( { layerType,
125 encode( supportedCrs ),
126 encode( supportedFormats ),
138 if ( layerType != QLatin1String(
"vector" ) )
140 error = QObject::tr(
"%1: Not a vector layer." ).arg( name );
151 if ( providerKey == QLatin1String(
"memory" ) )
153 error = QObject::tr(
"Cannot get memory layer." );
166 if ( layerType != QLatin1String(
"raster" ) )
168 error = QObject::tr(
"%1: Not a raster layer." ).arg( name );
188 if ( layerType != QLatin1String(
"mesh" ) )
190 error = QObject::tr(
"%1: Not a mesh layer." ).arg( name );
219 return data->hasFormat( QGIS_URILIST_MIMETYPE );
224 QMimeData *mimeData =
new QMimeData();
226 mimeData->setData( QGIS_URILIST_MIMETYPE, uriListToByteArray( layers ) );
233 QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
234 QDataStream stream( &encodedData, QIODevice::ReadOnly );
237 while ( !stream.atEnd() )
241 list.append(
Uri( xUri ) );
253 _addLayerTreeNodeToUriList( child, uris );
272 const auto constNodes = nodes;
274 _addLayerTreeNodeToUriList( node, uris );
275 return uriListToByteArray( uris );
280 if ( uri.
pId.isEmpty() )
283 const qint64 pid = uri.
pId.toLongLong();
284 return pid == QCoreApplication::applicationPid();
287QString QgsMimeDataUtils::encode(
const QStringList &items )
291 const thread_local QRegularExpression re( QStringLiteral(
"(?<!\\\\):" ) );
292 const auto constItems = items;
293 for (
const QString &item : constItems )
296 str.replace(
'\\', QLatin1String(
"\\\\" ) );
297 str.replace( re, QStringLiteral(
"\\:" ) );
298 encoded +=
str +
':';
300 return encoded.left( encoded.length() - 1 );
303QStringList QgsMimeDataUtils::decode(
const QString &encoded )
307 bool inEscape =
false;
308 const auto constEncoded = encoded;
309 for (
const QChar
c : constEncoded )
311 if (
c ==
'\\' && inEscape )
315 else if (
c ==
'\\' )
319 else if (
c ==
':' && !inEscape )
321 items.append( item );
330 items.append( item );
337 QByteArray encodedData;
339 QDataStream stream( &encodedData, QIODevice::WriteOnly );
340 const auto constLayers = layers;
341 for (
const Uri &u : constLayers )
@ Group
Composite group layer. Added in QGIS 3.24.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Layer tree node points to a map layer.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
This class is a base class for nodes in a layer tree.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Base class for all map layer types.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
static QByteArray layerTreeNodesToUriList(const QList< QgsLayerTreeNode * > &nodes)
Returns encoded URI list from a list of layer tree nodes.
static bool isUriList(const QMimeData *data)
QList< QgsMimeDataUtils::Uri > UriList
static QMimeData * encodeUriList(const UriList &layers)
Encodes a URI list to a new QMimeData object.
static UriList decodeUriList(const QMimeData *data)
static bool hasOriginatedFromCurrentAppInstance(const QgsMimeDataUtils::Uri &uri)
Returns true if uri originated from the current QGIS application instance.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
QgsCoordinateTransformContext transformContext
Represents a raster layer.
Represents a vector layer which manages a vector based data sets.
static Qgis::WkbType parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
static QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define QgsDebugMsgLevel(str, level)
QgsMeshLayer * meshLayer(bool &owner, QString &error) const
Gets mesh layer from uri if possible, otherwise returns nullptr and error is set.
Qgis::WkbType wkbType
WKB type, if associated with a vector layer, or QgsWkbTypes::Unknown if not yet known.
QString filePath
Path to file, if uri is associated with a file.
QString uri
Identifier of the data source recognized by its providerKey.
QString name
Human readable name to be used e.g. in layer tree.
QgsMapLayer * mapLayer() const
Returns the layer from the active project corresponding to this uri (if possible),...
QString pId
Unique ID associated with application instance.
QgsRasterLayer * rasterLayer(bool &owner, QString &error) const
Gets raster layer from uri if possible, otherwise returns nullptr and error is set.
QString providerKey
For "vector" / "raster" type: provider id.
QgsVectorLayer * vectorLayer(bool &owner, QString &error) const
Gets vector layer from uri if possible, otherwise returns nullptr and error is set.
QString layerId
Layer ID, if uri is associated with a layer from a QgsProject.
QString data() const
Returns encoded representation of the object.
Uri()=default
Constructs invalid URI.
QStringList supportedFormats
QString layerType
Type of URI.
Setting options for loading vector layers.