Skip to content

Commit 173c72d

Browse files
committedAug 12, 2011
Browser: correct drag&drop for canvas and browser itself
1 parent f680e2f commit 173c72d

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed
 

‎src/app/qgsbrowserdockwidget.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,45 @@
1111
#include "qgsrasterlayer.h"
1212
#include "qgsvectorlayer.h"
1313

14+
#include <QDragEnterEvent>
15+
/**
16+
Utility class for correct drag&drop handling.
17+
18+
We want to allow user to drag layers to qgis window. At the same time we do not
19+
accept drops of the items on our view - but if we ignore the drag enter action
20+
then qgis application consumes the drag events and it is possible to drop the
21+
items on the tree view although the drop is actually managed by qgis app.
22+
*/
23+
class QgsBrowserTreeView : public QTreeView
24+
{
25+
public:
26+
QgsBrowserTreeView( QWidget* parent ) : QTreeView(parent)
27+
{
28+
setDragDropMode( QTreeView::DragDrop ); // sets also acceptDrops + dragEnabled
29+
setSelectionMode( QAbstractItemView::ExtendedSelection );
30+
setContextMenuPolicy( Qt::CustomContextMenu );
31+
}
32+
33+
void dragEnterEvent(QDragEnterEvent* e)
34+
{
35+
// accept drag enter so that our widget will not get ignored
36+
// and drag events will not get passed to QgisApp
37+
e->accept();
38+
}
39+
void dragMoveEvent(QDragMoveEvent* e)
40+
{
41+
// ignore all possibilities where an item could be dropped
42+
// because we want that user drops the item on canvas / legend / app
43+
e->ignore();
44+
}
45+
};
46+
1447
QgsBrowserDockWidget::QgsBrowserDockWidget( QWidget * parent ) :
1548
QDockWidget( parent ), mModel( NULL )
1649
{
1750
setWindowTitle( tr( "Browser" ) );
1851

19-
mBrowserView = new QTreeView( this );
20-
mBrowserView->setDragEnabled( true );
21-
mBrowserView->setDragDropMode( QTreeView::DragOnly );
22-
mBrowserView->setSelectionMode( QAbstractItemView::ExtendedSelection );
23-
mBrowserView->setContextMenuPolicy( Qt::CustomContextMenu );
52+
mBrowserView = new QgsBrowserTreeView( this );
2453
setWidget( mBrowserView );
2554

2655
connect( mBrowserView, SIGNAL( customContextMenuRequested( const QPoint & ) ), this, SLOT( showContextMenu( const QPoint & ) ) );

‎src/gui/qgsmapcanvas.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,3 +1499,11 @@ void QgsMapCanvas::selectionChangedSlot()
14991499
emit selectionChanged( layer );
15001500
refresh();
15011501
}
1502+
1503+
void QgsMapCanvas::dragEnterEvent( QDragEnterEvent * e )
1504+
{
1505+
// By default graphics view delegates the drag events to graphics items.
1506+
// But we do not want that and by ignoring the drag enter we let the
1507+
// parent (e.g. QgisApp) to handle drops of map layers etc.
1508+
e->ignore();
1509+
}

‎src/gui/qgsmapcanvas.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
376376
//! Overridden paint event
377377
void paintEvent( QPaintEvent * e );
378378

379+
//! Overridden drag enter event
380+
void dragEnterEvent( QDragEnterEvent * e );
381+
379382
//! called when panning is in action, reset indicates end of panning
380383
void moveCanvasContents( bool reset = false );
381384

0 commit comments

Comments
 (0)
Please sign in to comment.