| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "tonegeneratordialog.h" |
| 52 | #include <QComboBox> |
| 53 | #include <QDialogButtonBox> |
| 54 | #include <QLabel> |
| 55 | #include <QPushButton> |
| 56 | #include <QVBoxLayout> |
| 57 | #include <QCheckBox> |
| 58 | #include <QSlider> |
| 59 | #include <QSpinBox> |
| 60 | |
| 61 | const int ToneGeneratorFreqMin = 1; |
| 62 | const int ToneGeneratorFreqMax = 1000; |
| 63 | const int ToneGeneratorFreqDefault = 440; |
| 64 | const int ToneGeneratorAmplitudeDefault = 75; |
| 65 | |
| 66 | ToneGeneratorDialog::ToneGeneratorDialog(QWidget *parent) |
| 67 | : QDialog(parent) |
| 68 | , m_toneGeneratorSweepCheckBox(new QCheckBox(tr(s: "Frequency sweep" ), this)) |
| 69 | , m_frequencySweepEnabled(true) |
| 70 | , m_toneGeneratorControl(new QWidget(this)) |
| 71 | , m_toneGeneratorFrequencyControl(new QWidget(this)) |
| 72 | , m_frequencySlider(new QSlider(Qt::Horizontal, this)) |
| 73 | , m_frequencySpinBox(new QSpinBox(this)) |
| 74 | , m_frequency(ToneGeneratorFreqDefault) |
| 75 | , m_amplitudeSlider(new QSlider(Qt::Horizontal, this)) |
| 76 | { |
| 77 | QVBoxLayout *dialogLayout = new QVBoxLayout(this); |
| 78 | |
| 79 | m_toneGeneratorSweepCheckBox->setChecked(true); |
| 80 | |
| 81 | // Configure tone generator controls |
| 82 | m_frequencySlider->setRange(min: ToneGeneratorFreqMin, max: ToneGeneratorFreqMax); |
| 83 | m_frequencySlider->setValue(ToneGeneratorFreqDefault); |
| 84 | m_frequencySpinBox->setRange(min: ToneGeneratorFreqMin, max: ToneGeneratorFreqMax); |
| 85 | m_frequencySpinBox->setValue(ToneGeneratorFreqDefault); |
| 86 | m_amplitudeSlider->setRange(min: 0, max: 100); |
| 87 | m_amplitudeSlider->setValue(ToneGeneratorAmplitudeDefault); |
| 88 | |
| 89 | // Add widgets to layout |
| 90 | QGridLayout *frequencyControlLayout = new QGridLayout; |
| 91 | QLabel *frequencyLabel = new QLabel(tr(s: "Frequency (Hz)" ), this); |
| 92 | frequencyControlLayout->addWidget(frequencyLabel, row: 0, column: 0, rowSpan: 2, columnSpan: 1); |
| 93 | frequencyControlLayout->addWidget(m_frequencySlider, row: 0, column: 1); |
| 94 | frequencyControlLayout->addWidget(m_frequencySpinBox, row: 1, column: 1); |
| 95 | m_toneGeneratorFrequencyControl->setLayout(frequencyControlLayout); |
| 96 | m_toneGeneratorFrequencyControl->setEnabled(false); |
| 97 | |
| 98 | QGridLayout *toneGeneratorLayout = new QGridLayout; |
| 99 | QLabel *amplitudeLabel = new QLabel(tr(s: "Amplitude" ), this); |
| 100 | toneGeneratorLayout->addWidget(m_toneGeneratorSweepCheckBox, row: 0, column: 1); |
| 101 | toneGeneratorLayout->addWidget(m_toneGeneratorFrequencyControl, row: 1, column: 0, rowSpan: 1, columnSpan: 2); |
| 102 | toneGeneratorLayout->addWidget(amplitudeLabel, row: 2, column: 0); |
| 103 | toneGeneratorLayout->addWidget(m_amplitudeSlider, row: 2, column: 1); |
| 104 | m_toneGeneratorControl->setLayout(toneGeneratorLayout); |
| 105 | m_toneGeneratorControl->setSizePolicy(hor: QSizePolicy::Preferred, ver: QSizePolicy::Fixed); |
| 106 | dialogLayout->addWidget(m_toneGeneratorControl); |
| 107 | |
| 108 | // Connect |
| 109 | connect(sender: m_toneGeneratorSweepCheckBox, signal: &QCheckBox::toggled, |
| 110 | receiver: this, slot: &ToneGeneratorDialog::frequencySweepEnabled); |
| 111 | connect(sender: m_frequencySlider, signal: &QSlider::valueChanged, |
| 112 | receiver: m_frequencySpinBox, slot: &QSpinBox::setValue); |
| 113 | connect(sender: m_frequencySpinBox, signal: QOverload<int>::of(ptr: &QSpinBox::valueChanged), |
| 114 | receiver: m_frequencySlider, slot: &QSlider::setValue); |
| 115 | |
| 116 | // Add standard buttons to layout |
| 117 | QDialogButtonBox *buttonBox = new QDialogButtonBox(this); |
| 118 | buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 119 | dialogLayout->addWidget(buttonBox); |
| 120 | |
| 121 | // Connect standard buttons |
| 122 | connect(sender: buttonBox->button(which: QDialogButtonBox::Ok), signal: &QPushButton::clicked, |
| 123 | receiver: this, slot: &ToneGeneratorDialog::accept); |
| 124 | connect(sender: buttonBox->button(which: QDialogButtonBox::Cancel), signal: &QPushButton::clicked, |
| 125 | receiver: this, slot: &ToneGeneratorDialog::reject); |
| 126 | |
| 127 | setLayout(dialogLayout); |
| 128 | } |
| 129 | |
| 130 | ToneGeneratorDialog::~ToneGeneratorDialog() |
| 131 | { |
| 132 | |
| 133 | } |
| 134 | |
| 135 | bool ToneGeneratorDialog::isFrequencySweepEnabled() const |
| 136 | { |
| 137 | return m_toneGeneratorSweepCheckBox->isChecked(); |
| 138 | } |
| 139 | |
| 140 | qreal ToneGeneratorDialog::frequency() const |
| 141 | { |
| 142 | return qreal(m_frequencySlider->value()); |
| 143 | } |
| 144 | |
| 145 | qreal ToneGeneratorDialog::amplitude() const |
| 146 | { |
| 147 | return qreal(m_amplitudeSlider->value()) / 100.0; |
| 148 | } |
| 149 | |
| 150 | void ToneGeneratorDialog::frequencySweepEnabled(bool enabled) |
| 151 | { |
| 152 | m_frequencySweepEnabled = enabled; |
| 153 | m_toneGeneratorFrequencyControl->setEnabled(!enabled); |
| 154 | } |
| 155 | |