From c08a847a10407af95a5463a3d6a08527ee509bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=8D=9A=E6=96=87?= Date: Mon, 20 Jun 2016 14:28:25 +0800 Subject: [PATCH] add drag & drop Change-Id: Ic940c761dee0abe2b1f2ed8952ef5070fe49a30e --- frame/item/appitem.cpp | 10 +++++++--- frame/item/dockitem.h | 3 +++ frame/panel/mainpanel.cpp | 35 +++++++++++++++++++++++++++++++++-- frame/panel/mainpanel.h | 7 +++++++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/frame/item/appitem.cpp b/frame/item/appitem.cpp index 2c9772e72..478ef4946 100644 --- a/frame/item/appitem.cpp +++ b/frame/item/appitem.cpp @@ -69,9 +69,11 @@ void AppItem::paintEvent(QPaintEvent *e) void AppItem::mouseReleaseEvent(QMouseEvent *e) { - // activate - // TODO: dbus signature changed - if (e->button() == Qt::LeftButton) + if (e->button() != Qt::LeftButton) + return; + + const QPoint distance = MousePressPos - e->pos(); + if (distance.manhattanLength() < APP_DRAG_THRESHOLD) m_itemEntry->Activate(); } @@ -129,12 +131,14 @@ void AppItem::startDrag() drag->setHotSpot(pixmap.rect().center()); drag->setMimeData(new QMimeData); + emit dragStarted(); const Qt::DropAction result = drag->exec(Qt::MoveAction); qDebug() << "dnd result: " << result; m_draging = false; update(); + setVisible(true); } void AppItem::initClientManager() diff --git a/frame/item/dockitem.h b/frame/item/dockitem.h index 0d308014b..c3c5e24e7 100644 --- a/frame/item/dockitem.h +++ b/frame/item/dockitem.h @@ -24,6 +24,9 @@ public: ItemType itemType() const; +signals: + void dragStarted() const; + protected: void paintEvent(QPaintEvent *e); void mousePressEvent(QMouseEvent *e); diff --git a/frame/panel/mainpanel.cpp b/frame/panel/mainpanel.cpp index b27250460..26485eac1 100644 --- a/frame/panel/mainpanel.cpp +++ b/frame/panel/mainpanel.cpp @@ -3,6 +3,8 @@ #include #include +DockItem *MainPanel::DragingItem = nullptr; + MainPanel::MainPanel(QWidget *parent) : QFrame(parent), m_itemLayout(new QBoxLayout(QBoxLayout::LeftToRight, this)), @@ -25,7 +27,10 @@ MainPanel::MainPanel(QWidget *parent) const QList itemList = m_itemController->itemList(); for (auto item : itemList) + { + initItemConnection(item); m_itemLayout->addWidget(item); + } setLayout(m_itemLayout); } @@ -52,16 +57,36 @@ void MainPanel::dragEnterEvent(QDragEnterEvent *e) { // TODO: check e->accept(); + + if (!DragingItem) + return; + DragingItem->show(); } void MainPanel::dragMoveEvent(QDragMoveEvent *e) { - qDebug() << e; + Q_UNUSED(e); + // qDebug() << e; +} + +void MainPanel::dragLeaveEvent(QDragLeaveEvent *e) +{ + Q_UNUSED(e) + + if (!DragingItem) + return; + DragingItem->hide(); } void MainPanel::dropEvent(QDropEvent *e) { - qDebug() << e; + Q_UNUSED(e) +// qDebug() << e; +} + +void MainPanel::initItemConnection(DockItem *item) +{ + connect(item, &DockItem::dragStarted, this, &MainPanel::itemDragStarted); } void MainPanel::adjustItemSize() @@ -82,6 +107,7 @@ void MainPanel::adjustItemSize() void MainPanel::itemInserted(const int index, DockItem *item) { + initItemConnection(item); m_itemLayout->insertWidget(index, item); item->setFixedWidth(80); @@ -93,3 +119,8 @@ void MainPanel::itemRemoved(DockItem *item) { m_itemLayout->removeWidget(item); } + +void MainPanel::itemDragStarted() +{ + DragingItem = qobject_cast(sender()); +} diff --git a/frame/panel/mainpanel.h b/frame/panel/mainpanel.h index d6d78d5e4..16b889238 100644 --- a/frame/panel/mainpanel.h +++ b/frame/panel/mainpanel.h @@ -20,16 +20,23 @@ private: void resizeEvent(QResizeEvent *e); void dragEnterEvent(QDragEnterEvent *e); void dragMoveEvent(QDragMoveEvent *e); + void dragLeaveEvent(QDragLeaveEvent *e); void dropEvent(QDropEvent *e); + void initItemConnection(DockItem *item); + +private slots: void adjustItemSize(); void itemInserted(const int index, DockItem *item); void itemRemoved(DockItem *item); + void itemDragStarted(); private: QBoxLayout *m_itemLayout; DockItemController *m_itemController; + + static DockItem *DragingItem; }; #endif // MAINPANEL_H