Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / QtCollider / widgets / QcLevelIndicator.cpp
blob50820afc795c7311241e9ac7dc91c63e1bcb315b
1 /************************************************************************
3 * Copyright 2010-2012 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 QC_DECLARE_QWIDGET_FACTORY(QcLevelIndicator);
29 QcLevelIndicator::QcLevelIndicator() :
30 QtCollider::Style::Client(this),
31 _value( 0.f ), _warning(0.6), _critical(0.8),
32 _peak( 0.f ), _drawPeak( false ),
33 _ticks(0), _majorTicks(0),
34 _clipped(false)
36 _clipTimer = new QTimer( this );
37 _clipTimer->setSingleShot(true);
38 _clipTimer->setInterval( 1000 );
39 connect( _clipTimer, SIGNAL(timeout()), this, SLOT(clipTimeout()) );
42 void QcLevelIndicator::clipTimeout()
44 _clipped = false;
45 update();
49 void QcLevelIndicator::paintEvent( QPaintEvent *e )
51 QPainter p(this);
53 QPalette plt = palette();
55 bool vertical = height() >= width();
57 float groove = vertical ? width() : height();
58 if( _ticks || _majorTicks ) groove -= 6;
60 float length = vertical ? height() : width();
62 float colorValue = _drawPeak ? _peak : _value;
64 if( colorValue > _critical ) {
65 _clipTimer->stop();
66 _clipped = true;
68 else if( _clipped && !_clipTimer->isActive() ) {
69 _clipTimer->start();
72 QColor c;
73 if( _clipped )
74 c = QColor(255,100,0);
75 else if( colorValue > _warning )
76 c = QColor( 255, 255, 0 );
77 else
78 c = QColor( 0, 255, 0 );
80 p.fillRect( vertical ? QRectF(0,0,groove,height()) : QRectF(0,0,width(),groove),
81 grooveColor() );
83 QRectF r;
85 if( vertical ) {
86 r.setWidth( groove );
87 r.setY( (1.f - _value) * length );
88 r.setBottom( height() );
90 else {
91 r.setHeight( groove );
92 r.setRight( _value * length );
95 p.fillRect( r, c );
97 #if 0
99 float y = 0.f;
100 float v = 1.f;
102 if( v > _value ) {
103 y = (1.f - _value) * h;
104 r.setBottom( y );
105 p.fillRect( r, QColor( 130,130,130 ) );
106 v = _value;
109 if( v > _critical ) {
110 r.moveTop( y );
111 y = (1.f - _critical) * h;
112 r.setBottom( y );
113 p.fillRect( r, QColor(255,100,0) );
114 v = _critical;
117 if( v > _warning ) {
118 r.moveTop( y );
119 y = (1.f - _warning) * h;
120 r.setBottom( y );
121 p.fillRect( r, QColor( 255, 255, 0 ) );
122 v = _warning;
125 if( v > 0.f ) {
126 r.moveTop( y );
127 r.setBottom( h );
128 p.fillRect( r, QColor( 0, 255, 0 ) );
130 #endif
132 if( _drawPeak && _peak > 0.f ) {
133 float peak = vertical ? _peak : 1 - _peak;
135 // compensate for border and peak line width
136 float val = (1.f - peak)
137 * ( length - 4 )
138 + 2;
139 QPen pen( QColor( 255, 200, 0 ) );
140 pen.setWidth( 2 );
141 pen.setCapStyle( Qt::FlatCap );
142 p.setPen( pen );
143 if( vertical )
144 p.drawLine( 0.f, val, groove, val );
145 else
146 p.drawLine( val, 0.f, val, groove );
149 if( _ticks ) {
150 QPen pen( plt.color(QPalette::WindowText) );
151 pen.setCapStyle( Qt::FlatCap );
152 p.setPen(pen);
153 float dVal = ( _ticks > 1 ) ? ( length-1) / (float)(_ticks-1) : 0.f;
154 float t = 0;
155 while( t < _ticks ) {
156 float v = t * dVal;
157 if( vertical )
158 p.drawLine( groove, v, width(), v );
159 else
160 p.drawLine( v, groove, v, height() );
161 t++;
165 if( _majorTicks ) {
166 QPen pen ( plt.color(QPalette::WindowText) );
167 pen.setCapStyle( Qt::FlatCap );
168 pen.setWidth( 3 );
169 p.setPen( pen );
170 float dVal = ( _majorTicks > 1 ) ? (length-3) / (float)(_majorTicks-1) : 0.f;
171 float t = 0;
172 while( t < _majorTicks ) {
173 float v = (int) (t * dVal) + 1;
174 if( vertical )
175 p.drawLine( groove, v, width(), v );
176 else
177 p.drawLine( v, groove, v, height() );
178 t++;