-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.cpp
248 lines (155 loc) · 5.27 KB
/
global.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "global.h"
qint64 random(qint64 a, qint64 b) {
static bool isFirst = true;
if (isFirst) {
QRandomGenerator::securelySeeded();
isFirst = false;
}
if (a == b)
return a;
if (a > b) {
qSwap(a, b);
}
return QRandomGenerator::global()->bounded(a, b);
}
// Проверка находится ли точка в области, ограниченной двумя другими точками
bool pointInAreaBetweenPoints(const MyPoint &point,
PairPoint twoPoints,
bool pointCanBeOnBorder) {
MyPoint pointA = twoPoints.first;
MyPoint pointB = twoPoints.second;
if (pointCanBeOnBorder) {
if ((point.x >= std::min(pointA.x, pointB.x) && point.x <= std::max(pointA.x, pointB.x)) &&
(point.y >= std::min(pointA.y, pointB.y) && point.y <= std::max(pointA.y, pointB.y)))
return true;
} else {
if ((point.x > std::min(pointA.x, pointB.x) && point.x < std::max(pointA.x, pointB.x)) &&
(point.y > std::min(pointA.y, pointB.y) && point.y < std::max(pointA.y, pointB.y)))
return true;
}
return false;
}
WaitWindow::WaitWindow(QObject *parent) : QObject(parent) {
}
// Создать окно ожидания
void WaitWindow::open(QString title, QString text, QWidget *parent) {
std::unique_lock<std::recursive_mutex> un(mainMtx);
if (isOpen == 1)
return;
isOpen = 1;
mesBox = new QMessageBox(parent);
connect(mesBox, &QDialog::rejected, this, &WaitWindow::closedByUserSlot);
mesBox->setWindowTitle(title);
mesBox->addButton("Отмена", QMessageBox::RejectRole);
mesBox->setWindowModality(Qt::ApplicationModal);
setText(text);
mesBox->setIcon(QMessageBox::Information);
mesBox->show();
}
// Задать текст окна
void WaitWindow::setText(QString text) {
std::unique_lock<std::recursive_mutex> un(mainMtx);
if (isOpen == 0)
return;
mesBox->setText(text);
}
// Удалить окно ожидания
void WaitWindow::close() {
std::unique_lock<std::recursive_mutex> un(mainMtx);
if (isOpen == 0)
return;
isOpen = 0;
disconnect(mesBox, &QDialog::rejected, this, &WaitWindow::closedByUserSlot);
mesBox->deleteLater();
}
// Закрытие окна ожидания пользователем
void WaitWindow::closedByUserSlot() {
close();
emit closedByUser();
}
// Замер времени
long MyTime::operator ()(uint8_t measurementNumber1or2) {
std::unique_lock<std::recursive_mutex> un(timerMtx);
switch (measurementNumber1or2) {
case 1: {
begin = chrono::steady_clock::now(); // Первый замер времени
pause = 0;
timeBeforePause = 0;
deltaTime = 0;
return 1;
}
case 2: {
if (pause == 1)
return timeBeforePause + deltaTime;
auto end = chrono::steady_clock::now(); // Второй замер времени
auto elapsed_mc = chrono::duration_cast
<chrono::microseconds>(end - begin); // Разница в микросекундах;
auto delay = elapsed_mc.count();
return delay + deltaTime;
}
default:
return 0;
}
}
MyTime::operator QString const() {
long delay = operator()(2);
QString str;
if (delay >= 1000000)
str = QString::number(delay / 1000000) + " секунд";
else if (delay >= 1000 && delay < 1000000)
str = QString::number(delay / 1000) + " миллисекунд";
else if (delay < 1000)
str = QString::number(delay) + " микросекунд";
return str;
}
// Пауза таймера
void MyTime::setPauseTimer(bool pause) {
std::unique_lock<std::recursive_mutex> un(timerMtx);
if (this->pause == pause)
return;
this->pause = pause;
auto end = chrono::steady_clock::now();
auto elapsed_mc = chrono::duration_cast
<chrono::microseconds>(end - begin);
long absoluteTime = elapsed_mc.count();
if (pause == 1) {
timeBeforePause = absoluteTime;
} else {
deltaTime -= (absoluteTime - timeBeforePause);
}
}
// Установить нужное значение таймера
void MyTime::setTimerValue(long value) {
std::unique_lock<std::recursive_mutex> un(timerMtx);
if (pause == 0) {
long delay = operator()(2);
deltaTime += value - delay;
} else {
timeBeforePause = value - deltaTime;
}
}
// Сон
void MyTime::sleep(long delayInMicroseconds, bool processEvents) {
if (delayInMicroseconds <= 0)
return;
if (processEvents == 1) {
MyTime time(1);
while (time(2) < delayInMicroseconds)
qApp->processEvents();
} else
std::this_thread::sleep_for(std::chrono::microseconds(delayInMicroseconds));
}
namespace std {
MyPoint floor(MyPoint point) {
return MyPoint(std::floor(point.x), std::floor(point.y));
}
MyPoint ceil(MyPoint point) {
return MyPoint(std::ceil(point.x), std::ceil(point.y));
}
MyPoint round(MyPoint point) {
return MyPoint(std::round(point.x), std::round(point.y));
}
MyPoint abs(MyPoint point) {
return MyPoint(std::abs(point.x), std::abs(point.y));
}
}