QListView subclass to provide drag-n-drop in the legend

git-svn-id: http://svn.osgeo.org/qgis/trunk@291 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
stevehalasz 2003-10-02 10:36:59 +00:00
parent 99d22bf47e
commit 5aba68f3e3
2 changed files with 126 additions and 0 deletions

84
src/qgslegendview.cpp Normal file
View File

@ -0,0 +1,84 @@
//
//
// C++ Implementation: $MODULE$
//
// Description: Subclassed QListView that handles drag-n-drop for changing layer order
//
//
// Author: Steve Halasz <stevehalasz at users.sourceforge.net>, (C) 2003
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include <qapplication.h>
#include <qdragobject.h>
#include <qlistview.h>
#include <qpoint.h>
#include "qgslegendview.h"
QgsLegendView::QgsLegendView( QWidget *parent, const char *name ):QListView( parent, name ), mousePressed( FALSE )
{
setAcceptDrops( TRUE );
viewport()->setAcceptDrops( TRUE );
}
void QgsLegendView::contentsDragEnterEvent( QDragEnterEvent* e )
{
QString text;
if ( QTextDrag::decode(e, text) ) {
// verify text is 'legend-drag' to avoid
// handling regular text drops
if ( text.compare("legend-drag") == 0 ) {
e->accept( TRUE );
}
}
}
void QgsLegendView::contentsDropEvent(QDropEvent* e)
{
QString text;
if ( QTextDrag::decode(e, text) ) {
// verify text is 'legend-drag' to avoid
// handling regular text drops
if ( text.compare("legend-drag") == 0 ) {
QListViewItem *item = itemAt( contentsToViewport(e->pos()) );
// insert item in motion after one it was dropped on
movingItem->moveItem( item );
emit zOrderChanged(this);
}
} else {
e->ignore();
return;
}
}
void QgsLegendView::contentsMousePressEvent( QMouseEvent* e )
{
QListView::contentsMousePressEvent( e );
QPoint p( contentsToViewport( e->pos() ) );
QListViewItem *i = itemAt( p );
if ( i ) {
presspos = e->pos();
mousePressed = TRUE;
}
}
void QgsLegendView::contentsMouseMoveEvent( QMouseEvent* e )
{
if ( mousePressed && ( presspos - e->pos() ).manhattanLength() > QApplication::startDragDistance() ) {
mousePressed = FALSE;
QListViewItem *item = itemAt( contentsToViewport(presspos) );
if ( item ) {
movingItem = item;
// create drag object with some useless text
QDragObject *d = new QTextDrag( "legend-drag", this );
d->drag();
}
}
}
void QgsLegendView::contentsMouseReleaseEvent( QMouseEvent * )
{
mousePressed = FALSE;
}

42
src/qgslegendview.h Normal file
View File

@ -0,0 +1,42 @@
//
//
// C++ Interface: $MODULE$
//
// Description: Subclassed QListView that handles drag-n-drop for changing layer order
//
//
// Author: Steve Halasz <stevehalasz at users.sourceforge.net>, (C) 2003
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include <qlistview.h>
class QgsLegendView : public QListView
{
Q_OBJECT
public:
QgsLegendView( QWidget *parent=0, const char *name=0 );
protected:
// override these to provide drag-n-drop
void contentsDragEnterEvent( QDragEnterEvent *e );
void contentsDropEvent( QDropEvent *e );
void contentsMouseMoveEvent( QMouseEvent *e );
void contentsMousePressEvent( QMouseEvent *e );
void contentsMouseReleaseEvent( QMouseEvent *e );
signals:
// broadcast that the stacking order has changed
// so the map canvas can be redrawn
void zOrderChanged( QgsLegendView *lv );
private:
// location of mouse press
QPoint presspos;
// keep track of if the mouse is pressed or not
bool mousePressed;
// keep track of the Item being dragged
QListViewItem *movingItem;
};