QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
45 QgsMimeDataUtils::Uri::Uri( QString& encData )
46 {
47  QStringList decoded = decode( encData );
48  if ( decoded.size() < 4 )
49  return;
50 
51  layerType = decoded[0];
52  providerKey = decoded[1];
53  name = decoded[2];
54  uri = decoded[3];
55 
56  if ( layerType == "raster" && decoded.size() == 6 )
57  {
58  supportedCrs = decode( decoded[4] );
59  supportedFormats = decode( decoded[5] );
60  }
61  else
62  {
63  supportedCrs.clear();
64  supportedFormats.clear();
65  }
66 
67  QgsDebugMsg( QString( "type:%1 key:%2 name:%3 uri:%4 supportedCRS:%5 supportedFormats:%6" )
68  .arg( layerType ).arg( providerKey ).arg( name ).arg( uri )
69  .arg( supportedCrs.join( ", " ) )
70  .arg( supportedFormats.join( ", " ) ) );
71 }
72 
74 {
75  return encode( QStringList() << layerType << providerKey << name << uri << encode( supportedCrs ) << encode( supportedFormats ) );
76 }
77 
78 // -----
79 
80 bool QgsMimeDataUtils::isUriList( const QMimeData* data )
81 {
82  return data->hasFormat( QGIS_URILIST_MIMETYPE );
83 }
84 
86 {
87  QMimeData *mimeData = new QMimeData();
88  QByteArray encodedData;
89 
90  QDataStream stream( &encodedData, QIODevice::WriteOnly );
91  foreach ( const Uri& u, layers )
92  {
93  stream << u.data();
94  }
95 
96  mimeData->setData( QGIS_URILIST_MIMETYPE, encodedData );
97  return mimeData;
98 }
99 
100 
102 {
103  QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
104  QDataStream stream( &encodedData, QIODevice::ReadOnly );
105  QString xUri; // extended uri: layer_type:provider_key:uri
106  UriList list;
107  while ( !stream.atEnd() )
108  {
109  stream >> xUri;
110  QgsDebugMsg( xUri );
111  list.append( Uri( xUri ) );
112  }
113  return list;
114 }
115 
116 QString QgsMimeDataUtils::encode( const QStringList& items )
117 {
118  QString encoded;
119  foreach ( const QString& item, items )
120  {
121  QString str = item;
122  str.replace( ":", "\\:" );
123  encoded += str + ":";
124  }
125  return encoded.left( encoded.length() - 1 );
126 }
127 
128 QStringList QgsMimeDataUtils::decode( const QString& encoded )
129 {
130  QStringList items;
131  QString item;
132  bool inEscape = false;
133  foreach ( const QChar& c, encoded )
134  {
135  if ( c == '\\' && inEscape )
136  {
137  item += c;
138  }
139  else if ( c == '\\' )
140  {
141  inEscape = true;
142  }
143  else if ( c == ':' && !inEscape )
144  {
145  items.append( item );
146  item = "";
147  }
148  else
149  {
150  item += c;
151  inEscape = false;
152  }
153  }
154  items.append( item );
155  return items;
156 }
157 
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
static UriList decodeUriList(const QMimeData *data)
static bool isUriList(const QMimeData *data)
static QMimeData * encodeUriList(UriList layers)
Uri(QgsLayerItem *layer)
QgsMapLayer::LayerType mapLayerType()
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:249
static const char * QGIS_URILIST_MIMETYPE
QList< Uri > UriList