add drag & drop

Change-Id: Ic940c761dee0abe2b1f2ed8952ef5070fe49a30e
This commit is contained in:
石博文 2016-06-20 14:28:25 +08:00 committed by Hualet Wang
parent dcca4fcad4
commit c08a847a10
4 changed files with 50 additions and 5 deletions

View File

@ -69,9 +69,11 @@ void AppItem::paintEvent(QPaintEvent *e)
void AppItem::mouseReleaseEvent(QMouseEvent *e) void AppItem::mouseReleaseEvent(QMouseEvent *e)
{ {
// activate if (e->button() != Qt::LeftButton)
// TODO: dbus signature changed return;
if (e->button() == Qt::LeftButton)
const QPoint distance = MousePressPos - e->pos();
if (distance.manhattanLength() < APP_DRAG_THRESHOLD)
m_itemEntry->Activate(); m_itemEntry->Activate();
} }
@ -129,12 +131,14 @@ void AppItem::startDrag()
drag->setHotSpot(pixmap.rect().center()); drag->setHotSpot(pixmap.rect().center());
drag->setMimeData(new QMimeData); drag->setMimeData(new QMimeData);
emit dragStarted();
const Qt::DropAction result = drag->exec(Qt::MoveAction); const Qt::DropAction result = drag->exec(Qt::MoveAction);
qDebug() << "dnd result: " << result; qDebug() << "dnd result: " << result;
m_draging = false; m_draging = false;
update(); update();
setVisible(true);
} }
void AppItem::initClientManager() void AppItem::initClientManager()

View File

@ -24,6 +24,9 @@ public:
ItemType itemType() const; ItemType itemType() const;
signals:
void dragStarted() const;
protected: protected:
void paintEvent(QPaintEvent *e); void paintEvent(QPaintEvent *e);
void mousePressEvent(QMouseEvent *e); void mousePressEvent(QMouseEvent *e);

View File

@ -3,6 +3,8 @@
#include <QBoxLayout> #include <QBoxLayout>
#include <QDragEnterEvent> #include <QDragEnterEvent>
DockItem *MainPanel::DragingItem = nullptr;
MainPanel::MainPanel(QWidget *parent) MainPanel::MainPanel(QWidget *parent)
: QFrame(parent), : QFrame(parent),
m_itemLayout(new QBoxLayout(QBoxLayout::LeftToRight, this)), m_itemLayout(new QBoxLayout(QBoxLayout::LeftToRight, this)),
@ -25,7 +27,10 @@ MainPanel::MainPanel(QWidget *parent)
const QList<DockItem *> itemList = m_itemController->itemList(); const QList<DockItem *> itemList = m_itemController->itemList();
for (auto item : itemList) for (auto item : itemList)
{
initItemConnection(item);
m_itemLayout->addWidget(item); m_itemLayout->addWidget(item);
}
setLayout(m_itemLayout); setLayout(m_itemLayout);
} }
@ -52,16 +57,36 @@ void MainPanel::dragEnterEvent(QDragEnterEvent *e)
{ {
// TODO: check // TODO: check
e->accept(); e->accept();
if (!DragingItem)
return;
DragingItem->show();
} }
void MainPanel::dragMoveEvent(QDragMoveEvent *e) 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) 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() void MainPanel::adjustItemSize()
@ -82,6 +107,7 @@ void MainPanel::adjustItemSize()
void MainPanel::itemInserted(const int index, DockItem *item) void MainPanel::itemInserted(const int index, DockItem *item)
{ {
initItemConnection(item);
m_itemLayout->insertWidget(index, item); m_itemLayout->insertWidget(index, item);
item->setFixedWidth(80); item->setFixedWidth(80);
@ -93,3 +119,8 @@ void MainPanel::itemRemoved(DockItem *item)
{ {
m_itemLayout->removeWidget(item); m_itemLayout->removeWidget(item);
} }
void MainPanel::itemDragStarted()
{
DragingItem = qobject_cast<DockItem *>(sender());
}

View File

@ -20,16 +20,23 @@ private:
void resizeEvent(QResizeEvent *e); void resizeEvent(QResizeEvent *e);
void dragEnterEvent(QDragEnterEvent *e); void dragEnterEvent(QDragEnterEvent *e);
void dragMoveEvent(QDragMoveEvent *e); void dragMoveEvent(QDragMoveEvent *e);
void dragLeaveEvent(QDragLeaveEvent *e);
void dropEvent(QDropEvent *e); void dropEvent(QDropEvent *e);
void initItemConnection(DockItem *item);
private slots:
void adjustItemSize(); void adjustItemSize();
void itemInserted(const int index, DockItem *item); void itemInserted(const int index, DockItem *item);
void itemRemoved(DockItem *item); void itemRemoved(DockItem *item);
void itemDragStarted();
private: private:
QBoxLayout *m_itemLayout; QBoxLayout *m_itemLayout;
DockItemController *m_itemController; DockItemController *m_itemController;
static DockItem *DragingItem;
}; };
#endif // MAINPANEL_H #endif // MAINPANEL_H