Add SwitchButton

Simple switch toogle button widget based on
https://github.com/KDAB/kdabtv/blob/master/Styling-Qt-Widgets/toggleswitch.h
This commit is contained in:
WarmUpTill 2023-07-14 19:25:17 +02:00 committed by WarmUpTill
parent d759ded64d
commit 1d7ce510f7
3 changed files with 141 additions and 0 deletions

View File

@ -302,6 +302,8 @@ target_sources(
src/utils/string-list.hpp
src/utils/striped-frame.cpp
src/utils/striped-frame.hpp
src/utils/switch-button.cpp
src/utils/switch-button.hpp
src/utils/sync-helper.cpp
src/utils/sync-helper.hpp
src/utils/transition-selection.cpp

102
src/utils/switch-button.cpp Normal file
View File

@ -0,0 +1,102 @@
#include "switch-button.hpp"
#include <QMouseEvent>
#include <QPainter>
#include <QPalette>
namespace advss {
static const int s_height = 20;
static const int s_innerMargin = 4;
static const int s_handleSize = s_height - s_innerMargin * 2;
static const int s_width = s_handleSize * 2 + s_innerMargin * 2;
SwitchButton::SwitchButton(QWidget *parent) : QWidget{parent}
{
setSizePolicy({QSizePolicy::Fixed, QSizePolicy::Fixed});
setFocusPolicy(Qt::TabFocus);
setAttribute(Qt::WA_Hover);
}
void SwitchButton::setChecked(bool checked)
{
if (_checked == checked) {
return;
}
_checked = checked;
emit toggled(checked);
update();
}
bool SwitchButton::isChecked() const
{
return _checked;
}
void SwitchButton::toggle()
{
setChecked(!_checked);
}
QSize SwitchButton::sizeHint() const
{
return QSize(s_width, s_height);
}
void SwitchButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QPalette pal = palette();
if (!isEnabled()) {
painter.setPen(pal.color(QPalette::Midlight));
painter.setOpacity(0.5);
} else if (_mouseDown) {
painter.setPen(pal.color(QPalette::Light));
} else if (underMouse() || hasFocus()) {
painter.setPen(QPen(pal.brush(QPalette::Highlight), 1));
} else {
painter.setPen(pal.color(QPalette::Midlight));
}
if (_checked) {
painter.setBrush(pal.color(QPalette::Button));
}
const qreal radius = height() / 2;
painter.drawRoundedRect(QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5),
radius, radius);
// Now draw the handle
QRect valueRect = rect().adjusted(s_innerMargin, s_innerMargin,
-s_innerMargin, -s_innerMargin);
valueRect.setWidth(valueRect.height());
if (_checked) {
valueRect.moveLeft(width() / 2);
}
painter.setBrush(pal.color(QPalette::Base));
painter.drawEllipse(valueRect);
}
void SwitchButton::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
_mouseDown = true;
} else {
event->ignore();
}
}
void SwitchButton::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && _mouseDown) {
_mouseDown = false;
toggle();
emit checked(_checked);
} else {
event->ignore();
}
}
} // namespace advss

View File

@ -0,0 +1,37 @@
#pragma once
#include <QWidget>
namespace advss {
/*
Simple switch toogle button widget
Based on https://github.com/KDAB/kdabtv/blob/master/Styling-Qt-Widgets/toggleswitch.h
*/
class SwitchButton : public QWidget {
Q_OBJECT
public:
explicit SwitchButton(QWidget *parent = nullptr);
void setChecked(bool checked);
bool isChecked() const;
void toggle();
QSize sizeHint() const override;
signals:
void checked(bool checked); // by user
void toggled(bool checked); // by user or by program
protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
private:
bool _checked = false;
bool _mouseDown = false;
};
} // namespace advss