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 "spectrograph.h"
43 #include <QMouseEvent>
45 #include <QTimerEvent>
47 const int NullTimerId = -1;
48 const int NullIndex = -1;
49 const int BarSelectionInterval = 2000;
51 Spectrograph::Spectrograph(QWidget *parent)
53 , m_barSelected(NullIndex)
54 , m_timerId(NullTimerId)
58 setMinimumHeight(100);
61 Spectrograph::~Spectrograph()
66 void Spectrograph::setParams(int numBars, qreal lowFreq, qreal highFreq)
68 Q_ASSERT(numBars > 0);
69 Q_ASSERT(highFreq > lowFreq);
70 m_bars.resize(numBars);
72 m_highFreq = highFreq;
76 void Spectrograph::timerEvent(QTimerEvent *event)
78 Q_ASSERT(event->timerId() == m_timerId);
79 Q_UNUSED(event) // suppress warnings in release builds
81 m_timerId = NullTimerId;
82 m_barSelected = NullIndex;
86 void Spectrograph::paintEvent(QPaintEvent *event)
90 QPainter painter(this);
91 painter.fillRect(rect(), Qt::black);
93 const int numBars = m_bars.count();
95 // Highlight region of selected bar
96 if (m_barSelected != NullIndex && numBars) {
97 QRect regionRect = rect();
98 regionRect.setLeft(m_barSelected * rect().width() / numBars);
99 regionRect.setWidth(rect().width() / numBars);
100 QColor regionColor(202, 202, 64);
101 painter.setBrush(Qt::DiagCrossPattern);
102 painter.fillRect(regionRect, regionColor);
103 painter.setBrush(Qt::NoBrush);
106 QColor barColor(51, 204, 102);
107 QColor clipColor(255, 255, 0);
110 const QColor gridColor = barColor.darker();
111 QPen gridPen(gridColor);
112 painter.setPen(gridPen);
113 painter.drawLine(rect().topLeft(), rect().topRight());
114 painter.drawLine(rect().topRight(), rect().bottomRight());
115 painter.drawLine(rect().bottomRight(), rect().bottomLeft());
116 painter.drawLine(rect().bottomLeft(), rect().topLeft());
118 QVector<qreal> dashes;
120 gridPen.setDashPattern(dashes);
121 painter.setPen(gridPen);
123 // Draw vertical lines between bars
125 const int numHorizontalSections = numBars;
126 QLine line(rect().topLeft(), rect().bottomLeft());
127 for (int i=1; i<numHorizontalSections; ++i) {
128 line.translate(rect().width()/numHorizontalSections, 0);
129 painter.drawLine(line);
133 // Draw horizontal lines
134 const int numVerticalSections = 10;
135 QLine line(rect().topLeft(), rect().topRight());
136 for (int i=1; i<numVerticalSections; ++i) {
137 line.translate(0, rect().height()/numVerticalSections);
138 painter.drawLine(line);
141 barColor = barColor.lighter();
142 barColor.setAlphaF(0.75);
143 clipColor.setAlphaF(0.75);
147 // Calculate width of bars and gaps
148 const int widgetWidth = rect().width();
149 const int barPlusGapWidth = widgetWidth / numBars;
150 const int barWidth = 0.8 * barPlusGapWidth;
151 const int gapWidth = barPlusGapWidth - barWidth;
152 const int paddingWidth = widgetWidth - numBars * (barWidth + gapWidth);
153 const int leftPaddingWidth = (paddingWidth + gapWidth) / 2;
154 const int barHeight = rect().height() - 2 * gapWidth;
156 for (int i=0; i<numBars; ++i) {
157 const qreal value = m_bars[i].value;
158 Q_ASSERT(value >= 0.0 && value <= 1.0);
160 bar.setLeft(rect().left() + leftPaddingWidth + (i * (gapWidth + barWidth)));
161 bar.setWidth(barWidth);
162 bar.setTop(rect().top() + gapWidth + (1.0 - value) * barHeight);
163 bar.setBottom(rect().bottom() - gapWidth);
165 QColor color = barColor;
166 if (m_bars[i].clipped)
169 painter.fillRect(bar, color);
174 void Spectrograph::mousePressEvent(QMouseEvent *event)
176 const QPoint pos = event->pos();
177 const int index = m_bars.count() * (pos.x() - rect().left()) / rect().width();
181 void Spectrograph::reset()
184 spectrumChanged(m_spectrum);
187 void Spectrograph::spectrumChanged(const FrequencySpectrum &spectrum)
189 m_spectrum = spectrum;
193 int Spectrograph::barIndex(qreal frequency) const
195 Q_ASSERT(frequency >= m_lowFreq && frequency < m_highFreq);
196 const qreal bandWidth = (m_highFreq - m_lowFreq) / m_bars.count();
197 const int index = (frequency - m_lowFreq) / bandWidth;
198 if(index <0 || index >= m_bars.count())
203 QPair<qreal, qreal> Spectrograph::barRange(int index) const
205 Q_ASSERT(index >= 0 && index < m_bars.count());
206 const qreal bandWidth = (m_highFreq - m_lowFreq) / m_bars.count();
207 return QPair<qreal, qreal>(index * bandWidth, (index+1) * bandWidth);
210 void Spectrograph::updateBars()
213 FrequencySpectrum::const_iterator i = m_spectrum.begin();
214 const FrequencySpectrum::const_iterator end = m_spectrum.end();
215 for ( ; i != end; ++i) {
216 const FrequencySpectrum::Element e = *i;
217 if (e.frequency >= m_lowFreq && e.frequency < m_highFreq) {
218 Bar &bar = m_bars[barIndex(e.frequency)];
219 bar.value = qMax(bar.value, e.amplitude);
220 bar.clipped |= e.clipped;
226 void Spectrograph::selectBar(int index) {
227 const QPair<qreal, qreal> frequencyRange = barRange(index);
228 const QString message = QString("%1 - %2 Hz")
229 .arg(frequencyRange.first)
230 .arg(frequencyRange.second);
231 emit infoMessage(message, BarSelectionInterval);
233 if (NullTimerId != m_timerId)
234 killTimer(m_timerId);
235 m_timerId = startTimer(BarSelectionInterval);
237 m_barSelected = index;