15 #include <QStringList>
28 #include <QRegularExpression>
30 static const char *QGIS_URILIST_MIMETYPE =
"application/x-vnd.qgis.qgis.uri";
35 const QStringList decoded = decode( encData );
36 if ( decoded.size() < 4 )
44 if (
layerType == QLatin1String(
"raster" ) && decoded.size() >= 6 )
55 if ( decoded.size() > 6 )
57 if ( decoded.size() > 7 )
58 pId = decoded.at( 7 );
59 if ( decoded.size() > 8 )
62 QgsDebugMsgLevel( QStringLiteral(
"type:%1 key:%2 name:%3 uri:%4 supportedCRS:%5 supportedFormats:%6" )
69 : providerKey( layer->providerType() )
70 , name( layer->name() )
71 , uri( layer->dataProvider() ? layer->dataProvider()->dataSourceUri() : layer->source() )
72 , layerId( layer->id() )
73 , pId( QString::number( QCoreApplication::applicationPid() ) )
75 switch ( layer->
type() )
80 wkbType = qobject_cast< QgsVectorLayer *>( layer )->wkbType();
96 layerType = QStringLiteral(
"pointcloud" );
101 layerType = QStringLiteral(
"vector-tile" );
116 return encode( QStringList() << layerType << providerKey << name << uri << encode( supportedCrs ) << encode( supportedFormats ) << layerId << pId <<
QgsWkbTypes::displayString( wkbType ) );
123 if ( layerType != QLatin1String(
"vector" ) )
125 error = QObject::tr(
"%1: Not a vector layer." ).arg( name );
136 if ( providerKey == QLatin1String(
"memory" ) )
138 error = QObject::tr(
"Cannot get memory layer." );
151 if ( layerType != QLatin1String(
"raster" ) )
153 error = QObject::tr(
"%1: Not a raster layer." ).arg( name );
173 if ( layerType != QLatin1String(
"mesh" ) )
175 error = QObject::tr(
"%1: Not a mesh layer." ).arg( name );
204 return data->hasFormat( QGIS_URILIST_MIMETYPE );
209 QMimeData *mimeData =
new QMimeData();
211 mimeData->setData( QGIS_URILIST_MIMETYPE, uriListToByteArray( layers ) );
218 QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
219 QDataStream stream( &encodedData, QIODevice::ReadOnly );
222 while ( !stream.atEnd() )
226 list.append(
Uri( xUri ) );
238 _addLayerTreeNodeToUriList( child, uris );
257 const auto constNodes = nodes;
259 _addLayerTreeNodeToUriList( node, uris );
260 return uriListToByteArray( uris );
265 if ( uri.
pId.isEmpty() )
268 const qint64 pid = uri.
pId.toLongLong();
269 return pid == QCoreApplication::applicationPid();
272 QString QgsMimeDataUtils::encode(
const QStringList &items )
276 QRegularExpression re( QStringLiteral(
"(?<!\\\\):" ) );
277 const auto constItems = items;
278 for (
const QString &item : constItems )
281 str.replace(
'\\', QLatin1String(
"\\\\" ) );
282 str.replace( re, QStringLiteral(
"\\:" ) );
283 encoded +=
str +
':';
285 return encoded.left( encoded.length() - 1 );
288 QStringList QgsMimeDataUtils::decode(
const QString &encoded )
292 bool inEscape =
false;
293 const auto constEncoded = encoded;
294 for ( QChar
c : constEncoded )
296 if (
c ==
'\\' && inEscape )
300 else if (
c ==
'\\' )
304 else if (
c ==
':' && !inEscape )
306 items.append( item );
315 items.append( item );
322 QByteArray encodedData;
324 QDataStream stream( &encodedData, QIODevice::WriteOnly );
325 const auto constLayers = layers;
326 for (
const Uri &u : constLayers )
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 bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
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 Type parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
static QString displayString(Type type) SIP_HOLDGIL
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
@ PointCloudLayer
Added in 3.18.
@ VectorTileLayer
Added in 3.14.
@ AnnotationLayer
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
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.
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.
QgsWkbTypes::Type wkbType
WKB type, if associated with a vector layer, or QgsWkbTypes::Unknown if not yet known.
Uri()=default
Constructs invalid URI.
QStringList supportedFormats
QString layerType
Type of URI.
Setting options for loading vector layers.