game_list_p.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-FileCopyrightText: 2015 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <map>
  6. #include <string>
  7. #include <utility>
  8. #include <QCoreApplication>
  9. #include <QFileInfo>
  10. #include <QObject>
  11. #include <QStandardItem>
  12. #include <QString>
  13. #include <QWidget>
  14. #include "common/common_types.h"
  15. #include "common/logging/log.h"
  16. #include "common/string_util.h"
  17. #include "yuzu/uisettings.h"
  18. #include "yuzu/util/util.h"
  19. enum class GameListItemType {
  20. Game = QStandardItem::UserType + 1,
  21. CustomDir = QStandardItem::UserType + 2,
  22. SdmcDir = QStandardItem::UserType + 3,
  23. UserNandDir = QStandardItem::UserType + 4,
  24. SysNandDir = QStandardItem::UserType + 5,
  25. AddDir = QStandardItem::UserType + 6,
  26. Favorites = QStandardItem::UserType + 7,
  27. };
  28. Q_DECLARE_METATYPE(GameListItemType);
  29. /**
  30. * Gets the default icon (for games without valid title metadata)
  31. * @param size The desired width and height of the default icon.
  32. * @return QPixmap default icon
  33. */
  34. static QPixmap GetDefaultIcon(u32 size) {
  35. QPixmap icon(size, size);
  36. icon.fill(Qt::transparent);
  37. return icon;
  38. }
  39. class GameListItem : public QStandardItem {
  40. public:
  41. // used to access type from item index
  42. static constexpr int TypeRole = Qt::UserRole + 1;
  43. static constexpr int SortRole = Qt::UserRole + 2;
  44. GameListItem() = default;
  45. explicit GameListItem(const QString& string) : QStandardItem(string) {
  46. setData(string, SortRole);
  47. }
  48. };
  49. /**
  50. * A specialization of GameListItem for path values.
  51. * This class ensures that for every full path value it holds, a correct string representation
  52. * of just the filename (with no extension) will be displayed to the user.
  53. * If this class receives valid title metadata, it will also display game icons and titles.
  54. */
  55. class GameListItemPath : public GameListItem {
  56. public:
  57. static constexpr int TitleRole = SortRole + 1;
  58. static constexpr int FullPathRole = SortRole + 2;
  59. static constexpr int ProgramIdRole = SortRole + 3;
  60. static constexpr int FileTypeRole = SortRole + 4;
  61. GameListItemPath() = default;
  62. GameListItemPath(const QString& game_path, const std::vector<u8>& picture_data,
  63. const QString& game_name, const QString& game_type, u64 program_id) {
  64. setData(type(), TypeRole);
  65. setData(game_path, FullPathRole);
  66. setData(game_name, TitleRole);
  67. setData(qulonglong(program_id), ProgramIdRole);
  68. setData(game_type, FileTypeRole);
  69. const u32 size = UISettings::values.game_icon_size.GetValue();
  70. QPixmap picture;
  71. if (!picture.loadFromData(picture_data.data(), static_cast<u32>(picture_data.size()))) {
  72. picture = GetDefaultIcon(size);
  73. }
  74. picture = picture.scaled(size, size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  75. setData(picture, Qt::DecorationRole);
  76. }
  77. int type() const override {
  78. return static_cast<int>(GameListItemType::Game);
  79. }
  80. QVariant data(int role) const override {
  81. if (role == Qt::DisplayRole || role == SortRole) {
  82. std::string filename;
  83. Common::SplitPath(data(FullPathRole).toString().toStdString(), nullptr, &filename,
  84. nullptr);
  85. const std::array<QString, 4> row_data{{
  86. QString::fromStdString(filename),
  87. data(FileTypeRole).toString(),
  88. QString::fromStdString(fmt::format("0x{:016X}", data(ProgramIdRole).toULongLong())),
  89. data(TitleRole).toString(),
  90. }};
  91. const auto& row1 = row_data.at(UISettings::values.row_1_text_id.GetValue());
  92. const int row2_id = UISettings::values.row_2_text_id.GetValue();
  93. if (role == SortRole) {
  94. return row1.toLower();
  95. }
  96. // None
  97. if (row2_id == 4) {
  98. return row1;
  99. }
  100. const auto& row2 = row_data.at(row2_id);
  101. if (row1 == row2) {
  102. return row1;
  103. }
  104. return QStringLiteral("%1\n %2").arg(row1, row2);
  105. }
  106. return GameListItem::data(role);
  107. }
  108. };
  109. class GameListItemCompat : public GameListItem {
  110. Q_DECLARE_TR_FUNCTIONS(GameListItemCompat)
  111. public:
  112. static constexpr int CompatNumberRole = SortRole;
  113. GameListItemCompat() = default;
  114. explicit GameListItemCompat(const QString& compatibility) {
  115. setData(type(), TypeRole);
  116. struct CompatStatus {
  117. QString color;
  118. const char* text;
  119. const char* tooltip;
  120. };
  121. // clang-format off
  122. const auto ingame_status =
  123. CompatStatus{QStringLiteral("#f2d624"), QT_TR_NOOP("Ingame"), QT_TR_NOOP("Game starts, but crashes or major glitches prevent it from being completed.")};
  124. static const std::map<QString, CompatStatus> status_data = {
  125. {QStringLiteral("0"), {QStringLiteral("#5c93ed"), QT_TR_NOOP("Perfect"), QT_TR_NOOP("Game can be played without issues.")}},
  126. {QStringLiteral("1"), {QStringLiteral("#47d35c"), QT_TR_NOOP("Playable"), QT_TR_NOOP("Game functions with minor graphical or audio glitches and is playable from start to finish.")}},
  127. {QStringLiteral("2"), ingame_status},
  128. {QStringLiteral("3"), ingame_status}, // Fallback for the removed "Okay" category
  129. {QStringLiteral("4"), {QStringLiteral("#FF0000"), QT_TR_NOOP("Intro/Menu"), QT_TR_NOOP("Game loads, but is unable to progress past the Start Screen.")}},
  130. {QStringLiteral("5"), {QStringLiteral("#828282"), QT_TR_NOOP("Won't Boot"), QT_TR_NOOP("The game crashes when attempting to startup.")}},
  131. {QStringLiteral("99"), {QStringLiteral("#000000"), QT_TR_NOOP("Not Tested"), QT_TR_NOOP("The game has not yet been tested.")}},
  132. };
  133. // clang-format on
  134. auto iterator = status_data.find(compatibility);
  135. if (iterator == status_data.end()) {
  136. LOG_WARNING(Frontend, "Invalid compatibility number {}", compatibility.toStdString());
  137. return;
  138. }
  139. const CompatStatus& status = iterator->second;
  140. setData(compatibility, CompatNumberRole);
  141. setText(tr(status.text));
  142. setToolTip(tr(status.tooltip));
  143. setData(CreateCirclePixmapFromColor(status.color), Qt::DecorationRole);
  144. }
  145. int type() const override {
  146. return static_cast<int>(GameListItemType::Game);
  147. }
  148. bool operator<(const QStandardItem& other) const override {
  149. return data(CompatNumberRole).value<QString>() <
  150. other.data(CompatNumberRole).value<QString>();
  151. }
  152. };
  153. /**
  154. * A specialization of GameListItem for size values.
  155. * This class ensures that for every numerical size value it holds (in bytes), a correct
  156. * human-readable string representation will be displayed to the user.
  157. */
  158. class GameListItemSize : public GameListItem {
  159. public:
  160. static constexpr int SizeRole = SortRole;
  161. GameListItemSize() = default;
  162. explicit GameListItemSize(const qulonglong size_bytes) {
  163. setData(type(), TypeRole);
  164. setData(size_bytes, SizeRole);
  165. }
  166. void setData(const QVariant& value, int role) override {
  167. // By specializing setData for SizeRole, we can ensure that the numerical and string
  168. // representations of the data are always accurate and in the correct format.
  169. if (role == SizeRole) {
  170. qulonglong size_bytes = value.toULongLong();
  171. GameListItem::setData(ReadableByteSize(size_bytes), Qt::DisplayRole);
  172. GameListItem::setData(value, SizeRole);
  173. } else {
  174. GameListItem::setData(value, role);
  175. }
  176. }
  177. int type() const override {
  178. return static_cast<int>(GameListItemType::Game);
  179. }
  180. /**
  181. * This operator is, in practice, only used by the TreeView sorting systems.
  182. * Override it so that it will correctly sort by numerical value instead of by string
  183. * representation.
  184. */
  185. bool operator<(const QStandardItem& other) const override {
  186. return data(SizeRole).toULongLong() < other.data(SizeRole).toULongLong();
  187. }
  188. };
  189. class GameListDir : public GameListItem {
  190. public:
  191. static constexpr int GameDirRole = Qt::UserRole + 2;
  192. explicit GameListDir(UISettings::GameDir& directory,
  193. GameListItemType dir_type_ = GameListItemType::CustomDir)
  194. : dir_type{dir_type_} {
  195. setData(type(), TypeRole);
  196. UISettings::GameDir* game_dir = &directory;
  197. setData(QVariant(UISettings::values.game_dirs.indexOf(directory)), GameDirRole);
  198. const int icon_size = UISettings::values.folder_icon_size.GetValue();
  199. switch (dir_type) {
  200. case GameListItemType::SdmcDir:
  201. setData(
  202. QIcon::fromTheme(QStringLiteral("sd_card"))
  203. .pixmap(icon_size)
  204. .scaled(icon_size, icon_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
  205. Qt::DecorationRole);
  206. setData(QObject::tr("Installed SD Titles"), Qt::DisplayRole);
  207. break;
  208. case GameListItemType::UserNandDir:
  209. setData(
  210. QIcon::fromTheme(QStringLiteral("chip"))
  211. .pixmap(icon_size)
  212. .scaled(icon_size, icon_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
  213. Qt::DecorationRole);
  214. setData(QObject::tr("Installed NAND Titles"), Qt::DisplayRole);
  215. break;
  216. case GameListItemType::SysNandDir:
  217. setData(
  218. QIcon::fromTheme(QStringLiteral("chip"))
  219. .pixmap(icon_size)
  220. .scaled(icon_size, icon_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
  221. Qt::DecorationRole);
  222. setData(QObject::tr("System Titles"), Qt::DisplayRole);
  223. break;
  224. case GameListItemType::CustomDir: {
  225. const QString icon_name = QFileInfo::exists(game_dir->path)
  226. ? QStringLiteral("folder")
  227. : QStringLiteral("bad_folder");
  228. setData(QIcon::fromTheme(icon_name).pixmap(icon_size).scaled(
  229. icon_size, icon_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
  230. Qt::DecorationRole);
  231. setData(game_dir->path, Qt::DisplayRole);
  232. break;
  233. }
  234. default:
  235. break;
  236. }
  237. }
  238. int type() const override {
  239. return static_cast<int>(dir_type);
  240. }
  241. /**
  242. * Override to prevent automatic sorting between folders and the addDir button.
  243. */
  244. bool operator<(const QStandardItem& other) const override {
  245. return false;
  246. }
  247. private:
  248. GameListItemType dir_type;
  249. };
  250. class GameListAddDir : public GameListItem {
  251. public:
  252. explicit GameListAddDir() {
  253. setData(type(), TypeRole);
  254. const int icon_size = UISettings::values.folder_icon_size.GetValue();
  255. setData(QIcon::fromTheme(QStringLiteral("list-add"))
  256. .pixmap(icon_size)
  257. .scaled(icon_size, icon_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
  258. Qt::DecorationRole);
  259. setData(QObject::tr("Add New Game Directory"), Qt::DisplayRole);
  260. }
  261. int type() const override {
  262. return static_cast<int>(GameListItemType::AddDir);
  263. }
  264. bool operator<(const QStandardItem& other) const override {
  265. return false;
  266. }
  267. };
  268. class GameListFavorites : public GameListItem {
  269. public:
  270. explicit GameListFavorites() {
  271. setData(type(), TypeRole);
  272. const int icon_size = UISettings::values.folder_icon_size.GetValue();
  273. setData(QIcon::fromTheme(QStringLiteral("star"))
  274. .pixmap(icon_size)
  275. .scaled(icon_size, icon_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
  276. Qt::DecorationRole);
  277. setData(QObject::tr("Favorites"), Qt::DisplayRole);
  278. }
  279. int type() const override {
  280. return static_cast<int>(GameListItemType::Favorites);
  281. }
  282. bool operator<(const QStandardItem& other) const override {
  283. return false;
  284. }
  285. };
  286. class GameList;
  287. class QHBoxLayout;
  288. class QTreeView;
  289. class QLabel;
  290. class QLineEdit;
  291. class QToolButton;
  292. class GameListSearchField : public QWidget {
  293. Q_OBJECT
  294. public:
  295. explicit GameListSearchField(GameList* parent = nullptr);
  296. QString filterText() const;
  297. void setFilterResult(int visible_, int total_);
  298. void clear();
  299. void setFocus();
  300. private:
  301. void changeEvent(QEvent*) override;
  302. void RetranslateUI();
  303. class KeyReleaseEater : public QObject {
  304. public:
  305. explicit KeyReleaseEater(GameList* gamelist_, QObject* parent = nullptr);
  306. private:
  307. GameList* gamelist = nullptr;
  308. QString edit_filter_text_old;
  309. protected:
  310. // EventFilter in order to process systemkeys while editing the searchfield
  311. bool eventFilter(QObject* obj, QEvent* event) override;
  312. };
  313. int visible;
  314. int total;
  315. QHBoxLayout* layout_filter = nullptr;
  316. QTreeView* tree_view = nullptr;
  317. QLabel* label_filter = nullptr;
  318. QLineEdit* edit_filter = nullptr;
  319. QLabel* label_filter_result = nullptr;
  320. QToolButton* button_filter_close = nullptr;
  321. };