From 594962d247575564e8e0a49d4f6d8aa22bfb9315 Mon Sep 17 00:00:00 2001
From: Stenzek <stenzek@gmail.com>
Date: Sun, 14 Jul 2024 13:03:10 +1000
Subject: [PATCH] Qt: Scale down custom icon pixmaps

Don't want it going outside of the control bounds.
---
 src/duckstation-qt/gamelistmodel.cpp | 24 ++++++++++++++++++++++--
 src/duckstation-qt/gamelistmodel.h   |  1 +
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/duckstation-qt/gamelistmodel.cpp b/src/duckstation-qt/gamelistmodel.cpp
index b2267a6fb..c1604feac 100644
--- a/src/duckstation-qt/gamelistmodel.cpp
+++ b/src/duckstation-qt/gamelistmodel.cpp
@@ -260,9 +260,12 @@ const QPixmap& GameListModel::getIconPixmapForEntry(const GameList::Entry* ge) c
     const std::string path = GameList::GetGameIconPath(ge->serial, ge->path);
     QPixmap pm;
     if (!path.empty() && pm.load(QString::fromStdString(path)))
+    {
+      fixIconPixmapSize(pm);
       return *m_memcard_pixmap_cache.Insert(ge->serial, std::move(pm));
-    else
-      return *m_memcard_pixmap_cache.Insert(ge->serial, m_type_pixmaps[static_cast<u32>(ge->type)]);
+    }
+
+    return *m_memcard_pixmap_cache.Insert(ge->serial, m_type_pixmaps[static_cast<u32>(ge->type)]);
   }
 
   return m_type_pixmaps[static_cast<u32>(ge->type)];
@@ -285,7 +288,10 @@ QIcon GameListModel::getIconForGame(const QString& path)
       {
         QPixmap newpm;
         if (!icon_path.empty() && newpm.load(QString::fromStdString(icon_path)))
+        {
+          fixIconPixmapSize(newpm);
           ret = QIcon(*m_memcard_pixmap_cache.Insert(entry->serial, std::move(newpm)));
+        }
       }
     }
   }
@@ -293,6 +299,20 @@ QIcon GameListModel::getIconForGame(const QString& path)
   return ret;
 }
 
+void GameListModel::fixIconPixmapSize(QPixmap& pm)
+{
+  const int width = pm.width();
+  const int height = pm.height();
+  const int max_dim = std::max(width, height);
+  if (max_dim == 16)
+    return;
+
+  const float scale = static_cast<float>(max_dim) / 16.0f;
+  const int new_width = static_cast<int>(static_cast<float>(width) / scale);
+  const int new_height = static_cast<int>(static_cast<float>(height) / scale);
+  pm = pm.scaled(new_width, new_height);
+}
+
 int GameListModel::getCoverArtWidth() const
 {
   return std::max(static_cast<int>(static_cast<float>(COVER_ART_WIDTH) * m_cover_scale), 1);
diff --git a/src/duckstation-qt/gamelistmodel.h b/src/duckstation-qt/gamelistmodel.h
index f059c158e..2fdbe06b3 100644
--- a/src/duckstation-qt/gamelistmodel.h
+++ b/src/duckstation-qt/gamelistmodel.h
@@ -105,6 +105,7 @@ private:
   void invalidateCoverForPath(const std::string& path);
 
   const QPixmap& getIconPixmapForEntry(const GameList::Entry* ge) const;
+  static void fixIconPixmapSize(QPixmap& pm);
 
   static QString formatTimespan(time_t timespan);