Finally, animation works.

This commit is contained in:
2025-12-19 21:20:14 +01:00
parent 552fcdf8f8
commit 826e506d00
6 changed files with 204 additions and 44 deletions

View File

@@ -7,11 +7,16 @@
#include <QTimer>
#include <QListView>
#include <QVariantAnimation>
#include <QPropertyAnimation>
#include <QPainter>
#include "bcitemdelegate.h"
#include "bcvalue.h"
#include "qapplication.h"
BCItemDelegate::BCItemDelegate(QListView *view)
: QStyledItemDelegate(view), _view{view}
{
@@ -136,28 +141,9 @@ QSize BCItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelI
}
void BCItemDelegate::onHighlightRow(int row)
{
qDebug() << " --- should highlight: " << row;
_highlightedRow = row;
// Trigger Repaint der View (damit der Rahmen sofort erscheint)
_view->viewport()->update();
// Timer: Nach 1 Sekunde wieder ausschalten (Temporär)
QTimer::singleShot(1000, this, [=, this]()
{
if(_highlightedRow == row)
{ // Nur zurücksetzen, wenn es noch dieselbe Zeile ist
_highlightedRow = -1;
if (_view)
_view->viewport()->update();
}
});
}
void BCItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
/*
// 1. Standard-Zeichnen (Text, Hintergrund, Selection) durchführen
QStyledItemDelegate::paint(painter, option, index);
@@ -179,7 +165,122 @@ void BCItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option
painter->restore();
}
*/
QStyledItemDelegate::paint(painter, option, index);
/*
if (index.row() == _highlightedRow && _opacity > 0.0)
{
painter->save();
qDebug() << " --- is highlight: " << index.row();
QColor highlightColor( 0xFF9800 );
highlightColor.setAlphaF(_opacity);
QPen pen(highlightColor, 3);
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
painter->drawRoundedRect(option.rect.adjusted(2, 2, -2, -2), 8, 8);
painter->restore();
}
*/
int row = index.row();
if (m_rowOpacities.contains(row))
{
qreal opacity = m_rowOpacities.value(row);
if (opacity > 0.01) {
painter->save();
painter->setOpacity(opacity);
painter->fillRect(option.rect, QColor(255, 140, 0, 120));
painter->restore();
}
}
}
void BCItemDelegate::onHighlightRow(int row)
{
qDebug() << " --- should highlight: " << row;
// Alte Animation für diese Zeile stoppen falls vorhanden
if (m_rowAnimations.contains(row))
{
m_rowAnimations[row]->stop();
m_rowAnimations[row]->deleteLater();
}
// 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%
}
m_rowOpacities[row] = opacity;
updateRow(row);
});
connect(anim, &QVariantAnimation::finished, this, [this, row, anim]()
{
m_rowOpacities.remove(row);
m_rowAnimations.remove(row);
updateRow(row);
anim->deleteLater();
});
m_rowAnimations[row] = anim;
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
// Optional: alle Highlights sofort clearen
void BCItemDelegate::clearAllHighlights()
{
for(auto* anim : std::as_const(m_rowAnimations))
{
anim->stop();
anim->deleteLater();
}
m_rowAnimations.clear();
m_rowOpacities.clear();
if (_view)
{
_view->viewport()->update();
}
}
void BCItemDelegate::updateRow(int row)
{
if (_view && row >= 0) {
QModelIndex idx = _view->model()->index(row, 0);
QRect rect = _view->visualRect(idx);
if (!rect.isEmpty()) {
_view->viewport()->update(rect);
}
}
}
QString BCItemDelegate::formatDisplayString(const QModelIndex &index) const
{
if (!index.isValid())