-
Notifications
You must be signed in to change notification settings - Fork 1
/
movable_resizable_widget.cpp
127 lines (109 loc) · 2.78 KB
/
movable_resizable_widget.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
#include "stdafx.h"
#include "movable_resizable_widget.h"
namespace psv
{
movable_resizable_widget::movable_resizable_widget(QWidget* parent) :
QWidget(parent),
captured_(false),
direction_(rd_no_resize)
{
setMouseTracking(true);
}
movable_resizable_widget::~movable_resizable_widget()
{
}
void movable_resizable_widget::enterEvent(QEvent* e)
{
captured_ = false;
unsetCursor();
}
void movable_resizable_widget::leaveEvent(QEvent* e)
{
captured_ = false;
unsetCursor();
}
void movable_resizable_widget::mousePressEvent(QMouseEvent* e)
{
QWidget::mousePressEvent(e);
if (e->button() != Qt::LeftButton || childAt(e->pos()))
// If there is any widget here, ignore this click
return;
captured_ = true;
direction_ = get_resizing_direction(e->pos());
set_resizing_cursor(direction_);
mouse_position_ = e->globalPos();
}
void movable_resizable_widget::mouseMoveEvent(QMouseEvent* e)
{
QWidget::mouseMoveEvent(e);
if (captured_ && (e->buttons() & Qt::LeftButton))
{
make_move_or_resize(direction_, (e->globalPos() - mouse_position_));
mouse_position_ = e->globalPos();
}
else
{
captured_ = false;
set_resizing_cursor(get_resizing_direction(e->pos()));
}
}
void movable_resizable_widget::mouseReleaseEvent(QMouseEvent* e)
{
QWidget::mouseReleaseEvent(e);
if (e->button() != Qt::LeftButton)
return;
captured_ = false;
set_resizing_cursor(get_resizing_direction(e->pos()));
}
movable_resizable_widget::resizing_direction movable_resizable_widget::get_resizing_direction(const QPoint& point)
{
auto x_end = width();
auto x_start = x_end - resize_area_size;
auto y_end = height();
auto y_start = y_end - resize_area_size;
if (point.x() >= x_start && point.x() <= x_end && point.y() >= y_start && point.y() <= y_end)
return rd_right_bottom;
else if (point.x() >= x_start && point.x() <= x_end && point.y() < y_start)
return rd_right;
else if (point.y() >= y_start && point.y() <= y_end && point.x() < x_start)
return rd_bottom;
else
return rd_no_resize;
}
void movable_resizable_widget::set_resizing_cursor(resizing_direction direction)
{
switch (direction)
{
case rd_right_bottom:
setCursor(Qt::SizeFDiagCursor);
break;
case rd_right:
setCursor(Qt::SizeHorCursor);
break;
case rd_bottom:
setCursor(Qt::SizeVerCursor);
break;
default:
unsetCursor();
break;
}
}
void movable_resizable_widget::make_move_or_resize(resizing_direction direction, const QPoint& diff)
{
switch (direction)
{
case rd_right_bottom:
window()->resize(window()->width() + diff.x(), window()->height() + diff.y());
break;
case rd_right:
window()->resize(window()->width() + diff.x(), window()->height());
break;
case rd_bottom:
window()->resize(window()->width(), window()->height() + diff.y());
break;
default:
window()->move(window()->pos() + diff);
break;
}
}
}