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 ************************************************************************/
23 #include "../QcWidgetFactory.h"
25 #include <QMouseEvent>
27 #include <QWheelEvent>
29 QC_DECLARE_QWIDGET_FACTORY(QcSlider
);
31 QcSlider::QcSlider() :
32 QtCollider::Style::Client(this),
37 setFocusPolicy( Qt::StrongFocus
);
38 setOrientation( Qt::Vertical
);
41 double QcSlider::pixelStep()
43 using namespace QtCollider::Style
;
45 QRect
contRect( sunkenContentsRect(rect()) );
46 int range
= (_ort
== Qt::Horizontal
) ? contRect
.width() : contRect
.height();
55 void QcSlider::setValue( double val
)
59 if (step
) val
= qRound(val
/ step
) * step
;
61 _value
= qBound(0.0, val
, 1.0);
66 void QcSlider::increment( double factor
)
68 double step
= qMax(_step
, pixelStep());
69 setValue( step
* factor
+ _value
);
73 void QcSlider::decrement( double factor
)
75 double step
= qMax(_step
, pixelStep());
76 setValue( - step
* factor
+ _value
);
80 void QcSlider::setOrientation( int i
)
82 _ort
= (Qt::Orientation
) i
;
84 if(_ort
== Qt::Horizontal
)
85 setSizePolicy( QSizePolicy::Expanding
, QSizePolicy::Preferred
);
87 setSizePolicy( QSizePolicy::Preferred
, QSizePolicy::Expanding
);
93 QSize
QcSlider::sizeHint() const
95 if( orientation() == Qt::Horizontal
)
96 return QSize(qMax(_hndLen
+ 10, 150), 20);
98 return QSize(20, qMax(_hndLen
+ 10, 150));
101 QSize
QcSlider::minimumSizeHint() const
103 if( orientation() == Qt::Horizontal
)
104 return QSize(_hndLen
+ 10, 20);
106 return QSize(20, _hndLen
+ 10);
109 void QcSlider::mousePressEvent ( QMouseEvent
*e
)
111 setValue( valueAt(e
->pos()) );
116 void QcSlider::mouseMoveEvent ( QMouseEvent
*e
)
118 if( !e
->buttons() ) return;
120 setValue( valueAt(e
->pos()) );
125 void QcSlider::wheelEvent ( QWheelEvent
*e
)
127 double step
= qMax(_step
, pixelStep());
129 double dval
= e
->delta() / 120.0 * step
;
130 setValue( _value
+ dval
);
134 void QcSlider::paintEvent( QPaintEvent
*e
)
136 using namespace QtCollider::Style
;
137 using QtCollider::Style::RoundRect
;
140 const QPalette
&plt
= palette();
143 p
.setRenderHint( QPainter::Antialiasing
, true );
145 QRect rGroove
= rect();
148 RoundRect
shGroove( rGroove
, 2 );
149 drawSunken( &p
, plt
, shGroove
, grooveColor(), hasFocus() ? focusColor() : QColor() );
152 QRect
rHandle( thumbRect() );
155 RoundRect
shHandle( rHandle
, 2 );
156 drawRaised( &p
, plt
, shHandle
, plt
.color(QPalette::Button
).lighter(105) );
161 QPen
pen(knobColor());
164 if(_ort
== Qt::Horizontal
) {
165 qreal center
= rHandle
.center().x() + 1;
166 QLine
line( center
, rHandle
.top() + 3, center
, rHandle
.bottom() - 2 );
168 pen
.setColor(plt
.color(QPalette::Light
));
170 qreal center
= rHandle
.center().y() + 1;
171 QLine
line( rHandle
.left() + 3, center
, rHandle
.right() - 2, center
);
173 pen
.setColor(plt
.color(QPalette::Light
));
177 QRect
QcSlider::thumbRect ()
179 using namespace QtCollider::Style
;
181 QRect
contRect( sunkenContentsRect(rect()) );
184 if( _ort
== Qt::Horizontal
) {
185 int pos
= _value
* (contRect
.width() - _hndLen
);
186 r
.setX( pos
+ contRect
.left() );
187 r
.setY( contRect
.top() );
188 r
.setSize( QSize( _hndLen
, contRect
.height() ) );
191 int pos
= _value
* (contRect
.height() - _hndLen
);
192 r
.setX( contRect
.left() );
193 r
.setY( contRect
.bottom() - pos
- _hndLen
+ 1 );
194 r
.setSize( QSize( contRect
.width(), _hndLen
) );
199 double QcSlider::valueAt( const QPoint
&pos
)
201 using namespace QtCollider::Style
;
203 QRect
contRect( sunkenContentsRect(rect()) );
204 if (_ort
== Qt::Horizontal
)
205 return xValue( pos
.x(), contRect
, QSize(_hndLen
, 0) );
207 return yValue( pos
.y(), contRect
, QSize(0, _hndLen
) );