Implement a little "raise on enter" animation for deck preview widgets. (#5844)

* Implement a little "raise on enter" animation for deck preview widgets.

* Why does the linter need to be run twice?

* Fix build.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2025-04-18 05:31:47 +02:00 committed by GitHub
parent 2dc1b875d2
commit 8af1f2b6d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 127 additions and 9 deletions

View file

@ -30,8 +30,9 @@
*
* Initializes the widget with a minimum height and sets the pixmap to a dirty state for initial loading.
*/
CardInfoPictureWidget::CardInfoPictureWidget(QWidget *parent, const bool hoverToZoomEnabled)
: QWidget(parent), info(nullptr), pixmapDirty(true), hoverToZoomEnabled(hoverToZoomEnabled)
CardInfoPictureWidget::CardInfoPictureWidget(QWidget *parent, const bool _hoverToZoomEnabled, const bool _raiseOnEnter)
: QWidget(parent), info(nullptr), pixmapDirty(true), hoverToZoomEnabled(_hoverToZoomEnabled),
raiseOnEnter(_raiseOnEnter)
{
setMinimumHeight(baseHeight);
if (hoverToZoomEnabled) {
@ -45,6 +46,17 @@ CardInfoPictureWidget::CardInfoPictureWidget(QWidget *parent, const bool hoverTo
hoverTimer->setSingleShot(true);
connect(hoverTimer, &QTimer::timeout, this, &CardInfoPictureWidget::showEnlargedPixmap);
// Store the widget's original position
originalPos = this->pos();
// Create the animation
animation = new QPropertyAnimation(this, "pos");
animation->setDuration(200); // 200ms animation duration
animation->setEasingCurve(QEasingCurve::OutQuad);
animation->setStartValue(originalPos);
animation->setEndValue(originalPos - QPoint(0, animationOffset));
connect(&SettingsCache::instance(), &SettingsCache::roundCardCornersChanged, this, [this](bool _roundCardCorners) {
Q_UNUSED(_roundCardCorners);
@ -84,6 +96,11 @@ void CardInfoPictureWidget::setHoverToZoomEnabled(const bool enabled)
setMouseTracking(enabled);
}
void CardInfoPictureWidget::setRaiseOnEnterEnabled(const bool enabled)
{
raiseOnEnter = enabled;
}
/**
* @brief Handles widget resizing by updating the pixmap size.
* @param event The resize event (unused).
@ -93,6 +110,7 @@ void CardInfoPictureWidget::setHoverToZoomEnabled(const bool enabled)
void CardInfoPictureWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
originalPos = pos(); // Update the baseline position
updatePixmap();
}
@ -234,6 +252,18 @@ void CardInfoPictureWidget::enterEvent(QEvent *event)
// Emit signal indicating a card is being hovered on
emit hoveredOnCard(info);
if (raiseOnEnter) {
if (animation->state() == QAbstractAnimation::Running) {
animation->pause(); // Pause current animation
} else {
originalPos = this->pos(); // Update the baseline position
animation->setStartValue(originalPos);
animation->setEndValue(originalPos - QPoint(0, animationOffset));
}
animation->setDirection(QAbstractAnimation::Forward);
animation->start();
}
}
/**
@ -243,10 +273,32 @@ void CardInfoPictureWidget::enterEvent(QEvent *event)
void CardInfoPictureWidget::leaveEvent(QEvent *event)
{
QWidget::leaveEvent(event);
if (hoverToZoomEnabled) {
hoverTimer->stop();
enlargedPixmapWidget->hide();
}
if (raiseOnEnter) {
if (animation->state() == QAbstractAnimation::Running) {
animation->pause(); // Pause current animation
}
animation->setDirection(QAbstractAnimation::Backward);
animation->start();
}
}
void CardInfoPictureWidget::moveEvent(QMoveEvent *event)
{
QWidget::moveEvent(event);
hoverTimer->stop();
enlargedPixmapWidget->hide();
if (animation->state() == QAbstractAnimation::Running) {
return;
}
originalPos = this->pos(); // Update the baseline position
}
/**

View file

@ -4,6 +4,7 @@
#include "../../../../game/cards/card_info.h"
#include "card_info_picture_enlarged_widget.h"
#include <QPropertyAnimation>
#include <QTimer>
#include <QWidget>
@ -17,17 +18,20 @@ class CardInfoPictureWidget : public QWidget
Q_OBJECT
public:
explicit CardInfoPictureWidget(QWidget *parent = nullptr, bool hoverToZoomEnabled = false);
explicit CardInfoPictureWidget(QWidget *parent = nullptr,
bool hoverToZoomEnabled = false,
bool raiseOnEnter = false);
CardInfoPtr getInfo()
{
return info;
}
[[nodiscard]] QSize sizeHint() const override;
void setHoverToZoomEnabled(bool enabled);
public slots:
void setCard(CardInfoPtr card);
void setScaleFactor(int scale); // New slot for scaling
void setHoverToZoomEnabled(bool enabled);
void setRaiseOnEnterEnabled(bool enabled);
void updatePixmap();
signals:
@ -45,6 +49,7 @@ protected:
void enterEvent(QEvent *event) override; // Qt5 signature
#endif
void leaveEvent(QEvent *event) override;
void moveEvent(QMoveEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void loadPixmap();
@ -65,10 +70,14 @@ private:
QPixmap resizedPixmap;
bool pixmapDirty;
bool hoverToZoomEnabled;
bool raiseOnEnter;
int hoverActivateThresholdInMs = 500;
CardInfoPictureEnlargedWidget *enlargedPixmapWidget;
int enlargedPixmapOffset = 10;
QTimer *hoverTimer;
QPropertyAnimation *animation;
QPoint originalPos; // Store the original position
const int animationOffset = 10; // Adjust this for how much the widget moves up
QMenu *createRightClickMenu();
QMenu *createViewRelatedCardsMenu();

View file

@ -9,6 +9,7 @@
* @brief Constructs a CardPictureWithTextOverlay widget.
* @param parent The parent widget.
* @param hoverToZoomEnabled If this widget will spawn a larger widget when hovered over.
* @param raiseOnEnter If this widget will raise slightly when entered.
* @param textColor The color of the overlay text.
* @param outlineColor The color of the outline around the text.
* @param fontSize The font size of the overlay text.
@ -18,11 +19,12 @@
*/
CardInfoPictureWithTextOverlayWidget::CardInfoPictureWithTextOverlayWidget(QWidget *parent,
const bool hoverToZoomEnabled,
const bool raiseOnEnter,
const QColor &textColor,
const QColor &outlineColor,
const int fontSize,
const Qt::Alignment alignment)
: CardInfoPictureWidget(parent, hoverToZoomEnabled), textColor(textColor), outlineColor(outlineColor),
: CardInfoPictureWidget(parent, hoverToZoomEnabled, raiseOnEnter), textColor(textColor), outlineColor(outlineColor),
fontSize(fontSize), textAlignment(alignment)
{
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

View file

@ -14,6 +14,7 @@ class CardInfoPictureWithTextOverlayWidget : public CardInfoPictureWidget
public:
explicit CardInfoPictureWithTextOverlayWidget(QWidget *parent = nullptr,
bool hoverToZoomEnabled = false,
bool raiseOnEnter = false,
const QColor &textColor = Qt::white,
const QColor &outlineColor = Qt::black,
int fontSize = 12,

View file

@ -1,5 +1,7 @@
#include "deck_preview_card_picture_widget.h"
#include "../../../../settings/cache_settings.h"
#include <QApplication>
#include <QFileInfo>
#include <QFontMetrics>
@ -22,15 +24,24 @@
*/
DeckPreviewCardPictureWidget::DeckPreviewCardPictureWidget(QWidget *parent,
const bool hoverToZoomEnabled,
const bool raiseOnEnter,
const QColor &textColor,
const QColor &outlineColor,
const int fontSize,
const Qt::Alignment alignment)
: CardInfoPictureWithTextOverlayWidget(parent, hoverToZoomEnabled, textColor, outlineColor, fontSize, alignment)
: CardInfoPictureWithTextOverlayWidget(parent,
hoverToZoomEnabled,
raiseOnEnter,
textColor,
outlineColor,
fontSize,
alignment)
{
singleClickTimer = new QTimer(this);
singleClickTimer->setSingleShot(true);
connect(singleClickTimer, &QTimer::timeout, this, [this]() { emit imageClicked(lastMouseEvent, this); });
connect(&SettingsCache::instance(), &SettingsCache::visualDeckStorageSelectionAnimationChanged, this,
&CardInfoPictureWidget::setRaiseOnEnterEnabled);
}
void DeckPreviewCardPictureWidget::mousePressEvent(QMouseEvent *event)

View file

@ -14,6 +14,7 @@ class DeckPreviewCardPictureWidget final : public CardInfoPictureWithTextOverlay
public:
explicit DeckPreviewCardPictureWidget(QWidget *parent,
bool hoverToZoomEnabled = false,
bool raiseOnEnter = false,
const QColor &textColor = Qt::white,
const QColor &outlineColor = Qt::black,
int fontSize = 12,

View file

@ -31,7 +31,8 @@ DeckPreviewWidget::DeckPreviewWidget(QWidget *_parent,
&VisualDeckStorageTagFilterWidget::refreshTags);
deckLoader->loadFromFileAsync(filePath, DeckLoader::getFormatFromName(filePath), false);
bannerCardDisplayWidget = new DeckPreviewCardPictureWidget(this);
bannerCardDisplayWidget =
new DeckPreviewCardPictureWidget(this, false, visualDeckStorageWidget->deckPreviewSelectionAnimationEnabled);
connect(bannerCardDisplayWidget, &DeckPreviewCardPictureWidget::imageClicked, this,
&DeckPreviewWidget::imageClickedEvent);

View file

@ -125,6 +125,10 @@ VisualDeckStorageWidget::VisualDeckStorageWidget(QWidget *parent) : QWidget(pare
tagFilterWidget = new VisualDeckStorageTagFilterWidget(this);
updateTagsVisibility(SettingsCache::instance().getVisualDeckStorageShowTagFilter());
deckPreviewSelectionAnimationEnabled = SettingsCache::instance().getVisualDeckStorageSelectionAnimation();
connect(&SettingsCache::instance(), &SettingsCache::visualDeckStorageSelectionAnimationChanged, this,
&VisualDeckStorageWidget::updateSelectionAnimationEnabled);
// deck area
scrollArea = new QScrollArea(this);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
@ -270,4 +274,9 @@ void VisualDeckStorageWidget::updateTagsVisibility(const bool visible)
} else {
tagFilterWidget->setHidden(true);
}
}
void VisualDeckStorageWidget::updateSelectionAnimationEnabled(const bool enabled)
{
deckPreviewSelectionAnimationEnabled = enabled;
}

View file

@ -31,6 +31,7 @@ public:
CardSizeWidget *cardSizeWidget;
VisualDeckStorageTagFilterWidget *tagFilterWidget;
bool deckPreviewSelectionAnimationEnabled;
public slots:
void createRootFolderWidget(); // Refresh the display of cards based on the current sorting option
@ -39,6 +40,7 @@ public slots:
void updateColorFilter();
void updateSearchFilter();
void updateTagsVisibility(bool visible);
void updateSelectionAnimationEnabled(bool enabled);
void updateSortOrder();
void resizeEvent(QResizeEvent *event) override;
void showEvent(QShowEvent *event) override;