QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
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" )
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() ) )
74 switch ( layer->
type() )
79 wkbType = qobject_cast< QgsVectorLayer *>( layer )->wkbType();
96 layerType = QStringLiteral(
"vector-tile" );
110 return encode( QStringList() << layerType << providerKey << name << uri << encode( supportedCrs ) << encode( supportedFormats ) << layerId << pId <<
QgsWkbTypes::displayString( wkbType ) );
117 if ( layerType != QLatin1String(
"vector" ) )
119 error = QObject::tr(
"%1: Not a vector layer." ).arg( name );
130 if ( providerKey == QLatin1String(
"memory" ) )
132 error = QObject::tr(
"Cannot get memory layer." );
145 if ( layerType != QLatin1String(
"raster" ) )
147 error = QObject::tr(
"%1: Not a raster layer." ).arg( name );
167 if ( layerType != QLatin1String(
"mesh" ) )
169 error = QObject::tr(
"%1: Not a mesh layer." ).arg( name );
198 return data->hasFormat( QGIS_URILIST_MIMETYPE );
203 QMimeData *mimeData =
new QMimeData();
205 mimeData->setData( QGIS_URILIST_MIMETYPE, uriListToByteArray( layers ) );
212 QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
213 QDataStream stream( &encodedData, QIODevice::ReadOnly );
216 while ( !stream.atEnd() )
220 list.append(
Uri( xUri ) );
232 _addLayerTreeNodeToUriList( child, uris );
251 const auto constNodes = nodes;
253 _addLayerTreeNodeToUriList( node, uris );
254 return uriListToByteArray( uris );
259 if ( uri.
pId.isEmpty() )
262 const qint64 pid = uri.
pId.toLongLong();
263 return pid == QCoreApplication::applicationPid();
266 QString QgsMimeDataUtils::encode(
const QStringList &items )
270 QRegularExpression re( QStringLiteral(
"(?<!\\\\):" ) );
271 const auto constItems = items;
272 for (
const QString &item : constItems )
275 str.replace(
'\\', QStringLiteral(
"\\\\" ) );
276 str.replace( re, QStringLiteral(
"\\:" ) );
277 encoded += str +
':';
279 return encoded.left( encoded.length() - 1 );
282 QStringList QgsMimeDataUtils::decode(
const QString &encoded )
286 bool inEscape =
false;
287 const auto constEncoded = encoded;
288 for ( QChar
c : constEncoded )
290 if (
c ==
'\\' && inEscape )
294 else if (
c ==
'\\' )
298 else if (
c ==
':' && !inEscape )
300 items.append( item );
309 items.append( item );
316 QByteArray encodedData;
318 QDataStream stream( &encodedData, QIODevice::WriteOnly );
319 const auto constLayers = layers;
320 for (
const Uri &u : constLayers )
QgsWkbTypes::Type wkbType
WKB type, if associated with a vector layer, or QgsWkbTypes::Unknown if not yet known.
QString name
Human readable name to be used e.g. in layer tree.
QString uri
Identifier of the data source recognized by its providerKey.
QStringList supportedFormats
#define QgsDebugMsgLevel(str, level)
static QByteArray layerTreeNodesToUriList(const QList< QgsLayerTreeNode * > &nodes)
Returns encoded URI list from a list of layer tree nodes.
QString layerType
Type of URI.
QgsCoordinateTransformContext transformContext
QgsMapLayer * mapLayer() const
Returns the layer from the active project corresponding to this uri (if possible),...
static QgsProject * instance()
Returns the QgsProject singleton instance.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
QList< QgsMimeDataUtils::Uri > UriList
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
static Type parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
QgsVectorLayer * vectorLayer(bool &owner, QString &error) const
Gets vector layer from uri if possible, otherwise returns nullptr and error is set.
QgsMeshLayer * meshLayer(bool &owner, QString &error) const
Gets mesh layer from uri if possible, otherwise returns nullptr and error is set.
static bool hasOriginatedFromCurrentAppInstance(const QgsMimeDataUtils::Uri &uri)
Returns true if uri originated from the current QGIS application instance.
QString pId
Unique ID associated with application instance.
QString data() const
Returns encoded representation of the object.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
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.
QString layerId
Layer ID, if uri is associated with a layer from a QgsProject.
Setting options for loading vector layers.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static QMimeData * encodeUriList(const UriList &layers)
Encodes a URI list to a new QMimeData object.
static UriList decodeUriList(const QMimeData *data)
Uri()=default
Constructs invalid URI.
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
static QString displayString(Type type)
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
@ VectorTileLayer
Added in 3.14.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
QgsMapLayerType type() const
Returns the type of the layer.
static bool isUriList(const QMimeData *data)