15 #include <QStringList> 29 static const char *QGIS_URILIST_MIMETYPE =
"application/x-vnd.qgis.qgis.uri";
34 const QStringList decoded = decode( encData );
35 if ( decoded.size() < 4 )
43 if (
layerType == QLatin1String(
"raster" ) && decoded.size() >= 6 )
54 if ( decoded.size() > 6 )
56 if ( decoded.size() > 7 )
57 pId = decoded.at( 7 );
58 if ( decoded.size() > 8 )
61 QgsDebugMsgLevel( QStringLiteral(
"type:%1 key:%2 name:%3 uri:%4 supportedCRS:%5 supportedFormats:%6" )
70 ,
uri( layer->dataProvider()->dataSourceUri() )
72 ,
pId( QString::number( QCoreApplication::applicationPid() ) )
74 switch ( layer->
type() )
111 if (
layerType != QLatin1String(
"vector" ) )
113 error = QObject::tr(
"%1: Not a vector layer." ).arg(
name );
126 error = QObject::tr(
"Cannot get memory layer." );
139 if (
layerType != QLatin1String(
"raster" ) )
141 error = QObject::tr(
"%1: Not a raster layer." ).arg(
name );
161 if (
layerType != QLatin1String(
"mesh" ) )
163 error = QObject::tr(
"%1: Not a mesh layer." ).arg(
name );
192 return data->hasFormat( QGIS_URILIST_MIMETYPE );
197 QMimeData *mimeData =
new QMimeData();
199 mimeData->setData( QGIS_URILIST_MIMETYPE, uriListToByteArray( layers ) );
206 QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
207 QDataStream stream( &encodedData, QIODevice::ReadOnly );
210 while ( !stream.atEnd() )
214 list.append(
Uri( xUri ) );
226 _addLayerTreeNodeToUriList( child, uris );
245 const auto constNodes = nodes;
247 _addLayerTreeNodeToUriList( node, uris );
248 return uriListToByteArray( uris );
253 if ( uri.
pId.isEmpty() )
256 const qint64 pid = uri.
pId.toLongLong();
257 return pid == QCoreApplication::applicationPid();
260 QString QgsMimeDataUtils::encode(
const QStringList &items )
264 QRegularExpression re( QStringLiteral(
"(?<!\\\\):" ) );
265 const auto constItems = items;
266 for (
const QString &item : constItems )
269 str.replace(
'\\', QStringLiteral(
"\\\\" ) );
270 str.replace( re, QStringLiteral(
"\\:" ) );
271 encoded += str +
':';
273 return encoded.left( encoded.length() - 1 );
276 QStringList QgsMimeDataUtils::decode(
const QString &encoded )
280 bool inEscape =
false;
281 const auto constEncoded = encoded;
282 for ( QChar
c : constEncoded )
284 if (
c ==
'\\' && inEscape )
288 else if (
c ==
'\\' )
292 else if (
c ==
':' && !inEscape )
294 items.append( item );
303 items.append( item );
310 QByteArray encodedData;
312 QDataStream stream( &encodedData, QIODevice::WriteOnly );
313 const auto constLayers = layers;
314 for (
const Uri &u : constLayers )
QString layerType
Type of URI.
static bool hasOriginatedFromCurrentAppInstance(const QgsMimeDataUtils::Uri &uri)
Returns true if uri originated from the current QGIS application instance.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Base class for all map layer types.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
QgsMapLayerType type() const
Returns the type of the layer.
Setting options for loading vector layers.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
static UriList decodeUriList(const QMimeData *data)
QString name
Human readable name to be used e.g. in layer tree.
static QMimeData * encodeUriList(const UriList &layers)
Encodes a URI list to a new QMimeData object.
QStringList supportedFormats
static Type parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
static bool isUriList(const QMimeData *data)
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
QgsMapLayer * mapLayer() const
Returns the layer from the active project corresponding to this uri (if possible), otherwise returns nullptr.
QString data() const
Returns encoded representation of the object.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
#define QgsDebugMsgLevel(str, level)
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
This class is a base class for nodes in a layer tree.
QgsVectorLayer * vectorLayer(bool &owner, QString &error) const
Gets vector layer from uri if possible, otherwise returns nullptr and error is set.
Uri()=default
Constructs invalid URI.
QgsMeshLayer * meshLayer(bool &owner, QString &error) const
Gets mesh layer from uri if possible, otherwise returns nullptr and error is set. ...
QgsCoordinateTransformContext transformContext
QgsMapLayer * layer() const
Returns the map layer associated with this node.
QString pId
Unique ID associated with application instance.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QList< QgsMimeDataUtils::Uri > UriList
static QString displayString(Type type)
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
QString providerKey
For "vector" / "raster" type: provider id.
QString layerId
Layer ID, if uri is associated with a layer from a QgsProject.
QString uri
Identifier of the data source recognized by its providerKey.
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
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.
QgsRasterLayer * rasterLayer(bool &owner, QString &error) const
Gets raster layer from uri if possible, otherwise returns nullptr and error is set.
QgsWkbTypes::Type wkbType
WKB type, if associated with a vector layer, or QgsWkbTypes::Unknown if not yet known.
Represents a vector layer which manages a vector based data sets.
Layer tree node points to a map layer.