Skip to content

Commit

Permalink
[FEAUTRE] Import of vectors to SpatiaLite in browser + move MIME type…
Browse files Browse the repository at this point in the history
… stuff to a separate file
  • Loading branch information
wonder-sk committed Nov 16, 2011
1 parent 462284d commit 6968bda
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 48 deletions.
32 changes: 10 additions & 22 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() );
}
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/app/qgsbrowserdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class QgsBrowserTreeView : public QTreeView
setSelectionMode( QAbstractItemView::ExtendedSelection );
setContextMenuPolicy( Qt::CustomContextMenu );
setHeaderHidden( true );
setDropIndicatorShown( true );
}

void dragEnterEvent( QDragEnterEvent* e )
Expand All @@ -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;
}
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ SET(QGIS_CORE_SRCS
qgsmaprenderer.cpp
qgsmaptopixel.cpp
qgsmessageoutput.cpp
qgsmimedatautils.cpp
qgscredentials.cpp
qgsoverlayobject.cpp
qgspalgeometry.cpp
Expand Down Expand Up @@ -306,6 +307,7 @@ SET(QGIS_CORE_HDRS
qgsmaprenderer.h
qgsmaptopixel.h
qgsmessageoutput.h
qgsmimedatautils.h
qgscredentials.h
qgsoverlayobjectpositionmanager.h
qgspallabeling.h
Expand Down
41 changes: 20 additions & 21 deletions src/core/qgsbrowsermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "qgis.h"
#include "qgsapplication.h"
#include "qgsdataprovider.h"
#include "qgsmimedatautils.h"
#include "qgslogger.h"
#include "qgsproviderregistry.h"

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

Expand Down Expand Up @@ -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() )
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsbrowsermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsdataitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
77 changes: 77 additions & 0 deletions src/core/qgsmimedatautils.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
34 changes: 34 additions & 0 deletions src/core/qgsmimedatautils.h
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 6968bda

Please sign in to comment.