game_list_p.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 constexpr int TypeRole = Qt::UserRole + 1;
  44. static constexpr int SortRole = Qt::UserRole + 2;
  45. GameListItem() = default;
  46. explicit 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 constexpr int TitleRole = SortRole + 1;
  59. static constexpr int FullPathRole = SortRole + 2;
  60. static constexpr int ProgramIdRole = SortRole + 3;
  61. static constexpr int FileTypeRole = SortRole + 4;
  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 || role == SortRole) {
  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 (role == SortRole) {
  95. return row1.toLower();
  96. }
  97. // None
  98. if (row2_id == 4) {
  99. return row1;
  100. }
  101. const auto& row2 = row_data.at(row2_id);
  102. if (row1 == row2) {
  103. return row1;
  104. }
  105. return QStringLiteral("%1\n %2").arg(row1, row2);
  106. }
  107. return GameListItem::data(role);
  108. }
  109. };
  110. class GameListItemCompat : public GameListItem {
  111. Q_DECLARE_TR_FUNCTIONS(GameListItemCompat)
  112. public:
  113. static constexpr int CompatNumberRole = SortRole;
  114. GameListItemCompat() = default;
  115. explicit GameListItemCompat(const QString& compatibility) {
  116. setData(type(), TypeRole);
  117. struct CompatStatus {
  118. QString color;
  119. const char* text;
  120. const char* tooltip;
  121. };
  122. // clang-format off
  123. static const std::map<QString, CompatStatus> status_data = {
  124. {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.")}},
  125. {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.")}},
  126. {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.")}},
  127. {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.")}},
  128. {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.")}},
  129. {QStringLiteral("5"), {QStringLiteral("#828282"), QT_TR_NOOP("Won't Boot"), QT_TR_NOOP("The game crashes when attempting to startup.")}},
  130. {QStringLiteral("99"), {QStringLiteral("#000000"), QT_TR_NOOP("Not Tested"), QT_TR_NOOP("The game has not yet been tested.")}},
  131. };
  132. // clang-format on
  133. auto iterator = status_data.find(compatibility);
  134. if (iterator == status_data.end()) {
  135. LOG_WARNING(Frontend, "Invalid compatibility number {}", compatibility.toStdString());
  136. return;
  137. }
  138. const CompatStatus& status = iterator->second;
  139. setData(compatibility, CompatNumberRole);
  140. setText(QObject::tr(status.text));
  141. setToolTip(QObject::tr(status.tooltip));
  142. setData(CreateCirclePixmapFromColor(status.color), Qt::DecorationRole);
  143. }
  144. int type() const override {
  145. return static_cast<int>(GameListItemType::Game);
  146. }
  147. bool operator<(const QStandardItem& other) const override {
  148. return data(CompatNumberRole).value<QString>() <
  149. other.data(CompatNumberRole).value<QString>();
  150. }
  151. };
  152. /**
  153. * A specialization of GameListItem for size values.
  154. * This class ensures that for every numerical size value it holds (in bytes), a correct
  155. * human-readable string representation will be displayed to the user.
  156. */
  157. class GameListItemSize : public GameListItem {
  158. public:
  159. static constexpr int SizeRole = SortRole;
  160. GameListItemSize() = default;
  161. explicit GameListItemSize(const qulonglong size_bytes) {
  162. setData(type(), TypeRole);
  163. setData(size_bytes, SizeRole);
  164. }
  165. void setData(const QVariant& value, int role) override {
  166. // By specializing setData for SizeRole, we can ensure that the numerical and string
  167. // representations of the data are always accurate and in the correct format.
  168. if (role == SizeRole) {
  169. qulonglong size_bytes = value.toULongLong();
  170. GameListItem::setData(ReadableByteSize(size_bytes), Qt::DisplayRole);
  171. GameListItem::setData(value, SizeRole);
  172. } else {
  173. GameListItem::setData(value, role);
  174. }
  175. }
  176. int type() const override {
  177. return static_cast<int>(GameListItemType::Game);
  178. }
  179. /**
  180. * This operator is, in practice, only used by the TreeView sorting systems.
  181. * Override it so that it will correctly sort by numerical value instead of by string
  182. * representation.
  183. */
  184. bool operator<(const QStandardItem& other) const override {
  185. return data(SizeRole).toULongLong() < other.data(SizeRole).toULongLong();
  186. }
  187. };
  188. class GameListDir : public GameListItem {
  189. public:
  190. static constexpr int GameDirRole = Qt::UserRole + 2;
  191. explicit GameListDir(UISettings::GameDir& directory,
  192. GameListItemType dir_type = GameListItemType::CustomDir)
  193. : dir_type{dir_type} {
  194. setData(type(), TypeRole);
  195. UISettings::GameDir* game_dir = &directory;
  196. setData(QVariant(UISettings::values.game_dirs.indexOf(directory)), GameDirRole);
  197. const int icon_size = std::min(static_cast<int>(UISettings::values.icon_size), 64);
  198. switch (dir_type) {
  199. case GameListItemType::SdmcDir:
  200. setData(
  201. QIcon::fromTheme(QStringLiteral("sd_card"))
  202. .pixmap(icon_size)
  203. .scaled(icon_size, icon_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
  204. Qt::DecorationRole);
  205. setData(QObject::tr("Installed SD Titles"), Qt::DisplayRole);
  206. break;
  207. case GameListItemType::UserNandDir:
  208. setData(
  209. QIcon::fromTheme(QStringLiteral("chip"))
  210. .pixmap(icon_size)
  211. .scaled(icon_size, icon_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
  212. Qt::DecorationRole);
  213. setData(QObject::tr("Installed NAND Titles"), Qt::DisplayRole);
  214. break;
  215. case GameListItemType::SysNandDir:
  216. setData(
  217. QIcon::fromTheme(QStringLiteral("chip"))
  218. .pixmap(icon_size)
  219. .scaled(icon_size, icon_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
  220. Qt::DecorationRole);
  221. setData(QObject::tr("System Titles"), Qt::DisplayRole);
  222. break;
  223. case GameListItemType::CustomDir: {
  224. const QString icon_name = QFileInfo::exists(game_dir->path)
  225. ? QStringLiteral("folder")
  226. : QStringLiteral("bad_folder");
  227. setData(QIcon::fromTheme(icon_name).pixmap(icon_size).scaled(
  228. icon_size, icon_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
  229. Qt::DecorationRole);
  230. setData(game_dir->path, Qt::DisplayRole);
  231. break;
  232. }
  233. default:
  234. break;
  235. }
  236. }
  237. int type() const override {
  238. return static_cast<int>(dir_type);
  239. }
  240. /**
  241. * Override to prevent automatic sorting between folders and the addDir button.
  242. */
  243. bool operator<(const QStandardItem& other) const override {
  244. return false;
  245. }
  246. private:
  247. GameListItemType dir_type;
  248. };
  249. class GameListAddDir : public GameListItem {
  250. public:
  251. explicit GameListAddDir() {
  252. setData(type(), TypeRole);
  253. const int icon_size = std::min(static_cast<int>(UISettings::values.icon_size), 64);
  254. setData(QIcon::fromTheme(QStringLiteral("plus"))
  255. .pixmap(icon_size)
  256. .scaled(icon_size, icon_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
  257. Qt::DecorationRole);
  258. setData(QObject::tr("Add New Game Directory"), Qt::DisplayRole);
  259. }
  260. int type() const override {
  261. return static_cast<int>(GameListItemType::AddDir);
  262. }
  263. bool operator<(const QStandardItem& other) const override {
  264. return false;
  265. }
  266. };
  267. class GameList;
  268. class QHBoxLayout;
  269. class QTreeView;
  270. class QLabel;
  271. class QLineEdit;
  272. class QToolButton;
  273. class GameListSearchField : public QWidget {
  274. Q_OBJECT
  275. public:
  276. explicit GameListSearchField(GameList* parent = nullptr);
  277. void setFilterResult(int visible, int total);
  278. void clear();
  279. void setFocus();
  280. private:
  281. class KeyReleaseEater : public QObject {
  282. public:
  283. explicit KeyReleaseEater(GameList* gamelist, QObject* parent = nullptr);
  284. private:
  285. GameList* gamelist = nullptr;
  286. QString edit_filter_text_old;
  287. protected:
  288. // EventFilter in order to process systemkeys while editing the searchfield
  289. bool eventFilter(QObject* obj, QEvent* event) override;
  290. };
  291. int visible;
  292. int total;
  293. QHBoxLayout* layout_filter = nullptr;
  294. QTreeView* tree_view = nullptr;
  295. QLabel* label_filter = nullptr;
  296. QLineEdit* edit_filter = nullptr;
  297. QLabel* label_filter_result = nullptr;
  298. QToolButton* button_filter_close = nullptr;
  299. };