-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
106 lines (94 loc) · 3.58 KB
/
Copy pathmainwindow.cpp
File metadata and controls
106 lines (94 loc) · 3.58 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "serialworker.h"
#include <QFileDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, configDialog(this)
{
ui->setupUi(this);
defaultStyle = ui->label->styleSheet();
SerialWorker *worker = new SerialWorker;
worker->moveToThread(&serialThread);
connect(&serialThread, &QThread::finished, worker, &QObject::deleteLater);
connect(ui->configButton, &QPushButton::clicked, &configDialog, &SerialConfigDialog::show);
connect(ui->startButton, &QPushButton::clicked, this, &MainWindow::onStartClicked);
connect(ui->stopButton, &QPushButton::clicked, this, [this](){serialThread.requestInterruption(); ui->statusLabel->setText(QStringLiteral("Stopping punching operation..."));});
connect(ui->fileButton, &QPushButton::clicked, this, &MainWindow::openFileDialog);
// Both editing the input box and selecting a file via "Browse" chooses the file
connect(ui->filePathLineEdit, &QLineEdit::editingFinished, this, [this](){auto name = ui->filePathLineEdit->text();
if (!name.isEmpty()) filename = name;});
// connect(worker, &SerialWorker::serialError, this, &MainWindow::onSerialError);
connect(worker, &SerialWorker::error, this, &MainWindow::onGenericError);
connect(worker, &SerialWorker::finished, this, &MainWindow::onPunchFinish);
connect(this, &MainWindow::startWorker, worker, &SerialWorker::start);
connect(worker, &SerialWorker::stopped, this, &MainWindow::onPunchStop);
serialThread.start();
}
MainWindow::~MainWindow()
{
serialThread.quit();
serialThread.wait();
delete ui;
}
void MainWindow::onSerialError(QSerialPort::SerialPortError err)
{
static const QString errText[] =
{
"No error occured",
"Serial port does not exist",
"Permission error",
"Open error",
"Error when attempting to write data",
"Error when attempting to read data",
"Resource error, check connection",
"An operation that is unsupported has been attempted",
"Unknown error",
"Timeout",
"Serial port was not open",
};
ui->statusLabel->setText("Error: " + errText[err]);
ui->statusLabel->setStyleSheet("QLabel {color: red}");
ui->startButton->setEnabled(true);
}
void MainWindow::onGenericError(const QString& err)
{
ui->statusLabel->setText("Error!");
ui->statusLabel->setStyleSheet("QLabel {color: red}");
ui->startButton->setEnabled(true);
ui->stopButton->setEnabled(false);
QMessageBox::critical(this, "Error", err);
}
void MainWindow::onPunchFinish()
{
ui->statusLabel->setText("Ready to connect");
ui->statusLabel->setStyleSheet(defaultStyle);
ui->startButton->setEnabled(true);
ui->stopButton->setEnabled(false);
}
void MainWindow::openFileDialog()
{
QString filename = QFileDialog::getOpenFileName(this,
"Open SVG Image", QString(), "Images (*.svg)");
if (!filename.isEmpty()) {
ui->filePathLineEdit->setText(filename);
this->filename = filename;
}
}
void MainWindow::onStartClicked()
{
ui->statusLabel->setText("Punch in progress...");
ui->statusLabel->setStyleSheet(defaultStyle);
ui->startButton->setEnabled(false);
ui->stopButton->setEnabled(true);
emit startWorker(filename, configDialog.getSerialConfig());
}
void MainWindow::onPunchStop()
{
ui->statusLabel->setText("Operation stopped.");
ui->statusLabel->setStyleSheet("QLabel {color: red}");
ui->stopButton->setEnabled(false);
ui->startButton->setEnabled(true);
}