qtcollider: declare factories without the use of static initialization
[supercollider.git] / QtCollider / widgets / QcSlider2D.cpp
blobe5a76cd8c7262faa9a9f01241a6bfd6edab9592b
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"
25 #include <QKeyEvent>
26 #include <QMouseEvent>
27 #include <QApplication>
28 #include <QPainter>
30 QC_DECLARE_QWIDGET_FACTORY(QcSlider2D);
32 QcSlider2D::QcSlider2D() :
33 _x( 0.f ),
34 _y( 0.f ),
35 _thumbSize( QSize( 12, 12 ) ),
36 _step( 0.01f )
38 setFocusPolicy( Qt::StrongFocus );
39 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
42 void QcSlider2D::incrementX( double factor )
44 setValue( QPointF( _step * factor + _x, _y ) );
47 void QcSlider2D::decrementX( double factor )
49 setValue( QPointF( -_step * factor + _x, _y ) );
52 void QcSlider2D::incrementY( double factor )
54 setValue( QPointF( _x, _step * factor + _y ) );
57 void QcSlider2D::decrementY( double factor )
59 setValue( QPointF( _x, -_step * factor + _y ) );
62 QRect QcSlider2D::thumbRect()
64 QRect r;
66 int xPos = _x * (width() - _thumbSize.width());
67 int yPos = (1.f - _y) * (height() - _thumbSize.height());
69 r.setX( xPos );
70 r.setY( yPos );
71 r.setSize( _thumbSize );
73 return r;
76 QPointF QcSlider2D::valueFromPos( const QPoint pos )
78 float x = (float) (pos.x() - (_thumbSize.width() * 0.5f) ) /
79 (width() - _thumbSize.width());
80 float y = (float) (height() - pos.y() - (_thumbSize.width() * 0.5f) ) /
81 ( height() - _thumbSize.height() );
82 return QPointF( x, y );
85 void QcSlider2D::setValue( const QPointF val, bool doAction )
87 float x = qMax( 0.f, qMin( 1.f, (float)val.x() ) );
88 float y= qMax( 0.f, qMin( 1.f, (float)val.y() ) );
89 if( x != _x || y != _y ) {
90 _x = x;
91 _y = y;
92 update();
93 if( doAction ) Q_EMIT( action() );
97 void QcSlider2D::mouseMoveEvent ( QMouseEvent * ev )
99 setValue( valueFromPos( ev->pos() ) );
102 void QcSlider2D::mousePressEvent ( QMouseEvent * ev )
104 setValue( valueFromPos( ev->pos() ) );
107 void QcSlider2D::keyPressEvent ( QKeyEvent *e )
109 float step = _step;
110 switch( e->key() ) {
111 case Qt::Key_Up:
112 modifyStep( &step );
113 setValue( QPointF( _x, _y + step ) ); break;
114 case Qt::Key_Down:
115 modifyStep( &step );
116 setValue( QPointF( _x, _y - step ) ); break;
117 case Qt::Key_Right:
118 modifyStep( &step );
119 setValue( QPointF( _x + step, _y ) ); break;
120 case Qt::Key_Left:
121 modifyStep( &step );
122 setValue( QPointF( _x - step, _y ) ); break;
123 case Qt::Key_N:
124 setValue( QPointF( 0.f, 0.f ) ); break;
125 case Qt::Key_X:
126 setValue( QPointF( 1.f, 1.f ) ); break;
127 case Qt::Key_C:
128 setValue( QPointF( 0.5f, 0.5f ) ); break;
129 case Qt::Key_R:
130 Q_EMIT( randomize() );
131 break;
132 default: QWidget::keyPressEvent( e );
136 void QcSlider2D::paintEvent ( QPaintEvent *e )
138 Q_UNUSED(e);
139 QPainter p;
140 p.begin(this);
142 QPalette plt = palette();
144 p.setBrush( plt.color( QPalette::Base ) );
145 p.setPen( plt.color( QPalette::Mid ) );
146 p.drawRect( rect().adjusted(0,0,-1,-1) );
148 p.fillRect( thumbRect(), plt.color( QPalette::Text ) );
149 p.end();