class library: PriorityQueue - implement removeValue, hide array
[supercollider.git] / QtCollider / widgets / QcLevelIndicator.cpp
blob6466feaa855a0a741402fe13ee1f8f1551fcb218
1 /************************************************************************
3 * Copyright 2010 Jakob Leben (jakob.leben@gmail.com)
5 * This file is part of SuperCollider Qt GUI.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ************************************************************************/
22 #include "QcLevelIndicator.h"
23 #include "../QcWidgetFactory.h"
25 #include <QPainter>
27 static QcWidgetFactory<QcLevelIndicator> factory;
29 QcLevelIndicator::QcLevelIndicator()
30 : _value( 0.f ), _warning(0.6), _critical(0.8),
31 _peak( 0.f ), _drawPeak( false ),
32 _ticks(0), _majorTicks(0),
33 _clipped(false)
35 _clipTimer = new QTimer( this );
36 _clipTimer->setSingleShot(true);
37 _clipTimer->setInterval( 1000 );
38 connect( _clipTimer, SIGNAL(timeout()), this, SLOT(clipTimeout()) );
41 void QcLevelIndicator::clipTimeout()
43 _clipped = false;
44 update();
48 void QcLevelIndicator::paintEvent( QPaintEvent *e )
50 QPainter p(this);
52 QPalette plt = palette();
54 bool vertical = height() >= width();
56 float groove = vertical ? width() : height();
57 if( _ticks || _majorTicks ) groove -= 6;
59 float length = vertical ? height() : width();
61 float colorValue = _drawPeak ? _peak : _value;
63 if( colorValue > _critical ) {
64 _clipped = true;
66 else if( _clipped ) {
67 _clipTimer->start();
70 QColor c;
71 if( _clipped )
72 c = QColor(255,100,0);
73 else if( colorValue > _warning )
74 c = QColor( 255, 255, 0 );
75 else
76 c = QColor( 0, 255, 0 );
78 p.fillRect( vertical ? QRectF(0,0,groove,height()) : QRectF(0,0,width(),groove),
79 QColor( 130,130,130 ) );
81 QRectF r;
83 if( vertical ) {
84 r.setWidth( groove );
85 r.setY( (1.f - _value) * length );
86 r.setBottom( height() );
88 else {
89 r.setHeight( groove );
90 r.setRight( _value * length );
93 p.fillRect( r, c );
95 #if 0
97 float y = 0.f;
98 float v = 1.f;
100 if( v > _value ) {
101 y = (1.f - _value) * h;
102 r.setBottom( y );
103 p.fillRect( r, QColor( 130,130,130 ) );
104 v = _value;
107 if( v > _critical ) {
108 r.moveTop( y );
109 y = (1.f - _critical) * h;
110 r.setBottom( y );
111 p.fillRect( r, QColor(255,100,0) );
112 v = _critical;
115 if( v > _warning ) {
116 r.moveTop( y );
117 y = (1.f - _warning) * h;
118 r.setBottom( y );
119 p.fillRect( r, QColor( 255, 255, 0 ) );
120 v = _warning;
123 if( v > 0.f ) {
124 r.moveTop( y );
125 r.setBottom( h );
126 p.fillRect( r, QColor( 0, 255, 0 ) );
128 #endif
130 if( _drawPeak && _peak > 0.f ) {
131 float peak = vertical ? _peak : 1 - _peak;
133 // compensate for border and peak line width
134 float val = (1.f - peak)
135 * ( length - 4 )
136 + 2;
137 QPen pen( QColor( 255, 200, 0 ) );
138 pen.setWidth( 2 );
139 p.setPen( pen );
140 if( vertical )
141 p.drawLine( 0.f, val, groove - 1, val );
142 else
143 p.drawLine( val, 0.f, val, groove - 1 );
146 if( _ticks ) {
147 p.setPen( QColor( 170, 170, 170 ) );
148 float dVal = ( _ticks > 1 ) ? ( length-1) / (float)(_ticks-1) : 0.f;
149 float t = 0;
150 while( t < _ticks ) {
151 float v = t * dVal;
152 if( vertical )
153 p.drawLine( groove, v, width(), v );
154 else
155 p.drawLine( v, groove, v, height() );
156 t++;
160 if( _majorTicks ) {
161 QPen pen ( QColor( 170, 170, 170 ) );
162 pen.setWidth( 3 );
163 p.setPen( pen );
164 float dVal = ( _majorTicks > 1 ) ? (length-3) / (float)(_majorTicks-1) : 0.f;
165 float t = 0;
166 while( t < _majorTicks ) {
167 float v = (int) (t * dVal) + 1;
168 if( vertical )
169 p.drawLine( groove, v, width(), v );
170 else
171 p.drawLine( v, groove, v, height() );
172 t++;
176 if( vertical ) {
177 r = rect().adjusted(0,0,0,-1);
178 r.setWidth( groove - 1 );
179 } else {
180 r = rect().adjusted(0,0,-1,0);
181 r.setHeight( groove - 1 );
184 p.setBrush( Qt::NoBrush );
185 p.setPen( plt.color( QPalette::Dark ) );
186 p.drawRect( r );