scide: implement selectionLength for openDocument
[supercollider.git] / QtCollider / widgets / QcSlider.cpp
blob15784ebf66a6cd5f3f1d227302d698ebf775bd93
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 "QcSlider.h"
23 #include "../QcWidgetFactory.h"
25 #include <QMouseEvent>
26 #include <QKeyEvent>
27 #include <QWheelEvent>
29 QC_DECLARE_QWIDGET_FACTORY(QcSlider);
31 QcSlider::QcSlider() :
32 QtCollider::Style::Client(this),
33 _value(0.0),
34 _step(0.0),
35 _hndLen(20)
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();
47 range -= _hndLen;
49 if(range > 0)
50 return 1.0 / range;
51 else
52 return 0.0;
55 void QcSlider::setValue( double val )
57 double step = _step;
58 modifyStep(&step);
59 if (step) val = qRound(val / step) * step;
61 _value = qBound(0.0, val, 1.0);
63 update();
66 void QcSlider::increment( double factor )
68 double step = qMax(_step, pixelStep());
69 setValue( step * factor + _value );
70 update();
73 void QcSlider::decrement( double factor )
75 double step = qMax(_step, pixelStep());
76 setValue( - step * factor + _value );
77 update();
80 void QcSlider::setOrientation( int i )
82 _ort = (Qt::Orientation) i;
84 if(_ort == Qt::Horizontal)
85 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
86 else
87 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
89 updateGeometry();
90 update();
93 QSize QcSlider::sizeHint() const
95 if( orientation() == Qt::Horizontal )
96 return QSize(qMax(_hndLen + 10, 150), 20);
97 else
98 return QSize(20, qMax(_hndLen + 10, 150));
101 QSize QcSlider::minimumSizeHint() const
103 if( orientation() == Qt::Horizontal )
104 return QSize(_hndLen + 10, 20);
105 else
106 return QSize(20, _hndLen + 10);
109 void QcSlider::mousePressEvent ( QMouseEvent *e )
111 setValue( valueAt(e->pos()) );
112 update();
113 Q_EMIT( action() );
116 void QcSlider::mouseMoveEvent ( QMouseEvent *e )
118 if( !e->buttons() ) return;
120 setValue( valueAt(e->pos()) );
121 update();
122 Q_EMIT( action() );
125 void QcSlider::wheelEvent ( QWheelEvent *e )
127 double step = qMax(_step, pixelStep());
128 modifyStep(&step);
129 double dval = e->delta() / 120.0 * step;
130 setValue( _value + dval );
131 Q_EMIT( action() );
134 void QcSlider::paintEvent( QPaintEvent *e )
136 using namespace QtCollider::Style;
137 using QtCollider::Style::RoundRect;
139 QPainter p(this);
140 const QPalette &plt = palette();
142 p.save();
143 p.setRenderHint( QPainter::Antialiasing, true );
145 QRect rGroove = rect();
147 // draw groove
148 RoundRect shGroove( rGroove, 2 );
149 drawSunken( &p, plt, shGroove, grooveColor(), hasFocus() ? focusColor() : QColor() );
151 // geometry
152 QRect rHandle( thumbRect() );
154 // draw handle
155 RoundRect shHandle( rHandle, 2 );
156 drawRaised( &p, plt, shHandle, plt.color(QPalette::Button).lighter(105) );
158 p.restore();
160 // draw marker
161 QPen pen(knobColor());
162 pen.setWidth(2);
163 p.setPen(pen);
164 if(_ort == Qt::Horizontal) {
165 qreal center = rHandle.center().x() + 1;
166 QLine line( center, rHandle.top() + 3, center, rHandle.bottom() - 2 );
167 p.drawLine(line);
168 pen.setColor(plt.color(QPalette::Light));
169 } else {
170 qreal center = rHandle.center().y() + 1;
171 QLine line( rHandle.left() + 3, center, rHandle.right() - 2, center );
172 p.drawLine(line);
173 pen.setColor(plt.color(QPalette::Light));
177 QRect QcSlider::thumbRect ()
179 using namespace QtCollider::Style;
181 QRect contRect( sunkenContentsRect(rect()) );
183 QRect r;
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() ) );
190 else {
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 ) );
196 return r;
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) );
206 else
207 return yValue( pos.y(), contRect, QSize(0, _hndLen) );