QGIS API Documentation  2.2.0-Valmiera
 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() <= 5 ) // PostGISTRaster layers yields five parts
72  {
73  layerType = parts[0];
74  providerKey = parts[1];
75  name = parts[2];
76  // fetchs PostGISRaster layers
77  if ( parts[3] == "PG" )
78  {
79  uri = parts[3] + ":" + parts[4];
80  }
81  else
82  {
83  uri = parts[3];
84  }
85  QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri );
86  }
87 }
88 
90 {
91  QString escapedName = name;
92  QString escapeUri = uri;
93  escapedName.replace( ":", "\\:" );
94  escapeUri.replace( ":", "\\:" );
95  return layerType + ":" + providerKey + ":" + escapedName + ":" + escapeUri;
96 }
97 
98 // -----
99 
100 bool QgsMimeDataUtils::isUriList( const QMimeData* data )
101 {
102  return data->hasFormat( QGIS_URILIST_MIMETYPE );
103 }
104 
106 {
107  QMimeData *mimeData = new QMimeData();
108  QByteArray encodedData;
109 
110  QDataStream stream( &encodedData, QIODevice::WriteOnly );
111  foreach ( const QgsMimeDataUtils::Uri& u, layers )
112  {
113  stream << u.data();
114  }
115 
116  mimeData->setData( QGIS_URILIST_MIMETYPE, encodedData );
117  return mimeData;
118 }
119 
120 
122 {
123  QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
124  QDataStream stream( &encodedData, QIODevice::ReadOnly );
125  QString xUri; // extended uri: layer_type:provider_key:uri
127  while ( !stream.atEnd() )
128  {
129  stream >> xUri;
130  QgsDebugMsg( xUri );
131  list.append( QgsMimeDataUtils::Uri( xUri ) );
132  }
133  return list;
134 }