game_list_p.h 12 KB

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