File: meter.cpp

package info (click to toggle)
muse 0.6.3-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 8,936 kB
  • ctags: 7,446
  • sloc: cpp: 66,262; sh: 8,355; makefile: 755; ansic: 172
file content (128 lines) | stat: -rw-r--r-- 3,603 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: meter.cpp,v 1.1.1.1 2003/10/29 10:05:24 wschweer Exp $
//
//  (C) Copyright 2000 Werner Schweer (ws@seh.de)
//=========================================================

#include <stdio.h>
#include <cmath>

#include "meter.h"
#include <qpainter.h>

extern int minMeter;

//---------------------------------------------------------
//   Meter
//---------------------------------------------------------

Meter::Meter(QWidget* parent)
   : QFrame(parent, "", WRepaintNoErase)
      {
      overflow    = false;
      val         = 0.0;
      maxVal      = 0.0;
      minScale    = minMeter;      // min value in dB
      maxScale    = 10;
      yellowScale = -10;
      redScale    = 0;
      setLineWidth(1);
      setMidLineWidth(0);
      setFrameStyle(Box | Raised);
      }

//---------------------------------------------------------
//   setVal
//---------------------------------------------------------

void Meter::setVal(int v, int max, bool ovl)
      {
      overflow = ovl;
      val      = double(v)/32767.0;
      maxVal   = double(max)/32767.0;
      update();
      }

//---------------------------------------------------------
//   resetPeaks
//    reset peak and overflow indicator
//---------------------------------------------------------

void Meter::resetPeaks()
      {
      maxVal   = val;
      overflow = val > 0.0;
      update();
      }

//---------------------------------------------------------
//   setRange
//---------------------------------------------------------

void Meter::setRange(double min, double max)
      {
      minScale = min;
      maxScale = max;
      update();
      }

//---------------------------------------------------------
//   paintEvent
//---------------------------------------------------------

void Meter::drawContents(QPainter* p)
      {
      double range = maxScale - minScale;

      int fw = frameWidth();
      int h  = height() - 2* fw;
      int yv = val == 0 ? h : int(((maxScale - (log10(val) * 20)) * h)/range);

      bitBlt(this, fw, fw,    &bgPm, 0, 0,  -1, yv,   CopyROP, true);
      bitBlt(this, fw, fw+yv, &fgPm, 0, yv, -1, h-yv, CopyROP, true);

      int ymax = maxVal == 0 ? 0 : int(((maxScale - (log10(maxVal) * 20)) * h)/range);
      p->setPen(white);
      p->drawLine(0, ymax, width()-2*fw, ymax);
      }

//---------------------------------------------------------
//   resizeEvent
//---------------------------------------------------------

void Meter::resizeEvent(QResizeEvent* ev)
      {
      int fw = frameWidth();

      double range = maxScale - minScale;
      int h  = ev->size().height() - 2*fw;
      int w  = ev->size().width()  - 2*fw;
      bgPm.resize(w, h);
      fgPm.resize(w, h);
      int y1 = int((maxScale - redScale) * h / range);
      int y2 = int((maxScale - yellowScale) * h / range);
      int y3 = h;

      QPainter p1(&fgPm);
      QPainter p2(&bgPm);

      p1.fillRect(0, 0,  w, y1,    QBrush(0xff0000));  // red
      p1.fillRect(0, y1, w, y2-y1, QBrush(0xffff00));  // yellow
      p1.fillRect(0, y2, w, y3-y2, QBrush(0x00ff00));  // green

      p2.fillRect(0, 0,  w, y1,    QBrush(0x8e0000));  // red
      p2.fillRect(0, y1, w, y2-y1, QBrush(0x8e8e00));  // yellow
      p2.fillRect(0, y2, w, y3-y2, QBrush(0x007000));  // green
      }

//---------------------------------------------------------
//   mousePressEvent
//---------------------------------------------------------

void Meter::mousePressEvent(QMouseEvent*)
      {
      emit mousePress();
      }