1 /*
2  *  Copyright (C) 2015, Mike Walters <mike@flomp.net>
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 
MainWindow()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 
41     plots = new PlotView(input);
42     setCentralWidget(plots);
43 
44     // Connect dock inputs
45     connect(dock, &SpectrogramControls::openFile, this, &MainWindow::openFile);
46     connect(dock->sampleRate, static_cast<void (QLineEdit::*)(const QString&)>(&QLineEdit::textChanged), this, static_cast<void (MainWindow::*)(QString)>(&MainWindow::setSampleRate));
47     connect(dock, static_cast<void (SpectrogramControls::*)(int, int)>(&SpectrogramControls::fftOrZoomChanged), plots, &PlotView::setFFTAndZoom);
48     connect(dock->powerMaxSlider, &QSlider::valueChanged, plots, &PlotView::setPowerMax);
49     connect(dock->powerMinSlider, &QSlider::valueChanged, plots, &PlotView::setPowerMin);
50     connect(dock->cursorsCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableCursors);
51     connect(dock->scalesCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableScales);
52     connect(dock->cursorSymbolsSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), plots, &PlotView::setCursorSegments);
53 
54     // Connect dock outputs
55     connect(plots, &PlotView::timeSelectionChanged, dock, &SpectrogramControls::timeSelectionChanged);
56     connect(plots, &PlotView::zoomIn, dock, &SpectrogramControls::zoomIn);
57     connect(plots, &PlotView::zoomOut, dock, &SpectrogramControls::zoomOut);
58 
59     // Set defaults after making connections so everything is in sync
60     dock->setDefaults();
61 
62 }
63 
openFile(QString fileName)64 void MainWindow::openFile(QString fileName)
65 {
66     QString title="%1: %2";
67     this->setWindowTitle(title.arg(QApplication::applicationName(),fileName.section('/',-1,-1)));
68 
69     // Try to parse osmocom_fft filenames and extract the sample rate and center frequency.
70     // Example file name: "name-f2.411200e+09-s5.000000e+06-t20160807180210.cfile"
71     QRegExp rx("(.*)-f(.*)-s(.*)-.*\\.cfile");
72     QString basename = fileName.section('/',-1,-1);
73 
74     if (rx.exactMatch(basename)) {
75         QString centerfreq = rx.cap(2);
76         QString samplerate = rx.cap(3);
77 
78         std::stringstream ss(samplerate.toUtf8().constData());
79 
80         // Needs to be a double as the number is in scientific format
81         double rate;
82         ss >> rate;
83         if (!ss.fail()) {
84             setSampleRate(rate);
85         }
86     }
87 
88     try
89     {
90         input->openFile(fileName.toUtf8().constData());
91     }
92     catch (const std::exception &ex)
93     {
94         QMessageBox msgBox(QMessageBox::Critical, "Inspectrum openFile error", QString("%1: %2").arg(fileName).arg(ex.what()));
95         msgBox.exec();
96     }
97 }
98 
setSampleRate(QString rate)99 void MainWindow::setSampleRate(QString rate)
100 {
101     auto sampleRate = rate.toDouble();
102     input->setSampleRate(sampleRate);
103     plots->setSampleRate(sampleRate);
104 
105     // Save the sample rate in settings as we're likely to be opening the same file across multiple runs
106     QSettings settings;
107     settings.setValue("SampleRate", sampleRate);
108 }
109 
setSampleRate(double rate)110 void MainWindow::setSampleRate(double rate)
111 {
112     dock->sampleRate->setText(QString::number(rate));
113 }
114 
setFormat(QString fmt)115 void MainWindow::setFormat(QString fmt)
116 {
117     input->setFormat(fmt.toUtf8().constData());
118 }
119