1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
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
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.
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."
39 ****************************************************************************/
41 #include "tonegeneratordialog.h"
43 #include <QDialogButtonBox>
45 #include <QPushButton>
46 #include <QVBoxLayout>
51 const int ToneGeneratorFreqMin = 1;
52 const int ToneGeneratorFreqMax = 1000;
53 const int ToneGeneratorFreqDefault = 440;
54 const int ToneGeneratorAmplitudeDefault = 75;
56 ToneGeneratorDialog::ToneGeneratorDialog(QWidget *parent)
58 , m_toneGeneratorSweepCheckBox(new QCheckBox(tr("Frequency sweep"), this))
59 , m_frequencySweepEnabled(true)
60 , m_toneGeneratorControl(new QWidget(this))
61 , m_toneGeneratorFrequencyControl(new QWidget(this))
62 , m_frequencySlider(new QSlider(Qt::Horizontal, this))
63 , m_frequencySpinBox(new QSpinBox(this))
64 , m_frequency(ToneGeneratorFreqDefault)
65 , m_amplitudeSlider(new QSlider(Qt::Horizontal, this))
67 QVBoxLayout *dialogLayout = new QVBoxLayout(this);
69 m_toneGeneratorSweepCheckBox->setChecked(true);
71 // Configure tone generator controls
72 m_frequencySlider->setRange(ToneGeneratorFreqMin, ToneGeneratorFreqMax);
73 m_frequencySlider->setValue(ToneGeneratorFreqDefault);
74 m_frequencySpinBox->setRange(ToneGeneratorFreqMin, ToneGeneratorFreqMax);
75 m_frequencySpinBox->setValue(ToneGeneratorFreqDefault);
76 m_amplitudeSlider->setRange(0, 100);
77 m_amplitudeSlider->setValue(ToneGeneratorAmplitudeDefault);
79 // Add widgets to layout
81 QScopedPointer<QGridLayout> frequencyControlLayout(new QGridLayout);
82 QLabel *frequencyLabel = new QLabel(tr("Frequency (Hz)"), this);
83 frequencyControlLayout->addWidget(frequencyLabel, 0, 0, 2, 1);
84 frequencyControlLayout->addWidget(m_frequencySlider, 0, 1);
85 frequencyControlLayout->addWidget(m_frequencySpinBox, 1, 1);
86 m_toneGeneratorFrequencyControl->setLayout(frequencyControlLayout.data());
87 frequencyControlLayout.take(); // ownership transferred to m_toneGeneratorFrequencyControl
88 m_toneGeneratorFrequencyControl->setEnabled(false);
90 QScopedPointer<QGridLayout> toneGeneratorLayout(new QGridLayout);
91 QLabel *amplitudeLabel = new QLabel(tr("Amplitude"), this);
92 toneGeneratorLayout->addWidget(m_toneGeneratorSweepCheckBox, 0, 1);
93 toneGeneratorLayout->addWidget(m_toneGeneratorFrequencyControl, 1, 0, 1, 2);
94 toneGeneratorLayout->addWidget(amplitudeLabel, 2, 0);
95 toneGeneratorLayout->addWidget(m_amplitudeSlider, 2, 1);
96 m_toneGeneratorControl->setLayout(toneGeneratorLayout.data());
97 m_toneGeneratorControl->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
98 dialogLayout->addWidget(m_toneGeneratorControl);
99 toneGeneratorLayout.take(); // ownership transferred
102 CHECKED_CONNECT(m_toneGeneratorSweepCheckBox, SIGNAL(toggled(bool)),
103 this, SLOT(frequencySweepEnabled(bool)));
104 CHECKED_CONNECT(m_frequencySlider, SIGNAL(valueChanged(int)),
105 m_frequencySpinBox, SLOT(setValue(int)));
106 CHECKED_CONNECT(m_frequencySpinBox, SIGNAL(valueChanged(int)),
107 m_frequencySlider, SLOT(setValue(int)));
109 // Add standard buttons to layout
110 QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
111 buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
112 dialogLayout->addWidget(buttonBox);
114 // Connect standard buttons
115 CHECKED_CONNECT(buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
116 this, SLOT(accept()));
117 CHECKED_CONNECT(buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()),
118 this, SLOT(reject()));
120 setLayout(dialogLayout);
123 ToneGeneratorDialog::~ToneGeneratorDialog()
128 bool ToneGeneratorDialog::isFrequencySweepEnabled() const
130 return m_toneGeneratorSweepCheckBox->isChecked();
133 qreal ToneGeneratorDialog::frequency() const
135 return qreal(m_frequencySlider->value());
138 qreal ToneGeneratorDialog::amplitude() const
140 return qreal(m_amplitudeSlider->value()) / 100.0;
143 void ToneGeneratorDialog::frequencySweepEnabled(bool enabled)
145 m_frequencySweepEnabled = enabled;
146 m_toneGeneratorFrequencyControl->setEnabled(!enabled);