Skip to content

Commit 6968bda

Browse files
committed
[FEAUTRE] Import of vectors to SpatiaLite in browser + move MIME type stuff to a separate file
1 parent 462284d commit 6968bda

File tree

10 files changed

+246
-48
lines changed

10 files changed

+246
-48
lines changed

src/app/qgisapp.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
#include "qgsmaptip.h"
137137
#include "qgsmergeattributesdialog.h"
138138
#include "qgsmessageviewer.h"
139+
#include "qgsmimedatautils.h"
139140
#include "qgsnewvectorlayerdialog.h"
140141
#include "qgsoptions.h"
141142
#include "qgspastetransformations.h"
@@ -681,31 +682,18 @@ void QgisApp::dropEvent( QDropEvent *event )
681682
openFile( fileName );
682683
}
683684
}
684-
if ( event->mimeData()->hasFormat( "application/x-vnd.qgis.qgis.uri" ) )
685+
if ( QgsMimeDataUtils::isUriList( event->mimeData() ) )
685686
{
686-
QByteArray encodedData = event->mimeData()->data( "application/x-vnd.qgis.qgis.uri" );
687-
QDataStream stream( &encodedData, QIODevice::ReadOnly );
688-
QString xUri; // extended uri: layer_type:provider_key:uri
689-
while ( !stream.atEnd() )
687+
QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( event->mimeData() );
688+
foreach( const QgsMimeDataUtils::Uri& u, lst )
690689
{
691-
stream >> xUri;
692-
QgsDebugMsg( xUri );
693-
QRegExp rx( "^([^:]+):([^:]+):([^:]+):(.+)" );
694-
if ( rx.indexIn( xUri ) != -1 )
690+
if ( u.layerType == "vector" )
695691
{
696-
QString layerType = rx.cap( 1 );
697-
QString providerKey = rx.cap( 2 );
698-
QString name = rx.cap( 3 );
699-
QString uri = rx.cap( 4 );
700-
QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri );
701-
if ( layerType == "vector" )
702-
{
703-
addVectorLayer( uri, name, providerKey );
704-
}
705-
else if ( layerType == "raster" )
706-
{
707-
addRasterLayer( uri, name, providerKey, QStringList(), QStringList(), QString(), QString() );
708-
}
692+
addVectorLayer( u.uri, u.name, u.providerKey );
693+
}
694+
else if ( u.layerType == "raster" )
695+
{
696+
addRasterLayer( u.uri, u.name, u.providerKey, QStringList(), QStringList(), QString(), QString() );
709697
}
710698
}
711699
}

src/app/qgsbrowserdockwidget.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class QgsBrowserTreeView : public QTreeView
3232
setSelectionMode( QAbstractItemView::ExtendedSelection );
3333
setContextMenuPolicy( Qt::CustomContextMenu );
3434
setHeaderHidden( true );
35+
setDropIndicatorShown( true );
3536
}
3637

3738
void dragEnterEvent( QDragEnterEvent* e )
@@ -42,9 +43,21 @@ class QgsBrowserTreeView : public QTreeView
4243
}
4344
void dragMoveEvent( QDragMoveEvent* e )
4445
{
45-
// ignore all possibilities where an item could be dropped
46-
// because we want that user drops the item on canvas / legend / app
47-
e->ignore();
46+
// do not accept drops above/below items
47+
/*if ( dropIndicatorPosition() != QAbstractItemView::OnItem )
48+
{
49+
QgsDebugMsg("drag not on item");
50+
e->ignore();
51+
return;
52+
}*/
53+
54+
QTreeView::dragMoveEvent( e );
55+
56+
if ( !e->provides( "application/x-vnd.qgis.qgis.uri" ) )
57+
{
58+
e->ignore();
59+
return;
60+
}
4861
}
4962
};
5063

src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ SET(QGIS_CORE_SRCS
7272
qgsmaprenderer.cpp
7373
qgsmaptopixel.cpp
7474
qgsmessageoutput.cpp
75+
qgsmimedatautils.cpp
7576
qgscredentials.cpp
7677
qgsoverlayobject.cpp
7778
qgspalgeometry.cpp
@@ -306,6 +307,7 @@ SET(QGIS_CORE_HDRS
306307
qgsmaprenderer.h
307308
qgsmaptopixel.h
308309
qgsmessageoutput.h
310+
qgsmimedatautils.h
309311
qgscredentials.h
310312
qgsoverlayobjectpositionmanager.h
311313
qgspallabeling.h

src/core/qgsbrowsermodel.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "qgis.h"
66
#include "qgsapplication.h"
77
#include "qgsdataprovider.h"
8+
#include "qgsmimedatautils.h"
89
#include "qgslogger.h"
910
#include "qgsproviderregistry.h"
1011

@@ -117,6 +118,8 @@ Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex & index ) const
117118
flags |= Qt::ItemIsDragEnabled;
118119
}
119120
}
121+
if ( ptr->acceptDrop() )
122+
flags |= Qt::ItemIsDropEnabled;
120123
return flags;
121124
}
122125

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

343346
QMimeData * QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
344347
{
345-
QMimeData *mimeData = new QMimeData();
346-
QByteArray encodedData;
347-
348-
QDataStream stream( &encodedData, QIODevice::WriteOnly );
349-
348+
QgsMimeDataUtils::UriList lst;
350349
foreach( const QModelIndex &index, indexes )
351350
{
352351
if ( index.isValid() )
@@ -355,25 +354,25 @@ QMimeData * QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
355354
if ( ptr->type() != QgsDataItem::Layer ) continue;
356355
QgsLayerItem *layer = ( QgsLayerItem* ) ptr;
357356
if ( layer->providerKey() == "wms" ) continue;
358-
QString layerType;
359-
switch ( layer->mapLayerType() )
360-
{
361-
case QgsMapLayer::VectorLayer:
362-
layerType = "vector";
363-
break;
364-
case QgsMapLayer::RasterLayer:
365-
layerType = "raster";
366-
break;
367-
default:
368-
continue;
369-
}
370-
QString xUri = layerType + ":" + layer->providerKey() + ":" + layer->name() + ":" + layer->uri();
371-
stream << xUri;
357+
lst.append( QgsMimeDataUtils::Uri( layer ) );
372358
}
373359
}
360+
return QgsMimeDataUtils::encodeUriList( lst );
361+
}
362+
363+
bool QgsBrowserModel::dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent )
364+
{
365+
Q_UNUSED( row );
366+
Q_UNUSED( column );
367+
368+
QgsDataItem* destItem = dataItem( parent );
369+
if ( !destItem )
370+
{
371+
QgsDebugMsg( "DROP PROBLEM!" );
372+
return false;
373+
}
374374

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

379378
QgsDataItem *QgsBrowserModel::dataItem( const QModelIndex &idx ) const

src/core/qgsbrowsermodel.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
4848
*/
4949
virtual QModelIndex parent( const QModelIndex &index ) const;
5050

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

53-
QMimeData * mimeData( const QModelIndexList &indexes ) const;
54+
/** Returns an object that contains serialized items of data corresponding to the list of indexes specified */
55+
virtual QMimeData * mimeData( const QModelIndexList &indexes ) const;
56+
57+
/** Handles the data supplied by a drag and drop operation that ended with the given action */
58+
virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent );
5459

5560
QgsDataItem *dataItem( const QModelIndex &idx ) const;
5661

src/core/qgsdataitem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef int dataCapabilities_t();
3636
typedef QgsDataItem * dataItem_t( QString, QgsDataItem* );
3737

3838

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

84+
// whether accepts drag&drop'd layers - e.g. for import
85+
virtual bool acceptDrop() { return false; }
86+
87+
// try to process the data dropped on this item
88+
virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
89+
8390
//
8491

8592
enum Capability

src/core/qgsmimedatautils.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "qgsmimedatautils.h"
2+
3+
#include "qgsdataitem.h"
4+
#include "qgslogger.h"
5+
6+
static const char* QGIS_URILIST_MIMETYPE = "application/x-vnd.qgis.qgis.uri";
7+
8+
QgsMimeDataUtils::Uri::Uri( QgsLayerItem* layerItem )
9+
: providerKey( layerItem->providerKey() ), name( layerItem->name() ), uri( layerItem->uri() )
10+
{
11+
switch ( layerItem->mapLayerType() )
12+
{
13+
case QgsMapLayer::VectorLayer:
14+
layerType = "vector";
15+
break;
16+
case QgsMapLayer::RasterLayer:
17+
layerType = "raster";
18+
break;
19+
}
20+
21+
}
22+
23+
QgsMimeDataUtils::Uri::Uri( QString& encData )
24+
{
25+
QRegExp rx( "^([^:]+):([^:]+):([^:]+):(.+)" );
26+
if ( rx.indexIn( encData ) != -1 )
27+
{
28+
layerType = rx.cap( 1 );
29+
providerKey = rx.cap( 2 );
30+
name = rx.cap( 3 );
31+
uri = rx.cap( 4 );
32+
QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri );
33+
}
34+
}
35+
36+
QString QgsMimeDataUtils::Uri::data() const
37+
{
38+
return layerType + ":" + providerKey + ":" + name + ":" + uri;
39+
}
40+
41+
// -----
42+
43+
bool QgsMimeDataUtils::isUriList( const QMimeData* data )
44+
{
45+
return data->hasFormat( QGIS_URILIST_MIMETYPE );
46+
}
47+
48+
QMimeData* QgsMimeDataUtils::encodeUriList( QgsMimeDataUtils::UriList layers )
49+
{
50+
QMimeData *mimeData = new QMimeData();
51+
QByteArray encodedData;
52+
53+
QDataStream stream( &encodedData, QIODevice::WriteOnly );
54+
foreach( const QgsMimeDataUtils::Uri& u, layers )
55+
{
56+
stream << u.data();
57+
}
58+
59+
mimeData->setData( QGIS_URILIST_MIMETYPE, encodedData );
60+
return mimeData;
61+
}
62+
63+
64+
QgsMimeDataUtils::UriList QgsMimeDataUtils::decodeUriList( const QMimeData* data )
65+
{
66+
QByteArray encodedData = data->data( QGIS_URILIST_MIMETYPE );
67+
QDataStream stream( &encodedData, QIODevice::ReadOnly );
68+
QString xUri; // extended uri: layer_type:provider_key:uri
69+
QgsMimeDataUtils::UriList list;
70+
while ( !stream.atEnd() )
71+
{
72+
stream >> xUri;
73+
QgsDebugMsg( xUri );
74+
list.append( QgsMimeDataUtils::Uri( xUri ) );
75+
}
76+
return list;
77+
}

src/core/qgsmimedatautils.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef QGSMIMEDATAUTILS_H
2+
#define QGSMIMEDATAUTILS_H
3+
4+
#include <QMimeData>
5+
6+
class QgsLayerItem;
7+
8+
class QgsMimeDataUtils
9+
{
10+
public:
11+
12+
struct Uri
13+
{
14+
Uri( QgsLayerItem* layer );
15+
Uri( QString& encData );
16+
17+
QString data() const;
18+
19+
QString layerType;
20+
QString providerKey;
21+
QString name;
22+
QString uri;
23+
};
24+
typedef QList<Uri> UriList;
25+
26+
static QMimeData* encodeUriList( UriList layers );
27+
28+
static bool isUriList( const QMimeData* data );
29+
30+
static UriList decodeUriList( const QMimeData* data );
31+
32+
};
33+
34+
#endif // QGSMIMEDATAUTILS_H

0 commit comments

Comments
 (0)