source: trunk/demos/spectrum/app/mainwidget.cpp@ 769

Last change on this file since 769 was 769, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

File size: 15.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "engine.h"
42#include "levelmeter.h"
43#include "mainwidget.h"
44#include "waveform.h"
45#include "progressbar.h"
46#include "settingsdialog.h"
47#include "spectrograph.h"
48#include "tonegeneratordialog.h"
49#include "utils.h"
50
51#include <QLabel>
52#include <QPushButton>
53#include <QHBoxLayout>
54#include <QVBoxLayout>
55#include <QStyle>
56#include <QMenu>
57#include <QFileDialog>
58#include <QTimerEvent>
59#include <QMessageBox>
60
61const int NullTimerId = -1;
62
63MainWidget::MainWidget(QWidget *parent)
64 : QWidget(parent)
65 , m_mode(NoMode)
66 , m_engine(new Engine(this))
67#ifndef DISABLE_WAVEFORM
68 , m_waveform(new Waveform(m_engine->buffer(), this))
69#endif
70 , m_progressBar(new ProgressBar(this))
71 , m_spectrograph(new Spectrograph(this))
72 , m_levelMeter(new LevelMeter(this))
73 , m_modeButton(new QPushButton(this))
74 , m_recordButton(new QPushButton(this))
75 , m_pauseButton(new QPushButton(this))
76 , m_playButton(new QPushButton(this))
77 , m_settingsButton(new QPushButton(this))
78 , m_infoMessage(new QLabel(tr("Select a mode to begin"), this))
79 , m_infoMessageTimerId(NullTimerId)
80 , m_settingsDialog(new SettingsDialog(
81 m_engine->availableAudioInputDevices(),
82 m_engine->availableAudioOutputDevices(),
83 this))
84 , m_toneGeneratorDialog(new ToneGeneratorDialog(this))
85 , m_modeMenu(new QMenu(this))
86 , m_loadFileAction(0)
87 , m_generateToneAction(0)
88 , m_recordAction(0)
89{
90 m_spectrograph->setParams(SpectrumNumBands, SpectrumLowFreq, SpectrumHighFreq);
91
92 createUi();
93 connectUi();
94}
95
96MainWidget::~MainWidget()
97{
98
99}
100
101
102//-----------------------------------------------------------------------------
103// Public slots
104//-----------------------------------------------------------------------------
105
106void MainWidget::stateChanged(QAudio::Mode mode, QAudio::State state)
107{
108 Q_UNUSED(mode);
109
110 updateButtonStates();
111
112 if (QAudio::ActiveState != state && QAudio::SuspendedState != state) {
113 m_levelMeter->reset();
114 m_spectrograph->reset();
115 }
116}
117
118void MainWidget::formatChanged(const QAudioFormat &format)
119{
120 infoMessage(formatToString(format), NullMessageTimeout);
121
122#ifndef DISABLE_WAVEFORM
123 if (QAudioFormat() != format) {
124 m_waveform->initialize(format, WaveformTileLength,
125 WaveformWindowDuration);
126 }
127#endif
128}
129
130void MainWidget::spectrumChanged(qint64 position, qint64 length,
131 const FrequencySpectrum &spectrum)
132{
133 m_progressBar->windowChanged(position, length);
134 m_spectrograph->spectrumChanged(spectrum);
135}
136
137void MainWidget::infoMessage(const QString &message, int timeoutMs)
138{
139 m_infoMessage->setText(message);
140
141 if (NullTimerId != m_infoMessageTimerId) {
142 killTimer(m_infoMessageTimerId);
143 m_infoMessageTimerId = NullTimerId;
144 }
145
146 if (NullMessageTimeout != timeoutMs)
147 m_infoMessageTimerId = startTimer(timeoutMs);
148}
149
150void MainWidget::errorMessage(const QString &heading, const QString &detail)
151{
152#ifdef Q_OS_SYMBIAN
153 const QString message = heading + "\n" + detail;
154 QMessageBox::warning(this, "", message, QMessageBox::Close);
155#else
156 QMessageBox::warning(this, heading, detail, QMessageBox::Close);
157#endif
158}
159
160void MainWidget::timerEvent(QTimerEvent *event)
161{
162 Q_ASSERT(event->timerId() == m_infoMessageTimerId);
163 Q_UNUSED(event) // suppress warnings in release builds
164 killTimer(m_infoMessageTimerId);
165 m_infoMessageTimerId = NullTimerId;
166 m_infoMessage->setText("");
167}
168
169void MainWidget::positionChanged(qint64 positionUs)
170{
171#ifndef DISABLE_WAVEFORM
172 qint64 positionBytes = audioLength(m_engine->format(), positionUs);
173 m_waveform->positionChanged(positionBytes);
174#else
175 Q_UNUSED(positionUs)
176#endif
177}
178
179void MainWidget::bufferDurationChanged(qint64 durationUs)
180{
181 m_progressBar->bufferDurationChanged(durationUs);
182}
183
184
185//-----------------------------------------------------------------------------
186// Private slots
187//-----------------------------------------------------------------------------
188
189void MainWidget::dataDurationChanged(qint64 duration)
190{
191#ifndef DISABLE_WAVEFORM
192 const qint64 dataLength = audioLength(m_engine->format(), duration);
193 m_waveform->dataLengthChanged(dataLength);
194#else
195 Q_UNUSED(duration)
196#endif
197
198 updateButtonStates();
199}
200
201void MainWidget::showFileDialog()
202{
203 reset();
204 const QString dir;
205 const QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open WAV file"), dir, "*.wav");
206 if (fileNames.count()) {
207 setMode(LoadFileMode);
208 m_engine->loadFile(fileNames.front());
209 updateButtonStates();
210 }
211}
212
213void MainWidget::showSettingsDialog()
214{
215 reset();
216 m_settingsDialog->exec();
217 if (m_settingsDialog->result() == QDialog::Accepted) {
218 m_engine->setAudioInputDevice(m_settingsDialog->inputDevice());
219 m_engine->setAudioOutputDevice(m_settingsDialog->outputDevice());
220 m_engine->setWindowFunction(m_settingsDialog->windowFunction());
221 }
222}
223
224void MainWidget::showToneGeneratorDialog()
225{
226 reset();
227 m_toneGeneratorDialog->exec();
228 if (m_toneGeneratorDialog->result() == QDialog::Accepted) {
229 setMode(GenerateToneMode);
230 const qreal amplitude = m_toneGeneratorDialog->amplitude();
231 if (m_toneGeneratorDialog->isFrequencySweepEnabled()) {
232 m_engine->generateSweptTone(amplitude);
233 } else {
234 const qreal frequency = m_toneGeneratorDialog->frequency();
235 const Tone tone(frequency, amplitude);
236 m_engine->generateTone(tone);
237 updateButtonStates();
238 }
239 }
240}
241
242void MainWidget::initializeRecord()
243{
244 reset();
245 setMode(RecordMode);
246 if (m_engine->initializeRecord())
247 updateButtonStates();
248}
249
250
251//-----------------------------------------------------------------------------
252// Private functions
253//-----------------------------------------------------------------------------
254
255void MainWidget::createUi()
256{
257 createMenus();
258
259 setWindowTitle(tr("Spectrum Analyser"));
260
261 QVBoxLayout *windowLayout = new QVBoxLayout(this);
262
263 m_infoMessage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
264 m_infoMessage->setAlignment(Qt::AlignHCenter);
265 windowLayout->addWidget(m_infoMessage);
266
267#ifdef SUPERIMPOSE_PROGRESS_ON_WAVEFORM
268 QScopedPointer<QHBoxLayout> waveformLayout(new QHBoxLayout);
269 waveformLayout->addWidget(m_progressBar);
270 m_progressBar->setMinimumHeight(m_waveform->minimumHeight());
271 waveformLayout->setMargin(0);
272 m_waveform->setLayout(waveformLayout.data());
273 waveformLayout.take();
274 windowLayout->addWidget(m_waveform);
275#else
276#ifndef DISABLE_WAVEFORM
277 windowLayout->addWidget(m_waveform);
278#endif // DISABLE_WAVEFORM
279 windowLayout->addWidget(m_progressBar);
280#endif // SUPERIMPOSE_PROGRESS_ON_WAVEFORM
281
282 // Spectrograph and level meter
283
284 QScopedPointer<QHBoxLayout> analysisLayout(new QHBoxLayout);
285 analysisLayout->addWidget(m_spectrograph);
286 analysisLayout->addWidget(m_levelMeter);
287 windowLayout->addLayout(analysisLayout.data());
288 analysisLayout.take();
289
290 // Button panel
291
292 const QSize buttonSize(30, 30);
293
294 m_modeButton->setText(tr("Mode"));
295
296 m_recordIcon = QIcon(":/images/record.png");
297 m_recordButton->setIcon(m_recordIcon);
298 m_recordButton->setEnabled(false);
299 m_recordButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
300 m_recordButton->setMinimumSize(buttonSize);
301
302 m_pauseIcon = style()->standardIcon(QStyle::SP_MediaPause);
303 m_pauseButton->setIcon(m_pauseIcon);
304 m_pauseButton->setEnabled(false);
305 m_pauseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
306 m_pauseButton->setMinimumSize(buttonSize);
307
308 m_playIcon = style()->standardIcon(QStyle::SP_MediaPlay);
309 m_playButton->setIcon(m_playIcon);
310 m_playButton->setEnabled(false);
311 m_playButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
312 m_playButton->setMinimumSize(buttonSize);
313
314 m_settingsIcon = QIcon(":/images/settings.png");
315 m_settingsButton->setIcon(m_settingsIcon);
316 m_settingsButton->setEnabled(true);
317 m_settingsButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
318 m_settingsButton->setMinimumSize(buttonSize);
319
320 QScopedPointer<QHBoxLayout> buttonPanelLayout(new QHBoxLayout);
321 buttonPanelLayout->addStretch();
322 buttonPanelLayout->addWidget(m_modeButton);
323 buttonPanelLayout->addWidget(m_recordButton);
324 buttonPanelLayout->addWidget(m_pauseButton);
325 buttonPanelLayout->addWidget(m_playButton);
326 buttonPanelLayout->addWidget(m_settingsButton);
327
328 QWidget *buttonPanel = new QWidget(this);
329 buttonPanel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
330 buttonPanel->setLayout(buttonPanelLayout.data());
331 buttonPanelLayout.take(); // ownership transferred to buttonPanel
332
333 QScopedPointer<QHBoxLayout> bottomPaneLayout(new QHBoxLayout);
334 bottomPaneLayout->addWidget(buttonPanel);
335 windowLayout->addLayout(bottomPaneLayout.data());
336 bottomPaneLayout.take(); // ownership transferred to windowLayout
337
338 // Apply layout
339
340 setLayout(windowLayout);
341}
342
343void MainWidget::connectUi()
344{
345 CHECKED_CONNECT(m_recordButton, SIGNAL(clicked()),
346 m_engine, SLOT(startRecording()));
347
348 CHECKED_CONNECT(m_pauseButton, SIGNAL(clicked()),
349 m_engine, SLOT(suspend()));
350
351 CHECKED_CONNECT(m_playButton, SIGNAL(clicked()),
352 m_engine, SLOT(startPlayback()));
353
354 CHECKED_CONNECT(m_settingsButton, SIGNAL(clicked()),
355 this, SLOT(showSettingsDialog()));
356
357 CHECKED_CONNECT(m_engine, SIGNAL(stateChanged(QAudio::Mode,QAudio::State)),
358 this, SLOT(stateChanged(QAudio::Mode,QAudio::State)));
359
360 CHECKED_CONNECT(m_engine, SIGNAL(formatChanged(const QAudioFormat &)),
361 this, SLOT(formatChanged(const QAudioFormat &)));
362
363 m_progressBar->bufferDurationChanged(m_engine->bufferDuration());
364
365 CHECKED_CONNECT(m_engine, SIGNAL(bufferDurationChanged(qint64)),
366 this, SLOT(bufferDurationChanged(qint64)));
367
368 CHECKED_CONNECT(m_engine, SIGNAL(dataDurationChanged(qint64)),
369 this, SLOT(dataDurationChanged(qint64)));
370
371 CHECKED_CONNECT(m_engine, SIGNAL(recordPositionChanged(qint64)),
372 m_progressBar, SLOT(recordPositionChanged(qint64)));
373
374 CHECKED_CONNECT(m_engine, SIGNAL(playPositionChanged(qint64)),
375 m_progressBar, SLOT(playPositionChanged(qint64)));
376
377 CHECKED_CONNECT(m_engine, SIGNAL(recordPositionChanged(qint64)),
378 this, SLOT(positionChanged(qint64)));
379
380 CHECKED_CONNECT(m_engine, SIGNAL(playPositionChanged(qint64)),
381 this, SLOT(positionChanged(qint64)));
382
383 CHECKED_CONNECT(m_engine, SIGNAL(levelChanged(qreal, qreal, int)),
384 m_levelMeter, SLOT(levelChanged(qreal, qreal, int)));
385
386 CHECKED_CONNECT(m_engine, SIGNAL(spectrumChanged(qint64, qint64, const FrequencySpectrum &)),
387 this, SLOT(spectrumChanged(qint64, qint64, const FrequencySpectrum &)));
388
389 CHECKED_CONNECT(m_engine, SIGNAL(infoMessage(QString, int)),
390 this, SLOT(infoMessage(QString, int)));
391
392 CHECKED_CONNECT(m_engine, SIGNAL(errorMessage(QString, QString)),
393 this, SLOT(errorMessage(QString, QString)));
394
395 CHECKED_CONNECT(m_spectrograph, SIGNAL(infoMessage(QString, int)),
396 this, SLOT(infoMessage(QString, int)));
397}
398
399void MainWidget::createMenus()
400{
401 m_modeButton->setMenu(m_modeMenu);
402
403 m_generateToneAction = m_modeMenu->addAction(tr("Play generated tone"));
404 m_recordAction = m_modeMenu->addAction(tr("Record and play back"));
405 m_loadFileAction = m_modeMenu->addAction(tr("Play file"));
406
407 m_loadFileAction->setCheckable(true);
408 m_generateToneAction->setCheckable(true);
409 m_recordAction->setCheckable(true);
410
411 connect(m_loadFileAction, SIGNAL(triggered(bool)), this, SLOT(showFileDialog()));
412 connect(m_generateToneAction, SIGNAL(triggered(bool)), this, SLOT(showToneGeneratorDialog()));
413 connect(m_recordAction, SIGNAL(triggered(bool)), this, SLOT(initializeRecord()));
414}
415
416void MainWidget::updateButtonStates()
417{
418 const bool recordEnabled = ((QAudio::AudioOutput == m_engine->mode() ||
419 (QAudio::ActiveState != m_engine->state() &&
420 QAudio::IdleState != m_engine->state())) &&
421 RecordMode == m_mode);
422 m_recordButton->setEnabled(recordEnabled);
423
424 const bool pauseEnabled = (QAudio::ActiveState == m_engine->state() ||
425 QAudio::IdleState == m_engine->state());
426 m_pauseButton->setEnabled(pauseEnabled);
427
428 const bool playEnabled = (m_engine->dataDuration() &&
429 (QAudio::AudioOutput != m_engine->mode() ||
430 (QAudio::ActiveState != m_engine->state() &&
431 QAudio::IdleState != m_engine->state())));
432 m_playButton->setEnabled(playEnabled);
433}
434
435void MainWidget::reset()
436{
437#ifndef DISABLE_WAVEFORM
438 m_waveform->reset();
439#endif
440 m_engine->reset();
441 m_levelMeter->reset();
442 m_spectrograph->reset();
443 m_progressBar->reset();
444}
445
446void MainWidget::setMode(Mode mode)
447{
448
449 m_mode = mode;
450 m_loadFileAction->setChecked(LoadFileMode == mode);
451 m_generateToneAction->setChecked(GenerateToneMode == mode);
452 m_recordAction->setChecked(RecordMode == mode);
453}
454
Note: See TracBrowser for help on using the repository browser.