Fixed slider painting, part III
This commit is contained in:
265
trash/bcvaluesliderold.cpp
Normal file
265
trash/bcvaluesliderold.cpp
Normal file
@@ -0,0 +1,265 @@
|
||||
/***************************************************************************
|
||||
|
||||
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 <QPushButton>
|
||||
|
||||
#include <bcvaluesliderold.h>
|
||||
|
||||
|
||||
BCValueSliderOld::BCValueSliderOld( QWidget *parent )
|
||||
: QWidget(parent)
|
||||
{
|
||||
setupUi(this);
|
||||
|
||||
// wir wollen ja modern sein
|
||||
_sliderStyle = std::make_unique<BCValueSliderStyle>();
|
||||
//_slider->setStyle(_sliderStyle.get());
|
||||
setAutoFillBackground(true);
|
||||
|
||||
QSizePolicy sp = _commitButton->sizePolicy();
|
||||
sp.setRetainSizeWhenHidden(true); // <--- Das ist der magische Schalter
|
||||
_commitButton->setSizePolicy(sp);
|
||||
|
||||
// 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)
|
||||
connect(_commitButton, &QPushButton::clicked, this, [this]()
|
||||
{
|
||||
emit valueCommited( value() );
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
int BCValueSliderOld::value() const
|
||||
{
|
||||
return _slider->value();
|
||||
}
|
||||
|
||||
void BCValueSliderOld::setValueAndRange( const BCValue::ValueRange& params )
|
||||
{
|
||||
_slider->setRange( params.min, params.max);
|
||||
// Block Signals verhindern Endlosschleifen, falls das Model
|
||||
// das Widget während des Updates neu setzt (passiert manchmal bei Live-Updates).
|
||||
if (params.value != _slider->value())
|
||||
{
|
||||
bool blocked = _slider->blockSignals(true);
|
||||
_slider->setValue(params.value);
|
||||
_slider->blockSignals(blocked);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
QRect BCValueSliderOld::clipToSliderRect( const QRect& rect)
|
||||
{
|
||||
return rect.adjusted
|
||||
(
|
||||
rect.width() - cTextBlockOffset, // Von rechts: cTextBlockOffset (==130) px (Breite der Progress Bar)
|
||||
0, // Oben: kein Offset
|
||||
-(cPaddingRight +24 ), // Rechts: 8px Padding
|
||||
0 // Unten: kein Offset
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Zeichnet eine passiven Slider, um den möglichen Wertebereich des übergebenen BCValue anzuzeigen.
|
||||
*/
|
||||
|
||||
void BCValueSliderOld::paintSliderIndicator(QPainter* painter, const QRect& rect, double ratio )
|
||||
{
|
||||
// Kleinen Slider-Indikator zeichnen
|
||||
painter->save();
|
||||
painter->setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
QRect barRect = rect;
|
||||
|
||||
const int third = rect.height() / 3;
|
||||
|
||||
barRect.setY( rect.y() + third);
|
||||
barRect.setHeight( third );
|
||||
|
||||
// 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);
|
||||
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
|
||||
|
||||
BCValueSliderOld::BCValueSliderStyle::BCValueSliderStyle()
|
||||
: QProxyStyle()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int BCValueSliderOld::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 BCValueSliderOld::BCValueSliderStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex* opt,SubControl sc, const QWidget* widget) const
|
||||
{
|
||||
if (cc == CC_Slider)
|
||||
{
|
||||
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;
|
||||
|
||||
return QRect(rect.x() + pixelPos, rect.center().y() - handleSize / 2, handleSize, handleSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
return QProxyStyle::subControlRect(cc, opt, sc, widget);
|
||||
}
|
||||
|
||||
void BCValueSliderOld::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 BCValueSliderOld::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);
|
||||
QRect handleRect = subControlRect(CC_Slider, slider, SC_SliderHandle, nullptr);
|
||||
// Das 'subControlRect' für den SC_SliderHandle ist _nicht_ mittig
|
||||
|
||||
handleRect.setY( handleRect.y() + 2 );
|
||||
|
||||
qDebug() << " --- drawHorizontalFluentSlider" << groove << " Handle: " << handleRect;
|
||||
|
||||
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);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
67
trash/bcvaluesliderold.h
Normal file
67
trash/bcvaluesliderold.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#ifndef BC_VALUESLIDER_H
|
||||
#define BC_VALUESLIDER_H
|
||||
|
||||
|
||||
#include <QWidget>
|
||||
#include <QTableView>
|
||||
#include <QSlider>
|
||||
#include <QPainter>
|
||||
#include <QProxyStyle>
|
||||
|
||||
#include <bcvalue.h>
|
||||
#include <ui_bcvaluesliderold.h>
|
||||
|
||||
class QSlider;
|
||||
class QPushButton;
|
||||
class BCValue;
|
||||
|
||||
|
||||
class BCValueSliderOld : public QWidget, private Ui::BCValueSliderOld
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
explicit BCValueSliderOld(QWidget *parent = nullptr);
|
||||
|
||||
int value() const;
|
||||
void setValueAndRange( const BCValue::ValueRange& params );
|
||||
|
||||
// helper functions
|
||||
static QRect clipToSliderRect( const QRect& rect);
|
||||
static void paintSliderIndicator(QPainter* painter, const QRect& rect, double ratio );
|
||||
|
||||
static constexpr int cToolIconOffset = 24;
|
||||
|
||||
signals:
|
||||
|
||||
void valueChanged(int value);
|
||||
void valueCommited(int value);
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
// Fluent Design Slider Style
|
||||
class BCValueSliderStyle : public QProxyStyle
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
BCValueSliderStyle();
|
||||
|
||||
int pixelMetric(PixelMetric metric, const QStyleOption* option = nullptr, const QWidget* widget = nullptr) const override;
|
||||
|
||||
QRect subControlRect(ComplexControl cc, const QStyleOptionComplex* opt, SubControl sc, const QWidget* widget) const override;
|
||||
void drawComplexControl(ComplexControl control, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget) const override;
|
||||
void drawHorizontalFluentSlider(QPainter* painter, const QStyleOptionSlider* slider, const QColor& activeColor, const QColor& bgColor) const;
|
||||
|
||||
};
|
||||
|
||||
static constexpr int cTextBlockOffset = 130;
|
||||
static constexpr int cPaddingRight = 8;
|
||||
static constexpr int cSliderWidth = 117;
|
||||
|
||||
std::unique_ptr<BCValueSliderStyle> _sliderStyle;
|
||||
};
|
||||
|
||||
#endif // BC_VALUESLIDER_H
|
||||
95
trash/bcvaluesliderold.ui
Normal file
95
trash/bcvaluesliderold.ui
Normal file
@@ -0,0 +1,95 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>BCValueSliderOld</class>
|
||||
<widget class="QWidget" name="BCValueSliderOld">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>111</width>
|
||||
<height>24</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>60</width>
|
||||
<height>24</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<property name="spacing">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="sizeConstraint">
|
||||
<enum>QLayout::SizeConstraint::SetMinimumSize</enum>
|
||||
</property>
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QSlider" name="_slider">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Orientation::Horizontal</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="_commitButton">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>24</width>
|
||||
<height>24</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>16777212</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="cursor">
|
||||
<cursorShape>PointingHandCursor</cursorShape>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="bionxcontrol.qrc">
|
||||
<normaloff>:/update.png</normaloff>:/update.png</iconset>
|
||||
</property>
|
||||
<property name="iconSize">
|
||||
<size>
|
||||
<width>24</width>
|
||||
<height>24</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="popupMode">
|
||||
<enum>QToolButton::ToolButtonPopupMode::InstantPopup</enum>
|
||||
</property>
|
||||
<property name="toolButtonStyle">
|
||||
<enum>Qt::ToolButtonStyle::ToolButtonIconOnly</enum>
|
||||
</property>
|
||||
<property name="autoRaise">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources>
|
||||
<include location="bionxcontrol.qrc"/>
|
||||
</resources>
|
||||
<connections/>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user