-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprog.hpp
More file actions
412 lines (372 loc) · 13.5 KB
/
Copy pathprog.hpp
File metadata and controls
412 lines (372 loc) · 13.5 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#ifndef TIPL_PROG_HPP
#define TIPL_PROG_HPP
#include <memory>
#include <iostream>
#include <chrono>
#include <thread>
#include <string>
#include <sstream>
#include <atomic>
#if defined(TIPL_USE_QT) && !defined(__CUDACC__)
#include <QDialog>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QPainter>
#include <QFontMetrics>
#include <QMouseEvent>
#include <QApplication>
#endif
#include "mt.hpp"
#include "po.hpp"
namespace tipl{
struct prog_status{
std::chrono::high_resolution_clock::time_point next_update_time,start_time;
std::string status,at;
unsigned int now = 0,total = 0;
};
inline std::vector<prog_status> status_list;
inline std::atomic_int status_count = 0;
inline bool prog_aborted = false,show_prog = false;
inline bool check_reentrant = false;
inline std::mutex print_mutex,msg_mutex;
inline std::string last_msg;
#if defined(TIPL_USE_QT) && !defined(__CUDACC__)
struct progress_dialog : public QDialog{
struct ring_widget : public QWidget{
QPushButton cancel{"Cancel",this};
int active = 0,pulse = 0;
ring_widget()
{
setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
setMinimumSize(220,220);
cancel.setFixedSize(140,140);
cancel.setStyleSheet(
"QPushButton{border-radius:70px;background:#3b3b3b;color:white;font-weight:bold;font-size:16px;}"
"QPushButton:hover{background:#555;font-size:18px;}QPushButton:pressed{background:#222;}");
connect(&cancel,&QPushButton::clicked,[]{prog_aborted = true;});
}
void resizeEvent(QResizeEvent*) override
{
cancel.move((width()-cancel.width())/2,(height()-cancel.height())/2);
}
void paintEvent(QPaintEvent*) override
{
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
auto plot_circle = [&](unsigned int now,unsigned int total,int k)
{
int r = 90 + k*24;
QRect rc(width()/2-r,height()/2-r,r*2,r*2);
int t = (pulse + k*25)%120;
p.setPen(QPen(QColor(230,232,235),13,Qt::SolidLine,Qt::RoundCap));
p.drawEllipse(rc);
p.setPen(QPen(QColor::fromHsv((205+k*28)%360,155,185 + (t < 60 ? t : 120-t)),13,Qt::SolidLine,Qt::RoundCap));
p.drawArc(rc,90*16,-int(360.0*16.0*now/total));
};
int k = 0;
for(auto& s : status_list)
if(s.now && s.total)
plot_circle(s.now,s.total,k++);
if(k == 0)
plot_circle(0,1,0);
}
};
QVBoxLayout box{this};
ring_widget rings;
QLabel text;
QPoint drag_pos;
bool dragging = false;
// parenting to the active modal dialog (e.g. AtlasDialog running its own exec() loop)
// lets this dialog join that modal session instead of being blocked/hidden behind it
progress_dialog(QWidget* parent = QApplication::activeModalWidget()):QDialog(parent)
{
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
box.setContentsMargins(8,8,8,8);
box.setSpacing(2);
text.setAlignment(Qt::AlignCenter);
text.setFixedWidth(310);
box.addWidget(&rings,1);
box.addWidget(&text,0,Qt::AlignCenter);
installEventFilter(this);
rings.installEventFilter(this);
text.installEventFilter(this);
resize(320,360);
}
void paintEvent(QPaintEvent*) override
{
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
p.setPen(Qt::NoPen);
p.setBrush(QColor(255,255,255,210));
p.drawRoundedRect(rect(),24,24);
}
bool eventFilter(QObject*,QEvent* event) override
{
if(event->type() == QEvent::MouseButtonPress)
{
auto* e = static_cast<QMouseEvent*>(event);
if(e->button() == Qt::LeftButton)
{
dragging = true;
drag_pos = e->globalPosition().toPoint() - frameGeometry().topLeft();
setCursor(Qt::SizeAllCursor);
return true;
}
}
if(event->type() == QEvent::MouseMove && dragging)
{
move(static_cast<QMouseEvent*>(event)->globalPosition().toPoint() - drag_pos);
return true;
}
if(event->type() == QEvent::MouseButtonRelease)
{
dragging = false;
unsetCursor();
return true;
}
return false;
}
// only steal focus while DSI Studio itself is the active application;
// if the user is attending to another app, stay out of the way
void activate_if_needed()
{
if(QApplication::applicationState() == Qt::ApplicationActive && !isActiveWindow())
{
raise();
activateWindow();
}
}
void refresh()
{
QStringList lines;
QFontMetrics fm(text.font());
rings.active = 0;
rings.pulse = (rings.pulse + 8)%120;
for(int i = 1;i < int(status_list.size());++i)
if(!status_list[i].status.empty())
{
auto s = QString::fromStdString(tipl::split(status_list[i].status,'\n').front());
if(!status_list[i].at.empty())
s += " " + QString::fromStdString(status_list[i].at);
lines << fm.elidedText(s,Qt::ElideRight,text.width()-8);
rings.active += status_list[i].total != 0;
}
text.setText(lines.empty() ? "working..." : lines.join('\n'));
text.setFixedHeight(fm.lineSpacing()*std::max<int>(1,lines.size())+4);
rings.update();
}
};
inline std::shared_ptr<progress_dialog> progressDialog;
#endif
class progress{
private:
void begin_prog(const std::string& status,bool show_now = false)
{
if(!tipl::is_main_thread())
return;
auto t = std::chrono::high_resolution_clock::now();
prog_aborted = false;
status_list.push_back({t,t,status,{}});
++status_count;
last_msg.clear();
#if defined(TIPL_USE_QT) && !defined(__CUDACC__)
if(show_prog && show_now && !progressDialog)
{
progressDialog.reset(new progress_dialog);
progressDialog->show();
progressDialog->activate_if_needed();
}
#endif
}
static bool check_prog(unsigned int now,unsigned int total)
{
if(prog_aborted)
return false;
if(!show_prog || !tipl::is_main_thread() || !status_count || check_reentrant)
return now < total;
auto& cur_status = status_list.back();
auto now_time = std::chrono::high_resolution_clock::now();
if(now == 0 || now_time < cur_status.next_update_time)
return now < total;
cur_status.next_update_time = now_time+std::chrono::milliseconds(200);
cur_status.at = "(" + std::to_string(now) + "/" + std::to_string(total) + ")";
cur_status.total = total;
cur_status.now = now;
if(now < total)
{
if(auto e = std::chrono::duration_cast<std::chrono::milliseconds>(now_time-cur_status.start_time).count()*(total-now)/now/60000; e)
cur_status.at += " "+std::to_string(e)+" min";
}
#if defined(TIPL_USE_QT) && !defined(__CUDACC__)
if(!progressDialog)
{
progressDialog.reset(new progress_dialog);
progressDialog->show();
progressDialog->activate_if_needed();
}
progressDialog->refresh();
check_reentrant = true;
QApplication::processEvents();
check_reentrant = false;
#endif
if(prog_aborted)
return progress::print("operation aborted",false,false,1),false;
return now < total;
}
static std::string get_head(bool head_node,bool tail_node)
{
int count = status_count.load();
std::string head;
for(int i = 1;i < count;++i)
head += "│ ";
if(!tipl::is_main_thread())
return head + "│ ";
if(!count)
return head_node ? "┌──" : tail_node ? "└──" : "";
head += head_node ? "├──┬──" : tail_node ? "│ " : "├──";
return tail_node ? head + "└──" : head;
}
static std::string get_color_line(std::string line,bool head_node,int error_code)
{
auto color = [](const char* c,const std::string& s){return std::string(c) + s + "\033[0m";};
if(error_code == 1) return color("\033[1;31m","❌" + line);
if(error_code == 2) return color("\033[1;31m","❗" + line);
if(tipl::begins_with(line,"sav")) return color("\033[1;35m","💾" + line);
if(tipl::begins_with(line,"open")) return color("\033[1;35m","📂" + line);
if(head_node) return color("\033[1;34m","📟" + line);
if(auto p = line.find('=');p != std::string::npos)
return color("\033[0;32m",line.substr(0,p)) + line.substr(p);
if(auto p = line.find(": ");p != std::string::npos)
return color("\033[0;33m",line.substr(0,p)) + line.substr(p);
return line;
}
public:
static void print(const std::string& status,bool head_node, bool tail_node,int error_code = 0)
{
std::scoped_lock<std::mutex> lock(print_mutex);
std::istringstream in(status);
std::string line;
while(std::getline(in,line))
{
if(line.empty())
continue;
auto new_line = get_color_line(line,head_node,error_code);
if(!tipl::is_main_thread())
{
std::ostringstream out;
out << "[thread " << std::this_thread::get_id() << "]" << new_line;
new_line = out.str();
}
std::cout << get_head(head_node,tail_node) + new_line << std::endl;
head_node = false;
}
}
public:
bool temporary = false;
progress(void):temporary(true){}
void operator=(progress&& rhs)
{
if(temporary && !rhs.temporary)
temporary = false,rhs.temporary = true;
}
progress(const std::string& status,bool show_now = false)
{
print(status,true,false);
begin_prog(status,show_now);
}
progress(const std::string& a,const std::string& b,bool show_now = false):progress(a + b,show_now) {}
static bool is_running(void) {return status_count > 1;}
static bool aborted(void) { return prog_aborted;}
template<typename value_type1,typename value_type2>
bool operator()(value_type1 now,value_type2 total) const
{
if(temporary)
return now < total;
return check_prog(uint32_t(now),uint32_t(total));
}
template<typename fun_type>
bool run(size_t total,fun_type&& fun)
{
unsigned int dummy = 0;
if (!show_prog || !tipl::is_main_thread() || status_list.empty() || check_reentrant)
return fun(dummy);
status_list.back().total = total;
status_list.back().now = 1;
std::atomic<bool> ended{false};
bool result = true;
std::thread worker_thread([&]() {
result = fun(status_list.back().now);
ended = true;
});
#if defined(TIPL_USE_QT) && !defined(__CUDACC__)
while (!ended && !prog_aborted)
{
if(status_count && std::chrono::high_resolution_clock::now() > status_list.back().next_update_time)
{
status_list.back().next_update_time = std::chrono::high_resolution_clock::now()+std::chrono::milliseconds(200);
if(progressDialog)
progressDialog->refresh();
check_reentrant = true;
QApplication::processEvents();
check_reentrant = false;
}
}
#endif
if (worker_thread.joinable())
worker_thread.join();
return result && !prog_aborted;
}
~progress(void)
{
if(!tipl::is_main_thread() || temporary || status_count == 0)
return;
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - status_list.back().start_time).count();
auto s=ms/1000; ms%=1000;
auto m=s/60; s%=60;
auto h=m/60; m%=60;
std::string t;
if(h) t += std::to_string(h) + "h";
if(m) t += std::to_string(m) + "m";
if(s) t += std::to_string(s) + "s";
t+=std::to_string(ms)+"ms";
status_list.pop_back();
--status_count;
print("⏱" + t,false,true);
{
std::scoped_lock<std::mutex> lock2(msg_mutex);
last_msg.clear();
}
#if defined(TIPL_USE_QT) && !defined(__CUDACC__)
if(!show_prog || !progressDialog)
return;
if(status_count <= 1)
progressDialog->close(),progressDialog.reset();
#endif
}
};
template<int code>
class output{
std::ostringstream s;
public:
~output()
{
auto out = s.str();
if(out.empty())
return;
if(out.back() == '\n')
out.pop_back();
progress::print(out,false,false,code);
std::scoped_lock<std::mutex> lock2(msg_mutex);
std::getline(std::istringstream(out),last_msg);
}
output& operator<<(std::ostream& (*v)(std::ostream&)){s << v; return *this;}
template<typename T> output& operator<<(const T& v){s << v; return *this;}
};
using out = output<0>;
using error = output<1>; //❌
using warning = output<2>; //❗
}
#endif//TIPL_PROG_HPP