Move item forward or backwards

This commit is contained in:
杨万青 2015-06-29 19:54:33 +08:00
parent f172da48d3
commit bc81b241a2
7 changed files with 102 additions and 46 deletions

View File

@ -36,7 +36,6 @@ void Panel::resize(const QSize &size)
{
QWidget::resize(size);
leftLayout->resize(this->width() * 2 / 3,this->height());
qWarning() << "=========++++++++++";
rightLayout->move(this->width() - rightLayout->width(),0);
}
@ -44,15 +43,16 @@ void Panel::resize(int width, int height)
{
QWidget::resize(width,height);
leftLayout->resize(this->width() * 2 / 3,this->height());
qWarning() << "=========++++++++++";
rightLayout->move(this->width() - rightLayout->width(),0);
}
void Panel::showScreenMask()
{
qWarning() << "[Info:]" << "Show Screen Mask.";
// qWarning() << "[Info:]" << "Show Screen Mask.";
maskWidget = new ScreenMask();
connect(maskWidget,SIGNAL(itemDropped(QPoint)),this,SLOT(slotItemDropped()));
connect(maskWidget,SIGNAL(itemEntered()),this,SLOT(slotEnteredMask()));
connect(maskWidget,SIGNAL(itemExited()),this,SLOT(slotExitedMask()));
//TODO change to Other ways to do this,it will hide the drag icon
parentWidget->hide();
@ -61,8 +61,10 @@ void Panel::showScreenMask()
void Panel::hideScreenMask()
{
qWarning() << "[Info:]" << "Hide Screen Mask.";
// qWarning() << "[Info:]" << "Hide Screen Mask.";
disconnect(maskWidget,SIGNAL(itemDropped(QPoint)),this,SLOT(slotItemDropped()));
disconnect(maskWidget,SIGNAL(itemEntered()),this,SLOT(slotEnteredMask()));
disconnect(maskWidget,SIGNAL(itemExited()),this,SLOT(slotExitedMask()));
maskWidget->hide();
maskWidget->deleteLater();
maskWidget = NULL;
@ -76,6 +78,18 @@ void Panel::slotDragStarted()
void Panel::slotItemDropped()
{
hideScreenMask();
leftLayout->relayout();
}
void Panel::slotEnteredMask()
{
leftLayout->relayout();
}
void Panel::slotExitedMask()
{
leftLayout->addSpacingItem();
// leftLayout->relayout();
}
Panel::~Panel()

View File

@ -26,6 +26,8 @@ signals:
public slots:
void slotDragStarted();
void slotItemDropped();
void slotEnteredMask();
void slotExitedMask();
private:
DockLayout * leftLayout;

View File

@ -144,17 +144,6 @@ void AppItem::dragEnterEvent(QDragEnterEvent *event)
event->setDropAction(Qt::MoveAction);
event->accept();
}
// if (event->mimeData()->hasFormat("application/x-dnditemdata")){
// if (event->source() == this){
// event->setDropAction(Qt::MoveAction);
// event->accept();
// }else{
// event->ignore();
// }
// }else{
// event->ignore();
// }
}
void AppItem::dragLeaveEvent(QDragLeaveEvent *event)

View File

@ -157,40 +157,32 @@ void DockLayout::relayout()
}
}
void DockLayout::addSpacingItem()
{
if (tmpAppMap.isEmpty())
return;
AppItem *tmpItem = tmpAppMap.firstKey();
for (int i = appList.count() -1;i > lastHoverIndex; i-- )
{
AppItem *targetItem = appList.at(i);
targetItem->setNextPos(targetItem->x() + tmpItem->width() + itemSpacing,0);
QPropertyAnimation *animation = new QPropertyAnimation(targetItem, "pos");
animation->setStartValue(targetItem->pos());
animation->setEndValue(targetItem->getNextPos());
animation->setDuration(150 + i * 10);
animation->setEasingCurve(QEasingCurve::InOutBack);
animation->start();
}
}
void DockLayout::dragoutFromLayout(int index)
{
AppItem * tmpItem = appList.takeAt(index);
tmpItem->setVisible(false);
tmpAppMap.insert(tmpItem,index);
if (index == appList.count())//note,target hast been remove before
{
return;//at the end of list
}
//move follow item,note,target hast been remove before
AppItem * followItem = appList.at(index);
followItem->setNextPos(followItem->x() - tmpItem->width() - itemSpacing,0);
//move last item
for (int i = index + 1; i < appList.count(); i ++)
{
AppItem * frontItem = appList.at(i - 1);
AppItem * targetItem = appList.at(i);
targetItem->setNextPos(frontItem->getNextPos().x() + frontItem->width() + itemSpacing,0);
}
for (int i = index; i < appList.count(); i ++)
{
AppItem *button= appList.at(i);
QPropertyAnimation *animation = new QPropertyAnimation(button, "pos");
animation->setStartValue(button->pos());
animation->setEndValue(button->getNextPos());
animation->setDuration(500 + i * 100);
animation->setEasingCurve(QEasingCurve::InOutBack);
animation->start();
}
}
void DockLayout::dragEnterEvent(QDragEnterEvent *event)
@ -206,7 +198,10 @@ void DockLayout::dropEvent(QDropEvent *event)
tmpItem->setVisible(true);
if (indexOf(tmpItem) == -1)
{
insertItem(tmpItem,lastHoverIndex);
if (movingForward)
insertItem(tmpItem,lastHoverIndex);
else
insertItem(tmpItem,lastHoverIndex + 1);
}
emit itemDropped();
@ -218,6 +213,8 @@ void DockLayout::slotItemDrag(AppItem *item)
int tmpIndex = indexOf(item);
if (tmpIndex != -1)
{
lastHoverIndex = tmpIndex;
m_lastPost = QCursor::pos();
dragoutFromLayout(tmpIndex);
emit dragStarted();
@ -237,7 +234,47 @@ void DockLayout::slotItemRelease(int x, int y, AppItem *item)
void DockLayout::slotItemEntered(QDragEnterEvent * event,AppItem *item)
{
this->lastHoverIndex = indexOf(item);
int tmpIndex = indexOf(item);
QPoint tmpPos = QCursor::pos();
if (tmpPos.x() - m_lastPost.x() == 0)
return;
switch (sortDirection)
{
case LeftToRight:
movingForward = tmpPos.x() - m_lastPost.x() < 0;
break;
case RightToLeft:
movingForward = tmpPos.x() - m_lastPost.x() > 0;
break;
case TopToBottom:
break;
case BottomToTop:
break;
}
m_lastPost = tmpPos;
lastHoverIndex = tmpIndex;
if (!tmpAppMap.isEmpty())
{
AppItem *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::InOutBack);
animation->start();
}
}
void DockLayout::slotItemExited(QDragLeaveEvent *event,AppItem *item)

View File

@ -5,6 +5,7 @@
#include <QList>
#include <QMap>
#include <QPropertyAnimation>
#include <QCursor>
#include "appitem.h"
class DockLayout : public QWidget
@ -40,6 +41,7 @@ public:
int indexOf(AppItem * item);
int indexOf(int x,int y);
void relayout();
void addSpacingItem();
void dragoutFromLayout(int index);
signals:
@ -73,7 +75,9 @@ private:
qreal topMargin = 0;
qreal bottomMargin = 0;
bool movingForward = false;
int lastHoverIndex = 0;
QPoint m_lastPost = QPoint(0,0);
};
#endif // DOCKLAYOUT_H

View File

@ -16,6 +16,13 @@ void ScreenMask::dragEnterEvent(QDragEnterEvent *event)
{
event->setDropAction(Qt::MoveAction);
event->accept();
emit itemEntered();
}
void ScreenMask::dragLeaveEvent(QDragLeaveEvent *event)
{
emit itemExited();
}
void ScreenMask::dropEvent(QDropEvent *event)

View File

@ -41,10 +41,13 @@ public:
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
void dropEvent(QDropEvent *event);
signals:
void itemDropped(QPoint pos);
void itemEntered();
void itemExited();
public slots:
};