Files
BionxControl/bcvaluedelegate.cpp

304 lines
7.8 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>
2026-01-10 22:18:54 +01:00
#include <bcvaluedelegate.h>
2026-01-11 01:12:28 +01:00
2026-01-11 11:37:52 +01:00
#include <bcvalueeditor.h>
2025-12-19 21:20:14 +01:00
2026-01-10 22:18:54 +01:00
BCValueDelegate::BCValueDelegate(const BCValueList& valueList, QTableView* view)
2025-12-29 15:44:06 +01:00
: QStyledItemDelegate{view}, _valueList{valueList}, _view{view}
{
}
2026-01-10 22:18:54 +01:00
QWidget* BCValueDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex& index) const
{
const BCValue& bcValue = *(_valueList[ index.row()].get());
2026-01-11 14:48:51 +01:00
qDebug() << " --- Create EDITOR: " << index.row() << " parent: " << parent->objectName();
2026-01-11 01:12:28 +01:00
2026-01-10 22:18:54 +01:00
Q_UNUSED(option)
Q_UNUSED(index)
2026-01-11 11:37:52 +01:00
auto* valueEditor = new BCValueEditor(bcValue, parent);
2026-01-10 22:18:54 +01:00
// Signal für sofortige Updates
2026-01-11 11:37:52 +01:00
connect(valueEditor, &BCValueEditor::valueChanged, this, [this, valueEditor](int newValue)
2026-01-10 22:18:54 +01:00
{
2026-01-11 11:37:52 +01:00
qDebug() << "---val changed: " << newValue;
2026-01-10 22:18:54 +01:00
// Commit data sofort bei Änderung
emit const_cast<BCValueDelegate*>(this)->commitData(valueEditor);
});
return valueEditor;
}
void BCValueDelegate::setEditorData(QWidget *editor, const QModelIndex& index) const
2025-12-17 16:26:22 +01:00
{
2026-01-11 20:01:33 +01:00
Q_UNUSED(editor)
Q_UNUSED(index)
2025-12-17 16:26:22 +01:00
2026-01-11 20:01:33 +01:00
// tue nix.
2026-01-11 11:37:52 +01:00
2025-12-17 16:26:22 +01:00
}
2026-01-10 22:18:54 +01:00
void BCValueDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const
2025-12-17 16:26:22 +01:00
{
2026-01-11 11:37:52 +01:00
qDebug() << " hier 2!";
if( index.column() == 1)
2026-01-07 17:13:35 +01:00
{
2026-01-11 11:37:52 +01:00
// Daten vom Editor zurück ins Model speichern (Beim Schließen)
BCValueEditor* slider = qobject_cast<BCValueEditor*>(editor);
if (slider)
{
qDebug() << " --- ok";
int value = slider->getValue();
model->setData(index, value, Qt::EditRole);
}
return;
2025-12-17 16:26:22 +01:00
}
2026-01-11 11:37:52 +01:00
QStyledItemDelegate::setModelData(editor, model, index);
2025-12-17 16:26:22 +01:00
}
2025-12-19 17:37:24 +01:00
2026-01-10 22:18:54 +01:00
void BCValueDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
2025-12-19 17:37:24 +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-11 14:48:51 +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-10 22:18:54 +01:00
void BCValueDelegate::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);
2026-01-11 11:37:52 +01:00
// Margin von 2px
const int m = 3;
QRect itemRect = option.rect.adjusted(m,m,-m,-m);
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
2026-01-11 14:48:51 +01:00
void BCValueDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index) const
{
Q_UNUSED(index)
QRect sliderRect = option.rect.adjusted(
option.rect.width() - cTextBlockOffset, // Von rechts: cTextBlockOffset (==130) px (Breite der Progress Bar)
0, // Oben: kein Offset
-cPaddingRight, // Rechts: 8px Padding
0 // Unten: kein Offset
);
editor->setGeometry(sliderRect); // Slider nur über Progress Bar
}
2026-01-09 00:45:26 +01:00
/**
2026-01-11 14:48:51 +01:00
* @brief Zeichnet eine passiven Slider, um den möglichen Wertebereich des übergebenen BCValue anzuzeigen.
2026-01-09 00:45:26 +01:00
*/
2026-01-11 11:37:52 +01:00
2026-01-10 22:18:54 +01:00
void BCValueDelegate::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);
2026-01-11 14:48:51 +01:00
int newX = option.rect.width() - cTextBlockOffset;
QRect barRect = option.rect.adjusted( newX,
2026-01-09 06:19:37 +01:00
option.rect.height() / 2 + 1,
2026-01-11 14:48:51 +01:00
- option.rect.width() + cTextBlockOffset + 117 - 18,
2026-01-09 06:19:37 +01:00
-option.rect.height() / 2 - 3);
2026-01-11 14:48:51 +01:00
double ratio = bcValue.calcMinMaxRatio();
2026-01-09 06:19:37 +01:00
if( ratio)
{
2026-01-11 14:48:51 +01:00
// Mini Progress Bar: der Gesamtbereich
2026-01-09 06:19:37 +01:00
painter->setPen(Qt::NoPen);
painter->setBrush(QColor(0xE0E0E0));
2026-01-11 14:48:51 +01:00
//QColor disabledText = option.palette.color(QPalette::Disabled, QPalette::Text);
//painter->setBrush(disabledText);
2026-01-09 06:19:37 +01:00
painter->drawRoundedRect(barRect, 2, 2);
QRect fillRect = barRect;
2026-01-11 20:01:33 +01:00
// ein wert darf näturlich nie über 100% eingestellt werden
ratio = qBound(0.0,ratio,1.0);
2026-01-09 06:19:37 +01:00
fillRect.setWidth(barRect.width() * ratio);
painter->setBrush(QColor(0x0078D4));
painter->drawRoundedRect(fillRect, 2, 2);
}
painter->restore();
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
*/
2026-01-10 22:18:54 +01:00
void BCValueDelegate::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
*/
2026-01-10 22:18:54 +01:00
void BCValueDelegate::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
*/
2026-01-10 22:18:54 +01:00
void BCValueDelegate::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);
}
}
}