From bc81b241a2b4b1a148a7f2f1f26b1dc6e9d801f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E4=B8=87=E9=9D=92?= Date: Mon, 29 Jun 2015 19:54:33 +0800 Subject: [PATCH] Move item forward or backwards --- dde-dock/Panel/panel.cpp | 22 ++++++-- dde-dock/Panel/panel.h | 2 + dde-dock/Widgets/appitem.cpp | 11 ---- dde-dock/Widgets/docklayout.cpp | 99 ++++++++++++++++++++++----------- dde-dock/Widgets/docklayout.h | 4 ++ dde-dock/Widgets/screenmask.cpp | 7 +++ dde-dock/Widgets/screenmask.h | 3 + 7 files changed, 102 insertions(+), 46 deletions(-) diff --git a/dde-dock/Panel/panel.cpp b/dde-dock/Panel/panel.cpp index c2fe0bc8b..4924711a3 100644 --- a/dde-dock/Panel/panel.cpp +++ b/dde-dock/Panel/panel.cpp @@ -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() diff --git a/dde-dock/Panel/panel.h b/dde-dock/Panel/panel.h index 46ec03d06..98ffeffbd 100644 --- a/dde-dock/Panel/panel.h +++ b/dde-dock/Panel/panel.h @@ -26,6 +26,8 @@ signals: public slots: void slotDragStarted(); void slotItemDropped(); + void slotEnteredMask(); + void slotExitedMask(); private: DockLayout * leftLayout; diff --git a/dde-dock/Widgets/appitem.cpp b/dde-dock/Widgets/appitem.cpp index 3473bcbe8..17a1322c0 100644 --- a/dde-dock/Widgets/appitem.cpp +++ b/dde-dock/Widgets/appitem.cpp @@ -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) diff --git a/dde-dock/Widgets/docklayout.cpp b/dde-dock/Widgets/docklayout.cpp index 6c0f319fc..0ed280d2e 100644 --- a/dde-dock/Widgets/docklayout.cpp +++ b/dde-dock/Widgets/docklayout.cpp @@ -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) diff --git a/dde-dock/Widgets/docklayout.h b/dde-dock/Widgets/docklayout.h index cef4be37e..c50b491c1 100644 --- a/dde-dock/Widgets/docklayout.h +++ b/dde-dock/Widgets/docklayout.h @@ -5,6 +5,7 @@ #include #include #include +#include #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 diff --git a/dde-dock/Widgets/screenmask.cpp b/dde-dock/Widgets/screenmask.cpp index 94e8d61a6..7813ba10c 100644 --- a/dde-dock/Widgets/screenmask.cpp +++ b/dde-dock/Widgets/screenmask.cpp @@ -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) diff --git a/dde-dock/Widgets/screenmask.h b/dde-dock/Widgets/screenmask.h index 85b61cce8..8b25a84e9 100644 --- a/dde-dock/Widgets/screenmask.h +++ b/dde-dock/Widgets/screenmask.h @@ -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: };