Skip to content
Permalink
Browse files
[FEAUTRE] Import of vectors to SpatiaLite in browser + move MIME type…
… stuff to a separate file
  • Loading branch information
wonder-sk committed Nov 16, 2011
1 parent 462284d commit 6968bda4a27b4ab994a2aea45381dbea373ddd67
@@ -136,6 +136,7 @@
#include "qgsmaptip.h"
#include "qgsmergeattributesdialog.h"
#include "qgsmessageviewer.h"
#include "qgsmimedatautils.h"
#include "qgsnewvectorlayerdialog.h"
#include "qgsoptions.h"
#include "qgspastetransformations.h"
@@ -681,31 +682,18 @@ void QgisApp::dropEvent( QDropEvent *event )
openFile( fileName );
}
}
if ( event->mimeData()->hasFormat( "application/x-vnd.qgis.qgis.uri" ) )
if ( QgsMimeDataUtils::isUriList( event->mimeData() ) )
{
QByteArray encodedData = event->mimeData()->data( "application/x-vnd.qgis.qgis.uri" );
QDataStream stream( &encodedData, QIODevice::ReadOnly );
QString xUri; // extended uri: layer_type:provider_key:uri
while ( !stream.atEnd() )
QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( event->mimeData() );
foreach( const QgsMimeDataUtils::Uri& u, lst )
{
stream >> xUri;
QgsDebugMsg( xUri );
QRegExp rx( "^([^:]+):([^:]+):([^:]+):(.+)" );
if ( rx.indexIn( xUri ) != -1 )
if ( u.layerType == "vector" )
{
QString layerType = rx.cap( 1 );
QString providerKey = rx.cap( 2 );
QString name = rx.cap( 3 );
QString uri = rx.cap( 4 );
QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri );
if ( layerType == "vector" )
{
addVectorLayer( uri, name, providerKey );
}
else if ( layerType == "raster" )
{
addRasterLayer( uri, name, providerKey, QStringList(), QStringList(), QString(), QString() );
}
addVectorLayer( u.uri, u.name, u.providerKey );
}
else if ( u.layerType == "raster" )
{
addRasterLayer( u.uri, u.name, u.providerKey, QStringList(), QStringList(), QString(), QString() );
}
}
}
@@ -32,6 +32,7 @@ class QgsBrowserTreeView : public QTreeView
setSelectionMode( QAbstractItemView::ExtendedSelection );
setContextMenuPolicy( Qt::CustomContextMenu );
setHeaderHidden( true );
setDropIndicatorShown( true );
}

void dragEnterEvent( QDragEnterEvent* e )
@@ -42,9 +43,21 @@ class QgsBrowserTreeView : public QTreeView
}
void dragMoveEvent( QDragMoveEvent* e )
{
// ignore all possibilities where an item could be dropped
// because we want that user drops the item on canvas / legend / app
e->ignore();
// do not accept drops above/below items
/*if ( dropIndicatorPosition() != QAbstractItemView::OnItem )
{
QgsDebugMsg("drag not on item");
e->ignore();
return;
}*/

QTreeView::dragMoveEvent( e );

if ( !e->provides( "application/x-vnd.qgis.qgis.uri" ) )
{
e->ignore();
return;
}
}
};

@@ -72,6 +72,7 @@ SET(QGIS_CORE_SRCS
qgsmaprenderer.cpp
qgsmaptopixel.cpp
qgsmessageoutput.cpp
qgsmimedatautils.cpp
qgscredentials.cpp
qgsoverlayobject.cpp
qgspalgeometry.cpp
@@ -306,6 +307,7 @@ SET(QGIS_CORE_HDRS
qgsmaprenderer.h
qgsmaptopixel.h
qgsmessageoutput.h
qgsmimedatautils.h
qgscredentials.h
qgsoverlayobjectpositionmanager.h
qgspallabeling.h
@@ -5,6 +5,7 @@
#include "qgis.h"
#include "qgsapplication.h"
#include "qgsdataprovider.h"
#include "qgsmimedatautils.h"
#include "qgslogger.h"
#include "qgsproviderregistry.h"

@@ -117,6 +118,8 @@ Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex & index ) const
flags |= Qt::ItemIsDragEnabled;
}
}
if ( ptr->acceptDrop() )
flags |= Qt::ItemIsDropEnabled;
return flags;
}

@@ -342,11 +345,7 @@ QStringList QgsBrowserModel::mimeTypes() const

QMimeData * QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;

QDataStream stream( &encodedData, QIODevice::WriteOnly );

QgsMimeDataUtils::UriList lst;
foreach( const QModelIndex &index, indexes )
{
if ( index.isValid() )
@@ -355,25 +354,25 @@ QMimeData * QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
if ( ptr->type() != QgsDataItem::Layer ) continue;
QgsLayerItem *layer = ( QgsLayerItem* ) ptr;
if ( layer->providerKey() == "wms" ) continue;
QString layerType;
switch ( layer->mapLayerType() )
{
case QgsMapLayer::VectorLayer:
layerType = "vector";
break;
case QgsMapLayer::RasterLayer:
layerType = "raster";
break;
default:
continue;
}
QString xUri = layerType + ":" + layer->providerKey() + ":" + layer->name() + ":" + layer->uri();
stream << xUri;
lst.append( QgsMimeDataUtils::Uri( layer ) );
}
}
return QgsMimeDataUtils::encodeUriList( lst );
}

bool QgsBrowserModel::dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent )
{
Q_UNUSED( row );
Q_UNUSED( column );

QgsDataItem* destItem = dataItem( parent );
if ( !destItem )
{
QgsDebugMsg( "DROP PROBLEM!" );
return false;
}

mimeData->setData( "application/x-vnd.qgis.qgis.uri", encodedData );
return mimeData;
return destItem->handleDrop( data, action );
}

QgsDataItem *QgsBrowserModel::dataItem( const QModelIndex &idx ) const
@@ -48,9 +48,14 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
*/
virtual QModelIndex parent( const QModelIndex &index ) const;

/** Returns a list of mime that can describe model indexes */
virtual QStringList mimeTypes() const;

QMimeData * mimeData( const QModelIndexList &indexes ) const;
/** Returns an object that contains serialized items of data corresponding to the list of indexes specified */
virtual QMimeData * mimeData( const QModelIndexList &indexes ) const;

/** Handles the data supplied by a drag and drop operation that ended with the given action */
virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent );

QgsDataItem *dataItem( const QModelIndex &idx ) const;

@@ -36,6 +36,7 @@ typedef int dataCapabilities_t();
typedef QgsDataItem * dataItem_t( QString, QgsDataItem* );



/** base class for all items in the model */
class CORE_EXPORT QgsDataItem : public QObject
{
@@ -80,6 +81,12 @@ class CORE_EXPORT QgsDataItem : public QObject
// list of actions provided by this item - usually used for popup menu on right-click
virtual QList<QAction*> actions() { return QList<QAction*>(); }

// whether accepts drag&drop'd layers - e.g. for import
virtual bool acceptDrop() { return false; }

// try to process the data dropped on this item
virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }

//

enum Capability
@@ -0,0 +1,77 @@
#include "qgsmimedatautils.h"

#include "qgsdataitem.h"
#include "qgslogger.h"

static const char* QGIS_URILIST_MIMETYPE = "application/x-vnd.qgis.qgis.uri";

QgsMimeDataUtils::Uri::Uri( QgsLayerItem* layerItem )
: providerKey( layerItem->providerKey() ), name( layerItem->name() ), uri( layerItem->uri() )
{
switch ( layerItem->mapLayerType() )
{
case QgsMapLayer::VectorLayer:
layerType = "vector";
break;
case QgsMapLayer::RasterLayer:
layerType = "raster";
break;
}

}

QgsMimeDataUtils::Uri::Uri( QString& encData )
{
QRegExp rx( "^([^:]+):([^:]+):([^:]+):(.+)" );
if ( rx.indexIn( encData ) != -1 )
{
layerType = rx.cap( 1 );
providerKey = rx.cap( 2 );
name = rx.cap( 3 );
uri = rx.cap( 4 );
QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri );
}
}

QString QgsMimeDataUtils::Uri::data() const
{
return layerType + ":" + providerKey + ":" + name + ":" + uri;
}

// -----

bool QgsMimeDataUtils::isUriList( const QMimeData* data )
{
return data->hasFormat( QGIS_URILIST_MIMETYPE );
}

QMimeData* QgsMimeDataUtils::encodeUriList( QgsMimeDataUtils::UriList layers )
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;

QDataStream stream( &encodedData, QIODevice::WriteOnly );
foreach( const QgsMimeDataUtils::Uri& u, layers )
{
stream << u.data();
}

mimeData->setData( QGIS_URILIST_MIMETYPE, encodedData );
return mimeData;
}


QgsMimeDataUtils::UriList QgsMimeDataUtils::decodeUriList( const QMimeData* data )
{
QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
QDataStream stream( &encodedData, QIODevice::ReadOnly );
QString xUri; // extended uri: layer_type:provider_key:uri
QgsMimeDataUtils::UriList list;
while ( !stream.atEnd() )
{
stream >> xUri;
QgsDebugMsg( xUri );
list.append( QgsMimeDataUtils::Uri( xUri ) );
}
return list;
}
@@ -0,0 +1,34 @@
#ifndef QGSMIMEDATAUTILS_H
#define QGSMIMEDATAUTILS_H

#include <QMimeData>

class QgsLayerItem;

class QgsMimeDataUtils
{
public:

struct Uri
{
Uri( QgsLayerItem* layer );
Uri( QString& encData );

QString data() const;

QString layerType;
QString providerKey;
QString name;
QString uri;
};
typedef QList<Uri> UriList;

static QMimeData* encodeUriList( UriList layers );

static bool isUriList( const QMimeData* data );

static UriList decodeUriList( const QMimeData* data );

};

#endif // QGSMIMEDATAUTILS_H

0 comments on commit 6968bda

Please sign in to comment.