lobby_p.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <utility>
  5. #include <QPixmap>
  6. #include <QStandardItem>
  7. #include <QStandardItemModel>
  8. #include "common/common_types.h"
  9. namespace Column {
  10. enum List {
  11. GAME_NAME,
  12. ROOM_NAME,
  13. MEMBER,
  14. HOST,
  15. TOTAL,
  16. };
  17. }
  18. class LobbyItem : public QStandardItem {
  19. public:
  20. LobbyItem() = default;
  21. explicit LobbyItem(const QString& string) : QStandardItem(string) {}
  22. virtual ~LobbyItem() override = default;
  23. };
  24. class LobbyItemName : public LobbyItem {
  25. public:
  26. static const int NameRole = Qt::UserRole + 1;
  27. static const int PasswordRole = Qt::UserRole + 2;
  28. LobbyItemName() = default;
  29. explicit LobbyItemName(bool has_password, QString name) : LobbyItem() {
  30. setData(name, NameRole);
  31. setData(has_password, PasswordRole);
  32. }
  33. QVariant data(int role) const override {
  34. if (role == Qt::DecorationRole) {
  35. bool has_password = data(PasswordRole).toBool();
  36. return has_password ? QIcon::fromTheme(QStringLiteral("lock")).pixmap(16) : QIcon();
  37. }
  38. if (role != Qt::DisplayRole) {
  39. return LobbyItem::data(role);
  40. }
  41. return data(NameRole).toString();
  42. }
  43. bool operator<(const QStandardItem& other) const override {
  44. return data(NameRole).toString().localeAwareCompare(other.data(NameRole).toString()) < 0;
  45. }
  46. };
  47. class LobbyItemDescription : public LobbyItem {
  48. public:
  49. static const int DescriptionRole = Qt::UserRole + 1;
  50. LobbyItemDescription() = default;
  51. explicit LobbyItemDescription(QString description) {
  52. setData(description, DescriptionRole);
  53. }
  54. QVariant data(int role) const override {
  55. if (role != Qt::DisplayRole) {
  56. return LobbyItem::data(role);
  57. }
  58. auto description = data(DescriptionRole).toString();
  59. description.prepend(QStringLiteral("Description: "));
  60. return description;
  61. }
  62. bool operator<(const QStandardItem& other) const override {
  63. return data(DescriptionRole)
  64. .toString()
  65. .localeAwareCompare(other.data(DescriptionRole).toString()) < 0;
  66. }
  67. };
  68. class LobbyItemGame : public LobbyItem {
  69. public:
  70. static const int TitleIDRole = Qt::UserRole + 1;
  71. static const int GameNameRole = Qt::UserRole + 2;
  72. static const int GameIconRole = Qt::UserRole + 3;
  73. LobbyItemGame() = default;
  74. explicit LobbyItemGame(u64 title_id, QString game_name, QPixmap smdh_icon) {
  75. setData(static_cast<unsigned long long>(title_id), TitleIDRole);
  76. setData(game_name, GameNameRole);
  77. if (!smdh_icon.isNull()) {
  78. setData(smdh_icon, GameIconRole);
  79. } else {
  80. setData(QIcon::fromTheme(QStringLiteral("chip")).pixmap(32), GameIconRole);
  81. }
  82. }
  83. QVariant data(int role) const override {
  84. if (role == Qt::DecorationRole) {
  85. auto val = data(GameIconRole);
  86. if (val.isValid()) {
  87. val = val.value<QPixmap>().scaled(32, 32, Qt::KeepAspectRatio,
  88. Qt::TransformationMode::SmoothTransformation);
  89. } else {
  90. auto blank_image = QPixmap(32, 32);
  91. blank_image.fill(Qt::black);
  92. val = blank_image;
  93. }
  94. return val;
  95. } else if (role != Qt::DisplayRole) {
  96. return LobbyItem::data(role);
  97. }
  98. return data(GameNameRole).toString();
  99. }
  100. bool operator<(const QStandardItem& other) const override {
  101. return data(GameNameRole)
  102. .toString()
  103. .localeAwareCompare(other.data(GameNameRole).toString()) < 0;
  104. }
  105. };
  106. class LobbyItemHost : public LobbyItem {
  107. public:
  108. static const int HostUsernameRole = Qt::UserRole + 1;
  109. static const int HostIPRole = Qt::UserRole + 2;
  110. static const int HostPortRole = Qt::UserRole + 3;
  111. static const int HostVerifyUIDRole = Qt::UserRole + 4;
  112. LobbyItemHost() = default;
  113. explicit LobbyItemHost(QString username, QString ip, u16 port, QString verify_uid) {
  114. setData(username, HostUsernameRole);
  115. setData(ip, HostIPRole);
  116. setData(port, HostPortRole);
  117. setData(verify_uid, HostVerifyUIDRole);
  118. }
  119. QVariant data(int role) const override {
  120. if (role != Qt::DisplayRole) {
  121. return LobbyItem::data(role);
  122. }
  123. return data(HostUsernameRole).toString();
  124. }
  125. bool operator<(const QStandardItem& other) const override {
  126. return data(HostUsernameRole)
  127. .toString()
  128. .localeAwareCompare(other.data(HostUsernameRole).toString()) < 0;
  129. }
  130. };
  131. class LobbyMember {
  132. public:
  133. LobbyMember() = default;
  134. LobbyMember(const LobbyMember& other) = default;
  135. explicit LobbyMember(QString username_, QString nickname_, u64 title_id_, QString game_name_)
  136. : username(std::move(username_)), nickname(std::move(nickname_)), title_id(title_id_),
  137. game_name(std::move(game_name_)) {}
  138. ~LobbyMember() = default;
  139. QString GetName() const {
  140. if (username.isEmpty() || username == nickname) {
  141. return nickname;
  142. } else {
  143. return QStringLiteral("%1 (%2)").arg(nickname, username);
  144. }
  145. }
  146. u64 GetTitleId() const {
  147. return title_id;
  148. }
  149. QString GetGameName() const {
  150. return game_name;
  151. }
  152. private:
  153. QString username;
  154. QString nickname;
  155. u64 title_id;
  156. QString game_name;
  157. };
  158. Q_DECLARE_METATYPE(LobbyMember);
  159. class LobbyItemMemberList : public LobbyItem {
  160. public:
  161. static const int MemberListRole = Qt::UserRole + 1;
  162. static const int MaxPlayerRole = Qt::UserRole + 2;
  163. LobbyItemMemberList() = default;
  164. explicit LobbyItemMemberList(QList<QVariant> members, u32 max_players) {
  165. setData(members, MemberListRole);
  166. setData(max_players, MaxPlayerRole);
  167. }
  168. QVariant data(int role) const override {
  169. if (role != Qt::DisplayRole) {
  170. return LobbyItem::data(role);
  171. }
  172. auto members = data(MemberListRole).toList();
  173. return QStringLiteral("%1 / %2 ")
  174. .arg(QString::number(members.size()), data(MaxPlayerRole).toString());
  175. }
  176. bool operator<(const QStandardItem& other) const override {
  177. // sort by rooms that have the most players
  178. int left_members = data(MemberListRole).toList().size();
  179. int right_members = other.data(MemberListRole).toList().size();
  180. return left_members < right_members;
  181. }
  182. };
  183. /**
  184. * Member information for when a lobby is expanded in the UI
  185. */
  186. class LobbyItemExpandedMemberList : public LobbyItem {
  187. public:
  188. static const int MemberListRole = Qt::UserRole + 1;
  189. LobbyItemExpandedMemberList() = default;
  190. explicit LobbyItemExpandedMemberList(QList<QVariant> members) {
  191. setData(members, MemberListRole);
  192. }
  193. QVariant data(int role) const override {
  194. if (role != Qt::DisplayRole) {
  195. return LobbyItem::data(role);
  196. }
  197. auto members = data(MemberListRole).toList();
  198. QString out;
  199. bool first = true;
  200. for (const auto& member : members) {
  201. if (!first)
  202. out.append(QStringLiteral("\n"));
  203. const auto& m = member.value<LobbyMember>();
  204. if (m.GetGameName().isEmpty()) {
  205. out += QString(QObject::tr("%1 is not playing a game")).arg(m.GetName());
  206. } else {
  207. out += QString(QObject::tr("%1 is playing %2")).arg(m.GetName(), m.GetGameName());
  208. }
  209. first = false;
  210. }
  211. return out;
  212. }
  213. };