mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-04 17:33:05 +00:00
Drag and move item position
This commit is contained in:
parent
fb97508ea7
commit
f91cc3ccdc
@ -6,11 +6,11 @@ Panel::Panel(QWidget *parent) : QWidget(parent)
|
|||||||
leftLayout->resize(1024,50);
|
leftLayout->resize(1024,50);
|
||||||
leftLayout->move(0,0);
|
leftLayout->move(0,0);
|
||||||
|
|
||||||
AppItem * b1 = new AppItem("App",":/test/Resources/images/brasero.png");b1->resize(50,50);
|
AppItem * b1 = new AppItem("App",":/test/Resources/images/brasero.png");b1->resize(50,50);b1->setAcceptDrops(true);
|
||||||
AppItem * b2 = new AppItem("App",":/test/Resources/images/crossover.png");b2->resize(50,50);
|
AppItem * b2 = new AppItem("App",":/test/Resources/images/crossover.png");b2->resize(50,50);b2->setAcceptDrops(true);
|
||||||
AppItem * b3 = new AppItem("App",":/test/Resources/images/gcr-gnupg.png");b3->resize(50,50);
|
AppItem * b3 = new AppItem("App",":/test/Resources/images/gcr-gnupg.png");b3->resize(50,50);b3->setAcceptDrops(true);
|
||||||
AppItem * b4 = new AppItem("App",":/test/Resources/images/display-im6.q16.png");b4->resize(50,50);
|
AppItem * b4 = new AppItem("App",":/test/Resources/images/display-im6.q16.png");b4->resize(50,50);b4->setAcceptDrops(true);
|
||||||
AppItem * b5 = new AppItem("App",":/test/Resources/images/eog.png");b5->resize(50,50);
|
AppItem * b5 = new AppItem("App",":/test/Resources/images/eog.png");b5->resize(50,50);b5->setAcceptDrops(true);
|
||||||
|
|
||||||
leftLayout->addItem(b1);
|
leftLayout->addItem(b1);
|
||||||
leftLayout->addItem(b2);
|
leftLayout->addItem(b2);
|
||||||
|
@ -3,9 +3,10 @@
|
|||||||
AppItem::AppItem(QWidget *parent) :
|
AppItem::AppItem(QWidget *parent) :
|
||||||
QFrame(parent)
|
QFrame(parent)
|
||||||
{
|
{
|
||||||
this->setParent(parent);
|
setParent(parent);
|
||||||
|
|
||||||
this->initBackground();
|
initBackground();
|
||||||
|
setAcceptDrops(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
AppItem::AppItem(QString title, QWidget *parent):
|
AppItem::AppItem(QString title, QWidget *parent):
|
||||||
@ -112,7 +113,7 @@ void AppItem::initBackground()
|
|||||||
|
|
||||||
void AppItem::mousePressEvent(QMouseEvent * event)
|
void AppItem::mousePressEvent(QMouseEvent * event)
|
||||||
{
|
{
|
||||||
// qWarning() << "mouse press...";
|
//qWarning() << "mouse press...";
|
||||||
emit mousePress(event->globalX(), event->globalY(),this);
|
emit mousePress(event->globalX(), event->globalY(),this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,16 +123,32 @@ void AppItem::mouseReleaseEvent(QMouseEvent * event)
|
|||||||
emit mouseRelease(event->globalX(), event->globalY(),this);
|
emit mouseRelease(event->globalX(), event->globalY(),this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AppItem::mouseMoveEvent(QMouseEvent * event)
|
|
||||||
{
|
|
||||||
emit mouseMove(event->globalX(), event->globalY(),this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AppItem::mouseDoubleClickEvent(QMouseEvent * event)
|
void AppItem::mouseDoubleClickEvent(QMouseEvent * event)
|
||||||
{
|
{
|
||||||
emit mouseDoubleClick(this);
|
emit mouseDoubleClick(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AppItem::mouseMoveEvent(QMouseEvent *event)
|
||||||
|
{
|
||||||
|
//this event will only execp onec then handle by Drag
|
||||||
|
emit dragStart(this);
|
||||||
|
|
||||||
|
Qt::MouseButtons btn = event->buttons();
|
||||||
|
if(btn == Qt::LeftButton)
|
||||||
|
{
|
||||||
|
QDrag* drag = new QDrag(this);
|
||||||
|
QMimeData* data = new QMimeData();
|
||||||
|
drag->setMimeData(data);
|
||||||
|
|
||||||
|
QPixmap pixmap(this->itemIconPath);
|
||||||
|
drag->setPixmap(pixmap);
|
||||||
|
|
||||||
|
drag->setHotSpot(QPoint(15,15));
|
||||||
|
|
||||||
|
drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void AppItem::enterEvent(QEvent *event)
|
void AppItem::enterEvent(QEvent *event)
|
||||||
{
|
{
|
||||||
emit mouseEntered(this);
|
emit mouseEntered(this);
|
||||||
@ -142,6 +159,35 @@ void AppItem::leaveEvent(QEvent *event)
|
|||||||
emit mouseExited(this);
|
emit mouseExited(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AppItem::dragEnterEvent(QDragEnterEvent *event)
|
||||||
|
{
|
||||||
|
emit dragEntered(event,this);
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
emit dragExited(event,this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AppItem::dropEvent(QDropEvent *event)
|
||||||
|
{
|
||||||
|
qWarning() << "Item get drop:" << event->pos();
|
||||||
|
emit drop(event,this);
|
||||||
|
}
|
||||||
|
|
||||||
AppItem::~AppItem()
|
AppItem::~AppItem()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -7,11 +7,16 @@
|
|||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
#include <QDrag>
|
#include <QDrag>
|
||||||
#include <QRectF>
|
#include <QRectF>
|
||||||
|
#include <QDrag>
|
||||||
|
#include <QMimeData>
|
||||||
|
#include <QPixmap>
|
||||||
#include "dockconstants.h"
|
#include "dockconstants.h"
|
||||||
#include "appicon.h"
|
#include "appicon.h"
|
||||||
#include "appbackground.h"
|
#include "appbackground.h"
|
||||||
#include "QDebug"
|
#include "QDebug"
|
||||||
|
|
||||||
|
class QDragEnterEvent;
|
||||||
|
class QDropEvent;
|
||||||
class AppItem : public QFrame
|
class AppItem : public QFrame
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@ -37,17 +42,23 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
void mousePressEvent(QMouseEvent *);
|
void mousePressEvent(QMouseEvent *);
|
||||||
void mouseReleaseEvent(QMouseEvent *);
|
void mouseReleaseEvent(QMouseEvent *);
|
||||||
void mouseMoveEvent(QMouseEvent *);
|
|
||||||
void mouseDoubleClickEvent(QMouseEvent *);
|
void mouseDoubleClickEvent(QMouseEvent *);
|
||||||
|
void mouseMoveEvent(QMouseEvent *);
|
||||||
void enterEvent(QEvent * event);
|
void enterEvent(QEvent * event);
|
||||||
void leaveEvent(QEvent * event);
|
void leaveEvent(QEvent * event);
|
||||||
|
void dragEnterEvent(QDragEnterEvent * event);
|
||||||
|
void dragLeaveEvent(QDragLeaveEvent * event);
|
||||||
|
void dropEvent(QDropEvent * event);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
void dragStart(AppItem *item);
|
||||||
|
void dragEntered(QDragEnterEvent * event,AppItem *item);
|
||||||
|
void dragExited(QDragLeaveEvent * event,AppItem *item);
|
||||||
|
void drop(QDropEvent * event,AppItem *item);
|
||||||
void mouseEntered(AppItem *item);
|
void mouseEntered(AppItem *item);
|
||||||
void mouseExited(AppItem *item);
|
void mouseExited(AppItem *item);
|
||||||
void mousePress(int x, int y, AppItem *item);
|
void mousePress(int x, int y, AppItem *item);
|
||||||
void mouseRelease(int x, int y, AppItem *item);
|
void mouseRelease(int x, int y, AppItem *item);
|
||||||
void mouseMove(int x, int y, AppItem *item);
|
|
||||||
void mouseDoubleClick( AppItem *item);
|
void mouseDoubleClick( AppItem *item);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
DockLayout::DockLayout(QWidget *parent) :
|
DockLayout::DockLayout(QWidget *parent) :
|
||||||
QWidget(parent)
|
QWidget(parent)
|
||||||
{
|
{
|
||||||
|
this->setAcceptDrops(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DockLayout::setParent(QWidget *parent)
|
void DockLayout::setParent(QWidget *parent)
|
||||||
@ -22,10 +23,10 @@ void DockLayout::insertItem(AppItem *item, int index)
|
|||||||
index = index > appCount ? appCount : (index < 0 ? 0 : index);
|
index = index > appCount ? appCount : (index < 0 ? 0 : index);
|
||||||
|
|
||||||
appList.insert(index,item);
|
appList.insert(index,item);
|
||||||
connect(item, SIGNAL(mouseMove(int,int,AppItem*)),this,SLOT(slotItemDrag(int,int,AppItem*)));
|
|
||||||
connect(item,SIGNAL(mouseRelease(int,int,AppItem*)),this,SLOT(slotItemRelease(int,int,AppItem*)));
|
connect(item,SIGNAL(mouseRelease(int,int,AppItem*)),this,SLOT(slotItemRelease(int,int,AppItem*)));
|
||||||
connect(item,SIGNAL(mouseEntered(AppItem*)),this,SLOT(slotItemEntered(AppItem*)));
|
connect(item, SIGNAL(dragStart(AppItem*)),this,SLOT(slotItemDrag(AppItem*)));
|
||||||
connect(item, SIGNAL(mouseExited(AppItem*)),this,SLOT(slotItemExited(AppItem*)));
|
connect(item,SIGNAL(dragEntered(QDragEnterEvent*,AppItem*)),this,SLOT(slotItemEntered(QDragEnterEvent*,AppItem*)));
|
||||||
|
connect(item,SIGNAL(dragExited(QDragLeaveEvent*,AppItem*)),this,SLOT(slotItemExited(QDragLeaveEvent*,AppItem*)));
|
||||||
|
|
||||||
relayout();
|
relayout();
|
||||||
}
|
}
|
||||||
@ -110,10 +111,11 @@ void DockLayout::dragoutFromLayout(int index)
|
|||||||
{
|
{
|
||||||
AppItem * tmpItem = appList.takeAt(index);
|
AppItem * tmpItem = appList.takeAt(index);
|
||||||
tmpItem->setVisible(false);
|
tmpItem->setVisible(false);
|
||||||
|
tmpAppMap.insert(tmpItem,index);
|
||||||
|
|
||||||
if (index == appList.count())//note,target hast been remove before
|
if (index == appList.count())//note,target hast been remove before
|
||||||
{
|
{
|
||||||
qWarning() << "out of range...";
|
// qWarning() << "end of list...";
|
||||||
return;//at the end of list
|
return;//at the end of list
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,7 +184,24 @@ int DockLayout::indexOf(int x, int y)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DockLayout::slotItemDrag(int x, int y, AppItem *item)
|
void DockLayout::dragEnterEvent(QDragEnterEvent *event)
|
||||||
|
{
|
||||||
|
event->setDropAction(Qt::MoveAction);
|
||||||
|
event->accept();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DockLayout::dropEvent(QDropEvent *event)
|
||||||
|
{
|
||||||
|
AppItem * tmpItem = tmpAppMap.firstKey();
|
||||||
|
tmpAppMap.remove(tmpItem);
|
||||||
|
tmpItem->setVisible(true);
|
||||||
|
if (indexOf(tmpItem) == -1)
|
||||||
|
{
|
||||||
|
insertItem(tmpItem,lastHoverIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DockLayout::slotItemDrag(AppItem *item)
|
||||||
{
|
{
|
||||||
// qWarning() << "Item draging..."<<x<<y<<item;
|
// qWarning() << "Item draging..."<<x<<y<<item;
|
||||||
int tmpIndex = indexOf(item);
|
int tmpIndex = indexOf(item);
|
||||||
@ -199,17 +218,16 @@ void DockLayout::slotItemRelease(int x, int y, AppItem *item)
|
|||||||
item->setVisible(true);
|
item->setVisible(true);
|
||||||
if (indexOf(item) == -1)
|
if (indexOf(item) == -1)
|
||||||
{
|
{
|
||||||
qWarning() << "---------" << lastHoverIndex;
|
|
||||||
insertItem(item,lastHoverIndex);
|
insertItem(item,lastHoverIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DockLayout::slotItemEntered(AppItem *item)
|
void DockLayout::slotItemEntered(QDragEnterEvent * event,AppItem *item)
|
||||||
{
|
{
|
||||||
this->lastHoverIndex = indexOf(item);
|
this->lastHoverIndex = indexOf(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DockLayout::slotItemExited(AppItem *item)
|
void DockLayout::slotItemExited(QDragLeaveEvent *event,AppItem *item)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -42,15 +42,15 @@ public:
|
|||||||
int indexOf(AppItem * item);
|
int indexOf(AppItem * item);
|
||||||
int indexOf(int x,int y);
|
int indexOf(int x,int y);
|
||||||
|
|
||||||
signals:
|
protected:
|
||||||
|
void dragEnterEvent(QDragEnterEvent *event);
|
||||||
public slots:
|
void dropEvent(QDropEvent *event);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void slotItemDrag(int x,int y,AppItem *item);
|
void slotItemDrag(AppItem *item);
|
||||||
void slotItemRelease(int x, int y, AppItem *item);
|
void slotItemRelease(int x, int y, AppItem *item);
|
||||||
void slotItemEntered(AppItem *item);
|
void slotItemEntered(QDragEnterEvent * event,AppItem *item);
|
||||||
void slotItemExited(AppItem *item);
|
void slotItemExited(QDragLeaveEvent *event,AppItem *item);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void sortLeftToRight();
|
void sortLeftToRight();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user