-
Notifications
You must be signed in to change notification settings - Fork 3
/
nesting_gui.cpp
520 lines (500 loc) · 18.1 KB
/
nesting_gui.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include "nesting_gui.h"
nesting_gui::nesting_gui(QWidget* parent) : QMainWindow(parent) {
ui.setupUi(this);
// Part1 设置各表格自动拉伸
ui.partsTable->horizontalHeader()->setSectionResizeMode(
QHeaderView::ResizeToContents);
ui.sheetsTable->horizontalHeader()->setSectionResizeMode(
QHeaderView::ResizeToContents);
ui.partsTable->setTargetColumn(2);
ui.sheetsTable->setTargetColumn(2);
ui.resultTable->horizontalHeader()->setSectionResizeMode(
QHeaderView::Stretch);
// Part2 设置QToolButton的action
ui.partsOpenCAD->setDefaultAction(ui.actionOpen_DXF_DWG_File);
ui.partsOpenCSV->setDefaultAction(ui.actionOpen_CSV_File);
ui.partsOpenSVG->setDefaultAction(ui.actionOpen_SVG_File);
ui.CreateSheetButton->setDefaultAction(ui.actionCreate_Sheet);
ui.ExportTXTButton->setDefaultAction(ui.actionTo_TXT_File);
ui.ExportSVGButton->setDefaultAction(ui.actionTo_SVG_File);
ui.ExportDXFButton->setDefaultAction(ui.actionTo_DXF_File);
// Part3 设置Nesting Tab中各控件的初始状态
ui.fixRun->setChecked(true);
ui.StopButton->setDisabled(true);
ui.progressBar->setValue(0);
// Part4 设置图表对象
// 坐标轴
axisX = new QValueAxis();
axisY = new QValueAxis();
axisX->setRange(0, 60);
axisX->setTickCount(7);
axisY->setRange(0, 1);
axisX->setTitleText("Time(s)");
axisY->setTitleText("Util(%)");
// 值
series = new QLineSeries();
series->setName("Util(%)");
series->useOpenGL();
// series->setPointsVisible(true);
// 图像
chart = new QChart();
chart->addAxis(axisX, Qt::AlignBottom);
chart->addAxis(axisY, Qt::AlignLeft);
chart->addSeries(series);
// chart->setAnimationOptions(QChart::SeriesAnimations);
chart->legend()->hide();
series->attachAxis(axisX);
series->attachAxis(axisY);
ui.resultChart->setRenderHint(QPainter::Antialiasing);
ui.resultChart->setChart(chart);
ui.resultChart->setRubberBand(QChartView::VerticalRubberBand);
// 滚动条
ui.ChartScrollBar->setRange(0, 30);
connect(ui.ChartScrollBar, &QScrollBar::valueChanged, [&](int value) {
// 计算偏移量
static bool scrolling = false;
static int previous = 0;
// 在水平方向上滚动
if (!scrolling) {
scrolling = true;
ui.resultChart->chart()->axisX()->setRange(value, value + 60);
previous = value;
scrolling = false;
}
});
// Part5 设置CurrentLayout
// 设置dockwidget只在tabindex=2时显现
connect(ui.tabWidget, &QTabWidget::currentChanged, ui.dockWidget,
[&](int index) {
if (index != 2) {
ui.dockWidget->hide();
} else {
ui.dockWidget->show();
}
});
ui.dockWidget->setWidget(ui.openGLWidget);
ui.dockWidget->hide();
this->addDockWidget(Qt::BottomDockWidgetArea, ui.dockWidget);
this->resizeDocks({ui.dockWidget}, {(int)(this->height() * 0.5)},
Qt::Vertical);
// Part7 设置时钟
timer = new QTimer(this);
connect(timer, &QTimer::timeout, [&]() {
// 处理progressBar
qDebug() << "timer START";
auto max = ui.progressBar->maximum();
auto min = ui.progressBar->minimum();
if (max != 0 || min != 0) {
if (time > max) {
if (timer->isActive()) {
qDebug() << "stop timer 2";
ui.StopButton->click();
}
} else {
ui.progressBar->setValue(time + 1);
}
}
// 处理Chart
if (ui.resultTable->rowCount() > 0) {
series->append(
time, ui.resultTable->item(0, 1)->data(Qt::DisplayRole).toReal());
} else {
series->append(time, 0);
}
ui.ChartScrollBar->setMaximum(ui.ChartScrollBar->maximum() + 1);
++time;
qDebug() << "timer END";
});
}
nesting_gui::~nesting_gui() {
delete chart;
delete series;
delete axisX;
delete axisY;
delete timer;
}
void nesting_gui::createSheet() {
auto rowIndex = ui.sheetsTable->rowCount();
if (rowIndex >= 1) {
QMessageBox::warning(this, "Warning",
"Currently, only one sheet is allowed to be used.");
return;
}
ui.tabWidget->setCurrentIndex(1);
QDialog dialog(this);
UICreateSheetDialog.setupUi(&dialog);
UICreateSheetDialog.label_3->setVisible(false);
UICreateSheetDialog.quantity->setVisible(false);
if (QDialog::Accepted == dialog.exec()) {
auto width = UICreateSheetDialog.width->value();
auto height = UICreateSheetDialog.height->value();
auto quantity = UICreateSheetDialog.quantity->value();
ui.sheetsTable->insertRow(rowIndex);
auto i1 = new QTableWidgetItem();
i1->setData(Qt::DisplayRole, width);
ui.sheetsTable->setItem(rowIndex, 0, i1);
auto i2 = new QTableWidgetItem();
i2->setData(Qt::DisplayRole, height);
ui.sheetsTable->setItem(rowIndex, 1, i2);
auto i4 = new QTableWidgetItem();
QPolygonF p({QPointF(0, 0), QPointF(width, 0), QPointF(width, height),
QPointF(0, height)});
i4->setData(Qt::DisplayRole, p);
ui.sheetsTable->setItem(rowIndex, 2, i4);
}
}
void nesting_gui::openCSV() {
ui.tabWidget->setCurrentIndex(0);
QString filePath =
QFileDialog::getOpenFileName(this, "Open CSV File", ".", "CSV (*.csv)");
if (filePath.isEmpty()) {
return;
}
std::vector<nesting::geo::Polygon_with_holes_2> pgns;
std::vector<std::uint32_t> rots;
std::vector<std::uint32_t> quat;
try {
nesting::read_from_csv(filePath.toStdString(), pgns, rots, quat);
} catch (const std::runtime_error& e) {
QMessageBox::warning(this, "Open CSV File", e.what());
}
storeData(pgns, rots, quat);
}
void nesting_gui::openCAD() {
ui.tabWidget->setCurrentIndex(0);
QStringList filePaths = QFileDialog::getOpenFileNames(
this, "Open DXF/DWG File", ".", "CAD (*.dxf *.dwg)");
if (filePaths.isEmpty()) {
return;
}
std::vector<nesting::geo::Polygon_with_holes_2> pgns;
std::vector<std::uint32_t> rots;
std::vector<std::uint32_t> quat;
for (auto& qstring : filePaths) {
try {
nesting::read_from_cad(qstring.toStdString(), pgns, rots, quat);
} catch (const std::runtime_error& e) {
QMessageBox::warning(this, "Open CAD File", e.what());
}
}
storeData(pgns, rots, quat);
}
void nesting_gui::openSVG() {
ui.tabWidget->setCurrentIndex(0);
QStringList filePaths =
QFileDialog::getOpenFileNames(this, "Open SVG File", ".", "SVG (*.svg)");
if (filePaths.isEmpty()) {
return;
}
std::vector<nesting::geo::Polygon_with_holes_2> pgns;
std::vector<std::uint32_t> rots;
std::vector<std::uint32_t> quat;
for (auto& qstring : filePaths) {
try {
nesting::read_from_svg(qstring.toStdString(), pgns, rots, quat);
} catch (const std::runtime_error& e) {
QMessageBox::warning(this, "Open SVG File", e.what());
}
}
storeData(pgns, rots, quat);
}
void nesting_gui::toTXT() {
if (ui.resultTable->rowCount() == 0) {
QMessageBox::warning(this, "Warning", "Solutions do not exist");
return;
}
QString filePath = QFileDialog::getSaveFileName(this, "Save File", ".",
"Text files (*.txt)");
if (!filePath.isEmpty()) {
auto i1 = ui.resultTable->item(0, 0);
auto i2 = ui.resultTable->item(0, 1);
auto i3 = ui.resultTable->item(0, 2);
auto util = i2->data(Qt::DisplayRole).value<double>();
auto pgns = i2->data(Qt::UserRole)
.value<std::vector<nesting::geo::Polygon_with_holes_2>>();
auto sheet_length =
ui.sheetsTable->item(0, 0)->data(Qt::DisplayRole).value<double>();
auto sheet_width =
ui.sheetsTable->item(0, 1)->data(Qt::DisplayRole).value<double>();
auto ret = nesting::write_to_txt(filePath.toStdString(), util, sheet_width,
sheet_length, pgns);
if (ret) {
QMessageBox::information(this, "Success", "File saved successfully");
} else {
QMessageBox::warning(this, "Fail", "File failed to save");
}
}
}
void nesting_gui::toDXF() {
if (ui.resultTable->rowCount() == 0) {
QMessageBox::warning(this, "Warning", "Solutions do not exist");
return;
}
QString filePath =
QFileDialog::getSaveFileName(this, "Save File", ".", "DXF (*.dxf)");
if (!filePath.isEmpty()) {
auto i1 = ui.resultTable->item(0, 0);
auto i2 = ui.resultTable->item(0, 1);
auto i3 = ui.resultTable->item(0, 2);
auto util = i2->data(Qt::DisplayRole).value<double>();
auto pgns = i2->data(Qt::UserRole)
.value<std::vector<nesting::geo::Polygon_with_holes_2>>();
auto sheet_length =
ui.sheetsTable->item(0, 0)->data(Qt::DisplayRole).value<double>();
auto sheet_width =
ui.sheetsTable->item(0, 1)->data(Qt::DisplayRole).value<double>();
auto ret = nesting::write_to_dxf(filePath.toStdString(), util, sheet_width,
sheet_length, pgns);
if (ret) {
QMessageBox::information(this, "Success", "File saved successfully");
} else {
QMessageBox::warning(this, "Fail", "File failed to save");
}
}
}
void nesting_gui::toSVG() {
if (ui.resultTable->rowCount() == 0) {
QMessageBox::warning(this, "Warning", "Solutions do not exist");
return;
}
QString filePath =
QFileDialog::getSaveFileName(this, "Save File", ".", "SVG (*.svg)");
if (!filePath.isEmpty()) {
auto i1 = ui.resultTable->item(0, 0);
auto i2 = ui.resultTable->item(0, 1);
auto i3 = ui.resultTable->item(0, 2);
auto util = i2->data(Qt::DisplayRole).value<double>();
auto pgns = i2->data(Qt::UserRole)
.value<std::vector<nesting::geo::Polygon_with_holes_2>>();
auto sheet_length =
ui.sheetsTable->item(0, 0)->data(Qt::DisplayRole).value<double>();
auto sheet_width =
ui.sheetsTable->item(0, 1)->data(Qt::DisplayRole).value<double>();
auto ret = nesting::write_to_svg(filePath.toStdString(), util, sheet_width,
sheet_length, pgns);
if (ret) {
QMessageBox::information(this, "Success", "File saved successfully");
} else {
QMessageBox::warning(this, "Fail", "File failed to save");
}
}
}
void nesting_gui::start() {
// 清空上次的ui
qDebug() << "start";
ui.progressBar->setRange(0, 100);
ui.progressBar->setValue(0);
series->clear();
ui.resultTable->clearContents();
ui.resultTable->setRowCount(0);
auto parts_count = ui.partsTable->rowCount();
if (parts_count == 0) {
QMessageBox::warning(this, "Warning", "Parts do not exsit");
return;
}
auto sheets_count = ui.sheetsTable->rowCount();
if (sheets_count == 0) {
QMessageBox::warning(this, "Warning", "Sheet does not exsit");
return;
}
// 获取part
std::vector<uint32_t> allowed_rotations;
std::vector<uint32_t> quantity;
std::vector<nesting::geo::Polygon_with_holes_2> polygons;
for (size_t i = 0; i < parts_count; i++) {
QTableWidgetItem* i0 = ui.partsTable->item(i, 0);
QTableWidgetItem* i1 = ui.partsTable->item(i, 1);
QTableWidgetItem* i2 = ui.partsTable->item(i, 2);
auto allowed_rotation = i0->data(Qt::DisplayRole).value<uint32_t>();
auto qua = i1->data(Qt::DisplayRole).value<uint32_t>();
auto pgn =
i2->data(Qt::UserRole).value<nesting::geo::Polygon_with_holes_2>();
allowed_rotations.push_back(allowed_rotation);
quantity.push_back(qua);
polygons.push_back(pgn);
}
// 获取sheet
QTableWidgetItem* i0 = ui.sheetsTable->item(0, 0);
QTableWidgetItem* i1 = ui.sheetsTable->item(0, 1);
double sheet_width = i0->data(Qt::DisplayRole).value<double>();
double sheet_height = i1->data(Qt::DisplayRole).value<double>();
ui.openGLWidget->set_sheet(sheet_width, sheet_height);
// 时间参数
int seconds = 24 * 3600;
if (ui.fixRun->isChecked()) {
auto time = ui.timeEdit->time();
auto h = time.hour();
auto m = time.minute();
auto s = time.second();
seconds = (h * 3600 + m * 60 + s);
ui.progressBar->setRange(0, seconds);
} else {
ui.progressBar->setRange(0, 0);
}
// 其他参数
auto need_simplify = ui.needSimplify->isChecked();
auto part_offset = ui.partSpacingSpinBox->value();
auto top_offset = ui.TopSpinBox->value();
auto bottom_offset = ui.BottomSpinBox->value();
auto left_offset = ui.LeftSpinBox->value();
auto right_offset = ui.RightSpinBox->value();
// ui更新
ui.ChartScrollBar->setRange(0, 30);
ui.ChartScrollBar->setValue(0);
ui.resultChart->chart()->axisX()->setRange(0, 60);
ui.startButton->setDisabled(true);
ui.StopButton->setEnabled(true);
ui.timeEdit->setDisabled(true);
ui.partSpacingSpinBox->setDisabled(true);
ui.TopSpinBox->setDisabled(true);
ui.LeftSpinBox->setDisabled(true);
ui.BottomSpinBox->setDisabled(true);
ui.RightSpinBox->setDisabled(true);
ui.fixRun->setDisabled(true);
ui.needSimplify->setDisabled(true);
// 开启时钟
time = 0;
timer->start(1000);
startWork(need_simplify, top_offset, left_offset, bottom_offset, right_offset,
part_offset, sheet_width, sheet_height, seconds, polygons,
allowed_rotations, quantity);
}
void nesting_gui::stop() {
qDebug() << "stop START";
// 关闭定时任务
if (timer->isActive()) {
timer->stop();
}
// 强制停止线程
if (worker != nullptr && worker->isRunning()) {
waitDialog = new QDialog(this);
UIWaitDialog.setupUi(waitDialog);
worker->quit();
worker->requestQuit();
waitDialog->setWindowFlag(Qt::WindowCloseButtonHint, false);
waitDialog->open();
QEventLoop loop;
connect(worker, &QThread::finished, &loop, &QEventLoop::quit);
loop.exec();
}
qDebug() << "stop END";
}
void nesting_gui::handleProgress(Solution solu,
const std::vector<nesting::Item>& sim,
const std::vector<nesting::Item>& ori) {
qDebug() << "handleProgress START";
qDebug() << solu.length;
qDebug() << solu.utilization;
// Part1 更新result table
auto rowIndex = ui.resultTable->rowCount();
ui.resultTable->insertRow(0);
auto i2 = new QTableWidgetItem();
i2->setData(Qt::DisplayRole, solu.utilization);
ui.resultTable->setItem(0, 1, i2);
auto i3 = new QTableWidgetItem();
i3->setData(Qt::DisplayRole, solu.time);
ui.resultTable->setItem(0, 2, i3);
auto part_offset = ui.partSpacingSpinBox->value();
auto bottom_offset = ui.BottomSpinBox->value();
auto left_offset = ui.LeftSpinBox->value();
auto i1 = new QTableWidgetItem();
auto pgns = nesting::postprocess(solu.solution, part_offset, left_offset, bottom_offset, sim, ori);
QVariant var1;
var1.setValue(pgns);
i2->setData(Qt::UserRole, var1);
auto size = pgns.size();
QList<QPolygonF> display_pgns;
for (size_t i = 0; i < size; i++) {
QPolygonF polygon;
auto& pwh = pgns[i];
auto& outer = pwh.outer_boundary();
for (auto v = outer.vertices_begin(); v != outer.vertices_end(); v++) {
polygon.append(QPointF(CGAL::to_double(v->x()), CGAL::to_double(v->y())));
}
display_pgns.append(polygon);
}
QVariant var2;
var2.setValue(display_pgns);
i1->setData(Qt::UserRole, var2);
i1->setData(Qt::DisplayRole, solu.length);
ui.resultTable->setItem(0, 0, i1);
ui.resultTable->setCurrentCell(0, 0);
// Part2 更新result chart
series->append(solu.time, solu.utilization);
qDebug() << "handleProgress END";
}
void nesting_gui::startWork(
const bool need_simplify,
const double top_offset,
const double left_offset,
const double bottom_offset,
const double right_offset,
const double part_offset,
const double sheet_width,
const double sheet_height,
const size_t max_time,
const std::vector<nesting::geo::Polygon_with_holes_2>& polygons,
const std::vector<uint32_t>& items_rotations,
const std::vector<uint32_t>& items_quantity) {
// Part6 设置线程
qDebug() << "start work START";
worker = new Worker(this);
connect(worker, &Worker::resultReady, this, &nesting_gui::handleProgress);
connect(worker, &Worker::finished, this, &nesting_gui::handleFinished);
connect(worker, &Worker::sendMessage, this, &nesting_gui::handleMessage);
worker->set(need_simplify, top_offset, left_offset, bottom_offset,
right_offset, part_offset, sheet_width, sheet_height, max_time,
polygons, items_rotations, items_quantity);
worker->start();
qDebug() << "start work END";
}
void nesting_gui::storeData(
std::vector<nesting::geo::Polygon_with_holes_2> pgns,
std::vector<std::uint32_t> rots,
std::vector<std::uint32_t> quat) {
auto numRows = pgns.size();
ui.partsTable->setRowCount(numRows);
for (size_t i = 0; i < numRows; ++i) {
auto i1 = new QTableWidgetItem();
i1->setData(Qt::DisplayRole, rots[i]);
ui.partsTable->setItem(i, 0, i1);
auto i2 = new QTableWidgetItem();
i2->setData(Qt::DisplayRole, quat[i]);
ui.partsTable->setItem(i, 1, i2);
auto i3 = new QTableWidgetItem();
qreal px = 0;
qreal py = 0;
QPolygonF pgn;
for (auto& v : pgns[i].outer_boundary()) {
px = CGAL::to_double(v.x());
py = CGAL::to_double(v.y());
pgn.append(QPointF(px, py));
}
i3->setData(Qt::DisplayRole, pgn);
QVariant var;
var.setValue(pgns[i]);
i3->setData(Qt::UserRole, var);
ui.partsTable->setItem(i, 2, i3);
}
}
void nesting_gui::handleFinished() {
qDebug() << "handleFinished()";
//线程对象应该会自动释放
if (worker != nullptr) {
qDebug() << worker->isFinished();
delete worker;
worker = nullptr;
}
if (waitDialog != nullptr) {
waitDialog->close();
delete waitDialog;
waitDialog = nullptr;
}
ui.StopButton->click();
QMessageBox::information(this, "Info", "Engine thread exited");
qDebug() << "handleFinished() END";
}
void nesting_gui::handleMessage(const QString& s) {
qDebug() << "handleMessage(): " << s;
QMessageBox::critical(this, "Error", s);
}