From cb553cf92885da437ff3a5eb15f9d817a193d1d1 Mon Sep 17 00:00:00 2001 From: "PANIK\\chris" Date: Sat, 10 Jan 2026 16:37:59 +0100 Subject: [PATCH] Added smile --- BionxControl.pro | 2 + bctoggleswitch.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++++ bctoggleswitch.h | 37 ++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 bctoggleswitch.cpp create mode 100644 bctoggleswitch.h diff --git a/BionxControl.pro b/BionxControl.pro index 75f8f7b..57e49c8 100644 --- a/BionxControl.pro +++ b/BionxControl.pro @@ -53,6 +53,7 @@ SOURCES += \ bcdrivertinycan.cpp \ bcsliderstyle.cpp \ bcthemeswitchbutton.cpp \ + bctoggleswitch.cpp \ bctransmitter.cpp \ bcvalue.cpp \ bcvaluemodel.cpp \ @@ -72,6 +73,7 @@ HEADERS += \ bcmainwindow.h \ bcsliderstyle.h \ bcthemeswitchbutton.h \ + bctoggleswitch.h \ bctransmitter.h \ bcvalue.h \ bcvaluemodel.h \ diff --git a/bctoggleswitch.cpp b/bctoggleswitch.cpp new file mode 100644 index 0000000..9eedb6b --- /dev/null +++ b/bctoggleswitch.cpp @@ -0,0 +1,122 @@ + +#include +#include +#include +#include +#include + +#include + + + + +BCToggleSwitch::BCToggleSwitch(QWidget *parent) + : QAbstractButton(parent) + , m_position(0.0f) +{ + setCheckable(true); + setCursor(Qt::PointingHandCursor); + setFixedSize(44, 22); // Standardgröße + + // Animation initialisieren + m_animation = new QPropertyAnimation(this, "position", this); + m_animation->setDuration(200); + m_animation->setEasingCurve(QEasingCurve::OutQuad); + + // Signal verknüpfen + connect(this, &QAbstractButton::toggled, this, [this](bool checked){ + m_animation->stop(); + m_animation->setStartValue(m_position); + m_animation->setEndValue(checked ? 1.0f : 0.0f); + m_animation->start(); + }); +} + +float BCToggleSwitch::position() const +{ + return m_position; +} + +void BCToggleSwitch::setPosition(float pos) { + m_position = pos; + update(); // Trigger Repaint +} + +QSize BCToggleSwitch::sizeHint() const +{ + return QSize(44, 22); +} + +void BCToggleSwitch::paintEvent(QPaintEvent *) +{ + QPainter p(this); + p.setRenderHint(QPainter::Antialiasing); + + // --- Farben --- + // Tipp: In einem echten Projekt diese Farben als const statics + // oder aus der QPalette laden. + QColor offBorderColor = QColor("#8D8D8D"); + QColor offKnobColor = QColor("#5D5D5D"); + QColor onColor = QColor("#0078D4"); // Fluent Blue + QColor white = Qt::white; + + QRectF rect = this->rect(); + qreal radius = rect.height() / 2.0; + + // 1. Hintergrund (Track) zeichnen + p.setPen(Qt::NoPen); + + if (isChecked() || m_position > 0.5f) + { + // AN-Zustand: Hintergrund gefüllt + p.setBrush(onColor); + p.drawRoundedRect(rect, radius, radius); + } + else + { + // AUS-Zustand: Nur Rahmen + p.setBrush(Qt::NoBrush); + + // Hover-Status prüfen + if (underMouse()) + p.setPen(QPen(offBorderColor.darker(120), 1.5)); + else + p.setPen(QPen(offBorderColor, 1.5)); + + // Rechteck etwas verkleinern, damit der Rahmen nicht abgeschnitten wird + p.drawRoundedRect(rect.adjusted(1, 1, -1, -1), radius - 1, radius - 1); + } + + // 2. Knopf (Thumb) zeichnen + qreal padding = 3.0; + qreal knobDiameter = rect.height() - (2 * padding); + + // Interpolation der Position + qreal startX = padding; + qreal endX = rect.width() - knobDiameter - padding; + qreal currentX = startX + (m_position * (endX - startX)); + + QRectF knobRect(currentX, padding, knobDiameter, knobDiameter); + + if (isChecked() || m_position > 0.5f) { + p.setBrush(white); + } else { + if (underMouse()) p.setBrush(offKnobColor.darker(110)); + else p.setBrush(offKnobColor); + } + + p.setPen(Qt::NoPen); + p.drawEllipse(knobRect); +} + +void BCToggleSwitch::enterEvent(QEnterEvent *event) +{ + update(); // Für Hover-Effekt neu zeichnen + QAbstractButton::enterEvent(event); +} + +void BCToggleSwitch::leaveEvent(QEvent *event) +{ + update(); // Hover entfernen + QAbstractButton::leaveEvent(event); +} diff --git a/bctoggleswitch.h b/bctoggleswitch.h new file mode 100644 index 0000000..24d6ade --- /dev/null +++ b/bctoggleswitch.h @@ -0,0 +1,37 @@ +#ifndef BCTOGGLESWITCH_H +#define BCTOGGLESWITCH_H + +#include + +// Vorwärtsdeklaration spart Include-Zeit +class QPropertyAnimation; + +class BCToggleSwitch : public QAbstractButton +{ + Q_OBJECT + // Property für die Animation (0.0 bis 1.0) + Q_PROPERTY(float position READ position WRITE setPosition) + +public: + + explicit BCToggleSwitch(QWidget *parent = nullptr); + + // Getter & Setter für die Animations-Property + float position() const; + void setPosition(float pos); + + QSize sizeHint() const override; + +protected: + + void paintEvent(QPaintEvent *event) override; + void enterEvent(QEnterEvent *event) override; + void leaveEvent(QEvent *event) override; + + float m_position; // 0.0 = Aus, 1.0 = An + QPropertyAnimation *m_animation; +}; + + + +#endif // BCTOGGLESWITCH_H