Files
eden/src/yuzu/game/game_card.cpp
T
crueter cc8451f764 [desktop] Rework game list to use MVP architecture (#4042)
Closes #3480

moves the game list model/worker/private stuff to qt_common for later
use in QML

- `qt_common/game_list/model.{cpp,h}` is the model
- `yuzu/game/game_{grid,tree}.*` are the views
- `yuzu/game/game_list.cpp` is the presenter

This was done very lazily in a manner that "works" while largely
maintaining existing structure as much as possible. Most of it is
copy-paste, with some bonus reworks/cleanups thrown in.

Signed-off-by: crueter <crueter@eden-emu.dev>
Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/4042
Reviewed-by: MaranBr <maranbr@eden-emu.dev>
Reviewed-by: Lizzie <lizzie@eden-emu.dev>
2026-06-02 04:08:24 +02:00

106 lines
3.8 KiB
C++

// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include <QPainter>
#include <QPainterPath>
#include "game_card.h"
#include "qt_common/config/uisettings.h"
GameCard::GameCard(QObject* parent) : QStyledItemDelegate{parent} {
setObjectName("GameCard");
}
void GameCard::paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const {
if (!index.isValid())
return;
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
constexpr int cardMargin = 8;
constexpr int cardCornerRadius = 10;
const int column = index.row() % m_columns;
const int cell_width = option.rect.width();
const int card_width = cell_width - m_padding;
const int row_width = m_columns * cell_width;
const int total_gap = row_width - cardMargin * 2 - m_columns * card_width;
const int gap = (m_columns > 1) ? (total_gap / (m_columns - 1)) : 0;
const int card_left = option.rect.left() - column * cell_width + cardMargin + column * (card_width + gap) + 4;
const QRect cardRect(card_left, option.rect.top() + 4, card_width - 8,
option.rect.height() - cardMargin);
QPalette palette = option.palette;
QColor backgroundColor = palette.window().color();
QColor borderColor = palette.dark().color();
QColor textColor = palette.text().color();
// highlight blue on select
if (option.state & QStyle::State_Selected) {
backgroundColor = palette.highlight().color();
borderColor = palette.highlight().color().lighter(150);
textColor = palette.highlightedText().color();
} else if (option.state & QStyle::State_MouseOver) {
backgroundColor = backgroundColor.lighter(120);
}
painter->setBrush(backgroundColor);
painter->setPen(QPen(borderColor, 1));
painter->drawRoundedRect(cardRect, cardCornerRadius, cardCornerRadius);
const u32 icon_size = UISettings::values.game_icon_size.GetValue();
QPixmap icon_pixmap = index.data(Qt::DecorationRole).value<QPixmap>();
QRect iconRect;
if (!icon_pixmap.isNull()) {
QSize scaled = icon_pixmap.size();
scaled.scale(icon_size, icon_size, Qt::KeepAspectRatio);
iconRect = {cardRect.left() + (cardRect.width() - scaled.width()) / 2,
cardRect.top() + cardMargin, scaled.width(), scaled.height()};
painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
painter->save();
QPainterPath clip_path;
clip_path.addRoundedRect(iconRect, cardCornerRadius, cardCornerRadius);
painter->setClipPath(clip_path);
painter->drawPixmap(iconRect, icon_pixmap);
painter->restore();
} else {
iconRect = {cardRect.left() + cardMargin, cardRect.top() + cardMargin,
static_cast<int>(icon_size), static_cast<int>(icon_size)};
}
if (UISettings::values.show_game_name.GetValue()) {
QRect textRect = cardRect;
textRect.setTop(iconRect.bottom() + cardMargin);
textRect.adjust(cardMargin, 0, -cardMargin, -cardMargin);
QString title = index.data(Qt::DisplayRole).toString().split(QLatin1Char('\n')).first();
painter->setPen(textColor);
QFont font = option.font;
font.setBold(true);
font.setPixelSize(std::max(11.0, std::sqrt(static_cast<double>(icon_size))));
painter->setFont(font);
painter->drawText(textRect, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, title);
}
painter->restore();
}
QSize GameCard::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
return m_size;
}
void GameCard::setSize(const QSize& newSize, const int padding, const int columns) {
m_size = newSize;
m_padding = padding;
m_columns = columns;
}