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"
26 #include <QMouseEvent>
27 #include <QApplication>
30 QC_DECLARE_QWIDGET_FACTORY(QcSlider2D
);
32 QcSlider2D::QcSlider2D() :
35 _thumbSize( QSize( 12, 12 ) ),
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()
66 int xPos
= _x
* (width() - _thumbSize
.width());
67 int yPos
= (1.f
- _y
) * (height() - _thumbSize
.height());
71 r
.setSize( _thumbSize
);
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
) {
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
)
113 setValue( QPointF( _x
, _y
+ step
) ); break;
116 setValue( QPointF( _x
, _y
- step
) ); break;
119 setValue( QPointF( _x
+ step
, _y
) ); break;
122 setValue( QPointF( _x
- step
, _y
) ); break;
124 setValue( QPointF( 0.f
, 0.f
) ); break;
126 setValue( QPointF( 1.f
, 1.f
) ); break;
128 setValue( QPointF( 0.5f
, 0.5f
) ); break;
130 Q_EMIT( randomize() );
132 default: QWidget::keyPressEvent( e
);
136 void QcSlider2D::paintEvent ( QPaintEvent
*e
)
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
) );