dragproxy.cpp
7.22 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "dragproxy.h"
#include <QMouseEvent>
#include <QTimerEvent>
#include <QCursor>
DragProxy::DragProxy(QWidget *parent)
: QObject()
{
m_proxyWidget = parent;
m_top = m_right = m_bottom = m_left = 0;
m_proxyWidget->setMouseTracking(true);
m_proxyWidget->installEventFilter(this); // 代理窗体事件
m_mousePressed = false;
m_regionPressed = Unknown;
m_cursorTimerId = 0;
}
DragProxy::~DragProxy()
{
}
void DragProxy::SetBorderWidth(int top, int right, int bottom, int left)
{
m_top = top;
m_right = right;
m_bottom = bottom;
m_left = left;
MakeRegions();
}
void DragProxy::UpdateGeometry(int x, int y, int w, int h)
{
int minWidth = m_proxyWidget->minimumWidth();
int minHeight = m_proxyWidget->minimumHeight();
int maxWidth = m_proxyWidget->maximumWidth();
int maxHeight = m_proxyWidget->maximumHeight();
if (w < minWidth || w > maxWidth || h < minHeight || h > maxHeight)
{
return;
}
m_proxyWidget->setGeometry(x, y, w, h);
}
#include <QDebug>
bool DragProxy::eventFilter(QObject* obj, QEvent* event)
{
QEvent::Type eventType = event->type();
if (eventType == QEvent::MouseMove)
{
QMouseEvent* mouseEvent = (QMouseEvent*)event;
QPoint curPosLocal = mouseEvent->pos();
DragProxy::WidgetRegion regionType = HitTest(curPosLocal);
QPoint curPosGlobal = m_proxyWidget->mapToGlobal(curPosLocal);
if (!m_mousePressed) // 鼠标未按下
{
switch (regionType)
{
case Top:
case Bottom:
m_proxyWidget->setCursor(Qt::SizeVerCursor);
break;
case TopRight:
case LeftBottom:
m_proxyWidget->setCursor(Qt::SizeBDiagCursor);
break;
case Right:
case Left:
m_proxyWidget->setCursor(Qt::SizeHorCursor);
break;
case RightBottom:
case LeftTop:
m_proxyWidget->setCursor(Qt::SizeFDiagCursor);
break;
default:
m_proxyWidget->setCursor(Qt::ArrowCursor);
break;
}
StartCursorTimer();
}
else // 鼠标已按下
{
QRect geo = m_proxyWidget->geometry();
if (m_regionPressed == Inner)
{
m_proxyWidget->move(m_originGeo.topLeft() + curPosGlobal - m_originPosGlobal);
}
else if (m_regionPressed == Top)
{
int dY = curPosGlobal.y() - m_originPosGlobal.y();
UpdateGeometry(m_originGeo.x(), m_originGeo.y() + dY, m_originGeo.width(), m_originGeo.height() - dY);
}
else if (m_regionPressed == TopRight)
{
QPoint dXY = curPosGlobal - m_originPosGlobal;
UpdateGeometry(m_originGeo.x(), m_originGeo.y() + dXY.y(), m_originGeo.width() + dXY.x(), m_originGeo.height() - dXY.y());
}
else if (m_regionPressed == Right)
{
int dX = curPosGlobal.x() - m_originPosGlobal.x();
UpdateGeometry(m_originGeo.x(), m_originGeo.y(), m_originGeo.width() + dX, m_originGeo.height());
}
else if (m_regionPressed == RightBottom)
{
QPoint dXY = curPosGlobal - m_originPosGlobal;
UpdateGeometry(m_originGeo.x(), m_originGeo.y(), m_originGeo.width() + dXY.x(), m_originGeo.height() + dXY.y());
}
else if (m_regionPressed == Bottom)
{
int dY = curPosGlobal.y() - m_originPosGlobal.y();
UpdateGeometry(m_originGeo.x(), m_originGeo.y(), m_originGeo.width(), m_originGeo.height() + dY);
}
else if (m_regionPressed == LeftBottom)
{
QPoint dXY = curPosGlobal - m_originPosGlobal;
UpdateGeometry(m_originGeo.x() + dXY.x(), m_originGeo.y(), m_originGeo.width() - dXY.x(), m_originGeo.height() + dXY.y());
}
else if (m_regionPressed == Left)
{
int dX = curPosGlobal.x() - m_originPosGlobal.x();
UpdateGeometry(m_originGeo.x() + dX, m_originGeo.y(), m_originGeo.width() - dX, m_originGeo.height());
}
else if (m_regionPressed == LeftTop)
{
QPoint dXY = curPosGlobal - m_originPosGlobal;
UpdateGeometry(m_originGeo.x() + dXY.x(), m_originGeo.y() + dXY.y(), m_originGeo.width() - dXY.x(), m_originGeo.height() - dXY.y());
}
}
}
else if (eventType == QEvent::MouseButtonPress)
{
QMouseEvent* mouseEvent = (QMouseEvent*)event;
if (mouseEvent->button() == Qt::LeftButton)
{
m_mousePressed = true;
QPoint curPos = mouseEvent->pos();
m_regionPressed = HitTest(curPos);
m_originPosGlobal = m_proxyWidget->mapToGlobal(curPos);
m_originGeo = m_proxyWidget->geometry();
StopCursorTimer();
}
}
else if (eventType == QEvent::MouseButtonRelease)
{
m_mousePressed = false;
m_regionPressed = Unknown;
m_proxyWidget->setCursor(Qt::ArrowCursor);
}
else if (eventType == QEvent::Resize)
{
MakeRegions();
}
else if (eventType == QEvent::Leave)
{
m_proxyWidget->setCursor(Qt::ArrowCursor);
StopCursorTimer();
}
else if (eventType == QEvent::Timer)
{
QTimerEvent* timerEvent = (QTimerEvent*)event;
if (timerEvent->timerId() == m_cursorTimerId)
{
if (m_regions[Inner].contains(m_proxyWidget->mapFromGlobal(QCursor::pos())))
{
m_proxyWidget->setCursor(Qt::ArrowCursor);
StopCursorTimer();
}
}
}
return QObject::eventFilter(obj, event);
}
void DragProxy::StartCursorTimer()
{
StopCursorTimer();
m_cursorTimerId = m_proxyWidget->startTimer(50);
}
void DragProxy::StopCursorTimer()
{
if (m_cursorTimerId != 0)
{
m_proxyWidget->killTimer(m_cursorTimerId);
m_cursorTimerId = 0;
}
}
void DragProxy::MakeRegions()
{
int width = m_proxyWidget->width();
int height = m_proxyWidget->height();
m_regions[Top] = QRect(m_left, 0, width - m_left - m_right, m_top);
m_regions[TopRight] = QRect(width - m_right, 0, m_right, m_top);
m_regions[Right] = QRect(width - m_right, m_top, m_right, height - m_top - m_bottom);
m_regions[RightBottom] = QRect(width - m_right, height - m_bottom, m_right, m_bottom);
m_regions[Bottom] = QRect(m_left, height - m_bottom, width - m_left - m_right, m_bottom);
m_regions[LeftBottom] = QRect(0, height - m_bottom, m_left, m_bottom);
m_regions[Left] = QRect(0, m_top, m_left, height - m_top - m_bottom);
m_regions[LeftTop] = QRect(0, 0, m_left, m_top);
m_regions[Inner] = QRect(m_left, m_top, width - m_left - m_right, height - m_top - m_bottom);
}
DragProxy::WidgetRegion DragProxy::HitTest(const QPoint& pos)
{
for (int i = 0; i < 9; i++)
{
const QRect rect = m_regions[i];
if (rect.contains(pos))
{
return DragProxy::WidgetRegion(i);
}
}
return Unknown;
}