QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgsmimedatautils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmimedatautils.cpp
3 ---------------------
4 begin : November 2011
5 copyright : (C) 2011 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#include "qgsmimedatautils.h"
16
17#include "qgslayertree.h"
18#include "qgslogger.h"
19#include "qgsmaplayerfactory.h"
20#include "qgsmeshlayer.h"
21#include "qgsrasterlayer.h"
22#include "qgsvectorlayer.h"
23
24#include <QRegularExpression>
25#include <QString>
26#include <QStringList>
27
28using namespace Qt::StringLiterals;
29
30static const char *QGIS_URILIST_MIMETYPE = "application/x-vnd.qgis.qgis.uri";
31
32QgsMimeDataUtils::Uri::Uri( const QString &encData )
33{
34 QgsDebugMsgLevel( "encData: " + encData, 4 );
35 const QStringList decoded = decode( encData );
36 if ( decoded.size() < 4 )
37 return;
38
39 layerType = decoded[0];
40 providerKey = decoded[1];
41 name = decoded[2];
42 uri = decoded[3];
43
44 if ( layerType == "raster"_L1 && decoded.size() >= 6 )
45 {
46 supportedCrs = decode( decoded[4] );
47 supportedFormats = decode( decoded[5] );
48 }
49 else
50 {
51 supportedCrs.clear();
52 supportedFormats.clear();
53 }
54
55 if ( decoded.size() > 6 )
56 layerId = decoded.at( 6 );
57 if ( decoded.size() > 7 )
58 pId = decoded.at( 7 );
59 if ( decoded.size() > 8 )
60 wkbType = QgsWkbTypes::parseType( decoded.at( 8 ) );
61 if ( decoded.size() > 9 )
62 filePath = decoded.at( 9 );
63
64 QgsDebugMsgLevel( u"type:%1 key:%2 name:%3 uri:%4 supportedCRS:%5 supportedFormats:%6"_s.arg( layerType, providerKey, name, uri, supportedCrs.join( ',' ), supportedFormats.join( ',' ) ), 2 );
65}
66
68 : providerKey( layer->providerType() )
69 , name( layer->name() )
70 , uri( layer->dataProvider() ? layer->dataProvider()->dataSourceUri() : layer->source() )
71 , layerId( layer->id() )
72 , pId( QString::number( QCoreApplication::applicationPid() ) )
73{
75 switch ( layer->type() )
76 {
78 {
79 wkbType = qobject_cast< QgsVectorLayer *>( layer )->wkbType();
80 break;
81 }
87 {
88 break;
89 }
90
94 {
95 // plugin layers do not have a standard way of storing their URI...
96 return;
97 }
98 }
99}
100
102{
104}
105
106QgsVectorLayer *QgsMimeDataUtils::Uri::vectorLayer( bool &owner, QString &error ) const
107{
108 owner = false;
109 error.clear();
110 if ( layerType != "vector"_L1 )
111 {
112 error = QObject::tr( "%1: Not a vector layer." ).arg( name );
113 return nullptr;
114 }
115
117 {
119 {
120 return vectorLayer;
121 }
122 }
123 if ( providerKey == "memory"_L1 )
124 {
125 error = QObject::tr( "Cannot get memory layer." );
126 return nullptr;
127 }
128
129 owner = true;
130 const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() }; // skip-keyword-check
131 return new QgsVectorLayer( uri, name, providerKey, options );
132}
133
134QgsRasterLayer *QgsMimeDataUtils::Uri::rasterLayer( bool &owner, QString &error ) const
135{
136 owner = false;
137 error.clear();
138 if ( layerType != "raster"_L1 )
139 {
140 error = QObject::tr( "%1: Not a raster layer." ).arg( name );
141 return nullptr;
142 }
143
145 {
147 {
148 return rasterLayer;
149 }
150 }
151
152 owner = true;
153 return new QgsRasterLayer( uri, name, providerKey );
154}
155
156QgsMeshLayer *QgsMimeDataUtils::Uri::meshLayer( bool &owner, QString &error ) const
157{
158 owner = false;
159 error.clear();
160 if ( layerType != "mesh"_L1 )
161 {
162 error = QObject::tr( "%1: Not a mesh layer." ).arg( name );
163 return nullptr;
164 }
165
167 {
169 {
170 return meshLayer;
171 }
172 }
173
174 owner = true;
175 return new QgsMeshLayer( uri, name, providerKey );
176}
177
179{
181 {
182 return QgsProject::instance()->mapLayer( layerId ); // skip-keyword-check
183 }
184 return nullptr;
185}
186
187// -----
188
189bool QgsMimeDataUtils::isUriList( const QMimeData *data )
190{
191 return data->hasFormat( QGIS_URILIST_MIMETYPE );
192}
193
195{
196 QMimeData *mimeData = new QMimeData();
197
198 mimeData->setData( QGIS_URILIST_MIMETYPE, uriListToByteArray( layers ) );
199 return mimeData;
200}
201
202
204{
205 QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
206 QDataStream stream( &encodedData, QIODevice::ReadOnly );
207 QString xUri; // extended uri: layer_type:provider_key:uri
208 UriList list;
209 while ( !stream.atEnd() )
210 {
211 stream >> xUri;
212 QgsDebugMsgLevel( xUri, 4 );
213 list.append( Uri( xUri ) );
214 }
215 return list;
216}
217
218
219static void _addLayerTreeNodeToUriList( QgsLayerTreeNode *node, QgsMimeDataUtils::UriList &uris )
220{
221 if ( QgsLayerTree::isGroup( node ) )
222 {
223 const auto constChildren = QgsLayerTree::toGroup( node )->children();
224 for ( QgsLayerTreeNode *child : constChildren )
225 _addLayerTreeNodeToUriList( child, uris );
226 }
227 else if ( QgsLayerTree::isLayer( node ) )
228 {
229 QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
230 QgsMapLayer *layer = nodeLayer->layer();
231 if ( !layer )
232 return;
233
234 if ( layer->type() == Qgis::LayerType::Plugin )
235 return; // plugin layers do not have a standard way of storing their URI...
236
237 uris << QgsMimeDataUtils::Uri( layer );
238 }
239}
240
241QByteArray QgsMimeDataUtils::layerTreeNodesToUriList( const QList<QgsLayerTreeNode *> &nodes )
242{
243 UriList uris;
244 const auto constNodes = nodes;
245 for ( QgsLayerTreeNode *node : constNodes )
246 _addLayerTreeNodeToUriList( node, uris );
247 return uriListToByteArray( uris );
248}
249
251{
252 if ( uri.pId.isEmpty() )
253 return false;
254
255 const qint64 pid = uri.pId.toLongLong();
256 return pid == QCoreApplication::applicationPid();
257}
258
259QString QgsMimeDataUtils::encode( const QStringList &items )
260{
261 QString encoded;
262 // Do not escape colon twice
263 const thread_local QRegularExpression re( u"(?<!\\\\):"_s );
264 const auto constItems = items;
265 for ( const QString &item : constItems )
266 {
267 QString str = item;
268 str.replace( '\\', "\\\\"_L1 );
269 str.replace( re, u"\\:"_s );
270 encoded += str + ':';
271 }
272 return encoded.left( encoded.length() - 1 );
273}
274
275QStringList QgsMimeDataUtils::decode( const QString &encoded )
276{
277 QStringList items;
278 QString item;
279 bool inEscape = false;
280 const auto constEncoded = encoded;
281 for ( const QChar c : constEncoded )
282 {
283 if ( c == '\\' && inEscape )
284 {
285 item += c;
286 }
287 else if ( c == '\\' )
288 {
289 inEscape = true;
290 }
291 else if ( c == ':' && !inEscape )
292 {
293 items.append( item );
294 item.clear();
295 }
296 else
297 {
298 item += c;
299 inEscape = false;
300 }
301 }
302 items.append( item );
303 return items;
304}
305
306
307QByteArray QgsMimeDataUtils::uriListToByteArray( const QgsMimeDataUtils::UriList &layers )
308{
309 QByteArray encodedData;
310
311 QDataStream stream( &encodedData, QIODevice::WriteOnly );
312 const auto constLayers = layers;
313 for ( const Uri &u : constLayers )
314 {
315 stream << u.data();
316 }
317 return encodedData;
318}
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:214
@ Plugin
Plugin based layer.
Definition qgis.h:209
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
Definition qgis.h:215
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Definition qgis.h:212
@ Vector
Vector layer.
Definition qgis.h:207
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:211
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:210
@ Raster
Raster layer.
Definition qgis.h:208
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Definition qgis.h:213
QgsMapLayer * layer() const
Returns the map layer associated with this node.
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 QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
static QString typeToString(Qgis::LayerType type)
Converts a map layer type to a string value.
Base class for all map layer types.
Definition qgsmaplayer.h:83
Qgis::LayerType type
Definition qgsmaplayer.h:93
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
Definition qgsproject.h:120
Represents a raster layer.
Represents a vector layer which manages a vector based dataset.
static Qgis::WkbType parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
static Q_INVOKABLE QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
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)
Definition qgslogger.h:63
QgsMeshLayer * meshLayer(bool &owner, QString &error) const
Gets mesh layer from uri if possible, otherwise returns nullptr and error is set.
Qgis::WkbType wkbType
WKB type, if associated with a vector layer, or QgsWkbTypes::Unknown if not yet known.
QString filePath
Path to file, if uri is associated with a file.
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.
Uri()=default
Constructs invalid URI.
QString layerType
Type of URI.
Setting options for loading vector layers.