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"
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),
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()
49 void QcLevelIndicator::paintEvent( QPaintEvent
*e
)
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
) {
68 else if( _clipped
&& !_clipTimer
->isActive() ) {
74 c
= QColor(255,100,0);
75 else if( colorValue
> _warning
)
76 c
= QColor( 255, 255, 0 );
78 c
= QColor( 0, 255, 0 );
80 p
.fillRect( vertical
? QRectF(0,0,groove
,height()) : QRectF(0,0,width(),groove
),
87 r
.setY( (1.f
- _value
) * length
);
88 r
.setBottom( height() );
91 r
.setHeight( groove
);
92 r
.setRight( _value
* length
);
103 y
= (1.f
- _value
) * h
;
105 p
.fillRect( r
, QColor( 130,130,130 ) );
109 if( v
> _critical
) {
111 y
= (1.f
- _critical
) * h
;
113 p
.fillRect( r
, QColor(255,100,0) );
119 y
= (1.f
- _warning
) * h
;
121 p
.fillRect( r
, QColor( 255, 255, 0 ) );
128 p
.fillRect( r
, QColor( 0, 255, 0 ) );
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
)
139 QPen
pen( QColor( 255, 200, 0 ) );
141 pen
.setCapStyle( Qt::FlatCap
);
144 p
.drawLine( 0.f
, val
, groove
, val
);
146 p
.drawLine( val
, 0.f
, val
, groove
);
150 QPen
pen( plt
.color(QPalette::WindowText
) );
151 pen
.setCapStyle( Qt::FlatCap
);
153 float dVal
= ( _ticks
> 1 ) ? ( length
-1) / (float)(_ticks
-1) : 0.f
;
155 while( t
< _ticks
) {
158 p
.drawLine( groove
, v
, width(), v
);
160 p
.drawLine( v
, groove
, v
, height() );
166 QPen
pen ( plt
.color(QPalette::WindowText
) );
167 pen
.setCapStyle( Qt::FlatCap
);
170 float dVal
= ( _majorTicks
> 1 ) ? (length
-3) / (float)(_majorTicks
-1) : 0.f
;
172 while( t
< _majorTicks
) {
173 float v
= (int) (t
* dVal
) + 1;
175 p
.drawLine( groove
, v
, width(), v
);
177 p
.drawLine( v
, groove
, v
, height() );