-
Notifications
You must be signed in to change notification settings - Fork 11
/
liveviewer.cpp
169 lines (156 loc) · 6.01 KB
/
liveviewer.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
#include "liveviewer.h"
#include "ui_liveviewer.h"
#include "portselectdialog.h"
#include "serial_io.h"
#include "viewerparam.h"
#include "vieweroption.h"
#include "viewerbutton.h"
#include <QTime>
#include <QSerialPort>
#include <QSerialPortInfo>
LiveViewer::LiveViewer(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::LiveViewer)
, io(nullptr)
{
ui->setupUi(this);
// FOR DEBUG TEST
// ui->paramScrollAreaWidgetContents->layout()->addWidget(new ViewerParam("TEST", 0.1, 5.2, 0.12, 1));
// ui->optionScrollAreaWidgetContents->layout()->addWidget(new ViewerOption("TEST", QStringList{"A", "B"}, "C"));
// ui->buttonScrollAreaWidgetContents->layout()->addWidget(new ViewerButton("TEST"));
}
LiveViewer::~LiveViewer()
{
delete ui;
ui = nullptr;
}
void LiveViewer::reset_ui(){
ui->imageComboBox->clear();
ui->displayLabel->clear();
ui->fpsLabel->setText("fps=0");
QLayoutItem *child = nullptr;
while(child = ui->paramScrollAreaWidgetContents->layout()->takeAt(0)){
delete child->widget();
delete child;
}
while(child = ui->optionScrollAreaWidgetContents->layout()->takeAt(0)){
delete child->widget();
delete child;
}
while(child = ui->buttonScrollAreaWidgetContents->layout()->takeAt(0)){
delete child->widget();
delete child;
}
if(io != nullptr){
io->deleteLater();
io = nullptr;
}
}
void LiveViewer::updateImage(const QString &name){
if(ui->imageComboBox->findText(name) == -1){
ui->imageComboBox->addItem(name);
}
}
void LiveViewer::updateImageData(const QString &name, const QImage& image){
int idx = ui->imageComboBox->findText(name);
if(idx == -1){
ui->imageComboBox->addItem(name);
ui->imageComboBox->setCurrentText(name);
}else{
ui->imageComboBox->setCurrentIndex(idx);
}
ui->displayLabel->setPixmap(QPixmap::fromImage(image.scaled(ui->displayLabel->size(), Qt::KeepAspectRatio)));
// update fps
static int fps = 0;
static int t1 = QTime::currentTime().second();
int t2 = QTime::currentTime().second();
fps++;
if(t2 > t1){
ui->fpsLabel->setText(QString("fps=%1").arg(fps));
fps = 0;
t1 = t2;
}
}
void LiveViewer::updateParam(const QString &name, double min_value, double max_value, double step_value, double current_value){
qDebug("[%s]: %d", __FUNCTION__, QThread::currentThread());
bool found = false;
for(int i=0; i<ui->paramScrollAreaWidgetContents->layout()->count(); i++){
auto *obj = ui->paramScrollAreaWidgetContents->layout()->itemAt(i)->widget();
if(obj->objectName() == name){
found = true;
static_cast<ViewerParam*>(obj)->setValue(current_value);
break;
}
}
if(!found){
auto *param = new ViewerParam(name, min_value, max_value, step_value, current_value);
if(io != nullptr) QObject::connect(param, &ViewerParam::valueChanged, io, &SerialIO::sendParam);
ui->paramScrollAreaWidgetContents->layout()->addWidget(param);
}
}
void LiveViewer::updateOption(const QString &name, bool current_option){
qDebug("[%s]: %d", __FUNCTION__, QThread::currentThread());
bool found = false;
for(int i=0; i<ui->optionScrollAreaWidgetContents->layout()->count(); i++){
auto *obj = ui->optionScrollAreaWidgetContents->layout()->itemAt(i)->widget();
if(obj->objectName() == name){
found = true;
static_cast<ViewerOption*>(obj)->setChecked(current_option);
break;
}
}
if(!found){
auto *option = new ViewerOption(name, current_option);
if(io != nullptr) QObject::connect(option, &ViewerOption::stateChanged, io, &SerialIO::sendOption);
ui->optionScrollAreaWidgetContents->layout()->addWidget(option);
}
}
void LiveViewer::updateButton(const QString &name){
qDebug("[%s]: %d", __FUNCTION__, QThread::currentThread());
bool found = false;
for(int i=0; i<ui->buttonScrollAreaWidgetContents->layout()->count(); i++){
auto *obj = ui->buttonScrollAreaWidgetContents->layout()->itemAt(i)->widget();
if(obj->objectName() == name){
found = true;
break;
}
}
if(!found){
auto *button = new ViewerButton(name);
if(io != nullptr) QObject::connect(button, &ViewerButton::clicked, io, &SerialIO::sendButton);
ui->buttonScrollAreaWidgetContents->layout()->addWidget(button);
}
}
void LiveViewer::on_menuSelectCOM_aboutToShow(){
ui->menuSelectCOM->clear();
for(auto &port:QSerialPortInfo::availablePorts()){
ui->menuSelectCOM->addAction(QString("%1(%2)").arg(port.portName(), port.description()));
}
if(ui->menuSelectCOM->isEmpty()){
ui->menuSelectCOM->addAction("no available port")->setDisabled(true);
}
}
void LiveViewer::on_menuSelectCOM_triggered(QAction* obj){
PortSelectDialog dialog(obj->text());
if(dialog.exec() == QDialog::Accepted){
// stop running io thread
if(io != nullptr) io->quit();
if(io != nullptr) io->wait();
if(io != nullptr) QObject::disconnect(io, &QThread::finished, this, &LiveViewer::reset_ui);
reset_ui();
// open serial port
QString port_name = obj->text().split("(")[0];
// define io thread
io = new SerialIO(port_name, dialog.baudrate());
// connect signals
QObject::connect(io, &SerialIO::imageParsed, this, &LiveViewer::updateImage);
QObject::connect(io, &SerialIO::imageDataParsed, this, &LiveViewer::updateImageData);
QObject::connect(io, &SerialIO::paramParsed, this, &LiveViewer::updateParam);
QObject::connect(io, &SerialIO::optionParsed, this, &LiveViewer::updateOption);
QObject::connect(io, &SerialIO::buttonParsed, this, &LiveViewer::updateButton);
QObject::connect(io, &QThread::finished, this, &LiveViewer::reset_ui);
QObject::connect(ui->imageComboBox, &QComboBox::currentTextChanged, io, &SerialIO::sendImage);
// start io thread
io->start();
}
}