| 1 | /* |
| 2 | * Copyright (C) 2015, Mike Walters <[email protected]> |
| 3 | * |
| 4 | * This file is part of inspectrum. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <QMessageBox> |
| 21 | #include <QtWidgets> |
| 22 | #include <QPixmapCache> |
| 23 | #include <QRubberBand> |
| 24 | #include <sstream> |
| 25 | |
| 26 | #include "mainwindow.h" |
| 27 | #include "util.h" |
| 28 | |
| 29 | MainWindow::MainWindow() |
| 30 | { |
| 31 | setWindowTitle(tr("inspectrum")); |
| 32 | |
| 33 | QPixmapCache::setCacheLimit(40960); |
| 34 | |
| 35 | dock = new SpectrogramControls(tr("Controls"), this); |
| 36 | dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); |
| 37 | addDockWidget(Qt::LeftDockWidgetArea, dock); |
| 38 | |
| 39 | input = new InputSource(); |
| 40 | input->subscribe(this); |
| 41 | |
| 42 | plots = new PlotView(input); |
| 43 | setCentralWidget(plots); |
| 44 | |
| 45 | // Connect dock inputs |
| 46 | connect(dock, &SpectrogramControls::openFile, this, &MainWindow::openFile); |
| 47 | connect(dock->sampleRate, static_cast<void (QLineEdit::*)(const QString&)>(&QLineEdit::textChanged), this, static_cast<void (MainWindow::*)(QString)>(&MainWindow::setSampleRate)); |
| 48 | connect(dock, static_cast<void (SpectrogramControls::*)(int, int)>(&SpectrogramControls::fftOrZoomChanged), plots, &PlotView::setFFTAndZoom); |
| 49 | connect(dock->powerMaxSlider, &QSlider::valueChanged, plots, &PlotView::setPowerMax); |
| 50 | connect(dock->powerMinSlider, &QSlider::valueChanged, plots, &PlotView::setPowerMin); |
| 51 | connect(dock->cursorsCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableCursors); |
| 52 | connect(dock->scalesCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableScales); |
| 53 | connect(dock->annosCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableAnnotations); |
| 54 | connect(dock->annosCheckBox, &QCheckBox::stateChanged, dock, &SpectrogramControls::enableAnnotations); |
| 55 | connect(dock->commentsCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableAnnotationCommentsTooltips); |
| 56 | connect(dock->cursorSymbolsSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), plots, &PlotView::setCursorSegments); |
| 57 | |
| 58 | // Connect dock outputs |
| 59 | connect(plots, &PlotView::timeSelectionChanged, dock, &SpectrogramControls::timeSelectionChanged); |
| 60 | connect(plots, &PlotView::zoomIn, dock, &SpectrogramControls::zoomIn); |
| 61 | connect(plots, &PlotView::zoomOut, dock, &SpectrogramControls::zoomOut); |
| 62 | |
| 63 | // Set defaults after making connections so everything is in sync |
| 64 | dock->setDefaults(); |
| 65 | |
| 66 | } |
| 67 | |
| 68 | void MainWindow::openFile(QString fileName) |
| 69 | { |
| 70 | QString title="%1: %2"; |
| 71 | this->setWindowTitle(title.arg(QApplication::applicationName(),fileName.section('/',-1,-1))); |
| 72 | |
| 73 | // Try to parse osmocom_fft filenames and extract the sample rate and center frequency. |
| 74 | // Example file name: "name-f2.411200e+09-s5.000000e+06-t20160807180210.cfile" |
| 75 | QRegularExpression rx(QRegularExpression::anchoredPattern("(.*)-f(.*)-s(.*)-.*\\.cfile")); |
| 76 | QString basename = fileName.section('/',-1,-1); |
| 77 | |
| 78 | auto match = rx.match(basename); |
| 79 | if (match.hasMatch()) { |
| 80 | QString centerfreq = match.captured(2); |
| 81 | QString samplerate = match.captured(3); |
| 82 | |
| 83 | std::stringstream ss(samplerate.toUtf8().constData()); |
| 84 | |
| 85 | // Needs to be a double as the number is in scientific format |
| 86 | double rate; |
| 87 | ss >> rate; |
| 88 | if (!ss.fail()) { |
| 89 | setSampleRate(rate); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | try |
| 94 | { |
| 95 | input->openFile(fileName.toUtf8().constData()); |
| 96 | } |
| 97 | catch (const std::exception &ex) |
| 98 | { |
| 99 | QMessageBox msgBox(QMessageBox::Critical, "Inspectrum openFile error", QString("%1: %2").arg(fileName).arg(ex.what())); |
| 100 | msgBox.exec(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void MainWindow::invalidateEvent() |
| 105 | { |
| 106 | plots->setSampleRate(input->rate()); |
| 107 | |
| 108 | // Only update the text box if it is not already representing |
| 109 | // the current value. Otherwise the cursor might jump or the |
| 110 | // representation might change (e.g. to scientific). |
| 111 | double currentValue = dock->sampleRate->text().toDouble(); |
| 112 | if(QString::number(input->rate()) != QString::number(currentValue)) { |
| 113 | setSampleRate(input->rate()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void MainWindow::setSampleRate(QString rate) |
| 118 | { |
| 119 | auto sampleRate = rate.toDouble(); |
| 120 | input->setSampleRate(sampleRate); |
| 121 | plots->setSampleRate(sampleRate); |
| 122 | |
| 123 | // Save the sample rate in settings as we're likely to be opening the same file across multiple runs |
| 124 | QSettings settings; |
| 125 | settings.setValue("SampleRate", sampleRate); |
| 126 | } |
| 127 | |
| 128 | void MainWindow::setSampleRate(double rate) |
| 129 | { |
| 130 | dock->sampleRate->setText(QString::number(rate)); |
| 131 | } |
| 132 | |
| 133 | void MainWindow::setFormat(QString fmt) |
| 134 | { |
| 135 | input->setFormat(fmt.toUtf8().constData()); |
| 136 | } |