game_list_p.h 11 KB

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