~ubuntu-branches/ubuntu/quantal/muse/quantal

« back to all changes in this revision

Viewing changes to awl/mslider.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2011-08-12 11:16:41 UTC
  • mto: (1.1.9) (10.1.6 sid)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: james.westby@ubuntu.com-20110812111641-72iatqb9jomjejko
ImportĀ upstreamĀ versionĀ 2.0~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//=============================================================================
 
2
//  Awl
 
3
//  Audio Widget Library
 
4
//  $Id:$
 
5
//
 
6
//  Copyright (C) 2002-2006 by Werner Schweer and others
 
7
//
 
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.
 
10
//
 
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.
 
15
//
 
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
//=============================================================================
 
20
 
 
21
#include "fastlog.h"
 
22
#include "mslider.h"
 
23
 
 
24
#include <QMouseEvent>
 
25
#include <QPainter>
 
26
 
 
27
namespace Awl {
 
28
 
 
29
//---------------------------------------------------------
 
30
//   MeterSlider
 
31
//---------------------------------------------------------
 
32
 
 
33
MeterSlider::MeterSlider(QWidget* parent)
 
34
   : VolSlider(parent)
 
35
      {
 
36
      setAttribute(Qt::WA_NoSystemBackground, true);
 
37
//      setAutoFillBackground(true);
 
38
      _channel    = 0;
 
39
      yellowScale = -16; //-10;
 
40
      redScale    = 0;
 
41
      _meterWidth = _scaleWidth * 3;
 
42
      setChannel(1);
 
43
      setMinimumHeight(50);
 
44
      }
 
45
 
 
46
//---------------------------------------------------------
 
47
//   sizeHint
 
48
//---------------------------------------------------------
 
49
 
 
50
QSize MeterSlider::sizeHint() const
 
51
        {
 
52
      int w = _meterWidth + _scaleWidth + _scaleWidth + 30;
 
53
        return orientation() == Qt::Vertical ? QSize(w, 200) : QSize(200, w);
 
54
      }
 
55
 
 
56
//---------------------------------------------------------
 
57
//   setChannel
 
58
//---------------------------------------------------------
 
59
 
 
60
void MeterSlider::setChannel(int n)
 
61
      {
 
62
      if (n > _channel) {
 
63
            for (int i = _channel; i < n; ++i) {
 
64
                  meterval.push_back(0.0f);
 
65
                  meterPeak.push_back(0.0f);
 
66
                  }
 
67
            }
 
68
      _channel = n;
 
69
      }
 
70
 
 
71
//---------------------------------------------------------
 
72
//   setMeterVal
 
73
//---------------------------------------------------------
 
74
 
 
75
void MeterSlider::setMeterVal(int channel, double v, double peak)
 
76
      {
 
77
      bool mustRedraw = false;
 
78
      if (meterval[channel] != v) {
 
79
            meterval[channel] = v;
 
80
            mustRedraw = true;
 
81
            }
 
82
      if (peak != meterPeak[channel]) {
 
83
            meterPeak[channel] = peak;
 
84
            mustRedraw = true;
 
85
            }
 
86
      if (mustRedraw) {
 
87
            int kh = sliderSize().height();
 
88
            int mh = height() - kh;
 
89
            update(20, kh / 2, _meterWidth-1, mh);
 
90
            }
 
91
      }
 
92
 
 
93
//---------------------------------------------------------
 
94
//   resetPeaks
 
95
//    reset peak and overflow indicator
 
96
//---------------------------------------------------------
 
97
 
 
98
void MeterSlider::resetPeaks()
 
99
      {
 
100
      for (int i = 0; i < _channel; ++i)
 
101
            meterPeak[i]  = meterval[i];
 
102
      update();
 
103
      }
 
104
 
 
105
//---------------------------------------------------------
 
106
//   resizeEvent
 
107
//---------------------------------------------------------
 
108
 
 
109
void MeterSlider::resizeEvent(QResizeEvent* /*ev*/)
 
110
      {
 
111
      int h  = height();
 
112
      int kh = sliderSize().height();
 
113
      int mh  = h - kh;
 
114
      int mw = _meterWidth / _channel;
 
115
 
 
116
      onPm  = QPixmap(mw, mh);
 
117
      offPm = QPixmap(mw, mh);
 
118
 
 
119
      double range = maxValue() - minValue();
 
120
      int h1 = mh - lrint((maxValue() - redScale) * mh / range);
 
121
      int h2 = mh - lrint((maxValue() - yellowScale) * mh / range);
 
122
 
 
123
      QColor yellowRed;
 
124
        yellowRed.setHsv(QColor(Qt::yellow).hue()-8,
 
125
                     QColor(Qt::yellow).saturation(),
 
126
                     QColor(Qt::yellow).value());
 
127
        QColor yellRedRed;
 
128
        yellRedRed.setHsv(QColor(Qt::yellow).hue()-16,
 
129
                      QColor(Qt::yellow).saturation(),
 
130
                      QColor(Qt::yellow).value());
 
131
 
 
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);
 
138
 
 
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);
 
153
 
 
154
      QPainter p;
 
155
      p.begin(&onPm);
 
156
      p.fillRect(0, 0, mw, mh, linearGrad);
 
157
      p.end();
 
158
      p.begin(&offPm);
 
159
        p.fillRect(0, 0, mw, mh, linearDarkGrad);
 
160
      p.end();
 
161
      }
 
162
 
 
163
//---------------------------------------------------------
 
164
//   paintEvent
 
165
//---------------------------------------------------------
 
166
 
 
167
void MeterSlider::paintEvent(QPaintEvent* ev)
 
168
      {
 
169
      int pixel    = height() - sliderSize().height();
 
170
      double range = maxValue() - minValue();
 
171
      int ppos     = int(pixel * (_value - minValue()) / range);
 
172
      if (_invert)
 
173
            ppos = pixel - ppos;
 
174
 
 
175
      QPainter p(this);
 
176
      p.setRenderHint(QPainter::Antialiasing, false);
 
177
 
 
178
      int h  = height();
 
179
      int kh = sliderSize().height();
 
180
 
 
181
      //---------------------------------------------------
 
182
      //    draw meter
 
183
      //---------------------------------------------------
 
184
 
 
185
      int mw = _meterWidth / _channel;
 
186
      int x  = 20;
 
187
      int y1 = kh / 2;
 
188
      int y3 = h - y1;
 
189
 
 
190
      int mh  = h - kh;
 
191
      p.setPen(QPen(Qt::white, 2));
 
192
 
 
193
      for (int i = 0; i < _channel; ++i) {
 
194
            int h = mh - (lrint(fast_log10(meterval[i]) * -20.0f * mh / range));
 
195
            if (h < 0)
 
196
                  h = 0;
 
197
            else if (h > mh)
 
198
                  h = mh;
 
199
 
 
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);
 
202
 
 
203
            //---------------------------------------------------
 
204
            //    draw peak line
 
205
            //---------------------------------------------------
 
206
 
 
207
            h = mh - (lrint(fast_log10(meterPeak[i]) * -20.0f * mh / range));
 
208
            if (h > mh)
 
209
                  h = mh;
 
210
              if (h > 0)
 
211
                    p.drawLine(x, y3-h, x+mw, y3-h);
 
212
 
 
213
            x += mw;
 
214
            }
 
215
 
 
216
      // optimize common case:
 
217
      if (ev->rect() == QRect(20, kh/2, _meterWidth-1, mh))
 
218
            return;
 
219
 
 
220
      QColor sc(isEnabled() ? _scaleColor : Qt::gray);
 
221
      QColor svc(isEnabled() ? _scaleValueColor : Qt::gray);
 
222
      p.setBrush(svc);
 
223
 
 
224
      //---------------------------------------------------
 
225
      //    draw scale
 
226
      //---------------------------------------------------
 
227
 
 
228
      int y2 = h - (ppos + y1);
 
229
      p.fillRect(x, y1, _scaleWidth, y2-y1, sc);
 
230
      p.fillRect(x, y2, _scaleWidth, y3-y2, svc);
 
231
 
 
232
      //---------------------------------------------------
 
233
      //    draw tick marks
 
234
      //---------------------------------------------------
 
235
 
 
236
        QFont f(p.font());
 
237
        f.setPointSize(6);
 
238
        p.setFont(f);
 
239
      p.setPen(QPen(Qt::darkGray, 2));
 
240
        QFontMetrics fm(f);
 
241
      int xt = 20 - fm.width("00") - 5;
 
242
 
 
243
      QString s;
 
244
        for (int i = 10; i < 70; i += 10) {
 
245
        h  = y1 + lrint(i * mh / range);
 
246
                s.setNum(i - 10);
 
247
                p.drawText(xt,  h - 3, s);
 
248
                p.drawLine(15, h, 20, h);
 
249
                }
 
250
 
 
251
      //---------------------------------------------------
 
252
      //    draw slider
 
253
      //---------------------------------------------------
 
254
 
 
255
      x  += _scaleWidth/2;
 
256
      p.setPen(QPen(svc, 0));
 
257
      p.translate(QPointF(x, y2));
 
258
      p.setRenderHint(QPainter::Antialiasing, true);
 
259
      p.drawPath(*points);
 
260
      }
 
261
 
 
262
//---------------------------------------------------------
 
263
//   mousePressEvent
 
264
//---------------------------------------------------------
 
265
 
 
266
void MeterSlider::mousePressEvent(QMouseEvent* ev)
 
267
      {
 
268
      if (ev->pos().x() < _meterWidth) {
 
269
            emit meterClicked();
 
270
            return;
 
271
            }
 
272
      VolSlider::mousePressEvent(ev);
 
273
      }
 
274
}
 
275