Update copyright headers
[qt:qt.git] / demos / spectrum / app / settingsdialog.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "settingsdialog.h"
42 #include <QComboBox>
43 #include <QDialogButtonBox>
44 #include <QLabel>
45 #include <QPushButton>
46 #include <QVBoxLayout>
47 #include <QCheckBox>
48 #include <QSlider>
49 #include <QSpinBox>
50
51 SettingsDialog::SettingsDialog(
52             const QList<QAudioDeviceInfo> &availableInputDevices,
53             const QList<QAudioDeviceInfo> &availableOutputDevices,
54             QWidget *parent)
55     :   QDialog(parent)
56     ,   m_windowFunction(DefaultWindowFunction)
57     ,   m_inputDeviceComboBox(new QComboBox(this))
58     ,   m_outputDeviceComboBox(new QComboBox(this))
59     ,   m_windowFunctionComboBox(new QComboBox(this))
60 {
61     QVBoxLayout *dialogLayout = new QVBoxLayout(this);
62
63     // Populate combo boxes
64
65     QAudioDeviceInfo device;
66     foreach (device, availableInputDevices)
67         m_inputDeviceComboBox->addItem(device.deviceName(),
68                                        QVariant::fromValue(device));
69     foreach (device, availableOutputDevices)
70         m_outputDeviceComboBox->addItem(device.deviceName(),
71                                        QVariant::fromValue(device));
72
73     m_windowFunctionComboBox->addItem(tr("None"), QVariant::fromValue(int(NoWindow)));
74     m_windowFunctionComboBox->addItem("Hann", QVariant::fromValue(int(HannWindow)));
75     m_windowFunctionComboBox->setCurrentIndex(m_windowFunction);
76
77     // Initialize default devices
78     if (!availableInputDevices.empty())
79         m_inputDevice = availableInputDevices.front();
80     if (!availableOutputDevices.empty())
81         m_outputDevice = availableOutputDevices.front();
82
83     // Add widgets to layout
84
85     QScopedPointer<QHBoxLayout> inputDeviceLayout(new QHBoxLayout);
86     QLabel *inputDeviceLabel = new QLabel(tr("Input device"), this);
87     inputDeviceLayout->addWidget(inputDeviceLabel);
88     inputDeviceLayout->addWidget(m_inputDeviceComboBox);
89     dialogLayout->addLayout(inputDeviceLayout.data());
90     inputDeviceLayout.take(); // ownership transferred to dialogLayout
91
92     QScopedPointer<QHBoxLayout> outputDeviceLayout(new QHBoxLayout);
93     QLabel *outputDeviceLabel = new QLabel(tr("Output device"), this);
94     outputDeviceLayout->addWidget(outputDeviceLabel);
95     outputDeviceLayout->addWidget(m_outputDeviceComboBox);
96     dialogLayout->addLayout(outputDeviceLayout.data());
97     outputDeviceLayout.take(); // ownership transferred to dialogLayout
98
99     QScopedPointer<QHBoxLayout> windowFunctionLayout(new QHBoxLayout);
100     QLabel *windowFunctionLabel = new QLabel(tr("Window function"), this);
101     windowFunctionLayout->addWidget(windowFunctionLabel);
102     windowFunctionLayout->addWidget(m_windowFunctionComboBox);
103     dialogLayout->addLayout(windowFunctionLayout.data());
104     windowFunctionLayout.take(); // ownership transferred to dialogLayout
105
106     // Connect
107     CHECKED_CONNECT(m_inputDeviceComboBox, SIGNAL(activated(int)),
108                     this, SLOT(inputDeviceChanged(int)));
109     CHECKED_CONNECT(m_outputDeviceComboBox, SIGNAL(activated(int)),
110                     this, SLOT(outputDeviceChanged(int)));
111     CHECKED_CONNECT(m_windowFunctionComboBox, SIGNAL(activated(int)),
112                     this, SLOT(windowFunctionChanged(int)));
113
114     // Add standard buttons to layout
115     QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
116     buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
117     dialogLayout->addWidget(buttonBox);
118
119     // Connect standard buttons
120     CHECKED_CONNECT(buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
121                     this, SLOT(accept()));
122     CHECKED_CONNECT(buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()),
123                     this, SLOT(reject()));
124
125     setLayout(dialogLayout);
126 }
127
128 SettingsDialog::~SettingsDialog()
129 {
130
131 }
132
133 void SettingsDialog::windowFunctionChanged(int index)
134 {
135     m_windowFunction = static_cast<WindowFunction>(
136             m_windowFunctionComboBox->itemData(index).value<int>());
137 }
138
139 void SettingsDialog::inputDeviceChanged(int index)
140 {
141     m_inputDevice = m_inputDeviceComboBox->itemData(index).value<QAudioDeviceInfo>();
142 }
143
144 void SettingsDialog::outputDeviceChanged(int index)
145 {
146     m_outputDevice = m_outputDeviceComboBox->itemData(index).value<QAudioDeviceInfo>();
147 }
148