Files
BionxControl/bcvalueslider.cpp

266 lines
7.5 KiB
C++
Raw Normal View History

2026-02-09 16:03:09 +01:00
/***************************************************************************
2026-01-10 22:18:54 +01:00
2026-02-09 16:03:09 +01:00
BionxControl
© 2025 -2026 christoph holzheuer
christoph.holzheuer@gmail.com
2026-01-11 11:37:52 +01:00
2026-02-09 16:03:09 +01:00
Using:
2026-01-10 22:18:54 +01:00
2026-02-09 16:03:09 +01:00
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 <QPushButton>
#include <bcvalueslider.h>
BCValueSlider::BCValueSlider( QWidget *parent )
2026-01-18 18:52:30 +01:00
: QWidget(parent)
2026-01-10 22:18:54 +01:00
{
2026-01-11 11:37:52 +01:00
setupUi(this);
2026-01-10 22:18:54 +01:00
2026-01-11 14:48:51 +01:00
// wir wollen ja modern sein
2026-03-06 17:36:27 +01:00
_sliderStyle = std::make_unique<BCValueSliderStyle>();
//_slider->setStyle(_sliderStyle.get());
2026-01-10 22:18:54 +01:00
setAutoFillBackground(true);
2026-01-11 14:48:51 +01:00
2026-01-11 11:37:52 +01:00
QSizePolicy sp = _commitButton->sizePolicy();
sp.setRetainSizeWhenHidden(true); // <--- Das ist der magische Schalter
_commitButton->setSizePolicy(sp);
2026-01-10 22:18:54 +01:00
// Wenn Slider bewegt wird -> Signal nach außen senden
connect(_slider, &QSlider::valueChanged, this, [this](int val)
{
emit valueChanged(val);
});
// Wenn Reset gedrückt wird -> Slider auf 0 (löst auch valueChanged aus)
2026-01-11 11:37:52 +01:00
connect(_commitButton, &QPushButton::clicked, this, [this]()
{
2026-01-21 17:07:00 +01:00
emit valueCommited( value() );
2026-01-10 22:18:54 +01:00
});
2026-01-18 18:52:30 +01:00
2026-01-10 22:18:54 +01:00
}
2026-02-09 16:03:09 +01:00
int BCValueSlider::value() const
2026-01-10 22:18:54 +01:00
{
return _slider->value();
}
2026-02-09 16:03:09 +01:00
void BCValueSlider::setValueAndRange( const BCValue::ValueRange& params )
2026-01-10 22:18:54 +01:00
{
2026-01-21 17:07:00 +01:00
_slider->setRange( params.min, params.max);
2026-01-11 14:48:51 +01:00
// Block Signals verhindern Endlosschleifen, falls das Model
2026-01-10 22:18:54 +01:00
// das Widget während des Updates neu setzt (passiert manchmal bei Live-Updates).
2026-01-21 17:07:00 +01:00
if (params.value != _slider->value())
2026-01-10 22:18:54 +01:00
{
bool blocked = _slider->blockSignals(true);
2026-01-21 17:07:00 +01:00
_slider->setValue(params.value);
2026-01-10 22:18:54 +01:00
_slider->blockSignals(blocked);
}
2026-01-19 16:44:52 +01:00
}
2026-01-22 22:16:19 +01:00
QRect BCValueSlider::clipToSliderRect( const QRect& rect)
2026-01-22 22:16:19 +01:00
{
2026-02-09 16:03:09 +01:00
return rect.adjusted
(
2026-01-22 22:16:19 +01:00
rect.width() - cTextBlockOffset, // Von rechts: cTextBlockOffset (==130) px (Breite der Progress Bar)
2026-02-09 16:03:09 +01:00
0, // Oben: kein Offset
-(cPaddingRight +24 ), // Rechts: 8px Padding
2026-02-09 16:03:09 +01:00
0 // Unten: kein Offset
);
2026-01-22 22:16:19 +01:00
}
2026-02-10 14:25:06 +01:00
/**
* @brief Zeichnet eine passiven Slider, um den möglichen Wertebereich des übergebenen BCValue anzuzeigen.
*/
void BCValueSlider::paintSliderIndicator(QPainter* painter, const QRect& rect, double ratio )
{
// Kleinen Slider-Indikator zeichnen
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
QRect barRect = rect;
2026-02-10 14:25:06 +01:00
2026-03-29 16:35:58 +02:00
const int third = rect.height() / 3;
2026-02-10 14:25:06 +01:00
2026-03-29 16:35:58 +02:00
barRect.setY( rect.y() + third);
barRect.setHeight( third );
2026-02-13 08:19:15 +01:00
2026-02-10 14:25:06 +01:00
// Mini Progress Bar: der Gesamtbereich
painter->setPen(Qt::NoPen);
painter->setBrush(QColor(0xE0E0E0));
painter->drawRoundedRect(barRect, 2, 2);
// Mini Progress Bar: der Wertebereich
barRect.setWidth( ratio * barRect.width() );
painter->setBrush(QColor(0x0078D4));
painter->drawRoundedRect(barRect, 2, 2);
2026-02-13 08:19:15 +01:00
2026-02-10 14:25:06 +01:00
painter->restore();
}
BCValueSlider::BCValueSliderStyle::BCValueSliderStyle()
: QProxyStyle()
{
}
int BCValueSlider::BCValueSliderStyle::pixelMetric(PixelMetric metric, const QStyleOption* option, const QWidget* widget ) const
{
switch (metric)
{
case PM_SliderThickness:
return 24; // Höhe für horizontalen Slider
case PM_SliderLength:
return 16; // Handle-Größe
case PM_SliderControlThickness:
return 16;
case PM_SliderSpaceAvailable:
if (option)
{
if (const QStyleOptionSlider* sliderOpt = qstyleoption_cast<const QStyleOptionSlider*>(option))
{
return sliderOpt->rect.width() - 20;
}
}
return QProxyStyle::pixelMetric(metric, option, widget);
default:
return QProxyStyle::pixelMetric(metric, option, widget);
}
}
QRect BCValueSlider::BCValueSliderStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex* opt,SubControl sc, const QWidget* widget) const
{
if (cc == CC_Slider)
{
2026-02-10 14:25:06 +01:00
if (const QStyleOptionSlider* slider = qstyleoption_cast<const QStyleOptionSlider*>(opt))
{
QRect rect = slider->rect;
int handleSize = 16;
if (sc == SC_SliderHandle)
{
// Handle Position korrekt berechnen
int range = slider->maximum - slider->minimum;
int pos = slider->sliderPosition - slider->minimum;
int pixelRange = rect.width() - handleSize;
int pixelPos = (range != 0) ? (pos * pixelRange) / range : 0;
2026-02-13 08:19:15 +01:00
return QRect(rect.x() + pixelPos, rect.center().y() - handleSize / 2, handleSize, handleSize);
2026-02-10 14:25:06 +01:00
}
}
}
return QProxyStyle::subControlRect(cc, opt, sc, widget);
}
void BCValueSlider::BCValueSliderStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget) const
{
if (control == CC_Slider)
{
if (const QStyleOptionSlider* slider = qstyleoption_cast<const QStyleOptionSlider*>(option)) {
painter->setRenderHint(QPainter::Antialiasing);
// Fluent Colors
QColor accentColor(0, 120, 212); // #0078D4
QColor bgColor(255, 255, 255); // White background
drawHorizontalFluentSlider(painter, slider, accentColor, bgColor);
return;
}
}
QProxyStyle::drawComplexControl(control, option, painter, widget);
}
void BCValueSlider::BCValueSliderStyle::drawHorizontalFluentSlider(QPainter* painter, const QStyleOptionSlider* slider,
const QColor& activeColor,
const QColor& bgColor) const
{
QRect groove = slider->rect;
paintSliderIndicator(painter, groove, 0.5 );
painter->setBrush(Qt::red);
2026-02-10 14:25:06 +01:00
QRect handleRect = subControlRect(CC_Slider, slider, SC_SliderHandle, nullptr);
// Das 'subControlRect' für den SC_SliderHandle ist _nicht_ mittig
2026-02-10 14:25:06 +01:00
handleRect.setY( handleRect.y() + 2 );
qDebug() << " --- drawHorizontalFluentSlider" << groove << " Handle: " << handleRect;
2026-02-10 14:25:06 +01:00
painter->drawRect(handleRect);
return;
// Handle (Thumb) - Fluent style is more subtle
int handleSize = 14;
QRect thumbRect(handleRect.center().x() - handleSize / 2,
handleRect.center().y() - handleSize / 2,
handleSize, handleSize);
/*
// Hover effect - subtle glow
if (slider->state & State_MouseOver)
{
painter->setBrush(QColor(activeColor.red(), activeColor.green(),
activeColor.blue(), 30));
int glowSize = 16;
QRect glow(handle.center().x() - glowSize / 2,
handle.center().y() - glowSize / 2,
glowSize, glowSize);
painter->drawEllipse(glow);
}
*/
// Thumb
painter->setBrush(bgColor);
painter->setPen(QPen(activeColor, 2));
painter->drawEllipse(thumbRect);
/*
// Inner circle for pressed state
if (slider->state & State_Sunken)
{
int innerSize = 6;
QRect inner(handle.center().x() - innerSize / 2,
handle.center().y() - innerSize / 2,
innerSize, innerSize);
painter->setPen(Qt::NoPen);
painter->setBrush(activeColor);
painter->drawEllipse(inner);
}
*/
}