QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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() ), name( layerItem->layerName() ), uri( layerItem->uri() )
26 {
27  switch ( layerItem->mapLayerType() )
28  {
30  layerType = "vector";
31  break;
33  layerType = "raster";
34  break;
36  layerType = "plugin";
37  break;
38  }
39 
40 }
41 
42 QgsMimeDataUtils::Uri::Uri( QString& encData )
43 {
44  QStringList parts;
45  QChar split = ':';
46  QChar escape = '\\';
47  QString part;
48  bool inEscape = false;
49  for ( int i = 0; i < encData.length(); ++i )
50  {
51  if ( encData.at( i ) == escape && !inEscape )
52  {
53  inEscape = true;
54  }
55  else if ( encData.at( i ) == split && !inEscape )
56  {
57  parts << part;
58  part = "";
59  }
60  else
61  {
62  part += encData.at( i );
63  inEscape = false;
64  }
65  }
66  if ( !part.isEmpty() )
67  {
68  parts << part;
69  }
70 
71  if ( parts.size() == 4 )
72  {
73  layerType = parts[0];
74  providerKey = parts[1];
75  name = parts[2];
76  uri = parts[3];
77  QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri );
78  }
79 }
80 
82 {
83  QString escapedName = name;
84  QString escapeUri = uri;
85  escapedName.replace( ":", "\\:" );
86  escapeUri.replace( ":", "\\:" );
87  return layerType + ":" + providerKey + ":" + escapedName + ":" + escapeUri;
88 }
89 
90 // -----
91 
92 bool QgsMimeDataUtils::isUriList( const QMimeData* data )
93 {
94  return data->hasFormat( QGIS_URILIST_MIMETYPE );
95 }
96 
98 {
99  QMimeData *mimeData = new QMimeData();
100  QByteArray encodedData;
101 
102  QDataStream stream( &encodedData, QIODevice::WriteOnly );
103  foreach ( const QgsMimeDataUtils::Uri& u, layers )
104  {
105  stream << u.data();
106  }
107 
108  mimeData->setData( QGIS_URILIST_MIMETYPE, encodedData );
109  return mimeData;
110 }
111 
112 
114 {
115  QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
116  QDataStream stream( &encodedData, QIODevice::ReadOnly );
117  QString xUri; // extended uri: layer_type:provider_key:uri
119  while ( !stream.atEnd() )
120  {
121  stream >> xUri;
122  QgsDebugMsg( xUri );
123  list.append( QgsMimeDataUtils::Uri( xUri ) );
124  }
125  return list;
126 }