diff --git a/mainwindow.cpp b/mainwindow.cpp index cb70dc0..fb61e3e 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -13,6 +13,7 @@ MainWindow::MainWindow() addDockWidget(Qt::LeftDockWidgetArea, dock); connect(dock, SIGNAL(fftSizeChanged(int)), &spectrogram, SLOT(setFFTSize(int))); + connect(dock->fileOpenButton, SIGNAL(clicked()), &spectrogram, SLOT(pickFile())); connect(dock->powerMaxSlider, SIGNAL(valueChanged(int)), &spectrogram, SLOT(setPowerMax(int))); connect(dock->powerMinSlider, SIGNAL(valueChanged(int)), &spectrogram, SLOT(setPowerMin(int))); } \ No newline at end of file diff --git a/spectrogram.cpp b/spectrogram.cpp index 35b84a2..9af9732 100644 --- a/spectrogram.cpp +++ b/spectrogram.cpp @@ -2,16 +2,16 @@ #include #include +#include #include #include #include Spectrogram::Spectrogram() { - inputSource = new InputSource("/home/mike/Downloads/hubsan-chopped.cfile", 1024); + inputSource = nullptr; powerMax = 0.0f; powerMin = -50.0f; - resize(inputSource->GetWidth(), inputSource->GetHeight()); } Spectrogram::~Spectrogram() @@ -19,6 +19,24 @@ Spectrogram::~Spectrogram() delete inputSource; } +void Spectrogram::pickFile() +{ + QString fileName = QFileDialog::getOpenFileName( + this, tr("Open File"), "", tr("Sample file (*.cfile *.bin);;All files (*.*)") + ); + if (fileName != nullptr) { + try { + InputSource *newFile = new InputSource(fileName.toUtf8().constData(), (inputSource != nullptr) ? inputSource->GetWidth() : 1024); + delete inputSource; + inputSource = newFile; + resize(inputSource->GetWidth(), inputSource->GetHeight()); + } catch (std::runtime_error e) { + // TODO: display error + return; + } + } +} + template const T& clamp (const T& value, const T& min, const T& max) { return std::min(max, std::max(min, value)); } diff --git a/spectrogram.h b/spectrogram.h index 3d86f7f..849d14c 100644 --- a/spectrogram.h +++ b/spectrogram.h @@ -11,6 +11,7 @@ public: ~Spectrogram(); public slots: + void pickFile(); void setFFTSize(int size); void setPowerMax(int power); void setPowerMin(int power); diff --git a/spectrogramcontrols.cpp b/spectrogramcontrols.cpp index 6d91271..f9e7f13 100644 --- a/spectrogramcontrols.cpp +++ b/spectrogramcontrols.cpp @@ -7,6 +7,9 @@ SpectrogramControls::SpectrogramControls(const QString & title, QWidget * parent widget = new QWidget(this); layout = new QFormLayout(widget); + fileOpenButton = new QPushButton("Open file...", widget); + layout->addRow(fileOpenButton); + fftSizeSlider = new QSlider(Qt::Horizontal, widget); fftSizeSlider->setRange(7, 13); fftSizeSlider->setValue(10); diff --git a/spectrogramcontrols.h b/spectrogramcontrols.h index 03b6007..c675c0b 100644 --- a/spectrogramcontrols.h +++ b/spectrogramcontrols.h @@ -2,6 +2,7 @@ #include #include +#include #include class SpectrogramControls : public QDockWidget { @@ -21,6 +22,7 @@ private: QFormLayout *layout; QSlider *fftSizeSlider; public: + QPushButton *fileOpenButton; QSlider *powerMaxSlider; QSlider *powerMinSlider; }; \ No newline at end of file