28SWSwitch::SWSwitch( QWidget* parent )
29 : QAbstractButton( parent ),
35 _thumb( Qt::lightGray ),
36 _anim( new QPropertyAnimation( this,
"offset", this ) )
39 setOffset( _height / 2 );
41 setBrush( Qt::green );
42 setBrush( QColor( 242,242,242 ) );
46void SWSwitch::paintEvent( QPaintEvent* event )
50 QPainter painter(
this );
51 painter.setPen( Qt::NoPen );
52 QRect bounding( _margin, _margin, width() - 2 * _margin, height() - 2 * _margin );
53 QRectF ecl( offset() - (_height / 2), _y - (_height / 2), height(), height() );
58 painter.setBrush( _isOn ? brush() : Qt::white );
59 painter.setOpacity( _isOn ? 0.5 : 0.38 );
60 painter.setRenderHint( QPainter::Antialiasing,
true );
62 painter.drawRoundedRect( bounding, rd, rd );
63 painter.setBrush( _thumb );
64 painter.setOpacity( 1.0 );
65 painter.drawEllipse( ecl );
69 painter.setBrush( Qt::black );
70 painter.setOpacity( 0.12 );
71 painter.drawRoundedRect( bounding, rd, rd );
72 painter.setOpacity( 1.0) ;
73 painter.setBrush( Qt::white );
74 painter.drawEllipse( ecl );
79void SWSwitch::mouseReleaseEvent( QMouseEvent* event )
81 if( event->button() & Qt::LeftButton )
84 _thumb = _isOn ? _brush : QBrush( Qt::lightGray );
88 qDebug() <<
"am einschalten";
89 _anim->setStartValue( _height / 2 );
90 _anim->setEndValue( width() - _height );
91 _anim->setDuration( SWANIMDURATION );
96 _anim->setStartValue( offset() );
97 _anim->setEndValue( _height / 2 );
98 _anim->setDuration( SWANIMDURATION );
102 QAbstractButton::mouseReleaseEvent( event );
106void SWSwitch::enterEvent( QEvent *event )
108 setCursor( Qt::PointingHandCursor );
109 QAbstractButton::enterEvent( event );
113QSize SWSwitch::sizeHint()
const
115 return QSize( 2 * (_height + _margin), _height + 2 * _margin );