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.
306 lines
7.7 KiB
306 lines
7.7 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 "listboxdnd.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 ListBoxItemDrag : public TQStoredDrag
|
||
15 years ago
|
{
|
||
|
public:
|
||
14 years ago
|
ListBoxItemDrag( ListBoxItemList & items, bool sendPtr = FALSE, TQListBox * parent = 0, const char * name = 0 );
|
||
15 years ago
|
~ListBoxItemDrag() {};
|
||
15 years ago
|
static bool canDecode( TQDragMoveEvent * event );
|
||
14 years ago
|
static bool decode( TQDropEvent * event, TQListBox * parent, TQListBoxItem * insertPoint );
|
||
15 years ago
|
enum ItemType { ListBoxText = 1, ListBoxPixmap = 2 };
|
||
|
};
|
||
|
// ------------------------------------------------------------------
|
||
|
|
||
15 years ago
|
ListBoxDnd::ListBoxDnd( TQListBox * eventSource, const char * name )
|
||
15 years ago
|
: ListDnd( eventSource, name ) { }
|
||
|
|
||
15 years ago
|
void ListBoxDnd::confirmDrop( TQListBoxItem * )
|
||
15 years ago
|
{
|
||
|
dropConfirmed = TRUE;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool ListBoxDnd::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();
|
||
|
TQListBoxItem * after = itemAt( pos );
|
||
15 years ago
|
|
||
15 years ago
|
if ( ListBoxItemDrag::decode( event, (TQListBox *) src, after ) ) {
|
||
15 years ago
|
event->accept();
|
||
15 years ago
|
TQListBox * src = (TQListBox *) this->src;
|
||
|
TQListBoxItem * item = ( after ? after->next() : src->firstItem() );
|
||
15 years ago
|
src->setCurrentItem( item );
|
||
|
emit dropped( item ); // ###FIX: Supports only one item!
|
||
|
}
|
||
|
}
|
||
|
|
||
|
line->hide();
|
||
|
dragInside = FALSE;
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool ListBoxDnd::mouseMoveEvent( TQMouseEvent * event )
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( event->state() & Qt::LeftButton ) {
|
||
15 years ago
|
if ( ( event->pos() - mousePressPos ).manhattanLength() > 3 ) {
|
||
|
|
||
|
ListBoxItemList list;
|
||
|
buildList( list );
|
||
15 years ago
|
ListBoxItemDrag * dragobject = new ListBoxItemDrag( list, (dMode & Internal), (TQListBox *) src );
|
||
15 years ago
|
|
||
|
// Emit signal for all dragged items
|
||
15 years ago
|
TQListBoxItem * i = list.first();
|
||
15 years ago
|
while ( i ) {
|
||
|
emit dragged( i );
|
||
|
i = list.next();
|
||
|
}
|
||
|
|
||
|
if ( dMode & Move ) {
|
||
|
removeList( list ); // "hide" items
|
||
|
}
|
||
|
|
||
|
dragobject->dragCopy();
|
||
|
|
||
|
if ( dMode & Move ) {
|
||
|
if ( dropConfirmed ) {
|
||
|
// ###FIX: memleak ? in internal mode, only pointers are transfered...
|
||
|
//list.setAutoDelete( TRUE );
|
||
|
list.clear();
|
||
|
dropConfirmed = FALSE;
|
||
|
}
|
||
|
insertList( list ); // "show" items
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
int ListBoxDnd::buildList( ListBoxItemList & list )
|
||
|
{
|
||
15 years ago
|
TQListBoxItem * i = ((TQListBox *)src)->firstItem();
|
||
15 years ago
|
while ( i ) {
|
||
|
if ( i->isSelected() ) {
|
||
15 years ago
|
((TQListBox *)src)->setSelected( i, FALSE );
|
||
15 years ago
|
list.append( i );
|
||
|
}
|
||
|
i = i->next();
|
||
|
}
|
||
|
return list.count();
|
||
|
}
|
||
|
|
||
|
void ListBoxDnd::insertList( ListBoxItemList & list )
|
||
|
{
|
||
15 years ago
|
TQListBoxItem * i = list.first();
|
||
15 years ago
|
while ( i ) {
|
||
15 years ago
|
((TQListBox *)src)->insertItem( i, i->prev() );
|
||
15 years ago
|
i = list.next();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void ListBoxDnd::removeList( ListBoxItemList & list )
|
||
|
{
|
||
15 years ago
|
TQListBoxItem * i = list.first();
|
||
15 years ago
|
while ( i ) {
|
||
14 years ago
|
((TQListBox *)src)->takeItem( i ); // remove item from TQListBox
|
||
15 years ago
|
i = list.next();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void ListBoxDnd::updateLine( const TQPoint & dragPos )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQListBox * src = (TQListBox *) this->src;
|
||
|
TQListBoxItem *item = itemAt( dragPos );
|
||
15 years ago
|
|
||
|
int ypos = item ?
|
||
13 years ago
|
( src->itemRect( item ).bottom() - ( line->height() / 2 ) ) :
|
||
|
( src->itemRect( ((TQListBox *)src)->firstItem() ).top() );
|
||
15 years ago
|
|
||
|
line->resize( src->viewport()->width(), line->height() );
|
||
|
line->move( 0, ypos );
|
||
|
}
|
||
|
|
||
15 years ago
|
TQListBoxItem * ListBoxDnd::itemAt( TQPoint pos )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQListBox * src = (TQListBox *) this->src;
|
||
|
TQListBoxItem * result = src->itemAt( pos );
|
||
|
TQListBoxItem * last = src->item( src->count() - 1 );
|
||
15 years ago
|
int i = src->index( result );
|
||
|
|
||
13 years ago
|
if ( result && ( pos.y() < (src->itemRect(result).top() + src->itemHeight(i)/2) ) )
|
||
15 years ago
|
result = result->prev();
|
||
13 years ago
|
else if ( !result && pos.y() > src->itemRect( last ).bottom() )
|
||
15 years ago
|
result = last;
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool ListBoxDnd::canDecode( TQDragEnterEvent * event )
|
||
15 years ago
|
{
|
||
|
return ListBoxItemDrag::canDecode( event );
|
||
|
}
|
||
|
|
||
|
|
||
|
// ------------------------------------------------------------------
|
||
|
// The Dragobject Implementation ------------------------------------
|
||
|
// ------------------------------------------------------------------
|
||
|
|
||
14 years ago
|
ListBoxItemDrag::ListBoxItemDrag( ListBoxItemList & items, bool sendPtr, TQListBox * parent, const char * name )
|
||
|
: TQStoredDrag( "qt/listboxitem", parent, name )
|
||
15 years ago
|
{
|
||
|
// ### FIX!
|
||
14 years ago
|
TQByteArray data( sizeof( TQ_INT32 ) + sizeof( TQListBoxItem ) * items.count() );
|
||
15 years ago
|
TQDataStream stream( data, IO_WriteOnly );
|
||
15 years ago
|
|
||
|
stream << items.count();
|
||
14 years ago
|
stream << (TQ_UINT8) sendPtr; // just transfer item pointer; omit data
|
||
15 years ago
|
|
||
15 years ago
|
TQListBoxItem * i = items.first();
|
||
15 years ago
|
|
||
|
if ( sendPtr ) {
|
||
|
|
||
|
while ( i ) {
|
||
|
|
||
14 years ago
|
stream << (TQ_ULONG) i; //###FIX: demands sizeof(ulong) >= sizeof(void*)
|
||
15 years ago
|
i = items.next();
|
||
|
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
|
||
|
while ( i ) {
|
||
|
|
||
14 years ago
|
TQ_UINT8 b = 0;
|
||
15 years ago
|
|
||
14 years ago
|
b = (TQ_UINT8) ( i->text() != TQString() ); // does item have text ?
|
||
15 years ago
|
stream << b;
|
||
|
if ( b ) {
|
||
|
stream << i->text();
|
||
|
}
|
||
|
|
||
14 years ago
|
b = (TQ_UINT8) ( !!i->pixmap() ); // does item have a pixmap ?
|
||
15 years ago
|
stream << b;
|
||
|
if ( b ) {
|
||
|
stream << ( *i->pixmap() );
|
||
|
}
|
||
|
|
||
14 years ago
|
stream << (TQ_UINT8) i->isSelectable();
|
||
15 years ago
|
|
||
|
i = items.next();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
setEncodedData( data );
|
||
|
}
|
||
|
|
||
15 years ago
|
bool ListBoxItemDrag::canDecode( TQDragMoveEvent * event )
|
||
15 years ago
|
{
|
||
|
return event->provides( "qt/listboxitem" );
|
||
|
}
|
||
|
|
||
14 years ago
|
bool ListBoxItemDrag::decode( TQDropEvent * event, TQListBox * parent, TQListBoxItem * after )
|
||
15 years ago
|
{
|
||
13 years ago
|
TQByteArray data = event->encodedData( "qt/listboxitem" );
|
||
15 years ago
|
|
||
|
if ( data.size() ) {
|
||
|
event->accept();
|
||
15 years ago
|
TQDataStream stream( data, IO_ReadOnly );
|
||
15 years ago
|
|
||
|
int count = 0;
|
||
|
stream >> count;
|
||
|
|
||
14 years ago
|
TQ_UINT8 recievePtr = 0; // data contains just item pointers; no data
|
||
15 years ago
|
stream >> recievePtr;
|
||
|
|
||
15 years ago
|
TQListBoxItem * item = 0;
|
||
15 years ago
|
|
||
|
if ( recievePtr ) {
|
||
|
|
||
|
for( int i = 0; i < count; i++ ) {
|
||
|
|
||
14 years ago
|
TQ_ULONG p = 0; //###FIX: demands sizeof(ulong) >= sizeof(void*)
|
||
15 years ago
|
stream >> p;
|
||
15 years ago
|
item = (TQListBoxItem *) p;
|
||
15 years ago
|
|
||
14 years ago
|
parent->insertItem( item, after );
|
||
15 years ago
|
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
|
||
|
for ( int i = 0; i < count; i++ ) {
|
||
|
|
||
14 years ago
|
TQ_UINT8 hasText = 0;
|
||
15 years ago
|
TQString text;
|
||
15 years ago
|
stream >> hasText;
|
||
|
if ( hasText ) {
|
||
|
stream >> text;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQ_UINT8 hasPixmap = 0;
|
||
15 years ago
|
TQPixmap pixmap;
|
||
15 years ago
|
stream >> hasPixmap;
|
||
|
if ( hasPixmap ) {
|
||
|
stream >> pixmap;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQ_UINT8 isSelectable = 0;
|
||
15 years ago
|
stream >> isSelectable;
|
||
|
|
||
|
if ( hasPixmap ) {
|
||
14 years ago
|
item = new TQListBoxPixmap( parent, pixmap, text, after );
|
||
15 years ago
|
} else {
|
||
14 years ago
|
item = new TQListBoxText( parent, text, after );
|
||
15 years ago
|
}
|
||
|
|
||
|
item->setSelectable( isSelectable );
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
return FALSE;
|
||
|
}
|