Skip to content

Commit b840b5a

Browse files
committed
Use QFileSystemWatcher instead of timer to refresh list of recordings
1 parent 62d3c3c commit b840b5a

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/qtgui/iq_tool.cpp

+24-14
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,16 @@ CIqTool::CIqTool(QWidget *parent) :
5959

6060
timer = new QTimer(this);
6161
connect(timer, SIGNAL(timeout()), this, SLOT(timeoutFunction()));
62+
63+
recdirWatcher = new QFileSystemWatcher(this);
64+
connect(recdirWatcher, SIGNAL(directoryChanged(const QString)), this, SLOT(directoryChanged(const QString)));
6265
}
6366

6467
CIqTool::~CIqTool()
6568
{
6669
timer->stop();
6770
delete timer;
71+
delete recdirWatcher;
6872
delete ui;
6973
delete recdir;
7074
delete error_palette;
@@ -104,8 +108,6 @@ void CIqTool::on_listWidget_currentTextChanged(const QString &currentText)
104108
/*! \brief Start/stop playback */
105109
void CIqTool::on_playButton_clicked(bool checked)
106110
{
107-
is_playing = checked;
108-
109111
if (checked)
110112
{
111113
if (current_file.isEmpty())
@@ -130,11 +132,15 @@ void CIqTool::on_playButton_clicked(bool checked)
130132
ui->recButton->setEnabled(false);
131133
emit startPlayback(recdir->absoluteFilePath(current_file),
132134
(float)sample_rate, center_freq);
135+
is_playing = true;
136+
timer->start(1000);
133137
}
134138
}
135139
else
136140
{
137141
emit stopPlayback();
142+
is_playing = false;
143+
timer->stop();
138144
ui->listWidget->setEnabled(true);
139145
ui->recButton->setEnabled(true);
140146
ui->slider->setValue(0);
@@ -153,6 +159,7 @@ void CIqTool::cancelPlayback()
153159
ui->listWidget->setEnabled(true);
154160
ui->recButton->setEnabled(true);
155161
is_playing = false;
162+
timer->stop();
156163
}
157164

158165

@@ -175,12 +182,14 @@ void CIqTool::on_recButton_clicked(bool checked)
175182
{
176183
ui->playButton->setEnabled(false);
177184
emit startRecording(recdir->path());
185+
timer->start(1000);
178186

179187
refreshDir();
180188
ui->listWidget->setCurrentRow(ui->listWidget->count()-1);
181189
}
182190
else
183191
{
192+
timer->stop();
184193
ui->playButton->setEnabled(true);
185194
emit stopRecording();
186195
}
@@ -199,6 +208,7 @@ void CIqTool::cancelRecording()
199208
ui->recButton->setChecked(false);
200209
ui->playButton->setEnabled(true);
201210
is_recording = false;
211+
timer->stop();
202212
}
203213

204214
/*! \brief Catch window close events.
@@ -209,6 +219,7 @@ void CIqTool::cancelRecording()
209219
*/
210220
void CIqTool::closeEvent(QCloseEvent *event)
211221
{
222+
recdirWatcher->removePath(recdir->path());
212223
timer->stop();
213224
hide();
214225
event->ignore();
@@ -220,7 +231,10 @@ void CIqTool::showEvent(QShowEvent * event)
220231
Q_UNUSED(event);
221232
refreshDir();
222233
refreshTimeWidgets();
223-
timer->start(1000);
234+
if (is_playing || is_recording) {
235+
timer->start(1000);
236+
}
237+
recdirWatcher->addPath(recdir->path());
224238
}
225239

226240

@@ -281,8 +295,6 @@ void CIqTool::on_recDirButton_clicked()
281295

282296
void CIqTool::timeoutFunction(void)
283297
{
284-
refreshDir();
285-
286298
if (is_playing)
287299
{
288300
// advance slider with one second
@@ -295,8 +307,14 @@ void CIqTool::timeoutFunction(void)
295307
refreshTimeWidgets();
296308
}
297309
}
298-
if (is_recording)
310+
if (is_recording) {
299311
refreshTimeWidgets();
312+
313+
// update rec_len; if the file being recorded is the one selected
314+
// in the list, the length will update periodically
315+
QFileInfo info(*recdir, current_file);
316+
rec_len = (int)(info.size() / (sample_rate * bytes_per_sample));
317+
}
300318
}
301319

302320
/*! \brief Refresh list of files in current working directory. */
@@ -315,14 +333,6 @@ void CIqTool::refreshDir()
315333
ui->listWidget->setCurrentRow(selection);
316334
sc->setSliderPosition(lastScroll);
317335
ui->listWidget->blockSignals(false);
318-
319-
if (is_recording)
320-
{
321-
// update rec_len; if the file being recorded is the one selected
322-
// in the list, the length will update periodically
323-
QFileInfo info(*recdir, current_file);
324-
rec_len = (int)(info.size() / (sample_rate * bytes_per_sample));
325-
}
326336
}
327337

328338
/*! \brief Refresh time labels and slider position

src/qtgui/iq_tool.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <QCloseEvent>
2727
#include <QDialog>
2828
#include <QDir>
29+
#include <QFileSystemWatcher>
2930
#include <QPalette>
3031
#include <QSettings>
3132
#include <QShowEvent>
@@ -89,9 +90,10 @@ private slots:
8990
private:
9091
Ui::CIqTool *ui;
9192

92-
QDir *recdir;
93-
QTimer *timer;
94-
QPalette *error_palette; /*!< Palette used to indicate an error. */
93+
QDir *recdir;
94+
QFileSystemWatcher *recdirWatcher;
95+
QTimer *timer;
96+
QPalette *error_palette; /*!< Palette used to indicate an error. */
9597

9698
QString current_file; /*!< Selected file in file browser. */
9799

0 commit comments

Comments
 (0)