-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsimcontrolwidget.cpp
More file actions
367 lines (322 loc) · 12.8 KB
/
Copy pathsimcontrolwidget.cpp
File metadata and controls
367 lines (322 loc) · 12.8 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
#include "simcontrolwidget.h"
#include "mcdriverobj.h"
#include "mainui.h"
#include "optionsmodel.h"
#include "simulationoptionsview.h"
#include <climits>
#include <QTimer>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QGridLayout>
#include <QFrame>
#include <QLineEdit>
#include <QToolButton>
#include <QProgressBar>
#include <QDoubleSpinBox>
#include <QSpinBox>
#include <QLabel>
#include <QStyle>
#include <QMessageBox>
SimControlWidget::SimControlWidget(MainUI *ui, QWidget *parent)
: QWidget{ parent }, mainui_(ui), driver_(ui->driverObj())
{
/* Create Controls */
QSize isz(46, 46);
QSize bsz(70, 70);
btStart = new QToolButton;
QIcon start_icon;
start_icon.addPixmap(QPixmap(":/assets/ionicons/play-circle-outline.svg"), QIcon::Normal,
QIcon::Off);
start_icon.addPixmap(QPixmap(":/assets/ionicons/pause-circle-outline.svg"), QIcon::Normal,
QIcon::On);
btStart->setIcon(start_icon);
// btStart->setIcon(QIcon(":/assets/ionicons/play-circle-outline.svg"));
btStart->setIconSize(isz);
btStart->setCheckable(true);
btStart->setToolTip("Start the simulation");
btStart->setText("Start");
btStart->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
btStart->setFixedSize(bsz);
btReset = new QToolButton;
btReset->setIcon(QIcon(":/assets/ionicons/refresh-circle-outline.svg"));
btReset->setIconSize(isz);
btReset->setToolTip("Reset");
btReset->setEnabled(false);
btReset->setToolTip("Reset the simulation");
btReset->setText("Reset");
btReset->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
btReset->setFixedSize(bsz);
progressBar = new QProgressBar;
progressBar->setFormat("%p%");
progressBar->setMinimum(0);
progressBar->setMaximum(1000);
runIndicator = new QLabel(QString("⣾"));
// runIndicator->setMinimumSize(QSize(40, 40));
runIndicator->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
// runIndicator->setStyleSheet("font-size: 24px");
// These /Run/xxx items are NOT connected to mapper
// because they can be overriden every time we run the sim
OptionsModel *model = mainui_->optionsModel;
QModelIndex driverOptionsIdx = model->index("Run");
// QSpinBox is limited to INT_MAX. Used QDoubleSpinBox with 0 decimals
// to support integer values up to 2^53 exactly
sbIons = new QDoubleSpinBox;
sbIons->setDecimals(0);
sbIons->setRange(1, static_cast<double>(McDriverObj::kMaxExactIons));
sbIons->setSingleStep(100'000);
sbIons->setValue(static_cast<double>(driver_->maxIons()));
simCtrls.push_back(sbIons);
QModelIndex idx = model->index("threads", 0, driverOptionsIdx);
OptionsItem *item = model->getItem(idx);
sbNThreads = (QSpinBox *)item->createEditor(this);
simCtrls.push_back(sbNThreads);
QModelIndex outputOptionsIdx = model->index("Output");
idx = model->index("storage_interval", 0, outputOptionsIdx);
item = model->getItem(idx);
sbUpdInterval = (QSpinBox *)item->createEditor(this);
simCtrls.push_back(sbUpdInterval);
idx = model->index("seed", 0, driverOptionsIdx);
item = model->getItem(idx);
sbSeed = (QSpinBox *)item->createEditor(this);
simCtrls.push_back(sbSeed);
// 0 means no time limit.
sbMaxTime = new QSpinBox;
sbMaxTime->setRange(0, INT_MAX);
sbMaxTime->setSingleStep(60);
sbMaxTime->setSuffix(" s");
sbMaxTime->setSpecialValueText("no limit");
sbMaxTime->setValue(driver_->maxCpuTime());
sbMaxTime->setToolTip("Stop after this many CPU seconds. With N threads, wall time will be ~1/N of this value. 0 means no time limit.");
/* Create Info items */
QStringList ctrlLabels{ "Ions to run", "Threads", "Upd period (ms)", "Seed" };
QStringList indicatorLabels{ "Ions finished", "Ions/s", "Elapsed", "ETC" };
QStringList typContent{ "10000000000000000", "100000", "00000:00:00", "00000:00:00" };
QColor clr = palette().color(QPalette::Window);
QString styleSheet = QString("background: %1").arg(clr.name());
for (int i = 0; i < indicatorLabels.size(); ++i) {
QLineEdit *edt = new QLineEdit;
edt->setReadOnly(true);
// edt->setFrame(false);
edt->setStyleSheet(styleSheet);
edt->setMinimumWidth(fontMetrics().horizontalAdvance(typContent.at(i)));
// edt->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
// edt->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
simIndicators.push_back(edt);
}
/* Layout Widgets */
QVBoxLayout *masterLayout = new QVBoxLayout;
masterLayout->setContentsMargins(0, 0, 0, 0);
setLayout(masterLayout);
{ // hline
QFrame *frm = new QFrame;
frm->setFrameStyle(QFrame::Raised);
frm->setFrameShape(QFrame::HLine);
masterLayout->addWidget(frm);
}
{
QHBoxLayout *hbox = new QHBoxLayout;
hbox->setContentsMargins(9, 0, 9, 9);
{ // buttons
QHBoxLayout *hbox2 = new QHBoxLayout;
hbox2->setContentsMargins(0, 0, 9, 0);
hbox2->addWidget(btStart);
hbox2->addWidget(btReset);
// hbox2->addWidget(runIndicator);
hbox2->setSpacing(0);
hbox->addLayout(hbox2);
}
{
QVBoxLayout *vbox = new QVBoxLayout;
// { // Indicators
// QHBoxLayout* hbox2 = new QHBoxLayout;
// for(int i=0; i<indicatorLabels.count(); ++i) {
// hbox2->addWidget(new QLabel(indicatorLabels.at(i)));
// hbox2->addWidget(simIndicators[i]);
// }
// hbox2->addStretch();
// vbox->addLayout(hbox2);
// }
{
QGridLayout *grid = new QGridLayout;
vbox->addLayout(grid);
int icol = 0;
for (int i = 0; i < 4; i++) {
grid->addWidget(new QLabel(ctrlLabels.at(i)), 0, icol);
grid->addWidget(new QLabel(indicatorLabels.at(i)), 1, icol);
// grid->setColumnStretch(icol,1);
icol++;
grid->addWidget(simCtrls[i], 0, icol);
grid->addWidget(simIndicators[i], 1, icol);
int w = fontMetrics().horizontalAdvance(typContent.at(i));
grid->setColumnMinimumWidth(icol, w);
// grid->setColumnStretch(icol,100);
icol++;
}
}
{
QHBoxLayout *hbox3 = new QHBoxLayout;
hbox3->addWidget(new QLabel("Max CPU time (s)"));
hbox3->addWidget(sbMaxTime);
hbox3->addStretch();
vbox->addLayout(hbox3);
}
{
QHBoxLayout *hbox2 = new QHBoxLayout;
hbox2->addWidget(runIndicator);
hbox2->addWidget(progressBar);
vbox->addLayout(hbox2);
}
hbox->addLayout(vbox);
}
masterLayout->addLayout(hbox);
}
/* create my timer */
simTimer = new QTimer;
/* Connect signals / slots */
connect(btStart, &QToolButton::toggled, this, &SimControlWidget::onStart);
connect(btReset, &QToolButton::clicked, this, &SimControlWidget::onReset);
connect(simTimer, &QTimer::timeout, this, &SimControlWidget::onSimTimer);
connect(driver_, &McDriverObj::simulationCreated, this, &SimControlWidget::onSimulationCreated);
bool ret = connect(driver_, &McDriverObj::statusChanged, this,
&SimControlWidget::onDriverStatusChanged, Qt::QueuedConnection);
assert(ret);
ret = connect(driver_, &McDriverObj::simulationStarted, this,
&SimControlWidget::onSimulationStarted, Qt::QueuedConnection);
assert(ret);
connect(driver_, &McDriverObj::configChanged, this, &SimControlWidget::revert);
connect(sbIons, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
[this](double v) { driver_->setMaxIons(static_cast<quint64>(v)); });
connect(sbNThreads, QOverload<int>::of(&QSpinBox::valueChanged), driver_,
&McDriverObj::setNThreads);
connect(sbSeed, QOverload<int>::of(&QSpinBox::valueChanged), driver_, &McDriverObj::setSeed);
connect(sbUpdInterval, QOverload<int>::of(&QSpinBox::valueChanged), driver_,
&McDriverObj::setUpdInterval);
connect(sbMaxTime, QOverload<int>::of(&QSpinBox::valueChanged), driver_,
&McDriverObj::setMaxCpuTime);
}
void SimControlWidget::onStart(bool b)
{
McDriverObj::DriverStatus st = driver_->status();
if (b) {
// already running ?
if (st == McDriverObj::mcRunning)
return;
// check if there are unsaved options
if (mainui_->optionsView->modified()) {
int ret =
QMessageBox::warning(window(), "Run Simulation",
"Changes to some options have not been applied.\n"
"Apply them before starting the simulation?",
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
if (ret == QMessageBox::Yes) {
mainui_->optionsView->submit();
} else {
if (ret != QMessageBox::No) {
btStart->setChecked(false);
return;
}
}
}
// validate
QString msg;
bool ret = driver_->validateOptions(&msg);
if (!ret) {
QMessageBox::warning(window(), "Run Simulation", msg);
btStart->setChecked(false);
return;
}
driver_->start(b);
btStart->setText("Pause");
} else {
driver_->start(b);
btStart->setText("Start");
}
}
void SimControlWidget::onReset()
{
int ret = QMessageBox::warning(window(), "Reset",
"Reset simulation?\n"
"This will discard all data.",
QMessageBox::Ok | QMessageBox::Cancel);
if (ret != QMessageBox::Ok)
return;
McDriverObj *D = driver_;
if (D->status() == McDriverObj::mcRunning)
onStart(false);
D->reset();
progressBar->setValue(0);
for (auto edt : simIndicators)
edt->setText("");
}
void SimControlWidget::onDriverStatusChanged()
{
const McDriverObj *D = driver_;
McDriverObj::DriverStatus st = D->status();
btReset->setEnabled(st != McDriverObj::mcReset);
if (st != McDriverObj::mcRunning) {
// btStart->setIcon(QIcon(":/assets/ionicons/play-circle-outline.png"));
if (btStart->isChecked()) {
btStart->setChecked(false);
btStart->setText("Start");
}
// runIndicator->setText("");
}
if (st == McDriverObj::mcReset) {
for (auto edt : simIndicators)
edt->setText("");
progressBar->setValue(0);
}
sbIons->setEnabled(st != McDriverObj::mcRunning);
sbNThreads->setEnabled(st != McDriverObj::mcRunning);
sbUpdInterval->setEnabled(st != McDriverObj::mcRunning);
sbSeed->setEnabled(st == McDriverObj::mcReset);
sbMaxTime->setEnabled(st != McDriverObj::mcRunning);
}
QString mytimefmt_(double t, bool ceil = false)
{
unsigned long ti = ceil ? std::ceil(t) : std::floor(t);
unsigned long s = ti % 60;
ti = (ti - s) / 60;
unsigned long m = ti % 60;
ti = (ti - m) / 60;
return QString("%1:%2:%3")
.arg(ti, 2, 10, QChar('0'))
.arg(m, 2, 10, QChar('0'))
.arg(s, 2, 10, QChar('0'));
}
void SimControlWidget::onSimTimer()
{
const McDriverObj::running_sim_info &info = driver_->sim_info();
QString cool_chars = "⣾⣷⣯⣟⡿⢿⣻⣽";
static int k = 0;
runIndicator->setText(QString(cool_chars[k++ & 7]));
progressBar->setValue(info.progress());
simIndicators[0]->setText(QString::number(info.nions()));
simIndicators[1]->setText(QString::number(info.ips(), 'g', 3));
simIndicators[2]->setText(mytimefmt_(info.elapsed()));
simIndicators[3]->setText(mytimefmt_(info.etc(), true));
}
void SimControlWidget::onSimulationStarted(bool b)
{
if (b) {
simTimer->start(100);
} else {
simTimer->stop();
onSimTimer();
}
}
void SimControlWidget::onSimulationCreated()
{
const McDriverObj::running_sim_info &info = driver_->sim_info();
progressBar->setValue(info.progress());
simIndicators[0]->setText(QString::number(info.nions()));
}
void SimControlWidget::revert()
{
sbIons->setValue(static_cast<double>(driver_->maxIons()));
sbNThreads->setValue(driver_->nThreads());
sbSeed->setValue(driver_->seed());
sbUpdInterval->setValue(driver_->updInterval());
sbMaxTime->setValue(driver_->maxCpuTime());
}