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 "spectrograph.h"
42 #include <QPainter>
43 #include <QMouseEvent>
44 #include <QDebug>
45 #include <QTimerEvent>
46 
47 const int NullTimerId = -1;
48 const int NullIndex = -1;
49 const int BarSelectionInterval = 2000;
50 
Spectrograph(QWidget * parent)51 Spectrograph::Spectrograph(QWidget *parent)
52     :   QWidget(parent)
53     ,   m_barSelected(NullIndex)
54     ,   m_timerId(NullTimerId)
55     ,   m_lowFreq(0.0)
56     ,   m_highFreq(0.0)
57 {
58     setMinimumHeight(100);
59 }
60 
~Spectrograph()61 Spectrograph::~Spectrograph()
62 {
63 
64 }
65 
setParams(int numBars,qreal lowFreq,qreal highFreq)66 void Spectrograph::setParams(int numBars, qreal lowFreq, qreal highFreq)
67 {
68     Q_ASSERT(numBars > 0);
69     Q_ASSERT(highFreq > lowFreq);
70     m_bars.resize(numBars);
71     m_lowFreq = lowFreq;
72     m_highFreq = highFreq;
73     updateBars();
74 }
75 
timerEvent(QTimerEvent * event)76 void Spectrograph::timerEvent(QTimerEvent *event)
77 {
78     Q_ASSERT(event->timerId() == m_timerId);
79     Q_UNUSED(event) // suppress warnings in release builds
80     killTimer(m_timerId);
81     m_timerId = NullTimerId;
82     m_barSelected = NullIndex;
83     update();
84 }
85 
paintEvent(QPaintEvent * event)86 void Spectrograph::paintEvent(QPaintEvent *event)
87 {
88     Q_UNUSED(event)
89 
90     QPainter painter(this);
91     painter.fillRect(rect(), Qt::black);
92 
93     const int numBars = m_bars.count();
94 
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);
104     }
105 
106     QColor barColor(51, 204, 102);
107     QColor clipColor(255, 255, 0);
108 
109     // Draw the outline
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());
117 
118     QVector<qreal> dashes;
119     dashes << 2 << 2;
120     gridPen.setDashPattern(dashes);
121     painter.setPen(gridPen);
122 
123     // Draw vertical lines between bars
124     if (numBars) {
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);
130         }
131     }
132 
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);
139     }
140 
141     barColor = barColor.lighter();
142     barColor.setAlphaF(0.75);
143     clipColor.setAlphaF(0.75);
144 
145     // Draw the bars
146     if (numBars) {
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;
155 
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);
159             QRect bar = rect();
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);
164 
165             QColor color = barColor;
166             if (m_bars[i].clipped)
167                 color = clipColor;
168 
169             painter.fillRect(bar, color);
170         }
171     }
172 }
173 
mousePressEvent(QMouseEvent * event)174 void Spectrograph::mousePressEvent(QMouseEvent *event)
175 {
176     const QPoint pos = event->pos();
177     const int index = m_bars.count() * (pos.x() - rect().left()) / rect().width();
178     selectBar(index);
179 }
180 
reset()181 void Spectrograph::reset()
182 {
183     m_spectrum.reset();
184     spectrumChanged(m_spectrum);
185 }
186 
spectrumChanged(const FrequencySpectrum & spectrum)187 void Spectrograph::spectrumChanged(const FrequencySpectrum &spectrum)
188 {
189     m_spectrum = spectrum;
190     updateBars();
191 }
192 
barIndex(qreal frequency) const193 int Spectrograph::barIndex(qreal frequency) const
194 {
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())
199         Q_ASSERT(false);
200     return index;
201 }
202 
barRange(int index) const203 QPair<qreal, qreal> Spectrograph::barRange(int index) const
204 {
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);
208 }
209 
updateBars()210 void Spectrograph::updateBars()
211 {
212     m_bars.fill(Bar());
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;
221         }
222     }
223     update();
224 }
225 
selectBar(int index)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);
232 
233     if (NullTimerId != m_timerId)
234         killTimer(m_timerId);
235     m_timerId = startTimer(BarSelectionInterval);
236 
237     m_barSelected = index;
238     update();
239 }
240 
241 
242