"Post Window" -> "Post window" prevents it being seen as two separate
[supercollider.git] / QtCollider / widgets / QcSlider2D.cpp
blob26ebe5fa847d19828878f79949df6f57c70faa39
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"
26 #include <QKeyEvent>
27 #include <QMouseEvent>
28 #include <QApplication>
29 #include <QPainter>
31 QC_DECLARE_QWIDGET_FACTORY(QcSlider2D);
33 QcSlider2D::QcSlider2D() :
34 QtCollider::Style::Client(this),
35 _x( 0.0 ),
36 _y( 0.0 ),
37 _thumbSize( QSize( 20, 20 ) ),
38 _step( 0.01 )
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 ) {
75 _x = x;
76 _y = y;
77 update();
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 );
89 setValue(val);
92 void QcSlider2D::mousePressEvent ( QMouseEvent * ev )
94 using namespace QtCollider::Style;
95 QPointF val = QtCollider::Style::value( QPointF(ev->pos()), sunkenContentsRect(rect()), _thumbSize );
96 setValue(val);
99 void QcSlider2D::keyPressEvent ( QKeyEvent *e )
101 double step = _step;
102 switch( e->key() ) {
103 case Qt::Key_Up:
104 modifyStep( &step );
105 setValue( QPointF( _x, _y + step ) ); break;
106 case Qt::Key_Down:
107 modifyStep( &step );
108 setValue( QPointF( _x, _y - step ) ); break;
109 case Qt::Key_Right:
110 modifyStep( &step );
111 setValue( QPointF( _x + step, _y ) ); break;
112 case Qt::Key_Left:
113 modifyStep( &step );
114 setValue( QPointF( _x - step, _y ) ); break;
115 case Qt::Key_N:
116 setValue( QPointF( 0.0, 0.0 ) ); break;
117 case Qt::Key_X:
118 setValue( QPointF( 1.0, 1.0 ) ); break;
119 case Qt::Key_C:
120 setValue( QPointF( 0.5, 0.5 ) ); break;
121 case Qt::Key_R:
122 Q_EMIT( randomize() );
123 break;
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;
134 Q_UNUSED(e);
136 QPainter p(this);
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 ) );