Files
BionxControl/bcanimateddelegate.cpp

363 lines
9.4 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"
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-29 20:10:05 +01:00
/*
2025-12-29 23:29:56 +01:00
QString BCAnimatedDelegate::displayText(const QVariant& dataValue, const QLocale& locale) const
2025-12-17 16:26:22 +01:00
{
// Wir prüfen, ob im Variant unser Struct steckt
2026-01-02 01:43:49 +01:00
if (dataValue.canConvert<const BCValue&>())
{
2026-01-02 01:43:49 +01:00
const BCValue& bc = dataValue.value<const BCValue/>();
2025-12-29 15:44:06 +01:00
//qDebug() << " --- YES: " << bc.label;
2025-12-17 16:26:22 +01:00
// Hier bauen wir den String zusammen, den man sieht,
// wenn KEIN Editor offen ist.
// Format: "Label: Wert Einheit"
2026-01-07 17:13:35 +01:00
return QString("%1: %2 %3").arg(bc.label, bc.formattedValue, "mmX");
2025-12-17 16:26:22 +01:00
}
2025-12-22 21:27:20 +01:00
else
{
2026-01-07 17:13:35 +01:00
//qDebug() << " --- Nö!";
2025-12-22 21:27:20 +01:00
}
2025-12-17 16:26:22 +01:00
// Fallback für normale Strings/Zahlen
return QStyledItemDelegate::displayText(dataValue, locale);
2025-12-17 16:26:22 +01:00
}
2025-12-29 20:10:05 +01:00
*/
2025-12-17 16:26:22 +01:00
2025-12-29 23:29:56 +01:00
QWidget *BCAnimatedDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, 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
QVariant rawData = index.data(Qt::EditRole);
2025-12-28 22:48:18 +01:00
//if (!rawData.canConvert<BCValue*>())
2025-12-17 16:26:22 +01:00
return QStyledItemDelegate::createEditor(parent, option, index);
2026-01-07 17:13:35 +01:00
2025-12-28 22:48:18 +01:00
const BCValue& bc = *rawData.value<BCValue*>();
2025-12-17 16:26:22 +01:00
// Nur bei Integern den Slider-Editor bauen
if (bc.value.typeId() == QMetaType::Int)
{
QWidget *container = new QWidget(parent);
container->setAutoFillBackground(true);
QHBoxLayout *layout = new QHBoxLayout(container);
layout->setContentsMargins(4, 0, 4, 0);
layout->setSpacing(10);
// Linkes Label (Name)
QLabel *lblName = new QLabel(bc.label, container);
lblName->setFixedWidth(80);
// Slider
QSlider *slider = new QSlider(Qt::Horizontal, container);
slider->setRange(0, 100);
slider->setObjectName("slider");
// Rechtes Label (Vorschau Wert + Einheit)
QLabel *lblUnit = new QLabel(container);
lblUnit->setFixedWidth(60);
lblUnit->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
lblUnit->setObjectName("lblUnit");
layout->addWidget(lblName);
layout->addWidget(slider);
layout->addWidget(lblUnit);
// Live-Update des Labels im Editor (aber noch kein Speichern im Model)
connect(slider, &QSlider::valueChanged, this, [=](int val){
lblUnit->setText(QString("%1 %2").arg(val).arg("mm2"));
});
return container;
2025-12-24 12:11:59 +01:00
2025-12-17 16:26:22 +01:00
}
2026-01-07 17:13:35 +01:00
*/
2025-12-17 16:26:22 +01:00
return QStyledItemDelegate::createEditor(parent, option, 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::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
{
2026-01-07 17:13:35 +01:00
/*
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);
}
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::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index) const
2025-12-17 16:26:22 +01:00
{
2025-12-29 15:44:06 +01:00
// __fix!
2025-12-17 16:26:22 +01:00
editor->setGeometry(option.rect);
}
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();
int col = index.column();
2025-12-19 21:20:14 +01:00
2026-01-06 15:59:57 +01:00
switch (col)
2025-12-19 21:20:14 +01:00
{
2026-01-06 15:59:57 +01:00
case 1:
2025-12-19 21:20:14 +01:00
2026-01-08 00:25:36 +01:00
if(_rowOpacities.contains(row))
2026-01-06 15:59:57 +01:00
paintHighlightRow(painter,option,index);
break;
2025-12-19 21:20:14 +01:00
2026-01-06 15:59:57 +01:00
case 2:
2025-12-19 21:20:14 +01:00
2026-01-06 15:59:57 +01:00
if( row>-1 && row <= _valueList.size() )
paintSliderIndicator(painter,option,index);
2025-12-19 21:20:14 +01:00
2026-01-06 15:59:57 +01:00
default:
break;
2025-12-19 21:20:14 +01:00
}
2025-12-19 17:37:24 +01:00
}
2025-12-19 21:20:14 +01:00
2025-12-29 23:29:56 +01:00
void BCAnimatedDelegate::paintHighlightRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
2025-12-29 20:10:05 +01:00
{
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
2025-12-21 23:20:22 +01:00
int row = index.row();
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)
2025-12-29 20:10:05 +01:00
QPen borderPen( Qt::red, 1);
2025-12-21 23:20:22 +01:00
painter->setPen(borderPen);
painter->setBrush(Qt::NoBrush);
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-06 15:59:57 +01:00
void BCAnimatedDelegate::paintSliderIndicator(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
2026-01-07 17:13:35 +01:00
const BCValue& bcValue = *(_valueList[ index.row()].get());
qDebug() << " --- paintSLider: " << bcValue.label << ": " << (int)bcValue.valueType;
// wenn Werte readOnly sind, dann brauchen keinen EditHint
if( bcValue.flags.testFlag(BCValue::Flag::ReadOnly) )
// || bcValue.valueType == BCValue::ValueType::Plain )
return;
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-07 17:13:35 +01:00
// baby-Slider-Indikator zeichnen
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);
painter->setBrush(QColor(0xE0E0E0));
painter->drawRoundedRect(barRect, 2, 2);
2026-01-07 17:13:35 +01:00
// Anteil zwischen min und max berechnen
double ratio = bcValue.calcRatio();
barRect.setWidth(barRect.width() * ratio );
2026-01-06 16:21:59 +01:00
painter->setBrush(QColor(0x0078D4));
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-06 15:59:57 +01:00
}
2025-12-19 21:20:14 +01:00
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);
}
// Optional: alle Highlights sofort clearen
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();
}
}
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);
}
}
}