forked from Demonist/Qt_widgets_dragAndDrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcverticalcontainer.cpp
132 lines (112 loc) · 3.29 KB
/
cverticalcontainer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "cverticalcontainer.h"
CVerticalContainer::CVerticalContainer(QWidget *parent) :
QWidget(parent),
_layout(new QVBoxLayout)
{
setLayout(_layout);
setAcceptDrops(true);
_dropLine.setFrameShape(QFrame::HLine);
}
void CVerticalContainer::appedWidget(QWidget *widget)
{
CDragableContainer *container = new CDragableContainer(widget);
connect(container, SIGNAL(movingUp()), this, SLOT(movingUp()));
connect(container, SIGNAL(movingDown()), this, SLOT(movingDown()));
_widgetsMap.insert(widget, container);
_layout->addWidget(container);
}
void CVerticalContainer::removeWidget(QWidget *widget)
{
if(_widgetsMap.contains(widget) == false)
{
qWarning() << "CVerticalContainer: can not find widget " << widget;
return;
}
CDragableContainer *container = _widgetsMap.take(widget);
_layout->removeWidget(container);
delete container;
}
void CVerticalContainer::dragEnterEvent(QDragEnterEvent *event)
{
QWidget::dragEnterEvent(event);
if(event->mimeData()->hasFormat(gDragableContainerMimeType))
event->accept();
}
void CVerticalContainer::dragMoveEvent(QDragMoveEvent *event)
{
QWidget::dragMoveEvent(event);
const int currentLineIndex = _layout->indexOf(&_dropLine);
QWidget *child = childAt(event->pos());
while(child && _layout->indexOf(child) == -1)
child = child->parentWidget();
if(child)
{
int newLineIndex = _layout->indexOf(child);
if(event->pos().y() > child->pos().y() + (child->height() / 2))
newLineIndex++;
if(currentLineIndex != -1
&& currentLineIndex < newLineIndex)
newLineIndex--;
if(newLineIndex != currentLineIndex)
{
if(currentLineIndex != -1)
_layout->removeWidget(&_dropLine);
_layout->insertWidget(newLineIndex, &_dropLine);
_dropLine.show();
}
}
event->accept();
}
void CVerticalContainer::dragLeaveEvent(QDragLeaveEvent *event)
{
QWidget::dragLeaveEvent(event);
if(QRect(QPoint(), size()).contains(mapFromGlobal(QCursor::pos()), false) == false) //fix from wrong leave event.
{
_layout->removeWidget(&_dropLine);
_dropLine.hide();
event->accept();
}
}
void CVerticalContainer::dropEvent(QDropEvent *event)
{
QWidget::dropEvent(event);
const QByteArray data = event->mimeData()->data(gDragableContainerMimeType);
CDragableContainer *dragingWidget = (CDragableContainer*)data.toULongLong();
const int dragingIndex = _layout->indexOf(dragingWidget);
int newIndex = _layout->indexOf(&_dropLine);
if(dragingIndex != -1 && dragingIndex != newIndex)
{
if(dragingIndex < newIndex)
newIndex--;
_layout->removeWidget(dragingWidget);
_layout->insertWidget(newIndex, dragingWidget);
}
_layout->removeWidget(&_dropLine);
_dropLine.hide();
event->acceptProposedAction();
event->accept();
}
void CVerticalContainer::movingUp()
{
CDragableContainer *container = static_cast<CDragableContainer*>( QObject::sender() );
const int index = _layout->indexOf(container);
if(index < 1)
QApplication::beep();
else
{
_layout->removeWidget(container);
_layout->insertWidget(index - 1, container);
}
}
void CVerticalContainer::movingDown()
{
CDragableContainer *container = static_cast<CDragableContainer*>( QObject::sender() );
const int index = _layout->indexOf(container);
if(index < 0 || index >= _layout->count() - 1)
QApplication::beep();
else
{
_layout->removeWidget(container);
_layout->insertWidget(index + 1, container);
}
}