common: win32utils - compile fix
[supercollider.git] / QtCollider / widgets / QcSlider2D.cpp
blobe7f165367fc8e61343161fd9ecfe0f86d50999ef
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.f ),
36 _y( 0.f ),
37 _thumbSize( QSize( 20, 20 ) ),
38 _step( 0.01f )
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 float x = qMax( 0.f, qMin( 1.f, (float)val.x() ) );
73 float y= qMax( 0.f, qMin( 1.f, (float)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 float 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.f, 0.f ) ); break;
117 case Qt::Key_X:
118 setValue( QPointF( 1.f, 1.f ) ); break;
119 case Qt::Key_C:
120 setValue( QPointF( 0.5f, 0.5f ) ); 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 QColor baseColor( grooveColor() );
143 drawSunken( &p, plt, frame, baseColor, hasFocus() ? focusColor() : QColor() );
145 Ellipse thumb(thumbRect());
146 drawRaised( &p, plt, thumb, plt.color(QPalette::Button).lighter(105) );
148 QRectF r( thumb._rect );
149 qreal wdif = r.width() * 0.3;
150 qreal hdif = r.height() * 0.3;
151 p.setPen( Qt::NoPen );
152 p.setBrush( plt.color(QPalette::ButtonText) );
153 p.drawEllipse( r.adjusted( wdif, hdif, -wdif, -hdif ) );