-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
127 lines (109 loc) · 3.85 KB
/
Copy pathMainWindow.cpp
File metadata and controls
127 lines (109 loc) · 3.85 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
#include "MainWindow.h"
#include "DldReader.h"
#include "PlotWidget.h"
#include <QCoreApplication>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QProgressBar>
#include <QPushButton>
#include <QSplashScreen>
#include <QStatusBar>
#include <QVBoxLayout>
#include <QWidget>
#include <QTextStream>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
setWindowTitle("DLD Viewer");
QWidget *central = new QWidget(this);
QVBoxLayout *vbox = new QVBoxLayout(central);
QHBoxLayout *hbox = new QHBoxLayout();
QPushButton *loadBtn = new QPushButton("Load DLD", central);
QPushButton *homeBtn = new QPushButton("Reset view", central);
QPushButton *quitBtn = new QPushButton("Quit", central);
hbox->addWidget(loadBtn);
hbox->addWidget(homeBtn);
hbox->addWidget(quitBtn);
hbox->addStretch();
m_plot = new PlotWidget(central);
vbox->addLayout(hbox);
vbox->addWidget(m_plot, 1);
setCentralWidget(central);
m_progress = new QProgressBar(this);
m_progress->setRange(0, 100);
m_progress->setValue(0);
m_progress->setTextVisible(true);
m_progress->setVisible(false);
statusBar()->addPermanentWidget(m_progress);
statusBar()->showMessage("Ready");
connect(loadBtn, &QPushButton::clicked, this, &MainWindow::loadFile);
connect(homeBtn, &QPushButton::clicked, m_plot, &PlotWidget::resetView);
connect(quitBtn, &QPushButton::clicked, this, &MainWindow::close);
connect(m_plot, &PlotWidget::benchmarkDone, this, &MainWindow::benchmarkFinished);
}
void MainWindow::setSplashScreen(QSplashScreen *splash) {
m_splash = splash;
}
void MainWindow::finishSplash() {
if (m_splash) {
m_splash->finish(this);
m_splash = nullptr;
}
}
bool MainWindow::loadFilePath(const QString &path) {
QVector<float> data;
DldLayout layout;
QString error;
m_progress->setValue(0);
m_progress->setVisible(true);
statusBar()->showMessage("Loading...");
int lastPct = -1;
auto progress = [&](int pct, const QString &stage) {
if (pct == lastPct) {
return;
}
lastPct = pct;
m_progress->setValue(pct);
QString label = stage.isEmpty() ? QString("Loading %1%").arg(pct)
: QString("%1 (%2%)").arg(stage).arg(pct);
statusBar()->showMessage(label);
if (m_splash) {
m_splash->showMessage(label, Qt::AlignHCenter | Qt::AlignBottom, Qt::white);
}
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
};
if (!readDldFile(path, data, layout, &error, progress)) {
m_progress->setVisible(false);
finishSplash();
if (QCoreApplication::arguments().contains("--bench")) {
QTextStream(stderr) << "Failed to read DLD: " << error << "\n";
} else {
QMessageBox::critical(this, "Failed to read DLD", error);
}
return false;
}
m_progress->setVisible(false);
finishSplash();
m_plot->setData(data, layout.sampleRate);
QString fmt = layout.bytesPerSample == 3 ? "24-bit" : (layout.bytesPerSample == 2 ? "16-bit" : "32-bit");
QString endian = layout.littleEndian ? "LE" : "BE";
statusBar()->showMessage(
QString("Loaded %1 | blocks=%2 samples=%3 rate=%4 Hz | %5 %6")
.arg(path)
.arg(layout.blockCount)
.arg(data.size())
.arg(layout.sampleRate > 0.0 ? QString::number(layout.sampleRate, 'f', 2) : "unknown")
.arg(fmt)
.arg(endian)
);
return true;
}
void MainWindow::startBenchmark(int frames) {
m_plot->startBenchmark(frames);
}
void MainWindow::loadFile() {
QString path = QFileDialog::getOpenFileName(this, "Open DLD", QString(), "DLD files (*.DLD);;All files (*.*)");
if (path.isEmpty()) {
return;
}
loadFilePath(path);
}