game_list_p.h 12 KB

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