Files
BionxControl/bctoggleswitch.cpp
Christoph Holzheuer 7993247027 cleanups.
2026-02-09 16:03:09 +01:00

157 lines
3.8 KiB
C++

/***************************************************************************
BionxControl
© 2025 -2026 christoph holzheuer
christoph.holzheuer@gmail.com
Using:
mhs_can_drv.c
© 2011 - 2023 by MHS-Elektronik GmbH & Co. KG, Germany
Klaus Demlehner, klaus@mhs-elektronik.de
@see www.mhs-elektronik.de
Based on Bionx data type descriptions from:
BigXionFlasher USB V 0.2.4 rev. 97
© 2011-2013 by Thomas Koenig <info@bigxionflasher.org>
@see www.bigxionflasher.org
Bionx Bike Info
© 2018 Thorsten Schmidt (tschmidt@ts-soft.de)
@see www.ts-soft.de
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 3 of the License, or
(at your option) any later version.
@see https://github.com/bikemike/bionx-bikeinfo
***************************************************************************/
#include "bctoggleswitch.h"
#include <QPainter>
#include <QPropertyAnimation>
#include <QEasingCurve>
#include <QEnterEvent>
#include <QEvent>
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(0x8D8D8D);
QColor offKnobColor = QColor(0x5D5D5D);
QColor onColor = QColor(0x0078D4); // 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);
}