Quantum GIS API Documentation  1.8
src/core/qgsmimedatautils.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgsmimedatautils.cpp
00003     ---------------------
00004     begin                : November 2011
00005     copyright            : (C) 2011 by Martin Dobias
00006     email                : wonder.sk at gmail.com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 #include "qgsmimedatautils.h"
00016 
00017 #include "qgsdataitem.h"
00018 #include "qgslogger.h"
00019 
00020 static const char* QGIS_URILIST_MIMETYPE = "application/x-vnd.qgis.qgis.uri";
00021 
00022 QgsMimeDataUtils::Uri::Uri( QgsLayerItem* layerItem )
00023     : providerKey( layerItem->providerKey() ), name( layerItem->layerName() ), uri( layerItem->uri() )
00024 {
00025   switch ( layerItem->mapLayerType() )
00026   {
00027     case QgsMapLayer::VectorLayer:
00028       layerType = "vector";
00029       break;
00030     case QgsMapLayer::RasterLayer:
00031       layerType = "raster";
00032       break;
00033     case QgsMapLayer::PluginLayer:
00034       layerType = "plugin";
00035       break;
00036   }
00037 
00038 }
00039 
00040 QgsMimeDataUtils::Uri::Uri( QString& encData )
00041 {
00042   QRegExp rx( "^([^:]+):([^:]+):([^:]+):(.+)" );
00043   if ( rx.indexIn( encData ) != -1 )
00044   {
00045     layerType = rx.cap( 1 );
00046     providerKey = rx.cap( 2 );
00047     name = rx.cap( 3 );
00048     uri = rx.cap( 4 );
00049     QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri );
00050   }
00051 }
00052 
00053 QString QgsMimeDataUtils::Uri::data() const
00054 {
00055   return layerType + ":" + providerKey + ":" + name + ":" + uri;
00056 }
00057 
00058 // -----
00059 
00060 bool QgsMimeDataUtils::isUriList( const QMimeData* data )
00061 {
00062   return data->hasFormat( QGIS_URILIST_MIMETYPE );
00063 }
00064 
00065 QMimeData* QgsMimeDataUtils::encodeUriList( QgsMimeDataUtils::UriList layers )
00066 {
00067   QMimeData *mimeData = new QMimeData();
00068   QByteArray encodedData;
00069 
00070   QDataStream stream( &encodedData, QIODevice::WriteOnly );
00071   foreach( const QgsMimeDataUtils::Uri& u, layers )
00072   {
00073     stream << u.data();
00074   }
00075 
00076   mimeData->setData( QGIS_URILIST_MIMETYPE, encodedData );
00077   return mimeData;
00078 }
00079 
00080 
00081 QgsMimeDataUtils::UriList QgsMimeDataUtils::decodeUriList( const QMimeData* data )
00082 {
00083   QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
00084   QDataStream stream( &encodedData, QIODevice::ReadOnly );
00085   QString xUri; // extended uri: layer_type:provider_key:uri
00086   QgsMimeDataUtils::UriList list;
00087   while ( !stream.atEnd() )
00088   {
00089     stream >> xUri;
00090     QgsDebugMsg( xUri );
00091     list.append( QgsMimeDataUtils::Uri( xUri ) );
00092   }
00093   return list;
00094 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines