Files
BionxControl/bcvaluedelegate.cpp

403 lines
11 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-17 16:26:22 +01:00
2025-12-19 21:20:14 +01:00
#include <QVariantAnimation>
#include <QPropertyAnimation>
#include <QPainter>
2026-01-18 18:52:30 +01:00
#include <bcdeviceview.h>
2026-01-10 22:18:54 +01:00
#include <bcvaluedelegate.h>
2026-02-09 16:03:09 +01:00
#include <bcvalueslider.h>
2026-03-30 21:24:29 +02:00
#include <bctoggleswitch.h>
2026-01-18 18:52:30 +01:00
BCValueDelegate::BCValueDelegate(const BCValueList& valueList, BCDeviceView* 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
{
Q_UNUSED(option)
Q_UNUSED(index)
2026-03-30 21:24:29 +02:00
const BCValue& bcValue = *(_valueList[ index.row() ].get());
//qDebug() << " --- Create Editor: " << bcValue.label() << " value: " << params.value << " min: " << params.min << " max: " << params.max << " ratio:" << bcValue.calcMinMaxRatio()*100.0 << '%';
if( bcValue.isBoolean() )
{
2026-04-01 16:11:28 +02:00
auto* toggleSwitch = new BCToggleSwitch{parent};
toggleSwitch->setChecked(bcValue.isChecked() );
// Signal für sofortige Updates
connect(toggleSwitch, &BCToggleSwitch::toggled, this, [this, toggleSwitch](bool checked)
{
qDebug() << "--- toggled: " << checked;
// Commit data sofort bei Änderung
emit const_cast<BCValueDelegate*>(this)->commitData(toggleSwitch);
});
return toggleSwitch;
2026-03-30 21:24:29 +02:00
}
2026-01-18 18:52:30 +01:00
2026-01-22 22:16:19 +01:00
BCValue::ValueRange params;
2026-03-30 21:24:29 +02:00
bool hasData = bcValue.hasValuesForSlider( params );
2026-01-19 16:44:52 +01:00
if( !hasData )
return nullptr;
2026-01-18 18:52:30 +01:00
2026-03-30 21:24:29 +02:00
auto* valueSlider = new BCValueSlider{parent};
valueSlider->setValueAndRange( params );
2026-01-10 22:18:54 +01:00
// Signal für sofortige Updates
2026-03-30 21:24:29 +02:00
connect(valueSlider, &BCValueSlider::valueChanged, this, [this, valueSlider]()
2026-01-10 22:18:54 +01:00
{
// Commit data sofort bei Änderung
2026-03-30 21:24:29 +02:00
emit const_cast<BCValueDelegate*>(this)->commitData(valueSlider);
2026-01-10 22:18:54 +01:00
});
2026-04-01 16:11:28 +02:00
/*
2026-01-18 18:52:30 +01:00
// Signal für sofortige Updates
2026-03-30 21:24:29 +02:00
connect(valueSlider, &BCValueSlider::valueCommited, this, [this, valueSlider](int newValue)
2026-01-18 18:52:30 +01:00
{
qDebug() << " --- value set:" << newValue;
// Commit data sofort bei Änderung
2026-03-30 21:24:29 +02:00
emit const_cast<BCValueDelegate*>(this)->commitData(valueSlider);
2026-01-18 18:52:30 +01:00
});
2026-04-01 16:11:28 +02:00
*/
2026-01-18 18:52:30 +01:00
2026-03-30 21:24:29 +02:00
return valueSlider;
2026-01-10 22:18:54 +01:00
}
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-03-30 21:24:29 +02:00
2026-03-29 23:30:42 +02:00
void BCValueDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Q_UNUSED(index)
2026-03-30 21:24:29 +02:00
const BCValue& bcValue = *(_valueList[ index.row()].get());
2026-04-01 16:11:28 +02:00
QRect editorRect = bcValue.isBoolean() ? adjustEditorRect(option.rect, 0, 6, -130, -6) : adjustEditorRect(option.rect, 0, 0, 8, 0);
/*
2026-03-30 21:24:29 +02:00
if( !bcValue.isBoolean())
{
2026-04-01 16:11:28 +02:00
editorRect = adjustEditorRect( option.rect,0,0,8,0 );
2026-03-30 21:24:29 +02:00
}
else
{
2026-04-01 16:11:28 +02:00
editorRect = adjustEditorRect(option.rect, 0, 6, -130, -6);
2026-03-30 21:24:29 +02:00
}
2026-04-01 16:11:28 +02:00
*/
2026-03-30 21:24:29 +02:00
editor->setGeometry(editorRect);
2026-03-29 23:30:42 +02:00
}
2026-03-30 21:24:29 +02: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-04-01 16:11:28 +02:00
if (index.column() != 1)
2026-01-11 11:37:52 +01:00
return;
2026-04-01 16:11:28 +02:00
QVariant reValue;
const BCValue& bcValue = *(_valueList[index.row()].get());
if (bcValue.isBoolean())
{
if (BCToggleSwitch* toggleSswitch = qobject_cast<BCToggleSwitch*>(editor))
reValue = toggleSswitch->isChecked() ? 1 : 0;
}
else
{
if (BCValueSlider* slider = qobject_cast<BCValueSlider*>(editor))
reValue = slider->value();
}
model->setData(index, reValue, Qt::EditRole);
return;
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-03-30 21:24:29 +02:00
int row = index.row();
if( index.column() != 1 || row<0 || row >= _valueList.size() )
2026-01-22 22:16:19 +01:00
return;
2026-03-29 23:30:42 +02:00
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
2026-04-01 16:11:28 +02:00
const BCValue& bcValue = *(_valueList[ index.row()].get());
2026-03-29 23:30:42 +02:00
2026-03-30 21:24:29 +02:00
if( bcValue.isBoolean() )
2026-03-29 23:30:42 +02:00
{
2026-03-31 18:18:37 +02:00
paintBooleanValue( painter, opt, bcValue );
2026-03-30 21:24:29 +02:00
}
else
{
// Standard-Zeichnen (Text, Hintergrund, Selection) durchführen
QStyledItemDelegate::paint(painter, opt, index);
2026-03-29 23:30:42 +02:00
}
2026-01-22 22:16:19 +01:00
if( !bcValue.isReadOnly() )
2025-12-19 21:20:14 +01:00
{
2026-02-09 16:03:09 +01:00
// Wir zeichnen boolean Values an toggle switches
2026-03-31 18:18:37 +02:00
if (bcValue.isBoolean())
{
//paintPlainToggleSwitch(painter, opt, bcValue);
}
2026-01-22 22:16:19 +01:00
else
2026-03-31 18:18:37 +02:00
{
paintPlainSliderIndicator(painter, opt.rect, bcValue.calcMinMaxRatio());
}
2026-01-09 06:19:37 +01:00
}
2026-01-22 22:16:19 +01:00
if(_rowOpacities.contains(row))
2026-03-29 23:30:42 +02:00
paintHighlightRow(painter, opt,index.row());
2026-01-22 22:16:19 +01:00
2026-01-09 06:19:37 +01:00
}
2025-12-19 21:20:14 +01:00
2026-03-29 23:30:42 +02:00
void BCValueDelegate::paintBooleanValue( QPainter *painter, const QStyleOptionViewItem& option, const BCValue& bcValue ) const
{
2026-03-30 21:24:29 +02:00
QRect textRect = option.rect.adjusted( 2,0,0,0);
2026-03-29 23:30:42 +02:00
2026-03-30 21:24:29 +02:00
// 3. Den tatsächlichen Wert aus dem Model auslesen
2026-03-31 18:18:37 +02:00
QString text = bcValue.rawValue() == 1 ? BCTags::Yes : BCTags::No;
2026-03-29 23:30:42 +02:00
// 3. Die korrekte Textfarbe ermitteln (extrem wichtig für die UX!)
// Wenn die Zeile markiert ist (Selected), muss der Text meist weiß sein,
// ansonsten schwarz (oder je nach System-Theme).
2026-03-30 21:24:29 +02:00
QPalette::ColorRole textRole = (option.state & QStyle::State_Selected)
2026-03-29 23:30:42 +02:00
? QPalette::HighlightedText
: QPalette::Text;
// 4. Den Text nativ durch den Style zeichnen lassen
2026-03-30 21:24:29 +02:00
QApplication::style()->drawItemText(
2026-03-29 23:30:42 +02:00
painter,
textRect,
Qt::AlignLeft | Qt::AlignVCenter, // Ausrichtung innerhalb von textRect
option.palette, // Farbpalette der View übernehmen
option.state & QStyle::State_Enabled, // Prüfen, ob die Zelle klickbar/aktiv ist
text, // Der zu zeichnende String
textRole // Die ermittelte Farb-Rolle
2026-03-30 21:24:29 +02:00
);
2026-04-01 16:11:28 +02:00
2026-03-29 23:30:42 +02:00
}
2026-04-01 16:11:28 +02:00
QRect BCValueDelegate::adjustEditorRect( const QRect& rect, int x1, int y1, int x2, int y2 ) const
2026-03-29 23:30:42 +02:00
{
return rect.adjusted
(
2026-04-01 16:11:28 +02:00
rect.width() - cTextBlockOffset + x1, // Von rechts: cTextBlockOffset (==130) px (Breite der Progress Bar)
y1, // Oben: kein Offset
x2, // Rechts: 8px Padding
y2 // Unten: kein Offset
2026-03-29 23:30:42 +02:00
);
}
2026-04-01 16:11:28 +02:00
// option.rect.adjusted(option.rect.width() - cTextBlockOffset,4,0,0);
/**
* @brief Zeichnet der 'Sliderindicator', also den Anteil des Gesamtwerts als Fortschrittsbalken
*/
2026-02-10 14:25:06 +01:00
void BCValueDelegate::paintPlainSliderIndicator(QPainter* painter, const QRect& rect, double ratio )const
2026-02-09 16:03:09 +01:00
{
2026-04-01 16:11:28 +02:00
QRect sliderRect = adjustEditorRect( rect, 0, 0, -35, 0 );
2026-03-29 23:30:42 +02:00
// Kleinen Slider-Indikator zeichnen
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
QRect barRect = sliderRect;
int yOffset = sliderRect.height()/2;
barRect.setY( rect.y() + yOffset - 3 );
barRect.setHeight( 6);
// Mini Progress Bar: der Gesamtbereich
painter->setPen(Qt::NoPen);
painter->setBrush(QColor(0xE0E0E0));
painter->drawRoundedRect(barRect, 4, 4);
// Mini Progress Bar: der Wertebereich
barRect.setWidth( ratio * barRect.width() );
painter->setBrush(QColor(0x0078D4));
painter->drawRoundedRect(barRect, 4, 4);
painter->restore();
2026-02-09 16:03:09 +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-02-10 14:25:06 +01:00
void BCValueDelegate::paintPlainToggleSwitch(QPainter* painter, const QStyleOptionViewItem& option, const BCValue& bcValue) const
2026-01-19 16:44:52 +01:00
{
2026-03-31 18:18:37 +02:00
BCToggleSwitch::paintToggleIndicator(painter, option.rect, bcValue.isChecked(), 20, option.widget->palette());
2026-01-19 16:44:52 +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%
2026-02-09 16:03:09 +01:00
}
else
{
2025-12-19 21:20:14 +01:00
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-02-09 16:03:09 +01:00
_rowOpacities.remove(row);
2026-01-08 00:25:36 +01:00
_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
/**
2026-02-09 16:03:09 +01:00
* @brief Stopt alle gerade laufenden Animationen
2026-01-09 00:45:26 +01:00
*/
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();
2026-02-09 16:03:09 +01:00
_rowOpacities.clear();
2025-12-19 21:20:14 +01:00
if (_view)
_view->viewport()->update();
2026-02-09 16:03:09 +01:00
2025-12-19 21:20:14 +01:00
}
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);
2026-02-09 16:03:09 +01:00
if (!rect.isEmpty())
2025-12-19 21:20:14 +01:00
_view->viewport()->update(rect);
}
}