Fix this up so I can drag things around properly, and have

them get to their destination without getting misplaced or a 
crash.

In short, keeping iterators to a changing QList is a Bad Idea(tm),
so I did the simple thing can used indices.

svn path=/trunk/KDE/kdebase/workspace/; revision=715952
icc-effect-5.14.5
Maks Orlovich 2007-09-23 15:23:10 +00:00
parent a717c047a3
commit 0d2e2cdfcc
2 changed files with 32 additions and 30 deletions

View File

@ -382,21 +382,21 @@ void ButtonDropSite::dropEvent( QDropEvent* e )
// collect information where to insert the dropped button // collect information where to insert the dropped button
ButtonList *buttonList = 0; ButtonList *buttonList = 0;
ButtonList::iterator buttonPosition; int buttonPosition;
if (leftDropArea().contains(p) ) { if (leftDropArea().contains(p) ) {
buttonList = &buttonsLeft; buttonList = &buttonsLeft;
buttonPosition = buttonsLeft.end(); buttonPosition = buttonsLeft.size();
} else if (rightDropArea().contains(p) ) { } else if (rightDropArea().contains(p) ) {
buttonList = &buttonsRight; buttonList = &buttonsRight;
buttonPosition = buttonsRight.begin(); buttonPosition = 0;
} else { } else {
ButtonDropSiteItem *aboveItem = buttonAt(p); ButtonDropSiteItem *aboveItem = buttonAt(p);
if (!aboveItem) if (!aboveItem)
return; // invalid drop. hasn't occurred _over_ a button (or left/right dropArea), return... return; // invalid drop. hasn't occurred _over_ a button (or left/right dropArea), return...
ButtonList::iterator it; int pos;
if (!getItemIterator(aboveItem, buttonList, it) ) { if (!getItemPos(aboveItem, buttonList, pos) ) {
// didn't find the aboveItem. unlikely to happen since buttonAt() already seems to have found // didn't find the aboveItem. unlikely to happen since buttonAt() already seems to have found
// something valid. anyway... // something valid. anyway...
return; return;
@ -410,12 +410,9 @@ void ButtonDropSite::dropEvent( QDropEvent* e )
if (p.x() < aboveItemRect.left()+aboveItemRect.width()/2 ) { if (p.x() < aboveItemRect.left()+aboveItemRect.width()/2 ) {
// insert before the item // insert before the item
buttonPosition = it; buttonPosition = pos;
} else { } else {
if (it != buttonList->end() ) buttonPosition = pos + 1;
buttonPosition = ++it;
else
buttonPosition = it; // already at the end(), can't increment the iterator!
} }
} }
@ -424,13 +421,18 @@ void ButtonDropSite::dropEvent( QDropEvent* e )
ButtonDropSiteItem *buttonItem = 0; ButtonDropSiteItem *buttonItem = 0;
if (e->source() == this && m_selected) { if (e->source() == this && m_selected) {
ButtonList *oldList = 0; ButtonList *oldList = 0;
ButtonList::iterator oldPos; int oldPos;
if (getItemIterator(m_selected, oldList, oldPos) ) { if (getItemPos(m_selected, oldList, oldPos) ) {
if (oldPos == buttonPosition) if (oldPos == buttonPosition && oldList == buttonList)
return; // button didn't change its position during the drag... return; // button didn't change its position during the drag...
oldList->erase(oldPos); oldList->removeAt(oldPos);
buttonItem = m_selected; buttonItem = m_selected;
// If we're inserting to the right of oldPos, in the same list,
// better adjust the index..
if (buttonList == oldList && buttonPosition > oldPos)
--buttonPosition;
} else { } else {
return; // m_selected not found, return... return; // m_selected not found, return...
} }
@ -452,28 +454,28 @@ void ButtonDropSite::dropEvent( QDropEvent* e )
update(); update();
} }
bool ButtonDropSite::getItemIterator(ButtonDropSiteItem *item, ButtonList* &list, ButtonList::iterator &iterator) bool ButtonDropSite::getItemPos(ButtonDropSiteItem *item, ButtonList* &list, int &pos)
{ {
if (!item) if (!item)
return false; return false;
int ind = buttonsLeft.indexOf(item); // try the left list first... pos = buttonsLeft.indexOf(item); // try the left list first...
if (ind < 0 ) { if (pos >= 0) {
ind = buttonsRight.indexOf(item); // try the right list...
if ( ind < 0 ) {
return false; // item hasn't been found in one of the list, return...
} else {
list = &buttonsRight;
iterator = buttonsRight.begin()+ind;
}
} else {
list = &buttonsLeft; list = &buttonsLeft;
buttonsLeft.begin()+ind; return true;
} }
pos = buttonsRight.indexOf(item); // try the right list...
if (pos >= 0) {
list = &buttonsRight;
return true; return true;
} }
list = 0;
pos = -1;
return false;
}
QRect ButtonDropSite::leftDropArea() QRect ButtonDropSite::leftDropArea()
{ {
// return a 10 pixel drop area... // return a 10 pixel drop area...
@ -600,7 +602,7 @@ void ButtonDropSite::drawButtonList(QPainter *p, const ButtonList& btns, int off
} }
} }
void ButtonDropSite::paintEvent( QPaintEvent* pe ) void ButtonDropSite::paintEvent( QPaintEvent* /*pe*/ )
{ {
QPainter p( this ); QPainter p( this );
int leftoffset = calcButtonListWidth( buttonsLeft ); int leftoffset = calcButtonListWidth( buttonsLeft );

View File

@ -188,9 +188,9 @@ class ButtonDropSite: public QFrame
private: private:
/** /**
* Try to find the item. If found, set its list and iterator and return true, else return false * Try to find the item. If found, set its list and index and return true, else return false
*/ */
bool getItemIterator(ButtonDropSiteItem *item, ButtonList* &list, ButtonList::iterator &iterator); bool getItemPos(ButtonDropSiteItem *item, ButtonList* &list, int &pos);
void cleanDropVisualizer(); void cleanDropVisualizer();
QRect m_oldDropVisualizer; QRect m_oldDropVisualizer;