QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <QStringList>
16 
17 #include "qgsmimedatautils.h"
18 
19 #include "qgsdataitem.h"
20 #include "qgslogger.h"
21 
22 static const char* QGIS_URILIST_MIMETYPE = "application/x-vnd.qgis.qgis.uri";
23 
25  : providerKey( layerItem->providerKey() )
26  , name( layerItem->layerName() )
27  , uri( layerItem->uri() )
28  , supportedCrs( layerItem->supportedCRS() )
29  , supportedFormats( layerItem->supportedFormats() )
30 {
31  switch ( layerItem->mapLayerType() )
32  {
34  layerType = "vector";
35  break;
37  layerType = "raster";
38  break;
40  layerType = "plugin";
41  break;
42  }
43 }
44 
46 {
47  QgsDebugMsg( "encData: " + encData );
48  QStringList decoded = decode( encData );
49  if ( decoded.size() < 4 )
50  return;
51 
52  layerType = decoded[0];
53  providerKey = decoded[1];
54  name = decoded[2];
55  uri = decoded[3];
56 
57  if ( layerType == "raster" && decoded.size() == 6 )
58  {
59  supportedCrs = decode( decoded[4] );
60  supportedFormats = decode( decoded[5] );
61  }
62  else
63  {
66  }
67 
68  QgsDebugMsg( QString( "type:%1 key:%2 name:%3 uri:%4 supportedCRS:%5 supportedFormats:%6" )
69  .arg( layerType, providerKey, name, uri,
70  supportedCrs.join( ", " ),
71  supportedFormats.join( ", " ) ) );
72 }
73 
75 {
76  return encode( QStringList() << layerType << providerKey << name << uri << encode( supportedCrs ) << encode( supportedFormats ) );
77 }
78 
79 // -----
80 
82 {
83  return data->hasFormat( QGIS_URILIST_MIMETYPE );
84 }
85 
87 {
88  QMimeData *mimeData = new QMimeData();
89  QByteArray encodedData;
90 
91  QDataStream stream( &encodedData, QIODevice::WriteOnly );
92  Q_FOREACH ( const Uri& u, layers )
93  {
94  stream << u.data();
95  }
96 
97  mimeData->setData( QGIS_URILIST_MIMETYPE, encodedData );
98  return mimeData;
99 }
100 
101 
103 {
104  QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
105  QDataStream stream( &encodedData, QIODevice::ReadOnly );
106  QString xUri; // extended uri: layer_type:provider_key:uri
107  UriList list;
108  while ( !stream.atEnd() )
109  {
110  stream >> xUri;
111  QgsDebugMsg( xUri );
112  list.append( Uri( xUri ) );
113  }
114  return list;
115 }
116 
117 QString QgsMimeDataUtils::encode( const QStringList& items )
118 {
119  QString encoded;
120  Q_FOREACH ( const QString& item, items )
121  {
122  QString str = item;
123  str.replace( '\\', "\\\\" );
124  str.replace( ':', "\\:" );
125  encoded += str + ':';
126  }
127  return encoded.left( encoded.length() - 1 );
128 }
129 
130 QStringList QgsMimeDataUtils::decode( const QString& encoded )
131 {
132  QStringList items;
133  QString item;
134  bool inEscape = false;
135  Q_FOREACH ( QChar c, encoded )
136  {
137  if ( c == '\\' && inEscape )
138  {
139  item += c;
140  }
141  else if ( c == '\\' )
142  {
143  inEscape = true;
144  }
145  else if ( c == ':' && !inEscape )
146  {
147  items.append( item );
148  item = "";
149  }
150  else
151  {
152  item += c;
153  inEscape = false;
154  }
155  }
156  items.append( item );
157  return items;
158 }
159 
void clear()
QByteArray data(const QString &mimeType) const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
virtual bool hasFormat(const QString &mimeType) const
static UriList decodeUriList(const QMimeData *data)
static QMimeData * encodeUriList(const UriList &layers)
QStringList supportedFormats
QString join(const QString &separator) const
int size() const
static bool isUriList(const QMimeData *data)
void append(const T &value)
bool atEnd() const
QString & replace(int position, int n, QChar after)
Uri(QgsLayerItem *layer)
QgsMapLayer::LayerType mapLayerType()
Returns QgsMapLayer::LayerType.
int length() const
QString left(int n) const
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:308
void setData(const QString &mimeType, const QByteArray &data)
static const char * QGIS_URILIST_MIMETYPE