Qt: Use QSize throughout GameListWidget

This commit is contained in:
Stenzek
2025-10-05 00:24:12 +10:00
parent 7b991def42
commit 9cc29fb603
2 changed files with 34 additions and 32 deletions

View File

@@ -83,8 +83,7 @@ static constexpr int GAME_ICON_SIZE = 16;
static constexpr int GAME_ICON_PADDING = 12;
static constexpr int GAME_ICON_ANIMATION_LOOPS = 5;
static void resizeAndPadImage(QImage* image, int expected_width, int expected_height, bool fill_with_top_left,
bool expand_to_fill)
static void resizeAndPadImage(QImage* image, const QSize& expected_size, bool fill_with_top_left, bool expand_to_fill)
{
// Get source image in RGB32 format for QPainter.
// fill_with_top_left is used for the game list background, which cannot be transparent.
@@ -95,8 +94,8 @@ static void resizeAndPadImage(QImage* image, int expected_width, int expected_he
*image = image->convertToFormat(expected_format);
const qreal dpr = image->devicePixelRatio();
const int dpr_expected_width = static_cast<int>(static_cast<qreal>(expected_width) * dpr);
const int dpr_expected_height = static_cast<int>(static_cast<qreal>(expected_height) * dpr);
const int dpr_expected_width = static_cast<int>(static_cast<qreal>(expected_size.width()) * dpr);
const int dpr_expected_height = static_cast<int>(static_cast<qreal>(expected_size.height()) * dpr);
if (image->width() == dpr_expected_width && image->height() == dpr_expected_height)
return;
@@ -142,11 +141,11 @@ static void resizeAndPadImage(QImage* image, int expected_width, int expected_he
*image = std::move(padded_image);
}
static void fastResizePixmap(QPixmap& pm, int expected_width, int expected_height)
static void fastResizePixmap(QPixmap& pm, const QSize& expected_size)
{
const qreal dpr = pm.devicePixelRatio();
const int dpr_expected_width = static_cast<int>(static_cast<qreal>(expected_width) * dpr);
const int dpr_expected_height = static_cast<int>(static_cast<qreal>(expected_height) * dpr);
const int dpr_expected_width = static_cast<int>(static_cast<qreal>(expected_size.width()) * dpr);
const int dpr_expected_height = static_cast<int>(static_cast<qreal>(expected_size.height()) * dpr);
pm = pm.scaled(QSize(dpr_expected_width, dpr_expected_height), Qt::IgnoreAspectRatio, Qt::FastTransformation);
}
@@ -290,11 +289,11 @@ void GameListModel::updateCoverScale()
if (loading_image.load(QStringLiteral("%1/images/placeholder.png").arg(QtHost::GetResourcesBasePath())))
{
loading_image.setDevicePixelRatio(m_device_pixel_ratio);
resizeAndPadImage(&loading_image, getCoverArtSize(), getCoverArtSize(), false, false);
resizeAndPadImage(&loading_image, getCoverArtSize(), false, false);
}
else
{
loading_image = QImage(getCoverArtSize(), getCoverArtSize(), QImage::Format_RGB32);
loading_image = QImage(getCoverArtSize(), QImage::Format_RGB32);
loading_image.setDevicePixelRatio(m_device_pixel_ratio);
loading_image.fill(QColor(0, 0, 0, 0));
}
@@ -304,11 +303,11 @@ void GameListModel::updateCoverScale()
if (m_placeholder_image.load(QStringLiteral("%1/images/cover-placeholder.png").arg(QtHost::GetResourcesBasePath())))
{
m_placeholder_image.setDevicePixelRatio(m_device_pixel_ratio);
resizeAndPadImage(&m_placeholder_image, getCoverArtSize(), getCoverArtSize(), false, false);
resizeAndPadImage(&m_placeholder_image, getCoverArtSize(), false, false);
}
else
{
m_placeholder_image = QImage(getCoverArtSize(), getCoverArtSize(), QImage::Format_RGB32);
m_placeholder_image = QImage(getCoverArtSize(), QImage::Format_RGB32);
m_placeholder_image.setDevicePixelRatio(m_device_pixel_ratio);
m_placeholder_image.fill(QColor(0, 0, 0, 0));
}
@@ -374,24 +373,24 @@ void GameListModel::loadOrGenerateCover(const GameList::Entry* ge)
{
QtAsyncTask::create(this, [path = ge->path, serial = ge->serial, save_title = std::string(ge->GetSaveTitle()),
display_title = QtUtils::StringViewToQString(ge->GetDisplayTitle(m_show_localized_titles)),
placeholder_image = m_placeholder_image, list = this, width = getCoverArtSize(),
height = getCoverArtSize(), scale = m_cover_scale, dpr = m_device_pixel_ratio,
placeholder_image = m_placeholder_image, list = this, size = getCoverArtSize(),
scale = m_cover_scale, dpr = m_device_pixel_ratio,
is_custom_title = ge->has_custom_title]() mutable {
QImage image;
loadOrGenerateCover(image, placeholder_image, width, height, scale, dpr, path, serial, save_title, display_title,
loadOrGenerateCover(image, placeholder_image, size, scale, dpr, path, serial, save_title, display_title,
is_custom_title);
return [path = std::move(path), image = std::move(image), list, scale]() { list->coverLoaded(path, image, scale); };
});
}
void GameListModel::createPlaceholderImage(QImage& image, const QImage& placeholder_image, int width, int height,
void GameListModel::createPlaceholderImage(QImage& image, const QImage& placeholder_image, const QSize& size,
float scale, const QString& title)
{
image = placeholder_image.copy();
if (image.isNull())
return;
resizeAndPadImage(&image, width, height, false, false);
resizeAndPadImage(&image, size, false, false);
QPainter painter;
if (painter.begin(&image))
@@ -402,8 +401,8 @@ void GameListModel::createPlaceholderImage(QImage& image, const QImage& placehol
painter.setFont(font);
const int margin = static_cast<int>(30.0f * scale);
const QRect text_rc(margin, margin, static_cast<int>(static_cast<float>(width - margin - margin)),
static_cast<int>(static_cast<float>(height - margin - margin)));
const QRect text_rc(margin, margin, static_cast<int>(static_cast<float>(size.width() - margin - margin)),
static_cast<int>(static_cast<float>(size.height() - margin - margin)));
// draw shadow first
painter.setPen(QColor(0, 0, 0, 160)); // semi-transparent black
@@ -416,8 +415,8 @@ void GameListModel::createPlaceholderImage(QImage& image, const QImage& placehol
}
}
void GameListModel::loadOrGenerateCover(QImage& image, const QImage& placeholder_image, int width, int height,
float scale, qreal dpr, const std::string& path, const std::string& serial,
void GameListModel::loadOrGenerateCover(QImage& image, const QImage& placeholder_image, const QSize& size, float scale,
qreal dpr, const std::string& path, const std::string& serial,
const std::string& save_title, const QString& display_title,
bool is_custom_title)
{
@@ -428,12 +427,12 @@ void GameListModel::loadOrGenerateCover(QImage& image, const QImage& placeholder
if (!image.isNull())
{
image.setDevicePixelRatio(dpr);
resizeAndPadImage(&image, width, height, false, false);
resizeAndPadImage(&image, size, false, false);
}
}
if (image.isNull())
createPlaceholderImage(image, placeholder_image, width, height, scale, display_title);
createPlaceholderImage(image, placeholder_image, size, scale, display_title);
}
void GameListModel::coverLoaded(const std::string& path, const QImage& image, float scale)
@@ -596,9 +595,10 @@ QIcon GameListModel::getIconForGame(const QString& path)
return ret;
}
int GameListModel::getCoverArtSize() const
QSize GameListModel::getCoverArtSize() const
{
return std::max(static_cast<int>(static_cast<float>(COVER_ART_SIZE) * m_cover_scale), 1);
const int size = std::max(static_cast<int>(static_cast<float>(COVER_ART_SIZE) * m_cover_scale), 1);
return QSize(size, size);
}
int GameListModel::getCoverArtSpacing() const
@@ -777,7 +777,7 @@ QVariant GameListModel::data(const QModelIndex& index, int role, const GameList:
case Column_Icon:
return QSize(getIconColumnWidth(), m_row_height);
case Column_Cover:
return QSize(getCoverArtSize(), getCoverArtSize());
return getCoverArtSize();
default:
return {};
}
@@ -812,7 +812,7 @@ QVariant GameListModel::data(const QModelIndex& index, int role, const GameList:
if (pm->is_loading)
pm->pixmap = m_loading_pixmap;
else
fastResizePixmap(pm->pixmap, getCoverArtSize(), getCoverArtSize());
fastResizePixmap(pm->pixmap, getCoverArtSize());
pm->scale = m_cover_scale;
}
@@ -1712,7 +1712,7 @@ void GameListWidget::updateBackground(bool reload_image)
}
QImage scaled_image = m_background_image;
resizeAndPadImage(&scaled_image, m_ui.stack->width(), m_ui.stack->height(), true, true);
resizeAndPadImage(&scaled_image, m_ui.stack->size(), true, true);
QPalette new_palette = qApp->palette(m_ui.stack);
new_palette.setBrush(QPalette::Window, QPixmap::fromImage(scaled_image));
@@ -2417,13 +2417,15 @@ void GameListGridView::adjustZoom(float delta)
void GameListGridView::updateLayout()
{
const int icon_width = m_model->getCoverArtSize();
const QSize image_size = m_model->getCoverArtSize();
const int icon_width = image_size.width();
const int item_spacing = m_model->getCoverArtSpacing();
const int item_margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this) * 2;
const int item_width = icon_width + item_margin + item_spacing;
// one line of text
const int item_height = item_width + (m_model->getShowCoverTitles() ? fontMetrics().height() : 0);
const int item_height =
image_size.height() + item_margin + item_spacing + (m_model->getShowCoverTitles() ? fontMetrics().height() : 0);
// the -1 here seems to be necessary otherwise we calculate too many columns..
// can't see where in qlistview.cpp it's coming from though.

View File

@@ -108,7 +108,7 @@ public:
float getCoverScale() const { return m_cover_scale; }
void setCoverScale(float scale);
int getCoverArtSize() const;
QSize getCoverArtSize() const;
int getCoverArtSpacing() const;
void refreshCovers();
void updateCacheSize(int num_rows, int num_columns, QSortFilterProxyModel* const sort_model, int top_left_row);
@@ -140,10 +140,10 @@ private:
void invalidateCoverForPath(const std::string& path);
void coverLoaded(const std::string& path, const QImage& image, float scale);
static void loadOrGenerateCover(QImage& image, const QImage& placeholder_image, int width, int height, float scale,
static void loadOrGenerateCover(QImage& image, const QImage& placeholder_image, const QSize& size, float scale,
qreal dpr, const std::string& path, const std::string& serial,
const std::string& save_title, const QString& display_title, bool is_custom_title);
static void createPlaceholderImage(QImage& image, const QImage& placeholder_image, int width, int height, float scale,
static void createPlaceholderImage(QImage& image, const QImage& placeholder_image, const QSize& size, float scale,
const QString& title);
const QPixmap& getIconPixmapForEntry(const GameList::Entry* ge) const;