Repair of sort error in the fast moving

This commit is contained in:
杨万青 2015-07-08 20:19:03 +08:00
parent 256c8fc854
commit e9f7f3e068
2 changed files with 88 additions and 16 deletions

View File

@ -57,11 +57,13 @@ void DockLayout::sortLeftToRight()
return;
appList.at(0)->move(itemSpacing,(height() - appList.at(0)->height()) / 2);
appList.at(0)->setNextPos(appList.at(0)->pos());
for (int i = 1; i < appList.count(); i ++)
{
AbstractDockItem * frontItem = appList.at(i - 1);
appList.at(i)->move(frontItem->pos().x() + frontItem->width() + itemSpacing,height() - appList.at(i)->height());
appList.at(i)->setNextPos(appList.at(i)->pos());
}
}
@ -99,6 +101,76 @@ bool DockLayout::hasSpacingItemInList()
return false;
}
int DockLayout::spacingItemIndex()
{
if (sortDirection == RightToLeft)
return -1;
if (appList.count() <= 1)
return -1;
if (appList.at(0)->getNextPos().x() > itemSpacing)
return 0;
for (int i = 1; i < appList.count(); i ++)
{
if (appList.at(i)->getNextPos().x() - itemSpacing != appList.at(i - 1)->getNextPos().x() + appList.at(i - 1)->width())
{
return i;
}
}
return -1;
}
void DockLayout::moveWithSpacingItem(int hoverIndex)
{
if (sortDirection == LeftToRight)
{
int spacintIndex = spacingItemIndex();
if (spacintIndex == -1)
return;
if (spacintIndex > hoverIndex)
{
for (int i = hoverIndex; i < spacintIndex; i ++)
{
AbstractDockItem *targetItem = appList.at(i);
targetItem->setNextPos(QPoint(targetItem->x() + tmpAppMap.firstKey()->width() + itemSpacing,0));
QPropertyAnimation *animation = new QPropertyAnimation(targetItem, "pos");
animation->setStartValue(targetItem->pos());
animation->setEndValue(targetItem->getNextPos());
animation->setDuration(200);
animation->setEasingCurve(QEasingCurve::OutCubic);
animation->start();
this->m_animationItemCount ++;
connect(animation,&QPropertyAnimation::finished,[=](){
this->m_animationItemCount --;
});
}
}
else
{
for (int i = spacintIndex; i <= hoverIndex; i ++)
{
AbstractDockItem *targetItem = appList.at(i);
targetItem->setNextPos(QPoint(targetItem->x() - tmpAppMap.firstKey()->width() - itemSpacing,0));
QPropertyAnimation *animation = new QPropertyAnimation(targetItem, "pos");
animation->setStartValue(targetItem->pos());
animation->setEndValue(targetItem->getNextPos());
animation->setDuration(200);
animation->setEasingCurve(QEasingCurve::OutCubic);
animation->start();
this->m_animationItemCount ++;
connect(animation,&QPropertyAnimation::finished,[=](){
this->m_animationItemCount --;
});
}
}
}
//TODO RightToLeft
}
int DockLayout::indexOf(AbstractDockItem *item)
{
return appList.indexOf(item);
@ -204,6 +276,7 @@ void DockLayout::dropEvent(QDropEvent *event)
}
emit itemDropped();
m_animationItemCount = 0;
}
void DockLayout::slotItemDrag()
@ -252,13 +325,24 @@ void DockLayout::slotItemEntered(QDragEnterEvent *)
if (tmpPos.x() - m_lastPost.x() == 0)
return;
bool lastState = movingForward;
switch (sortDirection)
{
case LeftToRight:
movingForward = tmpPos.x() - m_lastPost.x() < 0;
if (movingForward != lastState && m_animationItemCount > 0)
{
movingForward = lastState;
return;
}
break;
case RightToLeft:
movingForward = tmpPos.x() - m_lastPost.x() > 0;
if (movingForward != lastState && m_animationItemCount > 0)
{
movingForward = lastState;
return;
}
break;
}
@ -266,22 +350,7 @@ void DockLayout::slotItemEntered(QDragEnterEvent *)
if (!tmpAppMap.isEmpty())
{
AbstractDockItem *targetItem = appList.at(tmpIndex);
if (movingForward)
{
targetItem->setNextPos(QPoint(targetItem->x() + tmpAppMap.firstKey()->width() + itemSpacing,0));
}
else
{
targetItem->setNextPos(QPoint(targetItem->x() - tmpAppMap.firstKey()->width() - itemSpacing,0));
}
QPropertyAnimation *animation = new QPropertyAnimation(targetItem, "pos");
animation->setStartValue(targetItem->pos());
animation->setEndValue(targetItem->getNextPos());
animation->setDuration(200);
animation->setEasingCurve(QEasingCurve::OutCubic);
animation->start();
moveWithSpacingItem(tmpIndex);
}
}

View File

@ -56,7 +56,9 @@ private:
void addSpacingItem();
void dragoutFromLayout(int index);
bool hasSpacingItemInList();
int spacingItemIndex();
void moveWithSpacingItem(int hoverIndex);
private:
QList<AbstractDockItem *> appList;
QMap<AbstractDockItem *,int> tmpAppMap;//only one item inside
@ -66,6 +68,7 @@ private:
bool movingForward = false;
int lastHoverIndex = 0;
int m_animationItemCount = 0;
QPoint m_lastPost = QPoint(0,0);
};