You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
433 lines
11 KiB
433 lines
11 KiB
15 years ago
|
/**********************************************************************
|
||
|
** Copyright (C) 2002 Trolltech AS. All rights reserved.
|
||
|
**
|
||
14 years ago
|
** This file is part of TQt Designer.
|
||
15 years ago
|
**
|
||
|
** This file may be distributed and/or modified under the terms of the
|
||
|
** GNU General Public License version 2 as published by the Free Software
|
||
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
||
|
** packaging of this file.
|
||
|
**
|
||
14 years ago
|
** Licensees holding valid TQt Enterprise Edition or TQt Professional Edition
|
||
|
** licenses may use this file in accordance with the TQt Commercial License
|
||
15 years ago
|
** Agreement provided with the Software.
|
||
|
**
|
||
|
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
||
|
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||
|
**
|
||
|
** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
||
|
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
||
14 years ago
|
** information about TQt Commercial License Agreements.
|
||
15 years ago
|
**
|
||
|
** Contact info@trolltech.com if any conditions of this licensing are
|
||
|
** not clear to you.
|
||
|
**
|
||
|
**********************************************************************/
|
||
|
|
||
|
#include "listviewdnd.h"
|
||
15 years ago
|
#include <tqwidget.h>
|
||
|
#include <tqheader.h>
|
||
|
#include <tqpainter.h>
|
||
|
#include <tqdragobject.h>
|
||
|
#include <tqvaluelist.h>
|
||
15 years ago
|
|
||
|
// The Dragobject Declaration ---------------------------------------
|
||
14 years ago
|
class ListViewItemDrag : public TQStoredDrag
|
||
15 years ago
|
{
|
||
|
public:
|
||
|
enum DropRelation { Sibling, Child };
|
||
14 years ago
|
ListViewItemDrag( ListViewItemList & items, TQWidget * parent = 0, const char * name = 0 );
|
||
15 years ago
|
~ListViewItemDrag() {};
|
||
15 years ago
|
static bool canDecode( TQDragMoveEvent * event );
|
||
14 years ago
|
static bool decode( TQDropEvent * event, TQListView * parent, TQListViewItem * insertPoint, DropRelation dr );
|
||
15 years ago
|
};
|
||
|
// ------------------------------------------------------------------
|
||
|
|
||
15 years ago
|
ListViewDnd::ListViewDnd( TQListView * eventSource, const char * name )
|
||
15 years ago
|
: ListDnd( eventSource, name ) { }
|
||
|
|
||
15 years ago
|
void ListViewDnd::confirmDrop( TQListViewItem * )
|
||
15 years ago
|
{
|
||
|
dropConfirmed = TRUE;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool ListViewDnd::dropEvent( TQDropEvent * event )
|
||
15 years ago
|
{
|
||
|
if ( dragInside ) {
|
||
|
|
||
|
if ( dMode & NullDrop ) { // combined with Move, a NullDrop will delete an item
|
||
|
event->accept();
|
||
|
emit dropped( 0 ); // a NullDrop
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQPoint pos = event->pos();
|
||
15 years ago
|
|
||
|
ListViewItemDrag::DropRelation dr = ListViewItemDrag::Sibling;
|
||
15 years ago
|
TQListViewItem *item = itemAt( pos );
|
||
15 years ago
|
int dpos = dropDepth( item, pos );
|
||
|
|
||
|
if ( item ) {
|
||
|
if ( dpos > item->depth() && !(dMode & Flat) ) {
|
||
|
// Child node
|
||
|
dr = ListViewItemDrag::Child;
|
||
|
} else if ( dpos < item->depth() ) {
|
||
|
// Parent(s) Sibling
|
||
|
while ( item && (item->depth() > dpos) )
|
||
14 years ago
|
item = item->parent();
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
if ( ListViewItemDrag::decode( event, (TQListView *) src, item, dr ) ) {
|
||
15 years ago
|
event->accept();
|
||
|
emit dropped( 0 ); // Use ID instead of item?
|
||
|
}
|
||
|
}
|
||
|
|
||
|
line->hide();
|
||
|
dragInside = FALSE;
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool ListViewDnd::mouseMoveEvent( TQMouseEvent * event )
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( event->state() & Qt::LeftButton ) {
|
||
15 years ago
|
if ( ( event->pos() - mousePressPos ).manhattanLength() > 3 ) {
|
||
|
ListViewItemList list;
|
||
|
|
||
|
if ( dMode & Flat )
|
||
|
buildFlatList( list );
|
||
|
else
|
||
|
buildTreeList( list );
|
||
|
|
||
15 years ago
|
ListViewItemDrag * dragobject = new ListViewItemDrag( list, (TQListView *) src );
|
||
15 years ago
|
|
||
|
if ( dMode & Move ) {
|
||
|
disabledItems = list;
|
||
|
setVisibleItems( FALSE );
|
||
|
}
|
||
|
|
||
|
dragobject->dragCopy();
|
||
|
|
||
|
if ( dMode & Move ) {
|
||
|
// Did the target accept the drop?
|
||
|
if ( dropConfirmed ) {
|
||
|
// Shouldn't autoDelete handle this?
|
||
|
for( list.first(); list.current(); list.next() )
|
||
|
delete list.current();
|
||
|
dropConfirmed = FALSE;
|
||
|
} else {
|
||
|
// Reenable disabled items since
|
||
|
// drag'n'drop was aborted
|
||
|
setVisibleItems( TRUE );
|
||
|
}
|
||
|
disabledItems.clear();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
int ListViewDnd::buildFlatList( ListViewItemList & list )
|
||
|
{
|
||
|
bool addKids = FALSE;
|
||
15 years ago
|
TQListViewItem *nextSibling = 0;
|
||
|
TQListViewItem *nextParent = 0;
|
||
|
TQListViewItemIterator it = ((TQListView *)src)->firstChild();
|
||
15 years ago
|
for ( ; *it; it++ ) {
|
||
|
// Hit the nextSibling, turn of child processing
|
||
|
if ( (*it) == nextSibling )
|
||
|
addKids = FALSE;
|
||
|
|
||
|
if ( (*it)->isSelected() ) {
|
||
|
if ( (*it)->childCount() == 0 ) {
|
||
13 years ago
|
// Selected, no children
|
||
15 years ago
|
list.append( *it );
|
||
|
} else if ( !addKids ) {
|
||
|
// Children processing not set, so set it
|
||
|
// Also find the item were we shall quit
|
||
13 years ago
|
// processing children...if any such item
|
||
15 years ago
|
addKids = TRUE;
|
||
|
nextSibling = (*it)->nextSibling();
|
||
14 years ago
|
nextParent = (*it)->parent();
|
||
15 years ago
|
while ( nextParent && !nextSibling ) {
|
||
|
nextSibling = nextParent->nextSibling();
|
||
14 years ago
|
nextParent = nextParent->parent();
|
||
15 years ago
|
}
|
||
|
}
|
||
|
} else if ( ((*it)->childCount() == 0) && addKids ) {
|
||
13 years ago
|
// Leaf node, and we _do_ process children
|
||
15 years ago
|
list.append( *it );
|
||
|
}
|
||
|
}
|
||
|
return list.count();
|
||
|
}
|
||
|
|
||
|
int ListViewDnd::buildTreeList( ListViewItemList & list )
|
||
|
{
|
||
15 years ago
|
TQListViewItemIterator it = ((TQListView *)src)->firstChild();
|
||
15 years ago
|
for ( ; *it; it++ ) {
|
||
|
if ( (*it)->isSelected() )
|
||
|
list.append( *it );
|
||
|
}
|
||
|
return list.count();
|
||
|
}
|
||
|
|
||
|
void ListViewDnd::setVisibleItems( bool b )
|
||
|
{
|
||
|
if ( disabledItems.isEmpty() )
|
||
|
return;
|
||
|
|
||
|
disabledItems.first();
|
||
|
do {
|
||
|
disabledItems.current()->setVisible( b );
|
||
|
} while ( disabledItems.next() );
|
||
|
}
|
||
|
|
||
15 years ago
|
void ListViewDnd::updateLine( const TQPoint & dragPos )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQListViewItem * item = itemAt(dragPos);
|
||
|
TQListView * src = (TQListView *) this->src;
|
||
15 years ago
|
|
||
|
int ypos = item ?
|
||
13 years ago
|
( src->itemRect( item ).bottom() - ( line->height() / 2 ) ) :
|
||
|
( src->itemRect( src->firstChild() ).top() );
|
||
15 years ago
|
|
||
|
int xpos = dropDepth( item, dragPos ) * src->treeStepSize();
|
||
|
line->resize( src->viewport()->width() - xpos, line->height() );
|
||
|
line->move( xpos, ypos );
|
||
|
}
|
||
|
|
||
15 years ago
|
TQListViewItem * ListViewDnd::itemAt( TQPoint pos )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQListView * src = (TQListView *) this->src;
|
||
15 years ago
|
int headerHeight = (int)(src->header()->height());
|
||
|
pos.ry() -= headerHeight;
|
||
15 years ago
|
TQListViewItem * result = src->itemAt( pos );
|
||
15 years ago
|
|
||
|
if ( result && ( pos.ry() < (src->itemPos(result) + result->height()/2) ) )
|
||
|
result = result->itemAbove();
|
||
|
|
||
14 years ago
|
// Wind back if has parent, and we're in flat mode
|
||
|
while ( result && result->parent() && (dMode & Flat) )
|
||
|
result = result->parent();
|
||
15 years ago
|
|
||
14 years ago
|
// Wind back if has parent, and we're in flat mode
|
||
|
while ( result && !result->isVisible() && result->parent() )
|
||
|
result = result->parent();
|
||
15 years ago
|
|
||
13 years ago
|
if ( !result && src->firstChild() && (pos.y() > src->itemRect(src->firstChild()).bottom()) ) {
|
||
15 years ago
|
result = src->lastItem();
|
||
|
if ( !result->isVisible() )
|
||
|
// Handle special case where last item is actually hidden
|
||
|
result = result->itemAbove();
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
15 years ago
|
int ListViewDnd::dropDepth( TQListViewItem * item, TQPoint pos )
|
||
15 years ago
|
{
|
||
|
if ( !item || (dMode & Flat) )
|
||
|
return 0;
|
||
|
|
||
|
int result = 0;
|
||
|
int itemDepth = item->depth();
|
||
15 years ago
|
int indentSize = ((TQListView *)src)->treeStepSize();
|
||
15 years ago
|
int itemLeft = indentSize * itemDepth;
|
||
|
int childMargin = indentSize*2;
|
||
|
if ( pos.x() > itemLeft + childMargin ) {
|
||
|
result = itemDepth + 1;
|
||
|
} else if ( pos.x() < itemLeft ) {
|
||
|
result = pos.x() / indentSize;
|
||
|
} else {
|
||
|
result = itemDepth;
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool ListViewDnd::canDecode( TQDragEnterEvent * event )
|
||
15 years ago
|
{
|
||
|
return ListViewItemDrag::canDecode( event );
|
||
|
}
|
||
|
|
||
|
// ------------------------------------------------------------------
|
||
|
// The Dragobject Implementation ------------------------------------
|
||
|
// ------------------------------------------------------------------
|
||
|
|
||
15 years ago
|
TQDataStream & operator<< ( TQDataStream & stream, const TQListViewItem & item );
|
||
|
TQDataStream & operator>> ( TQDataStream & stream, TQListViewItem & item );
|
||
15 years ago
|
|
||
14 years ago
|
ListViewItemDrag::ListViewItemDrag( ListViewItemList & items, TQWidget * parent, const char * name )
|
||
|
: TQStoredDrag( "qt/listviewitem", parent, name )
|
||
15 years ago
|
{
|
||
|
// ### FIX!
|
||
14 years ago
|
TQByteArray data( sizeof( TQ_INT32 ) + sizeof( TQListViewItem ) * items.count() );
|
||
15 years ago
|
TQDataStream stream( data, IO_WriteOnly );
|
||
15 years ago
|
|
||
|
stream << items.count();
|
||
|
|
||
15 years ago
|
TQListViewItem *i = items.first();
|
||
15 years ago
|
while ( i ) {
|
||
|
stream << *i;
|
||
|
i = items.next();
|
||
|
}
|
||
|
|
||
|
setEncodedData( data );
|
||
|
}
|
||
|
|
||
15 years ago
|
bool ListViewItemDrag::canDecode( TQDragMoveEvent * event )
|
||
15 years ago
|
{
|
||
|
return event->provides( "qt/listviewitem" );
|
||
|
}
|
||
|
|
||
14 years ago
|
bool ListViewItemDrag::decode( TQDropEvent * event, TQListView * parent, TQListViewItem * insertPoint, DropRelation dr )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQByteArray data = event->encodedData( "qt/listviewitem" );
|
||
14 years ago
|
TQListViewItem* itemParent = insertPoint ? insertPoint->parent() : 0;
|
||
15 years ago
|
|
||
|
// Change from sibling (default) to child creation
|
||
|
if ( insertPoint && dr == Child ) {
|
||
|
itemParent = insertPoint;
|
||
|
insertPoint = 0;
|
||
|
}
|
||
|
|
||
|
if ( data.size() ) {
|
||
|
event->accept();
|
||
15 years ago
|
TQDataStream stream( data, IO_ReadOnly );
|
||
15 years ago
|
|
||
|
int count = 0;
|
||
|
stream >> count;
|
||
|
|
||
|
for( int i = 0; i < count; i++ ) {
|
||
|
if ( itemParent ) {
|
||
15 years ago
|
insertPoint = new TQListViewItem( itemParent, insertPoint );
|
||
15 years ago
|
itemParent->setOpen( TRUE );
|
||
14 years ago
|
} else { // No parent for insertPoint, use TQListView
|
||
|
insertPoint = new TQListViewItem( parent, insertPoint );
|
||
15 years ago
|
}
|
||
|
stream >> *insertPoint;
|
||
|
}
|
||
|
return TRUE;
|
||
|
}
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
TQDataStream & operator<< ( TQDataStream & stream, const TQListViewItem & item )
|
||
15 years ago
|
{
|
||
|
int columns = item.listView()->columns();
|
||
|
stream << columns;
|
||
|
|
||
14 years ago
|
TQ_UINT8 b = 0;
|
||
15 years ago
|
|
||
|
int i;
|
||
|
for ( i = 0; i < columns; i++ ) {
|
||
14 years ago
|
b = (TQ_UINT8) ( item.text( i ) != TQString() ); // does column i have a string ?
|
||
15 years ago
|
stream << b;
|
||
|
if ( b ) {
|
||
|
stream << item.text( i );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for ( i = 0; i < columns; i++ ) {
|
||
14 years ago
|
b = (TQ_UINT8) ( !!item.pixmap( i ) ); // does column i have a pixmap ?
|
||
15 years ago
|
stream << b;
|
||
|
if ( b ) {
|
||
|
stream << ( *item.pixmap( i ) );
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
stream << (TQ_UINT8) item.isOpen();
|
||
|
stream << (TQ_UINT8) item.isSelectable();
|
||
|
stream << (TQ_UINT8) item.isExpandable();
|
||
|
stream << (TQ_UINT8) item.dragEnabled();
|
||
|
stream << (TQ_UINT8) item.dropEnabled();
|
||
|
stream << (TQ_UINT8) item.isVisible();
|
||
15 years ago
|
|
||
|
for ( i = 0; i < columns; i++ ) {
|
||
14 years ago
|
stream << (TQ_UINT8) item.renameEnabled( i );
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
stream << (TQ_UINT8) item.multiLinesEnabled();
|
||
15 years ago
|
stream << item.childCount();
|
||
|
|
||
|
if ( item.childCount() > 0 ) {
|
||
15 years ago
|
TQListViewItem * child = item.firstChild();
|
||
15 years ago
|
while ( child ) {
|
||
|
stream << ( *child ); // recursive call
|
||
|
child = child->nextSibling();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQDataStream & operator>> ( TQDataStream & stream, TQListViewItem & item )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQ_INT32 columns;
|
||
15 years ago
|
stream >> columns;
|
||
|
|
||
14 years ago
|
TQ_UINT8 b = 0;
|
||
15 years ago
|
|
||
15 years ago
|
TQString text;
|
||
15 years ago
|
int i;
|
||
|
for ( i = 0; i < columns; i++ ) {
|
||
|
stream >> b;
|
||
|
if ( b ) { // column i has string ?
|
||
|
stream >> text;
|
||
|
item.setText( i, text );
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
TQPixmap pixmap;
|
||
15 years ago
|
for ( i = 0; i < columns; i++ ) {
|
||
|
stream >> b; // column i has pixmap ?
|
||
|
if ( b ) {
|
||
|
stream >> pixmap;
|
||
|
item.setPixmap( i, pixmap );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
stream >> b;
|
||
|
item.setOpen( b );
|
||
|
|
||
|
stream >> b;
|
||
|
item.setSelectable( b );
|
||
|
|
||
|
stream >> b;
|
||
|
item.setExpandable( b );
|
||
|
|
||
|
stream >> b;
|
||
|
item.setDragEnabled( b );
|
||
|
|
||
|
stream >> b;
|
||
|
item.setDropEnabled( b );
|
||
|
|
||
|
stream >> b;
|
||
|
item.setVisible( b );
|
||
|
|
||
|
for ( i = 0; i < columns; i++ ) {
|
||
|
stream >> b;
|
||
|
item.setRenameEnabled( i, b );
|
||
|
}
|
||
|
|
||
|
stream >> b;
|
||
|
item.setMultiLinesEnabled( b );
|
||
|
|
||
|
int childCount;
|
||
|
stream >> childCount;
|
||
|
|
||
15 years ago
|
TQListViewItem *child = 0;
|
||
|
TQListViewItem *prevchild = 0;
|
||
15 years ago
|
for ( i = 0; i < childCount; i++ ) {
|
||
15 years ago
|
child = new TQListViewItem( &item, prevchild );
|
||
15 years ago
|
stream >> ( *child );
|
||
|
item.insertItem( child );
|
||
|
prevchild = child;
|
||
|
}
|
||
|
|
||
|
return stream;
|
||
|
}
|