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 "QcRangeSlider.h"
23 #include "../QcWidgetFactory.h"
26 #include <QMouseEvent>
27 #include <QApplication>
30 static QcWidgetFactory
<QcRangeSlider
> factory
;
32 QcRangeSlider::QcRangeSlider() :
38 setFocusPolicy( Qt::StrongFocus
);
39 setOrientation( Qt::Vertical
);
42 void QcRangeSlider::setOrientation( Qt::Orientation o
)
46 if( _ort
== Qt::Horizontal
) {
47 setSizePolicy( QSizePolicy::Expanding
, QSizePolicy::Preferred
);
50 setSizePolicy( QSizePolicy::Preferred
, QSizePolicy::Expanding
);
57 void QcRangeSlider::setLoValue( float val
)
59 val
= qMax( 0.f
, qMin( 1.f
, val
) );
70 void QcRangeSlider::setHiValue( float val
)
72 val
= qMax( 0.f
, qMin( 1.f
, val
) );
83 QSize
QcRangeSlider::sizeHint() const
85 return ( _ort
== Qt::Horizontal
? QSize( 100, 20 ) : QSize( 20, 100 ) );
88 QSize
QcRangeSlider::minimumSizeHint() const
90 return ( _ort
== Qt::Horizontal
? QSize( 30, 10 ) : QSize( 10, 30 ) );
93 void QcRangeSlider::increment( double factor
)
95 moveBy( factor
* _step
);
98 void QcRangeSlider::decrement( double factor
)
100 moveBy( -factor
* _step
);
103 void QcRangeSlider::increment()
110 void QcRangeSlider::decrement()
117 QRect
QcRangeSlider::thumbRect()
121 if( _ort
== Qt::Horizontal
) {
122 r
.setX( _lo
* width() );
123 r
.setRight( _hi
* width() );
124 r
.setHeight( height() );
127 r
.setY( (1.f
- _hi
) * height() );
128 r
.setBottom( (1.f
- _lo
) * height() );
129 r
.setWidth( width() );
135 float QcRangeSlider::valueFromPos( const QPoint
& pos
)
137 float val
= _ort
== Qt::Horizontal
?
138 (float)pos
.x() / width() :
139 1.f
- ((float)pos
.y() / height());
143 void QcRangeSlider::moveBy( float dif
)
145 if( _lo
+ dif
< 0.f
) {
149 else if( _hi
+ dif
> 1.f
) {
160 void QcRangeSlider::mouseMoveEvent ( QMouseEvent
* e
)
162 if( mouseMode
== Drag
) {
163 QPoint pt
= e
->pos() + dragOffset
;
165 if( _ort
== Qt::Horizontal
)
166 dif
= ( (float)pt
.x() / width() ) - _lo
;
168 dif
= 1.f
- ( (float)pt
.y() / height() ) - _hi
;
170 if( dif
== 0.f
) return;
175 float val
= valueFromPos( e
->pos() );
176 if( mouseMode
== SetLo
) {
177 if( val
> _hi
) mouseMode
= SetHi
;
180 else if( mouseMode
== SetHi
) {
181 if( val
< _lo
) mouseMode
= SetLo
;
189 void QcRangeSlider::mousePressEvent ( QMouseEvent
* e
)
191 QRect r
= thumbRect();
192 if( e
->modifiers() & Qt::ShiftModifier
) {
193 float center
= (_hi
+ _lo
) * 0.5;
194 float val
= valueFromPos( e
->pos() );
205 else if( r
.contains( e
->pos() ) ){
207 dragOffset
= r
.topLeft() - e
->pos();
210 _lo
= _hi
= valueFromPos( e
->pos() );
217 void QcRangeSlider::mouseReleaseEvent ( QMouseEvent
* e
)
223 void QcRangeSlider::keyPressEvent ( QKeyEvent
*e
)
233 _lo
= 0.f
; _hi
= 1.f
; update(); break;
235 _lo
= 0.f
; _hi
= 0.f
; update(); break;
237 _lo
= 1.f
; _hi
= 1.f
; update(); break;
239 _lo
= 0.5; _hi
= 0.5; update(); break;
241 return QWidget::keyPressEvent( e
);
246 void QcRangeSlider::paintEvent ( QPaintEvent
*e
)
252 QPalette plt
= palette();
254 p
.setBrush( plt
.color( QPalette::Base
) );
255 p
.setPen( plt
.color( QPalette::Mid
) );
256 p
.drawRect( rect().adjusted(0,0,-1,-1) );
258 p
.fillRect( thumbRect(), plt
.color( QPalette::Text
) );