Files
BionxControl/bctoggleswitch.cpp

235 lines
6.3 KiB
C++
Raw Permalink Normal View History

2026-02-09 16:03:09 +01:00
/***************************************************************************
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
***************************************************************************/
2026-01-10 16:37:59 +01:00
2026-01-15 21:26:10 +01:00
#include "bctoggleswitch.h"
2026-01-10 16:37:59 +01:00
#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);
2026-04-01 16:11:28 +02:00
// Widget ist jetzt mit setGeometry / Layouts skalierbar
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
2026-01-10 16:37:59 +01:00
// Animation initialisieren
m_animation = new QPropertyAnimation(this, "position", this);
m_animation->setDuration(200);
m_animation->setEasingCurve(QEasingCurve::OutQuad);
// Signal verknüpfen
2026-03-29 23:30:42 +02:00
connect(this, &QAbstractButton::toggled, this, [this](bool checked)
{
2026-01-10 16:37:59 +01:00
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;
}
2026-01-15 21:26:10 +01:00
void BCToggleSwitch::setPosition(float pos)
{
2026-01-10 16:37:59 +01:00
m_position = pos;
update(); // Trigger Repaint
}
QSize BCToggleSwitch::sizeHint() const
{
return QSize(44, 22);
}
2026-04-01 16:11:28 +02:00
QSize BCToggleSwitch::minimumSizeHint() const
{
return QSize(30, 16);
}
2026-01-10 16:37:59 +01:00
void BCToggleSwitch::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
2026-04-01 16:11:28 +02:00
QRect ownRect = rect();
// Standard-Hintergrundfarbe des aktuellen Qt-Themes
p.fillRect(ownRect, palette().window());
2026-03-30 21:24:29 +02:00
2026-03-31 18:18:37 +02:00
// Auf deaktivierten Zustand prüfen und die statische Methode aufrufen
if (!isEnabled()) {
2026-04-01 16:11:28 +02:00
paintToggleIndicator(&p, ownRect, isChecked(), m_position, palette());
2026-03-31 18:18:37 +02:00
return;
}
2026-04-01 16:11:28 +02:00
// --- Farben aus der aktuellen Palette ---
QColor onColor = palette().color(QPalette::Highlight);
QColor offBorderColor = palette().color(QPalette::Mid);
QColor offKnobColor = palette().color(QPalette::Dark);
QColor knobOnColor = palette().color(QPalette::HighlightedText);
2026-01-10 16:37:59 +01:00
2026-04-01 16:11:28 +02:00
// Proportionale Werte aus der Widget-Höhe ableiten
qreal h = ownRect.height();
qreal radius = h / 2.0;
qreal penW = qMax(1.0, h / 14.0); // Strichstärke skaliert mit
qreal padding = qMax(2.0, h * 0.14); // Innenabstand skaliert mit
2026-01-10 16:37:59 +01:00
// 1. Hintergrund (Track) zeichnen
p.setPen(Qt::NoPen);
if (isChecked() || m_position > 0.5f)
{
// AN-Zustand: Hintergrund gefüllt
p.setBrush(onColor);
2026-04-01 16:11:28 +02:00
p.drawRoundedRect(ownRect, radius, radius);
2026-01-10 16:37:59 +01:00
}
else
{
// AUS-Zustand: Nur Rahmen
p.setBrush(Qt::NoBrush);
// Hover-Status prüfen
if (underMouse())
2026-04-01 16:11:28 +02:00
p.setPen(QPen(offBorderColor.darker(120), penW));
2026-01-10 16:37:59 +01:00
else
2026-04-01 16:11:28 +02:00
p.setPen(QPen(offBorderColor, penW));
2026-01-10 16:37:59 +01:00
// Rechteck etwas verkleinern, damit der Rahmen nicht abgeschnitten wird
2026-04-01 16:11:28 +02:00
qreal inset = penW / 2.0;
p.drawRoundedRect(QRectF(ownRect).adjusted(inset, inset, -inset, -inset),
radius - inset, radius - inset);
2026-01-10 16:37:59 +01:00
}
// 2. Knopf (Thumb) zeichnen
2026-04-01 16:11:28 +02:00
qreal knobDiameter = h - (2 * padding);
2026-01-10 16:37:59 +01:00
// Interpolation der Position
qreal startX = padding;
2026-04-01 16:11:28 +02:00
qreal endX = ownRect.width() - knobDiameter - padding;
2026-01-10 16:37:59 +01:00
qreal currentX = startX + (m_position * (endX - startX));
QRectF knobRect(currentX, padding, knobDiameter, knobDiameter);
2026-01-15 21:26:10 +01:00
if (isChecked() || m_position > 0.5f)
{
2026-04-01 16:11:28 +02:00
p.setBrush(knobOnColor);
2026-01-15 21:26:10 +01:00
}
else
{
if (underMouse())
p.setBrush(offKnobColor.darker(110));
else
p.setBrush(offKnobColor);
2026-01-10 16:37:59 +01:00
}
p.setPen(Qt::NoPen);
p.drawEllipse(knobRect);
}
2026-03-31 18:18:37 +02:00
void BCToggleSwitch::paintToggleIndicator(QPainter* p, const QRect& rect, bool isChecked, float position, const QPalette& palette)
{
2026-04-01 16:11:28 +02:00
// --- Farben für den deaktivierten Zustand aus der Palette ---
QColor disabledBorderColor = palette.color(QPalette::Disabled, QPalette::Mid);
QColor disabledKnobColor = palette.color(QPalette::Disabled, QPalette::Dark);
QColor disabledOnColor = palette.color(QPalette::Disabled, QPalette::Highlight);
QColor disabledKnobOnColor = palette.color(QPalette::Disabled, QPalette::HighlightedText);
2026-03-31 18:18:37 +02:00
QRectF r(rect);
2026-04-01 16:11:28 +02:00
qreal h = r.height();
qreal radius = h / 2.0;
qreal penW = qMax(1.0, h / 14.0);
qreal padding = qMax(2.0, h * 0.14);
2026-03-31 18:18:37 +02:00
// 1. Hintergrund (Track) zeichnen
p->setPen(Qt::NoPen);
if (isChecked || position > 0.5f)
{
// AN-Zustand (deaktiviert)
p->setBrush(disabledOnColor);
p->drawRoundedRect(r, radius, radius);
}
else
{
// AUS-Zustand (deaktiviert)
p->setBrush(Qt::NoBrush);
2026-04-01 16:11:28 +02:00
p->setPen(QPen(disabledBorderColor, penW));
qreal inset = penW / 2.0;
p->drawRoundedRect(r.adjusted(inset, inset, -inset, -inset),
radius - inset, radius - inset);
2026-03-31 18:18:37 +02:00
}
// 2. Knopf (Thumb) zeichnen
2026-04-01 16:11:28 +02:00
qreal knobDiameter = h - (2 * padding);
2026-03-31 18:18:37 +02:00
// X- und Y-Offsets des übergebenen Rects berücksichtigen!
2026-04-01 16:11:28 +02:00
qreal startX = r.x() + padding;
qreal endX = r.x() + r.width() - knobDiameter - padding;
2026-03-31 18:18:37 +02:00
qreal currentX = startX + (position * (endX - startX));
QRectF knobRect(currentX, r.y() + padding, knobDiameter, knobDiameter);
p->setPen(Qt::NoPen);
if (isChecked || position > 0.5f)
{
2026-04-01 16:11:28 +02:00
p->setBrush(disabledKnobOnColor);
2026-03-31 18:18:37 +02:00
}
else
{
p->setBrush(disabledKnobColor);
}
p->drawEllipse(knobRect);
}
2026-04-01 16:11:28 +02:00
2026-01-10 16:37:59 +01:00
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);
}