1 /* -*- c++ -*- */
2 /*
3  * Gqrx SDR: Software defined radio receiver powered by GNU Radio and Qt
4  *           https://gqrx.dk/
5  *
6  * Copyright 2013-2016 Alexandru Csete OZ9AEC.
7  *
8  * Gqrx is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3, or (at your option)
11  * any later version.
12  *
13  * Gqrx is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Gqrx; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street,
21  * Boston, MA 02110-1301, USA.
22  */
23 #include <QFileDialog>
24 #include <QPalette>
25 #include <QDebug>
26 
27 #include "audio_options.h"
28 #include "ui_audio_options.h"
29 
CAudioOptions(QWidget * parent)30 CAudioOptions::CAudioOptions(QWidget *parent) :
31     QDialog(parent),
32     ui(new Ui::CAudioOptions)
33 {
34     ui->setupUi(this);
35 
36     work_dir = new QDir();
37 
38     error_palette = new QPalette();
39     error_palette->setColor(QPalette::Text, Qt::red);
40 }
41 
~CAudioOptions()42 CAudioOptions::~CAudioOptions()
43 {
44     delete work_dir;
45     delete error_palette;
46     delete ui;
47 }
48 
49 
50 /**
51  * Catch window close events.
52  *
53  * This method is called when the user closes the audio options dialog
54  * window using the window close icon. We catch the event and hide the
55  * dialog but keep it around for later use.
56  */
closeEvent(QCloseEvent * event)57 void CAudioOptions::closeEvent(QCloseEvent *event)
58 {
59     hide();
60     event->ignore();
61 
62     // check if we ended up with empty dir, if yes reset to $HOME
63     if (ui->recDirEdit->text().isEmpty())
64     {
65         setRecDir(QDir::homePath());
66         emit newRecDirSelected(QDir::homePath());
67     }
68 
69     if (ui->udpHost->text().isEmpty())
70     {
71         ui->udpHost->setText("localhost");
72     }
73 }
74 
75 /** Set initial location of WAV files. */
setRecDir(const QString & dir)76 void CAudioOptions::setRecDir(const QString &dir)
77 {
78     ui->recDirEdit->setText(dir);
79 }
80 
81 /** Set new UDP host name or IP. */
setUdpHost(const QString & host)82 void CAudioOptions::setUdpHost(const QString &host)
83 {
84     ui->udpHost->setText(host);
85 }
86 
87 /** Set new UDP port. */
setUdpPort(int port)88 void CAudioOptions::setUdpPort(int port)
89 {
90     ui->udpPort->setValue(port);
91 }
92 
93 /** Set new UDP stereo setting. */
setUdpStereo(bool stereo)94 void CAudioOptions::setUdpStereo(bool stereo)
95 {
96     ui->udpStereo->setChecked(stereo);
97 }
98 
99 
setFftSplit(int pct_2d)100 void CAudioOptions::setFftSplit(int pct_2d)
101 {
102     ui->fftSplitSlider->setValue(pct_2d);
103 }
104 
getFftSplit(void) const105 int  CAudioOptions::getFftSplit(void) const
106 {
107     return ui->fftSplitSlider->value();
108 }
109 
on_fftSplitSlider_valueChanged(int value)110 void CAudioOptions::on_fftSplitSlider_valueChanged(int value)
111 {
112     emit newFftSplit(value);
113 }
114 
setPandapterRange(int min,int max)115 void CAudioOptions::setPandapterRange(int min, int max)
116 {
117     if (min < max && max <= 0)
118         ui->pandRangeSlider->setValues(min, max);
119 }
120 
getPandapterRange(int * min,int * max) const121 void CAudioOptions::getPandapterRange(int * min, int * max) const
122 {
123     *min = ui->pandRangeSlider->minimumValue();
124     *max = ui->pandRangeSlider->maximumValue();
125 }
126 
on_pandRangeSlider_valuesChanged(int min,int max)127 void CAudioOptions::on_pandRangeSlider_valuesChanged(int min, int max)
128 {
129     if (ui->audioLockButton->isChecked())
130         ui->wfRangeSlider->setValues(min, max);
131 
132     m_pand_last_modified = true;
133     emit newPandapterRange(min, max);
134 }
135 
setWaterfallRange(int min,int max)136 void CAudioOptions::setWaterfallRange(int min, int max)
137 {
138     if (min < max && max <= 0)
139         ui->wfRangeSlider->setValues(min, max);
140 }
141 
getWaterfallRange(int * min,int * max) const142 void CAudioOptions::getWaterfallRange(int * min, int * max) const
143 {
144     *min = ui->wfRangeSlider->minimumValue();
145     *max = ui->wfRangeSlider->maximumValue();
146 }
147 
on_wfRangeSlider_valuesChanged(int min,int max)148 void CAudioOptions::on_wfRangeSlider_valuesChanged(int min, int max)
149 {
150     if (ui->audioLockButton->isChecked())
151         ui->pandRangeSlider->setValues(min, max);
152 
153     m_pand_last_modified = false;
154     emit newWaterfallRange(min, max);
155 }
156 
setLockButtonState(bool checked)157 void CAudioOptions::setLockButtonState(bool checked)
158 {
159     ui->audioLockButton->setChecked(checked);
160 }
161 
getLockButtonState(void) const162 bool CAudioOptions::getLockButtonState(void) const
163 {
164     return ui->audioLockButton->isChecked();
165 }
166 
setPandapterSliderValues(float min,float max)167 void CAudioOptions::setPandapterSliderValues(float min, float max)
168 {
169     ui->pandRangeSlider->blockSignals(true);
170     ui->pandRangeSlider->setValues((int)min, (int)max);
171     if (ui->audioLockButton->isChecked())
172         ui->wfRangeSlider->setValues((int) min, (int) max);
173     m_pand_last_modified = true;
174     ui->pandRangeSlider->blockSignals(false);
175 }
176 
177 /** Lock button toggled */
on_audioLockButton_toggled(bool checked)178 void CAudioOptions::on_audioLockButton_toggled(bool checked)
179 {
180     if (checked) {
181         if (m_pand_last_modified)
182         {
183             int min = ui->pandRangeSlider->minimumValue();
184             int max = ui->pandRangeSlider->maximumValue();
185             ui->wfRangeSlider->setPositions(min, max);
186         }
187         else
188         {
189             int min = ui->wfRangeSlider->minimumValue();
190             int max = ui->wfRangeSlider->maximumValue();
191             ui->pandRangeSlider->setPositions(min, max);
192         }
193     }
194 }
195 
196 
197 /**
198  * Slot called when the recordings directory has changed either
199  * because of user input or programmatically.
200  */
on_recDirEdit_textChanged(const QString & dir)201 void CAudioOptions::on_recDirEdit_textChanged(const QString &dir)
202 {
203 
204     if (work_dir->exists(dir))
205     {
206         ui->recDirEdit->setPalette(QPalette());  // Clear custom color
207         emit newRecDirSelected(dir);
208     }
209     else
210     {
211         ui->recDirEdit->setPalette(*error_palette);  // indicate error
212     }
213 }
214 
215 /** Slot called when the user clicks on the "Select" button. */
on_recDirButton_clicked()216 void CAudioOptions::on_recDirButton_clicked()
217 {
218     QString dir = QFileDialog::getExistingDirectory(this, tr("Select a directory"),
219                                                     ui->recDirEdit->text(),
220                                                     QFileDialog::ShowDirsOnly |
221                                                     QFileDialog::DontResolveSymlinks);
222 
223     if (!dir.isNull())
224         ui->recDirEdit->setText(dir);
225 }
226 
227 /** UDP host name has changed. */
on_udpHost_textChanged(const QString & text)228 void CAudioOptions::on_udpHost_textChanged(const QString &text)
229 {
230     if (!text.isEmpty())
231         emit newUdpHost(text);
232 }
233 
234 /** UDP port number has changed. */
on_udpPort_valueChanged(int port)235 void CAudioOptions::on_udpPort_valueChanged(int port)
236 {
237     emit newUdpPort(port);
238 }
239 
240 /** UDP stereo setting has changed. */
on_udpStereo_stateChanged(int state)241 void CAudioOptions::on_udpStereo_stateChanged(int state)
242 {
243     emit newUdpStereo(state);
244 }
245