| 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 "spectrograph.h" |
| 52 | #include <QDebug> |
| 53 | #include <QMouseEvent> |
| 54 | #include <QPainter> |
| 55 | #include <QTimerEvent> |
| 56 | |
| 57 | const int NullTimerId = -1; |
| 58 | const int NullIndex = -1; |
| 59 | const int BarSelectionInterval = 2000; |
| 60 | |
| 61 | Spectrograph::Spectrograph(QWidget *parent) |
| 62 | : QWidget(parent) |
| 63 | , m_barSelected(NullIndex) |
| 64 | , m_timerId(NullTimerId) |
| 65 | , m_lowFreq(0.0) |
| 66 | , m_highFreq(0.0) |
| 67 | { |
| 68 | setMinimumHeight(100); |
| 69 | } |
| 70 | |
| 71 | Spectrograph::~Spectrograph() |
| 72 | { |
| 73 | |
| 74 | } |
| 75 | |
| 76 | void Spectrograph::setParams(int numBars, qreal lowFreq, qreal highFreq) |
| 77 | { |
| 78 | Q_ASSERT(numBars > 0); |
| 79 | Q_ASSERT(highFreq > lowFreq); |
| 80 | m_bars.resize(asize: numBars); |
| 81 | m_lowFreq = lowFreq; |
| 82 | m_highFreq = highFreq; |
| 83 | updateBars(); |
| 84 | } |
| 85 | |
| 86 | void Spectrograph::timerEvent(QTimerEvent *event) |
| 87 | { |
| 88 | Q_ASSERT(event->timerId() == m_timerId); |
| 89 | Q_UNUSED(event) // suppress warnings in release builds |
| 90 | killTimer(id: m_timerId); |
| 91 | m_timerId = NullTimerId; |
| 92 | m_barSelected = NullIndex; |
| 93 | update(); |
| 94 | } |
| 95 | |
| 96 | void Spectrograph::paintEvent(QPaintEvent *event) |
| 97 | { |
| 98 | Q_UNUSED(event) |
| 99 | |
| 100 | QPainter painter(this); |
| 101 | painter.fillRect(r: rect(), c: Qt::black); |
| 102 | |
| 103 | const int numBars = m_bars.count(); |
| 104 | |
| 105 | // Highlight region of selected bar |
| 106 | if (m_barSelected != NullIndex && numBars) { |
| 107 | QRect regionRect = rect(); |
| 108 | regionRect.setLeft(m_barSelected * rect().width() / numBars); |
| 109 | regionRect.setWidth(rect().width() / numBars); |
| 110 | QColor regionColor(202, 202, 64); |
| 111 | painter.setBrush(Qt::DiagCrossPattern); |
| 112 | painter.fillRect(regionRect, color: regionColor); |
| 113 | painter.setBrush(Qt::NoBrush); |
| 114 | } |
| 115 | |
| 116 | QColor barColor(51, 204, 102); |
| 117 | QColor clipColor(255, 255, 0); |
| 118 | |
| 119 | // Draw the outline |
| 120 | const QColor gridColor = barColor.darker(); |
| 121 | QPen gridPen(gridColor); |
| 122 | painter.setPen(gridPen); |
| 123 | painter.drawLine(p1: rect().topLeft(), p2: rect().topRight()); |
| 124 | painter.drawLine(p1: rect().topRight(), p2: rect().bottomRight()); |
| 125 | painter.drawLine(p1: rect().bottomRight(), p2: rect().bottomLeft()); |
| 126 | painter.drawLine(p1: rect().bottomLeft(), p2: rect().topLeft()); |
| 127 | |
| 128 | QVector<qreal> dashes; |
| 129 | dashes << 2 << 2; |
| 130 | gridPen.setDashPattern(dashes); |
| 131 | painter.setPen(gridPen); |
| 132 | |
| 133 | // Draw vertical lines between bars |
| 134 | if (numBars) { |
| 135 | const int numHorizontalSections = numBars; |
| 136 | QLine line(rect().topLeft(), rect().bottomLeft()); |
| 137 | for (int i=1; i<numHorizontalSections; ++i) { |
| 138 | line.translate(adx: rect().width()/numHorizontalSections, ady: 0); |
| 139 | painter.drawLine(line); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Draw horizontal lines |
| 144 | const int numVerticalSections = 10; |
| 145 | QLine line(rect().topLeft(), rect().topRight()); |
| 146 | for (int i=1; i<numVerticalSections; ++i) { |
| 147 | line.translate(adx: 0, ady: rect().height()/numVerticalSections); |
| 148 | painter.drawLine(line); |
| 149 | } |
| 150 | |
| 151 | barColor = barColor.lighter(); |
| 152 | barColor.setAlphaF(0.75); |
| 153 | clipColor.setAlphaF(0.75); |
| 154 | |
| 155 | // Draw the bars |
| 156 | if (numBars) { |
| 157 | // Calculate width of bars and gaps |
| 158 | const int widgetWidth = rect().width(); |
| 159 | const int barPlusGapWidth = widgetWidth / numBars; |
| 160 | const int barWidth = 0.8 * barPlusGapWidth; |
| 161 | const int gapWidth = barPlusGapWidth - barWidth; |
| 162 | const int paddingWidth = widgetWidth - numBars * (barWidth + gapWidth); |
| 163 | const int leftPaddingWidth = (paddingWidth + gapWidth) / 2; |
| 164 | const int barHeight = rect().height() - 2 * gapWidth; |
| 165 | |
| 166 | for (int i=0; i<numBars; ++i) { |
| 167 | const qreal value = m_bars[i].value; |
| 168 | Q_ASSERT(value >= 0.0 && value <= 1.0); |
| 169 | QRect bar = rect(); |
| 170 | bar.setLeft(rect().left() + leftPaddingWidth + (i * (gapWidth + barWidth))); |
| 171 | bar.setWidth(barWidth); |
| 172 | bar.setTop(rect().top() + gapWidth + (1.0 - value) * barHeight); |
| 173 | bar.setBottom(rect().bottom() - gapWidth); |
| 174 | |
| 175 | QColor color = barColor; |
| 176 | if (m_bars[i].clipped) |
| 177 | color = clipColor; |
| 178 | |
| 179 | painter.fillRect(bar, color); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void Spectrograph::mousePressEvent(QMouseEvent *event) |
| 185 | { |
| 186 | const QPoint pos = event->pos(); |
| 187 | const int index = m_bars.count() * (pos.x() - rect().left()) / rect().width(); |
| 188 | selectBar(index); |
| 189 | } |
| 190 | |
| 191 | void Spectrograph::reset() |
| 192 | { |
| 193 | m_spectrum.reset(); |
| 194 | spectrumChanged(spectrum: m_spectrum); |
| 195 | } |
| 196 | |
| 197 | void Spectrograph::spectrumChanged(const FrequencySpectrum &spectrum) |
| 198 | { |
| 199 | m_spectrum = spectrum; |
| 200 | updateBars(); |
| 201 | } |
| 202 | |
| 203 | int Spectrograph::barIndex(qreal frequency) const |
| 204 | { |
| 205 | Q_ASSERT(frequency >= m_lowFreq && frequency < m_highFreq); |
| 206 | const qreal bandWidth = (m_highFreq - m_lowFreq) / m_bars.count(); |
| 207 | const int index = (frequency - m_lowFreq) / bandWidth; |
| 208 | if (index <0 || index >= m_bars.count()) |
| 209 | Q_ASSERT(false); |
| 210 | return index; |
| 211 | } |
| 212 | |
| 213 | QPair<qreal, qreal> Spectrograph::barRange(int index) const |
| 214 | { |
| 215 | Q_ASSERT(index >= 0 && index < m_bars.count()); |
| 216 | const qreal bandWidth = (m_highFreq - m_lowFreq) / m_bars.count(); |
| 217 | return QPair<qreal, qreal>(index * bandWidth, (index+1) * bandWidth); |
| 218 | } |
| 219 | |
| 220 | void Spectrograph::updateBars() |
| 221 | { |
| 222 | m_bars.fill(from: Bar()); |
| 223 | FrequencySpectrum::const_iterator i = m_spectrum.begin(); |
| 224 | const FrequencySpectrum::const_iterator end = m_spectrum.end(); |
| 225 | for ( ; i != end; ++i) { |
| 226 | const FrequencySpectrum::Element e = *i; |
| 227 | if (e.frequency >= m_lowFreq && e.frequency < m_highFreq) { |
| 228 | Bar &bar = m_bars[barIndex(frequency: e.frequency)]; |
| 229 | bar.value = qMax(a: bar.value, b: e.amplitude); |
| 230 | bar.clipped |= e.clipped; |
| 231 | } |
| 232 | } |
| 233 | update(); |
| 234 | } |
| 235 | |
| 236 | void Spectrograph::selectBar(int index) { |
| 237 | const QPair<qreal, qreal> frequencyRange = barRange(index); |
| 238 | const QString message = QString("%1 - %2 Hz" ) |
| 239 | .arg(a: frequencyRange.first) |
| 240 | .arg(a: frequencyRange.second); |
| 241 | emit infoMessage(message, intervalMs: BarSelectionInterval); |
| 242 | |
| 243 | if (NullTimerId != m_timerId) |
| 244 | killTimer(id: m_timerId); |
| 245 | m_timerId = startTimer(interval: BarSelectionInterval); |
| 246 | |
| 247 | m_barSelected = index; |
| 248 | update(); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | |