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 "QcSlider2D.h"
23 #include "../QcWidgetFactory.h"
24 #include "../style/routines.hpp"
27 #include <QMouseEvent>
28 #include <QApplication>
31 QC_DECLARE_QWIDGET_FACTORY(QcSlider2D
);
33 QcSlider2D::QcSlider2D() :
34 QtCollider::Style::Client(this),
37 _thumbSize( QSize( 20, 20 ) ),
40 setFocusPolicy( Qt::StrongFocus
);
41 setSizePolicy( QSizePolicy::Expanding
, QSizePolicy::Expanding
);
44 void QcSlider2D::incrementX( double factor
)
46 setValue( QPointF( _step
* factor
+ _x
, _y
) );
49 void QcSlider2D::decrementX( double factor
)
51 setValue( QPointF( -_step
* factor
+ _x
, _y
) );
54 void QcSlider2D::incrementY( double factor
)
56 setValue( QPointF( _x
, _step
* factor
+ _y
) );
59 void QcSlider2D::decrementY( double factor
)
61 setValue( QPointF( _x
, -_step
* factor
+ _y
) );
64 QRect
QcSlider2D::thumbRect()
66 using namespace QtCollider::Style
;
67 return QtCollider::Style::rect( QPointF(_x
,_y
), sunkenContentsRect(rect()), _thumbSize
);
70 void QcSlider2D::setValue( const QPointF val
, bool doAction
)
72 double x
= qMax( 0.0, qMin( 1.0, (double)val
.x() ) );
73 double y
= qMax( 0.0, qMin( 1.0, (double)val
.y() ) );
74 if( x
!= _x
|| y
!= _y
) {
78 if( doAction
) Q_EMIT( action() );
82 void QcSlider2D::mouseMoveEvent ( QMouseEvent
* ev
)
84 using namespace QtCollider::Style
;
86 if( !ev
->buttons() ) return;
88 QPointF val
= QtCollider::Style::value( QPointF(ev
->pos()), sunkenContentsRect(rect()), _thumbSize
);
92 void QcSlider2D::mousePressEvent ( QMouseEvent
* ev
)
94 using namespace QtCollider::Style
;
95 QPointF val
= QtCollider::Style::value( QPointF(ev
->pos()), sunkenContentsRect(rect()), _thumbSize
);
99 void QcSlider2D::keyPressEvent ( QKeyEvent
*e
)
105 setValue( QPointF( _x
, _y
+ step
) ); break;
108 setValue( QPointF( _x
, _y
- step
) ); break;
111 setValue( QPointF( _x
+ step
, _y
) ); break;
114 setValue( QPointF( _x
- step
, _y
) ); break;
116 setValue( QPointF( 0.0, 0.0 ) ); break;
118 setValue( QPointF( 1.0, 1.0 ) ); break;
120 setValue( QPointF( 0.5, 0.5 ) ); break;
122 Q_EMIT( randomize() );
124 default: QWidget::keyPressEvent( e
);
128 void QcSlider2D::paintEvent ( QPaintEvent
*e
)
130 using namespace QtCollider::Style
;
131 using QtCollider::Style::Ellipse
;
132 using QtCollider::Style::RoundRect
;
137 p
.setRenderHint( QPainter::Antialiasing
, true );
139 QPalette plt
= palette();
141 RoundRect
frame(rect(), 2);
142 drawSunken( &p
, plt
, frame
, grooveColor(), hasFocus() ? focusColor() : QColor() );
144 Ellipse
thumb(thumbRect());
145 drawRaised( &p
, plt
, thumb
, plt
.color(QPalette::Button
).lighter(105) );
147 QRectF
r( thumb
._rect
);
148 qreal wdif
= r
.width() * 0.3;
149 qreal hdif
= r
.height() * 0.3;
150 p
.setPen( Qt::NoPen
);
151 p
.setBrush( knobColor() );
152 p
.drawEllipse( r
.adjusted( wdif
, hdif
, -wdif
, -hdif
) );