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

Last change on this file since 1168 was 846, checked in by Dmitry A. Kuminov, 14 years ago

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

File size: 15.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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(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::audioPositionChanged(qint64 position)
170{
171#ifndef DISABLE_WAVEFORM
172 m_waveform->audioPositionChanged(position);
173#else
174 Q_UNUSED(position)
175#endif
176}
177
178void MainWidget::bufferLengthChanged(qint64 length)
179{
180 m_progressBar->bufferLengthChanged(length);
181}
182
183
184//-----------------------------------------------------------------------------
185// Private slots
186//-----------------------------------------------------------------------------
187
188void MainWidget::showFileDialog()
189{
190 const QString dir;
191 const QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open WAV file"), dir, "*.wav");
192 if (fileNames.count()) {
193 reset();
194 setMode(LoadFileMode);
195 m_engine->loadFile(fileNames.front());
196 updateButtonStates();
197 } else {
198 updateModeMenu();
199 }
200}
201
202void MainWidget::showSettingsDialog()
203{
204 m_settingsDialog->exec();
205 if (m_settingsDialog->result() == QDialog::Accepted) {
206 m_engine->setAudioInputDevice(m_settingsDialog->inputDevice());
207 m_engine->setAudioOutputDevice(m_settingsDialog->outputDevice());
208 m_engine->setWindowFunction(m_settingsDialog->windowFunction());
209 }
210}
211
212void MainWidget::showToneGeneratorDialog()
213{
214 m_toneGeneratorDialog->exec();
215 if (m_toneGeneratorDialog->result() == QDialog::Accepted) {
216 reset();
217 setMode(GenerateToneMode);
218 const qreal amplitude = m_toneGeneratorDialog->amplitude();
219 if (m_toneGeneratorDialog->isFrequencySweepEnabled()) {
220 m_engine->generateSweptTone(amplitude);