kstars
draglistbox.cpp
Go to the documentation of this file.00001 /*************************************************************************** 00002 draglistbox.cpp - description 00003 ------------------- 00004 begin : Sun May 29 2005 00005 copyright : (C) 2005 by Jason Harris 00006 email : kstars@30doradus.org 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include <qdragobject.h> 00019 #include <klocale.h> 00020 #include <kdebug.h> 00021 00022 #include "draglistbox.h" 00023 00024 DragListBox::DragListBox( QWidget *parent, const char *name, WFlags f ) 00025 : KListBox( parent, name, f ) { 00026 00027 setAcceptDrops( TRUE ); 00028 dragging = FALSE; 00029 } 00030 00031 DragListBox::~DragListBox() {} 00032 00033 void DragListBox::dragEnterEvent( QDragEnterEvent *evt ) 00034 { 00035 if ( QTextDrag::canDecode( evt ) ) 00036 evt->accept(); 00037 } 00038 00039 bool DragListBox::contains( const QString &s ) const { 00040 for ( uint i=0; i<count(); ++i ) 00041 if ( text(i) == s ) return true; 00042 00043 return false; 00044 } 00045 00046 void DragListBox::dropEvent( QDropEvent *evt ) { 00047 QString text; 00048 00049 int i = int( float(evt->pos().y())/float(itemHeight()) + 0.5 ) + topItem(); 00050 if ( i > count() + 1 ) i = count() + 1; 00051 00052 if ( QTextDrag::decode( evt, text ) ) { 00053 //If we dragged an "Ignore item from the FieldList to the FieldPool, then we don't 00054 //need to insert the item, because FieldPool already has a persistent Ignore item. 00055 if ( !( text == i18n("Ignore" ) && QString(evt->source()->name()) == "FieldList" && 00056 evt->source() != this )) { 00057 insertItem( text, i ); 00058 } 00059 00060 //If we dragged the "Ignore" item from FieldPool to FieldList, then we don't 00061 //want to remove the item from the FieldPool 00062 if ( !( text == i18n("Ignore" ) && QString(evt->source()->name()) == "FieldPool" && 00063 evt->source() != this ) ) { 00064 DragListBox *fp = (DragListBox*)evt->source(); 00065 fp->removeItem( fp->currentItem() ); 00066 } 00067 } 00068 } 00069 00070 00071 void DragListBox::mousePressEvent( QMouseEvent *evt ) { 00072 QListBox::mousePressEvent( evt ); 00073 dragging = TRUE; 00074 00075 //Record position of the Ignore item; we may have to restore it. 00076 if ( currentText() == i18n("Ignore") ) 00077 IgnoreIndex = currentItem(); 00078 else 00079 IgnoreIndex = -1; 00080 } 00081 00082 00083 void DragListBox::mouseMoveEvent( QMouseEvent * ) 00084 { 00085 if ( dragging ) { 00086 QDragObject *drag = new QTextDrag( currentText(), this ); 00087 drag->dragMove(); 00088 dragging = FALSE; 00089 } 00090 } 00091 00092 #include "draglistbox.moc"