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( QString(
"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 );
176 uri.
layerType = QStringLiteral(
"vector" );
177 if ( uri.
providerKey == QStringLiteral(
"memory" ) )
179 QUrl url = QUrl::fromEncoded( uri.
uri.toUtf8() );
180 url.addQueryItem( QStringLiteral(
"pid" ), QString::number( QCoreApplication::applicationPid() ) );
181 url.addQueryItem( QStringLiteral(
"layerid" ), layer->
id() );
182 uri.
uri = QString( url.toEncoded() );
187 uri.
layerType = QStringLiteral(
"raster" );
202 _addLayerTreeNodeToUriList( node, uris );
203 return uriListToByteArray( uris );
206 QString QgsMimeDataUtils::encode(
const QStringList &items )
210 QRegularExpression re(
"(?<!\\\\):" );
211 Q_FOREACH (
const QString &item, items )
214 str.replace(
'\\', QLatin1String(
"\\\\" ) );
215 str.replace( re, QLatin1String(
"\\:" ) );
216 encoded += str +
':';
218 return encoded.left( encoded.length() - 1 );
221 QStringList QgsMimeDataUtils::decode(
const QString &encoded )
225 bool inEscape =
false;
226 Q_FOREACH ( QChar
c, encoded )
228 if ( c ==
'\\' && inEscape )
232 else if ( c ==
'\\' )
236 else if ( c ==
':' && !inEscape )
238 items.append( item );
247 items.append( item );
254 QByteArray encodedData;
256 QDataStream stream( &encodedData, QIODevice::WriteOnly );
257 Q_FOREACH (
const Uri &u, layers )
QString layerType
Type of URI. Recognized types: "vector" / "raster" / "mesh" / "plugin" / "custom" / "project"...
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.
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)
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 0 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 0 and error is set.
QgsMapLayer * layer() const
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 0 and error is set.
Represents a vector layer which manages a vector based data sets.
Layer tree node points to a map layer.