-
Notifications
You must be signed in to change notification settings - Fork 1
/
paintwigdet.cpp
118 lines (108 loc) · 2.89 KB
/
paintwigdet.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
#include <paintwidget.h>
PaintWidget::PaintWidget(int obj_num, double updateTimeMs)
{
updateTime = updateTimeMs;
ballIt = list.end();
int i = 0;
while (i < obj_num)
{
QTime timer;
timer.start ();
for(;timer.elapsed() < updateTime*5;) {}
AddRandomBall();
i++;
}
MyThread.SetUpdateTime(updateTime);
MyThread.SetList(&list);
MyThread.SetMutex(&mutex);
MyThread.start();
connect(&MyThread, SIGNAL(finished()), this, SLOT(update()));
connect(&timerUpdate, SIGNAL(timeout()), &MyThread, SLOT(TotalCounter()));
timerUpdate.start(updateTime);
}
void PaintWidget::paintEvent(QPaintEvent * )
{
mutex.lock();
QPainter painter(this);
painter.setBrush(Qt::green);
std::list<Circle>::iterator it = list.begin();
while (it != list.end())
{
painter.drawEllipse((*it).GetX(), (*it).GetY(), (*it).GetRadius(), (*it).GetRadius());
++it;
}
mutex.unlock();
}
void PaintWidget::AddRandomBall()
{
Circle Ball;
list.push_back(Ball);
ballIt = list.end();
}
void PaintWidget::mousePressEvent(QMouseEvent * event)
{
mutex.lock();
if (event->button() == Qt::RightButton)
{
left_pressed = false;
std::list<Circle>::iterator it = list.begin();
while (it != list.end())
{
if ((abs((*it).GetX() - event->pos().x()) < (*it).GetRadius())
&& ((abs((*it).GetY() - event->pos().y()) < (*it).GetRadius())))
{
list.erase(it);
ballIt = list.end();
mutex.unlock();
return;
}
it++;
}
QPoint position = event->pos();
AddBallXY(position.x(), position.y());
}
else if (event->button() == Qt::LeftButton)
{
left_pressed = true;
std::list<Circle>::iterator it = list.begin();
while (it != list.end())
{
if ((abs((*it).GetX() - event->pos().x()) < (*it).GetRadius())
&& ((abs((*it).GetY() - event->pos().y()) < (*it).GetRadius())))
{
(*it).LockByMouse(true);
ballIt = it;
break;
}
ballIt = list.end();
it++;
}
}
mutex.unlock();
}
void PaintWidget::mouseMoveEvent(QMouseEvent * event)
{
mutex.lock();
if (ballIt != list.end() && left_pressed) (*ballIt).SetXY(event->x(), event->y());
mutex.unlock();
}
void PaintWidget::mouseReleaseEvent(QMouseEvent * event)
{
mutex.lock();
if (event->button() == Qt::LeftButton)
{
left_pressed = false;
if (ballIt != list.end())
{
(*ballIt).LockByMouse(false);
ballIt = list.end();
}
}
mutex.unlock();
}
void PaintWidget::AddBallXY(int x, int y)
{
Circle Ball(x, y);
list.push_back(Ball);
ballIt = list.end();
}