Files
raDIYo/swtoggleswitch.h
2025-08-05 22:36:00 +02:00

97 lines
2.3 KiB
C++

/***************************************************************************
source::worx libWidgets
Copyright © 2021 c.holzheuer
chris@sourceworx.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
***************************************************************************/
//
// https://stackoverflow.com/questions/14780517/toggle-switch-in-qt
// Abbas Perçin
// Computer Engineer
#ifndef SWTOGGLESWITCH_H
#define SWTOGGLESWITCH_H
#include <QWidget>
#include <QPushButton>
#include <QDebug>
#include <swdialwidget.h>
class QPropertyAnimation;
class SWDialAdapter;
/**
* @brief Widget, das einen Schalter im Android-Style darstellt.
* gefunden auf StackOverflow:
* https://stackoverflow.com/questions/14780517/toggle-switch-in-qt/51023362
*/
class SWToggleSwitch : public QAbstractButton, public SWDialWidget
{
Q_OBJECT
Q_PROPERTY( int _offset READ offset WRITE setOffset NOTIFY onOffsetChanged )
public:
explicit SWToggleSwitch( QWidget* parent = nullptr, int trackRadius=16, int thumbRadius=20 );
//explicit SWToggleSwitch( int trackRadius, int thumbRadius, QWidget* parent = nullptr );
virtual ~SWToggleSwitch();
void setTrackAndThumbRadius( int trackRadius, int thumbRadius );
QSize sizeHint() const override;
public slots:
void onOffsetChanged()
{}
//void onDialPushed() override;
void setChecked( bool checked );
signals:
void idActivated( int );
void widgetClicked( SWDialWidget* );
protected:
void paintEvent(QPaintEvent *) override;
void resizeEvent(QResizeEvent*) override;
void mouseReleaseEvent(QMouseEvent *) override;
void enterEvent(QEvent *event) override;
int offset();
void setOffset(int value);
private:
qreal _offset;
qreal _baseOffset;
qreal _margin;
qreal _trackRadius;
qreal _thumbRadius;
qreal _opacity;
QPropertyAnimation* _animation;
QHash<bool, qreal> _endOffset;
QHash<bool, QBrush> _trackColor;
QHash<bool, QBrush> _thumbColor;
QHash<bool, QColor> _textColor;
QHash<bool, QString> _thumbText;
};
#endif // SWTOGGLESWITCH_H