15 #include <QStringList> 29 static const char *QGIS_URILIST_MIMETYPE =
"application/x-vnd.qgis.qgis.uri";
34 QStringList decoded = decode( encData );
35 if ( decoded.size() < 4 )
43 if (
layerType == QLatin1String(
"raster" ) && decoded.size() == 6 )
54 QgsDebugMsg( QStringLiteral(
"type:%1 key:%2 name:%3 uri:%4 supportedCRS:%5 supportedFormats:%6" )
68 if (
layerType != QLatin1String(
"vector" ) )
70 error = QObject::tr(
"%1: Not a vector layer." ).arg(
name );
75 QUrl url = QUrl::fromEncoded(
uri.toUtf8() );
76 if ( !url.hasQueryItem( QStringLiteral(
"pid" ) ) || !url.hasQueryItem( QStringLiteral(
"layerid" ) ) )
78 error = QObject::tr(
"Memory layer uri does not contain process or layer id." );
81 qint64 pid = url.queryItemValue( QStringLiteral(
"pid" ) ).toLongLong();
82 if ( pid != QCoreApplication::applicationPid() )
84 error = QObject::tr(
"Memory layer from another QGIS instance." );
87 QString layerId = url.queryItemValue( QStringLiteral(
"layerid" ) );
91 error = QObject::tr(
"Cannot get memory layer." );
103 if (
layerType != QLatin1String(
"raster" ) )
105 error = QObject::tr(
"%1: Not a raster layer." ).arg(
name );
115 if (
layerType != QLatin1String(
"mesh" ) )
117 error = QObject::tr(
"%1: Not a mesh layer." ).arg(
name );
128 return data->hasFormat( QGIS_URILIST_MIMETYPE );
133 QMimeData *mimeData =
new QMimeData();
135 mimeData->setData( QGIS_URILIST_MIMETYPE, uriListToByteArray( layers ) );
142 QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
143 QDataStream stream( &encodedData, QIODevice::ReadOnly );
146 while ( !stream.atEnd() )
150 list.append(
Uri( xUri ) );
161 _addLayerTreeNodeToUriList( child, uris );
175 switch ( layer->
type() )
179 uri.
layerType = QStringLiteral(
"vector" );
180 if ( uri.
providerKey == QStringLiteral(
"memory" ) )
182 QUrl url = QUrl::fromEncoded( uri.
uri.toUtf8() );
183 url.addQueryItem( QStringLiteral(
"pid" ), QString::number( QCoreApplication::applicationPid() ) );
184 url.addQueryItem( QStringLiteral(
"layerid" ), layer->
id() );
185 uri.
uri = QString( url.toEncoded() );
191 uri.
layerType = QStringLiteral(
"raster" );
210 _addLayerTreeNodeToUriList( node, uris );
211 return uriListToByteArray( uris );
214 QString QgsMimeDataUtils::encode(
const QStringList &items )
218 QRegularExpression re(
"(?<!\\\\):" );
219 Q_FOREACH (
const QString &item, items )
222 str.replace(
'\\', QLatin1String(
"\\\\" ) );
223 str.replace( re, QLatin1String(
"\\:" ) );
224 encoded += str +
':';
226 return encoded.left( encoded.length() - 1 );
229 QStringList QgsMimeDataUtils::decode(
const QString &encoded )
233 bool inEscape =
false;
234 Q_FOREACH ( QChar
c, encoded )
236 if ( c ==
'\\' && inEscape )
240 else if ( c ==
'\\' )
244 else if ( c ==
':' && !inEscape )
246 items.append( item );
255 items.append( item );
262 QByteArray encodedData;
264 QDataStream stream( &encodedData, QIODevice::WriteOnly );
265 Q_FOREACH (
const Uri &u, layers )
QString layerType
Type of URI.
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.
virtual QgsDataProvider * dataProvider()
Returns the layer's data provider, it may be null.
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
QgsMapLayer::LayerType type() const
Returns the type of the layer.
virtual QString name() const =0
Returns a provider name.
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
QString data() const
Returns encoded representation of the object.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
virtual QString dataSourceUri(bool expandAuthConfig=false) const
Gets the data source specification.
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. ...
QgsMapLayer * layer() const
Returns the map layer associated with this node.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QList< QgsMimeDataUtils::Uri > UriList
QString providerKey
For "vector" / "raster" type: provider id.
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.
Represents a vector layer which manages a vector based data sets.
Layer tree node points to a map layer.