Skip to content

Commit

Permalink
Browser: correct drag&drop for canvas and browser itself
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Aug 12, 2011
1 parent f680e2f commit 173c72d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/app/qgsbrowserdockwidget.cpp
Expand Up @@ -11,16 +11,45 @@
#include "qgsrasterlayer.h"
#include "qgsvectorlayer.h"

#include <QDragEnterEvent>
/**
Utility class for correct drag&drop handling.
We want to allow user to drag layers to qgis window. At the same time we do not
accept drops of the items on our view - but if we ignore the drag enter action
then qgis application consumes the drag events and it is possible to drop the
items on the tree view although the drop is actually managed by qgis app.
*/
class QgsBrowserTreeView : public QTreeView
{
public:
QgsBrowserTreeView( QWidget* parent ) : QTreeView(parent)
{
setDragDropMode( QTreeView::DragDrop ); // sets also acceptDrops + dragEnabled
setSelectionMode( QAbstractItemView::ExtendedSelection );
setContextMenuPolicy( Qt::CustomContextMenu );
}

void dragEnterEvent(QDragEnterEvent* e)
{
// accept drag enter so that our widget will not get ignored
// and drag events will not get passed to QgisApp
e->accept();
}
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();
}
};

QgsBrowserDockWidget::QgsBrowserDockWidget( QWidget * parent ) :
QDockWidget( parent ), mModel( NULL )
{
setWindowTitle( tr( "Browser" ) );

mBrowserView = new QTreeView( this );
mBrowserView->setDragEnabled( true );
mBrowserView->setDragDropMode( QTreeView::DragOnly );
mBrowserView->setSelectionMode( QAbstractItemView::ExtendedSelection );
mBrowserView->setContextMenuPolicy( Qt::CustomContextMenu );
mBrowserView = new QgsBrowserTreeView( this );
setWidget( mBrowserView );

connect( mBrowserView, SIGNAL( customContextMenuRequested( const QPoint & ) ), this, SLOT( showContextMenu( const QPoint & ) ) );
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -1499,3 +1499,11 @@ void QgsMapCanvas::selectionChangedSlot()
emit selectionChanged( layer );
refresh();
}

void QgsMapCanvas::dragEnterEvent( QDragEnterEvent * e )
{
// By default graphics view delegates the drag events to graphics items.
// But we do not want that and by ignoring the drag enter we let the
// parent (e.g. QgisApp) to handle drops of map layers etc.
e->ignore();
}
3 changes: 3 additions & 0 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -376,6 +376,9 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! Overridden paint event
void paintEvent( QPaintEvent * e );

//! Overridden drag enter event
void dragEnterEvent( QDragEnterEvent * e );

//! called when panning is in action, reset indicates end of panning
void moveCanvasContents( bool reset = false );

Expand Down

0 comments on commit 173c72d

Please sign in to comment.