1
//=============================================================================
3
// Audio Widget Library
6
// Copyright (C) 2002-2006 by Werner Schweer and others
8
// This program is free software; you can redistribute it and/or modify
9
// it under the terms of the GNU General Public License version 2.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
//=============================================================================
24
#include <QMouseEvent>
29
//---------------------------------------------------------
31
//---------------------------------------------------------
33
MeterSlider::MeterSlider(QWidget* parent)
36
setAttribute(Qt::WA_NoSystemBackground, true);
37
// setAutoFillBackground(true);
39
yellowScale = -16; //-10;
41
_meterWidth = _scaleWidth * 3;
46
//---------------------------------------------------------
48
//---------------------------------------------------------
50
QSize MeterSlider::sizeHint() const
52
int w = _meterWidth + _scaleWidth + _scaleWidth + 30;
53
return orientation() == Qt::Vertical ? QSize(w, 200) : QSize(200, w);
56
//---------------------------------------------------------
58
//---------------------------------------------------------
60
void MeterSlider::setChannel(int n)
63
for (int i = _channel; i < n; ++i) {
64
meterval.push_back(0.0f);
65
meterPeak.push_back(0.0f);
71
//---------------------------------------------------------
73
//---------------------------------------------------------
75
void MeterSlider::setMeterVal(int channel, double v, double peak)
77
bool mustRedraw = false;
78
if (meterval[channel] != v) {
79
meterval[channel] = v;
82
if (peak != meterPeak[channel]) {
83
meterPeak[channel] = peak;
87
int kh = sliderSize().height();
88
int mh = height() - kh;
89
update(20, kh / 2, _meterWidth-1, mh);
93
//---------------------------------------------------------
95
// reset peak and overflow indicator
96
//---------------------------------------------------------
98
void MeterSlider::resetPeaks()
100
for (int i = 0; i < _channel; ++i)
101
meterPeak[i] = meterval[i];
105
//---------------------------------------------------------
107
//---------------------------------------------------------
109
void MeterSlider::resizeEvent(QResizeEvent* /*ev*/)
112
int kh = sliderSize().height();
114
int mw = _meterWidth / _channel;
116
onPm = QPixmap(mw, mh);
117
offPm = QPixmap(mw, mh);
119
double range = maxValue() - minValue();
120
int h1 = mh - lrint((maxValue() - redScale) * mh / range);
121
int h2 = mh - lrint((maxValue() - yellowScale) * mh / range);
124
yellowRed.setHsv(QColor(Qt::yellow).hue()-8,
125
QColor(Qt::yellow).saturation(),
126
QColor(Qt::yellow).value());
128
yellRedRed.setHsv(QColor(Qt::yellow).hue()-16,
129
QColor(Qt::yellow).saturation(),
130
QColor(Qt::yellow).value());
132
QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, mh));
133
linearGrad.setColorAt(0, Qt::red);
134
linearGrad.setColorAt(1-(double)(h1-5)/(double)mh, yellRedRed);
135
linearGrad.setColorAt(1-(double)(h1-6)/(double)mh, yellowRed);
136
linearGrad.setColorAt(1-(double)h2/(double)mh, Qt::yellow);
137
linearGrad.setColorAt(1, Qt::green);
139
QColor darkYellowRed;
140
darkYellowRed.setHsv(QColor(Qt::darkYellow).hue()-8,
141
QColor(Qt::darkYellow).saturation(),
142
QColor(Qt::darkYellow).value());
143
QColor darkYellRedRed;
144
darkYellRedRed.setHsv(QColor(Qt::darkYellow).hue()-16,
145
QColor(Qt::darkYellow).saturation(),
146
QColor(Qt::darkYellow).value());
147
QLinearGradient linearDarkGrad(QPointF(0, 0), QPointF(0, mh));
148
linearDarkGrad.setColorAt(0, Qt::darkRed);
149
linearDarkGrad.setColorAt(1-(double)(h1-5)/(double)mh, darkYellRedRed);
150
linearDarkGrad.setColorAt(1-(double)(h1-6)/(double)mh, darkYellowRed);
151
linearDarkGrad.setColorAt(1-(double)h2/(double)mh, Qt::darkYellow);
152
linearDarkGrad.setColorAt(1, Qt::darkGreen);
156
p.fillRect(0, 0, mw, mh, linearGrad);
159
p.fillRect(0, 0, mw, mh, linearDarkGrad);
163
//---------------------------------------------------------
165
//---------------------------------------------------------
167
void MeterSlider::paintEvent(QPaintEvent* ev)
169
int pixel = height() - sliderSize().height();
170
double range = maxValue() - minValue();
171
int ppos = int(pixel * (_value - minValue()) / range);
176
p.setRenderHint(QPainter::Antialiasing, false);
179
int kh = sliderSize().height();
181
//---------------------------------------------------
183
//---------------------------------------------------
185
int mw = _meterWidth / _channel;
191
p.setPen(QPen(Qt::white, 2));
193
for (int i = 0; i < _channel; ++i) {
194
int h = mh - (lrint(fast_log10(meterval[i]) * -20.0f * mh / range));
200
p.drawPixmap(x, y1+mh-h, mw, h, onPm, 0, y1+mh-h, mw, h);
201
p.drawPixmap(x, y1, mw, mh-h, offPm, 0, y1, mw, mh-h);
203
//---------------------------------------------------
205
//---------------------------------------------------
207
h = mh - (lrint(fast_log10(meterPeak[i]) * -20.0f * mh / range));
211
p.drawLine(x, y3-h, x+mw, y3-h);
216
// optimize common case:
217
if (ev->rect() == QRect(20, kh/2, _meterWidth-1, mh))
220
QColor sc(isEnabled() ? _scaleColor : Qt::gray);
221
QColor svc(isEnabled() ? _scaleValueColor : Qt::gray);
224
//---------------------------------------------------
226
//---------------------------------------------------
228
int y2 = h - (ppos + y1);
229
p.fillRect(x, y1, _scaleWidth, y2-y1, sc);
230
p.fillRect(x, y2, _scaleWidth, y3-y2, svc);
232
//---------------------------------------------------
234
//---------------------------------------------------
239
p.setPen(QPen(Qt::darkGray, 2));
241
int xt = 20 - fm.width("00") - 5;
244
for (int i = 10; i < 70; i += 10) {
245
h = y1 + lrint(i * mh / range);
247
p.drawText(xt, h - 3, s);
248
p.drawLine(15, h, 20, h);
251
//---------------------------------------------------
253
//---------------------------------------------------
256
p.setPen(QPen(svc, 0));
257
p.translate(QPointF(x, y2));
258
p.setRenderHint(QPainter::Antialiasing, true);
262
//---------------------------------------------------------
264
//---------------------------------------------------------
266
void MeterSlider::mousePressEvent(QMouseEvent* ev)
268
if (ev->pos().x() < _meterWidth) {
272
VolSlider::mousePressEvent(ev);