Files
BionxControl/bcanimateddelegate.cpp

388 lines
9.9 KiB
C++
Raw Normal View History

2025-12-26 14:07:55 +01:00
/***************************************************************************
BionxControl
2026-01-03 23:51:14 +01:00
© 2025 -2026 christoph holzheuer
2025-12-26 14:07:55 +01:00
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
***************************************************************************/
2025-12-17 16:26:22 +01:00
#include <QSlider>
#include <QLabel>
#include <QHBoxLayout>
#include <QWidget>
#include <QDebug>
2025-12-19 17:37:24 +01:00
#include <QPainter>
#include <QTimer>
2025-12-24 14:44:54 +01:00
#include <QTableView>
2025-12-17 16:26:22 +01:00
2025-12-19 21:20:14 +01:00
#include <QVariantAnimation>
#include <QPropertyAnimation>
#include <QPainter>
2025-12-29 23:29:56 +01:00
#include "bcanimateddelegate.h"
2026-01-09 00:45:26 +01:00
#include "bcsliderstyle.h"
2025-12-19 21:20:14 +01:00
2025-12-29 20:10:05 +01:00
2025-12-29 23:29:56 +01:00
BCAnimatedDelegate::BCAnimatedDelegate(const BCValueList& valueList, QTableView* view)
2025-12-29 15:44:06 +01:00
: QStyledItemDelegate{view}, _valueList{valueList}, _view{view}
{
}
2025-12-17 16:26:22 +01:00
2025-12-29 23:29:56 +01:00
void BCAnimatedDelegate::setEditorData(QWidget *editor, const QModelIndex& index) const
2025-12-17 16:26:22 +01:00
{
2026-01-07 17:13:35 +01:00
/*
2025-12-17 16:26:22 +01:00
// Daten vom Model in den Editor laden
2025-12-28 22:48:18 +01:00
const BCValue& bc = *index.data(Qt::EditRole).value<BCValue*>();
2025-12-17 16:26:22 +01:00
QSlider *slider = editor->findChild<QSlider*>("slider");
QLabel *lblUnit = editor->findChild<QLabel*>("lblUnit");
2026-01-07 17:13:35 +01:00
if (slider && lblUnit)
{
2025-12-21 18:31:16 +01:00
bool olDriverState = slider->blockSignals(true);
2026-01-07 17:13:35 +01:00
slider->setValue(bc.formattedValue.toInt());
2025-12-21 18:31:16 +01:00
slider->blockSignals(olDriverState);
2026-01-07 17:13:35 +01:00
lblUnit->setText(QString("%1 %2").arg(bc.formattedValue.toInt()).arg( "mm3"));
}
else
{
2025-12-17 16:26:22 +01:00
QStyledItemDelegate::setEditorData(editor, index);
}
2026-01-07 17:13:35 +01:00
*/
2025-12-17 16:26:22 +01:00
}
2025-12-29 23:29:56 +01:00
void BCAnimatedDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const
2025-12-17 16:26:22 +01:00
{
// Daten vom Editor zurück ins Model speichern (Beim Schließen)
QSlider *slider = editor->findChild<QSlider*>("slider");
2026-01-07 17:13:35 +01:00
if (slider)
{
2025-12-17 16:26:22 +01:00
int value = slider->value();
model->setData(index, value, Qt::EditRole);
2026-01-07 17:13:35 +01:00
}
else
{
2025-12-17 16:26:22 +01:00
QStyledItemDelegate::setModelData(editor, model, index);
}
}
2025-12-29 23:29:56 +01:00
void BCAnimatedDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index) const
2025-12-17 16:26:22 +01:00
{
2026-01-09 06:19:37 +01:00
Q_UNUSED(index)
/*
QRect barRect = option.rect.adjusted(option.rect.width() - 55,
option.rect.height() / 2 - 2,
-8,
-option.rect.height() / 2 + 2);
*/
QRect sliderRect = option.rect.adjusted(
option.rect.width() - 125, // Von rechts: 115px (Breite der Progress Bar)
0, // Oben: kein Offset
-8, // Rechts: 8px Padding
0 // Unten: kein Offset
);
editor->setGeometry(sliderRect); // Slider nur über Progress Bar
2026-01-09 10:47:29 +01:00
2025-12-17 16:26:22 +01:00
}
2025-12-18 18:39:52 +01:00
2025-12-29 23:29:56 +01:00
QSize BCAnimatedDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex& index) const
2025-12-18 18:39:52 +01:00
{
return QStyledItemDelegate::sizeHint(option,index);
/*
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
opt.text = formatDisplayString(index);
QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
return style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), opt.widget);
*/
}
2025-12-19 17:37:24 +01:00
2025-12-29 23:29:56 +01:00
void BCAnimatedDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
2025-12-19 17:37:24 +01:00
{
2025-12-29 15:44:06 +01:00
2026-01-06 15:59:57 +01:00
// Standard-Zeichnen (Text, Hintergrund, Selection) durchführen
2025-12-19 17:37:24 +01:00
QStyledItemDelegate::paint(painter, option, index);
2026-01-06 15:59:57 +01:00
int row = index.row();
2025-12-19 21:20:14 +01:00
2026-01-09 06:19:37 +01:00
if( index.column() == 1 )
2025-12-19 21:20:14 +01:00
{
2026-01-09 06:19:37 +01:00
if( row>-1 && row <= _valueList.size() )
{
const BCValue& bcValue = *(_valueList[ index.row()].get());
if( !bcValue.isReadOnly())
2026-01-09 10:47:29 +01:00
paintSliderIndicator(painter,option,bcValue);
2026-01-09 06:19:37 +01:00
}
if(_rowOpacities.contains(row))
2026-01-09 10:47:29 +01:00
paintHighlightRow(painter,option,index.row());
2026-01-09 06:19:37 +01:00
}
}
2025-12-19 21:20:14 +01:00
2026-01-09 06:19:37 +01:00
QWidget* BCAnimatedDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex& index) const
{
const BCValue& bcValue = *(_valueList[ index.row()].get());
2025-12-19 21:20:14 +01:00
2026-01-09 06:19:37 +01:00
Q_UNUSED(option)
Q_UNUSED(index)
auto* slider = new QSlider(Qt::Horizontal, parent);
slider->setRange(0, 100);
slider->setSingleStep(1);
slider->setPageStep(10);
slider->setStyle(new FluentSliderStyle());
// Signal für sofortige Updates
connect(slider, &QSlider::valueChanged, this, [this, slider]()
{
// Commit data sofort bei Änderung
emit const_cast<BCAnimatedDelegate*>(this)->commitData(slider);
});
return slider;
2025-12-19 17:37:24 +01:00
}
2025-12-19 21:20:14 +01:00
2026-01-09 00:45:26 +01:00
2026-01-08 14:55:47 +01:00
void BCAnimatedDelegate::paintHighlightRow(QPainter* painter, const QStyleOptionViewItem& option, int row) const
2025-12-29 20:10:05 +01:00
{
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
2026-01-08 00:25:36 +01:00
qreal opacity =_rowOpacities.value(row);
2025-12-21 23:20:22 +01:00
painter->setOpacity(opacity);
// Margin von 4px
2025-12-29 20:10:05 +01:00
QRect itemRect = option.rect.adjusted(3, 3, -3, -3);
2025-12-21 23:20:22 +01:00
// Border (2px solid #2196F3)
2026-01-08 14:55:47 +01:00
// oranger rahmen
QPen borderPen( QColor(0xFF8C00), 1);
2025-12-21 23:20:22 +01:00
painter->setPen(borderPen);
painter->setBrush(Qt::NoBrush);
2026-01-08 14:55:47 +01:00
// highlight background
//QColor highlightColor = option.palette.highlight().color();
//highlightColor.setAlphaF(0.3); // 0.0 bis 1.0 (float ist oft lesbarer)
//painter->fillRect(option.rect, highlightColor);
2025-12-29 20:10:05 +01:00
painter->drawRoundedRect(itemRect, 2, 2);
2025-12-21 23:20:22 +01:00
painter->restore();
2026-01-06 15:59:57 +01:00
2025-12-21 23:20:22 +01:00
}
2026-01-09 00:45:26 +01:00
/**
* @brief Zeichnet eine passiven Slider, um den Wertebereich des übergebenen BCValue anzuzeigen.
* @param painter
* @param option
* @param bcValue
*/
2026-01-08 14:55:47 +01:00
void BCAnimatedDelegate::paintSliderIndicator(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const
2026-01-06 15:59:57 +01:00
{
2026-01-09 06:19:37 +01:00
// Text und kleiner Slider-Indikator zeichnen
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
/*
QRect barRect = option.rect.adjusted(option.rect.width() - 130,
option.rect.height() / 2 - 2,
-8,
-option.rect.height() / 2 + 2);
*/
QRect barRect = option.rect.adjusted(option.rect.width() - 130,
option.rect.height() / 2 + 1,
-8,
-option.rect.height() / 2 - 3);
double ratio = bcValue.calcRatio();
if( ratio)
{
// Mini Progress Bar
painter->setPen(Qt::NoPen);
painter->setBrush(QColor(0xE0E0E0));
painter->drawRoundedRect(barRect, 2, 2);
QRect fillRect = barRect;
fillRect.setWidth(barRect.width() * ratio);
painter->setBrush(QColor(0x0078D4));
painter->drawRoundedRect(fillRect, 2, 2);
}
painter->restore();
/// -------
/*
2026-01-06 16:21:59 +01:00
// Hintergrund
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.highlight());
}
else
{
2026-01-06 22:39:41 +01:00
QColor bcColor = option.palette.color(QPalette::Base);
painter->fillRect(option.rect, bcColor);
2026-01-06 16:21:59 +01:00
}
2026-01-09 06:19:37 +01:00
*/
2026-01-06 16:21:59 +01:00
2026-01-09 06:19:37 +01:00
/*
2026-01-07 17:13:35 +01:00
// baby-Slider-Indikator zeichnen
2026-01-09 00:45:26 +01:00
// Anteil zwischen min und max berechnen
double ratio = bcValue.calcRatio();
if( !ratio)
return;
2026-01-06 16:21:59 +01:00
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
QRect barRect = option.rect.adjusted
(
8,
option.rect.height() / 2 - 2,
-8,
-option.rect.height() / 2 + 2
);
// Mini Progress Bar
painter->setPen(Qt::NoPen);
2026-01-09 00:45:26 +01:00
QColor disabledText = option.palette.color(QPalette::Disabled, QPalette::Text);
painter->setBrush(disabledText);
2026-01-06 16:21:59 +01:00
painter->drawRoundedRect(barRect, 2, 2);
2026-01-07 17:13:35 +01:00
barRect.setWidth(barRect.width() * ratio );
2026-01-06 16:21:59 +01:00
painter->setBrush(QColor(0x0078D4));
2026-01-09 00:45:26 +01:00
//painter->setBrush(Qt::green);
//painter->setBrush( );
2026-01-07 17:13:35 +01:00
painter->drawRoundedRect(barRect, 2, 2);
2026-01-06 16:21:59 +01:00
painter->restore();
2026-01-09 06:19:37 +01:00
*/
2026-01-06 15:59:57 +01:00
}
2025-12-19 21:20:14 +01:00
2026-01-09 00:45:26 +01:00
/**
* @brief Startet die Animation für die übergebene Zeile
* @param row
*/
2025-12-29 23:29:56 +01:00
void BCAnimatedDelegate::onHighlightRow(int row)
2025-12-19 21:20:14 +01:00
{
// Alte Animation für diese Zeile stoppen falls vorhanden
2026-01-08 00:25:36 +01:00
if (_rowAnimations.contains(row))
2025-12-19 21:20:14 +01:00
{
2026-01-08 00:25:36 +01:00
_rowAnimations[row]->stop();
_rowAnimations[row]->deleteLater();
2025-12-19 21:20:14 +01:00
}
// QVariantAnimation ist flexibler als QPropertyAnimation
auto* anim = new QVariantAnimation(this);
anim->setDuration(800);
anim->setStartValue(0.0);
anim->setEndValue(1.0);
// Custom Easing für Fade-in/out Effekt
anim->setEasingCurve(QEasingCurve::OutQuad);
connect(anim, &QVariantAnimation::valueChanged, this, [this, row](const QVariant& value)
{
qreal progress = value.toReal();
qreal opacity;
// Schnelles Fade-in (20%), langsames Fade-out (80%)
if (progress < 0.2) {
opacity = progress * 5.0; // 0->1 in 20%
} else {
opacity = 1.0 - ((progress - 0.2) / 0.8); // 1->0 in 80%
}
2026-01-08 00:25:36 +01:00
_rowOpacities[row] = opacity;
2025-12-19 21:20:14 +01:00
updateRow(row);
});
connect(anim, &QVariantAnimation::finished, this, [this, row, anim]()
{
2026-01-08 00:25:36 +01:00
_rowOpacities.remove(row);
_rowAnimations.remove(row);
2025-12-19 21:20:14 +01:00
updateRow(row);
anim->deleteLater();
});
2026-01-08 00:25:36 +01:00
_rowAnimations[row] = anim;
2025-12-19 21:20:14 +01:00
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
2026-01-09 00:45:26 +01:00
/**
* @brief Sopt alle gerade laufenden Animationen
*/
2025-12-29 23:29:56 +01:00
void BCAnimatedDelegate::clearAllHighlights()
2025-12-19 21:20:14 +01:00
{
2026-01-08 00:25:36 +01:00
for(auto* anim : std::as_const(_rowAnimations))
2025-12-19 21:20:14 +01:00
{
anim->stop();
anim->deleteLater();
}
2026-01-08 00:25:36 +01:00
_rowAnimations.clear();
_rowOpacities.clear();
2025-12-19 21:20:14 +01:00
if (_view)
{
_view->viewport()->update();
}
}
2026-01-09 00:45:26 +01:00
/**
* @brief Zeichnet die übegebene Zeile neu.
* @param row
*/
2025-12-29 23:29:56 +01:00
void BCAnimatedDelegate::updateRow(int row)
2025-12-19 21:20:14 +01:00
{
2025-12-20 01:23:57 +01:00
if (_view && _view->model() && row >= 0)
{
2025-12-29 20:10:05 +01:00
QModelIndex idx = _view->model()->index(row,1);
2025-12-19 21:20:14 +01:00
QRect rect = _view->visualRect(idx);
if (!rect.isEmpty()) {
_view->viewport()->update(rect);
}
}
}