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() ), 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  if ( encData.isEmpty() )
50  return;
51  for ( int i = 0; i < encData.length(); ++i )
52  {
53  if ( encData.at( i ) == escape && !inEscape )
54  {
55  inEscape = true;
56  }
57  else if ( encData.at( i ) == split && !inEscape )
58  {
59  parts << part;
60  part = "";
61  }
62  else
63  {
64  part += encData.at( i );
65  inEscape = false;
66  }
67  }
68  if ( !part.isEmpty() )
69  {
70  parts << part;
71  }
72 
73  if ( parts.size() <= 5 ) // PostGISTRaster layers yields five parts
74  {
75  layerType = parts.value( 0 );
76  providerKey = parts.value( 1 );
77  name = parts.value( 2 );
78  // fetchs PostGISRaster layers
79  if ( parts.value( 3 ) == "PG" )
80  {
81  uri = parts.value( 3 ) + ":" + parts.value( 4 );
82  }
83  else
84  {
85  uri = parts.value( 3 );
86  }
87  QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri );
88  }
89 }
90 
92 {
93  QString escapedName = name;
94  QString escapeUri = uri;
95  escapedName.replace( ":", "\\:" );
96  escapeUri.replace( "\\", "\\\\" );
97  escapeUri.replace( ":", "\\:" );
98  return layerType + ":" + providerKey + ":" + escapedName + ":" + escapeUri;
99 }
100 
101 // -----
102 
103 bool QgsMimeDataUtils::isUriList( const QMimeData* data )
104 {
105  return data->hasFormat( QGIS_URILIST_MIMETYPE );
106 }
107 
109 {
110  QMimeData *mimeData = new QMimeData();
111  QByteArray encodedData;
112 
113  QDataStream stream( &encodedData, QIODevice::WriteOnly );
114  foreach ( const QgsMimeDataUtils::Uri& u, layers )
115  {
116  stream << u.data();
117  }
118 
119  mimeData->setData( QGIS_URILIST_MIMETYPE, encodedData );
120  return mimeData;
121 }
122 
123 
125 {
126  QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
127  QDataStream stream( &encodedData, QIODevice::ReadOnly );
128  QString xUri; // extended uri: layer_type:provider_key:uri
130  while ( !stream.atEnd() )
131  {
132  stream >> xUri;
133  QgsDebugMsg( xUri );
134  list.append( QgsMimeDataUtils::Uri( xUri ) );
135  }
136  return list;
137 }
#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:246
static const char * QGIS_URILIST_MIMETYPE
QList< Uri > UriList