15 #include <QStringList> 28 static const char *QGIS_URILIST_MIMETYPE =
"application/x-vnd.qgis.qgis.uri";
33 QStringList decoded = decode( encData );
34 if ( decoded.size() < 4 )
42 if (
layerType == QLatin1String(
"raster" ) && decoded.size() == 6 )
53 QgsDebugMsg( QString(
"type:%1 key:%2 name:%3 uri:%4 supportedCRS:%5 supportedFormats:%6" )
67 if (
layerType != QLatin1String(
"vector" ) )
69 error = QObject::tr(
"%1: Not a vector layer." ).arg(
name );
74 QUrl url = QUrl::fromEncoded(
uri.toUtf8() );
75 if ( !url.hasQueryItem( QStringLiteral(
"pid" ) ) || !url.hasQueryItem( QStringLiteral(
"layerid" ) ) )
77 error = QObject::tr(
"Memory layer uri does not contain process or layer id." );
80 qint64 pid = url.queryItemValue( QStringLiteral(
"pid" ) ).toLongLong();
81 if ( pid != QCoreApplication::applicationPid() )
83 error = QObject::tr(
"Memory layer from another QGIS instance." );
86 QString layerId = url.queryItemValue( QStringLiteral(
"layerid" ) );
90 error = QObject::tr(
"Cannot get memory layer." );
102 if (
layerType != QLatin1String(
"raster" ) )
104 error = QObject::tr(
"%1: Not a raster layer." ).arg(
name );
115 return data->hasFormat( QGIS_URILIST_MIMETYPE );
120 QMimeData *mimeData =
new QMimeData();
122 mimeData->setData( QGIS_URILIST_MIMETYPE, uriListToByteArray( layers ) );
129 QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
130 QDataStream stream( &encodedData, QIODevice::ReadOnly );
133 while ( !stream.atEnd() )
137 list.append(
Uri( xUri ) );
148 _addLayerTreeNodeToUriList( child, uris );
163 uri.
layerType = QStringLiteral(
"vector" );
164 if ( uri.
providerKey == QStringLiteral(
"memory" ) )
166 QUrl url = QUrl::fromEncoded( uri.
uri.toUtf8() );
167 url.addQueryItem( QStringLiteral(
"pid" ), QString::number( QCoreApplication::applicationPid() ) );
168 url.addQueryItem( QStringLiteral(
"layerid" ), layer->
id() );
169 uri.
uri = QString( url.toEncoded() );
174 uri.
layerType = QStringLiteral(
"raster" );
189 _addLayerTreeNodeToUriList( node, uris );
190 return uriListToByteArray( uris );
193 QString QgsMimeDataUtils::encode(
const QStringList &items )
196 Q_FOREACH (
const QString &item, items )
199 str.replace(
'\\', QLatin1String(
"\\\\" ) );
200 str.replace(
':', QLatin1String(
"\\:" ) );
201 encoded += str +
':';
203 return encoded.left( encoded.length() - 1 );
206 QStringList QgsMimeDataUtils::decode(
const QString &encoded )
210 bool inEscape =
false;
211 Q_FOREACH ( QChar c, encoded )
213 if ( c ==
'\\' && inEscape )
217 else if ( c ==
'\\' )
221 else if ( c ==
':' && !inEscape )
223 items.append( item );
232 items.append( item );
239 QByteArray encodedData;
241 QDataStream stream( &encodedData, QIODevice::WriteOnly );
242 Q_FOREACH (
const Uri &u, layers )
QString layerType
Type of URI. Recognized types: "vector" / "raster" / "plugin" / "custom".
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
Return a provider name.
static bool isUriList(const QMimeData *data)
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
Get 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
Get vector layer from uri if possible, otherwise returns 0 and error is set.
Uri()=default
Constructs invalid URI.
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.
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
Get 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.