Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
enabled drag-and-drop from qbrowser to qgis, except wms
  • Loading branch information
blazek committed Jun 18, 2011
1 parent bb9e992 commit 9028bf7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
28 changes: 27 additions & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -636,7 +636,7 @@ QgisApp::~QgisApp()

void QgisApp::dragEnterEvent( QDragEnterEvent *event )
{
if ( event->mimeData()->hasUrls() )
if ( event->mimeData()->hasUrls() || event->mimeData()->hasFormat( "application/x-vnd.qgis.qgis.uri" ) )
{
event->acceptProposedAction();
}
Expand All @@ -657,6 +657,32 @@ void QgisApp::dropEvent( QDropEvent *event )
openFile( fileName );
}
}
if ( event->mimeData()->hasFormat( "application/x-vnd.qgis.qgis.uri" ) )
{
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
stream >> xUri;
QgsDebugMsg( xUri );
QRegExp rx( "^([^:]+):([^:]+):([^:]+):(.+)" );
if ( rx.indexIn( xUri ) != -1 )
{
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() );
}
}
}

event->acceptProposedAction();
}

Expand Down
39 changes: 30 additions & 9 deletions src/browser/qgsbrowsermodel.cpp
Expand Up @@ -85,7 +85,11 @@ Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex & index ) const
QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer();
if ( ptr->type() == QgsDataItem::Layer )
{
flags |= Qt::ItemIsDragEnabled;
QgsLayerItem *layer = ( QgsLayerItem* ) ptr;
if ( layer->providerKey() != "wms" )
{
flags |= Qt::ItemIsDragEnabled;
}
}
return flags;
}
Expand Down Expand Up @@ -272,28 +276,45 @@ void QgsBrowserModel::connectItem( QgsDataItem* item )
QStringList QgsBrowserModel::mimeTypes() const
{
QStringList types;
types << "text/uri-list";
// In theory the mime type convention is: application/x-vnd.<vendor>.<application>.<type>
// but it seems a bit over formalized. Would be an application/x-qgis-uri better?
types << "application/x-vnd.qgis.qgis.uri";
return types;
}

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

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

foreach (const QModelIndex &index, indexes) {
if (index.isValid()) {
foreach( const QModelIndex &index, indexes )
{
if ( index.isValid() )
{
QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer();
if ( ptr->type() != QgsDataItem::Layer ) continue;
QgsLayerItem *layer = ( QgsLayerItem* ) ptr;
QString text = layer->uri();
stream << text;
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;
}
}

mimeData->setData("text/uri-list", encodedData);
mimeData->setData( "application/x-vnd.qgis.qgis.uri", encodedData );
return mimeData;
}

Expand Down

0 comments on commit 9028bf7

Please sign in to comment.