Open Source Snapmatic picture and Savegame viewer/editor for GTA V https://gta5view.syping.de/

PlayerListDialog.cpp 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*****************************************************************************
  2. * gta5view Grand Theft Auto V Profile Viewer
  3. * Copyright (C) 2016-2018 Syping
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *****************************************************************************/
  18. #include "PlayerListDialog.h"
  19. #include "ui_PlayerListDialog.h"
  20. #include "AppEnv.h"
  21. #include <QFontMetrics>
  22. #include <QInputDialog>
  23. #include <QMessageBox>
  24. #include <QPainter>
  25. #include <QPixmap>
  26. #include <QDebug>
  27. PlayerListDialog::PlayerListDialog(QStringList players, ProfileDatabase *profileDB, QWidget *parent) :
  28. QDialog(parent), players(players), profileDB(profileDB),
  29. ui(new Ui::PlayerListDialog)
  30. {
  31. // Set Window Flags
  32. setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
  33. listUpdated = false;
  34. ui->setupUi(this);
  35. ui->cmdCancel->setDefault(true);
  36. ui->cmdCancel->setFocus();
  37. // Set Icon for Apply Button
  38. if (QIcon::hasThemeIcon("dialog-ok-apply"))
  39. {
  40. ui->cmdApply->setIcon(QIcon::fromTheme("dialog-ok-apply"));
  41. }
  42. else if (QIcon::hasThemeIcon("dialog-apply"))
  43. {
  44. ui->cmdApply->setIcon(QIcon::fromTheme("dialog-apply"));
  45. }
  46. else if (QIcon::hasThemeIcon("gtk-apply"))
  47. {
  48. ui->cmdApply->setIcon(QIcon::fromTheme("gtk-apply"));
  49. }
  50. else if (QIcon::hasThemeIcon("dialog-ok"))
  51. {
  52. ui->cmdApply->setIcon(QIcon::fromTheme("dialog-ok"));
  53. }
  54. else if (QIcon::hasThemeIcon("gtk-ok"))
  55. {
  56. ui->cmdApply->setIcon(QIcon::fromTheme("dialog-ok"));
  57. }
  58. // Set Icon for Cancel Button
  59. if (QIcon::hasThemeIcon("dialog-cancel"))
  60. {
  61. ui->cmdCancel->setIcon(QIcon::fromTheme("dialog-cancel"));
  62. }
  63. else if (QIcon::hasThemeIcon("gtk-cancel"))
  64. {
  65. ui->cmdCancel->setIcon(QIcon::fromTheme("gtk-cancel"));
  66. }
  67. // Set Icon for Manage Buttons
  68. if (QIcon::hasThemeIcon("go-previous") && QIcon::hasThemeIcon("go-next") && QIcon::hasThemeIcon("list-add"))
  69. {
  70. ui->cmdMakeAv->setIcon(QIcon::fromTheme("go-previous"));
  71. ui->cmdMakeSe->setIcon(QIcon::fromTheme("go-next"));
  72. ui->cmdMakeAd->setIcon(QIcon::fromTheme("list-add"));
  73. }
  74. else
  75. {
  76. drawSwitchButtons();
  77. }
  78. buildInterface();
  79. // DPI calculation
  80. qreal screenRatio = AppEnv::screenRatio();
  81. resize(500 * screenRatio, 350 * screenRatio);
  82. }
  83. PlayerListDialog::~PlayerListDialog()
  84. {
  85. for (QObject *object : ui->listAvPlayers->children())
  86. {
  87. delete object;
  88. }
  89. for (QObject *object : ui->listSePlayers->children())
  90. {
  91. delete object;
  92. }
  93. delete ui;
  94. }
  95. void PlayerListDialog::drawSwitchButtons()
  96. {
  97. QFont painterFont = ui->cmdApply->font();
  98. QPalette palette;
  99. QFontMetrics fontMetrics(painterFont);
  100. QRect makeAvRect = fontMetrics.boundingRect(QRect(0, 0, 0, 0), Qt::AlignCenter | Qt::TextDontClip, "<");
  101. QRect makeSeRect = fontMetrics.boundingRect(QRect(0, 0, 0, 0), Qt::AlignCenter | Qt::TextDontClip, ">");
  102. QRect makeAdRect = fontMetrics.boundingRect(QRect(0, 0, 0, 0), Qt::AlignCenter | Qt::TextDontClip, "+");
  103. int makeAvSize;
  104. if (makeAvRect.height() > makeAvRect.width())
  105. {
  106. makeAvSize = makeAvRect.height();
  107. }
  108. else
  109. {
  110. makeAvSize = makeAvRect.width();
  111. }
  112. int makeSeSize;
  113. if (makeSeRect.height() > makeSeRect.width())
  114. {
  115. makeSeSize = makeSeRect.height();
  116. }
  117. else
  118. {
  119. makeSeSize = makeSeRect.width();
  120. }
  121. int makeAdSize;
  122. if (makeAdRect.height() > makeAdRect.width())
  123. {
  124. makeAdSize = makeAdRect.height();
  125. }
  126. else
  127. {
  128. makeAdSize = makeAdRect.width();
  129. }
  130. QImage avImage(makeAvSize, makeAvSize, QImage::Format_ARGB32_Premultiplied);
  131. avImage.fill(Qt::transparent);
  132. QImage seImage(makeSeSize, makeSeSize, QImage::Format_ARGB32_Premultiplied);
  133. seImage.fill(Qt::transparent);
  134. QImage adImage(makeAdSize, makeAdSize, QImage::Format_ARGB32_Premultiplied);
  135. adImage.fill(Qt::transparent);
  136. QPainter avPainter(&avImage);
  137. avPainter.setFont(painterFont);
  138. avPainter.setBrush(palette.buttonText());
  139. avPainter.setPen(palette.buttonText().color());
  140. avPainter.drawText(0, 0, makeAvSize, makeAvSize, Qt::AlignCenter | Qt::TextDontClip, "<");
  141. avPainter.end();
  142. QPainter sePainter(&seImage);
  143. sePainter.setFont(painterFont);
  144. sePainter.setBrush(palette.buttonText());
  145. sePainter.setPen(palette.buttonText().color());
  146. sePainter.drawText(0, 0, makeSeSize, makeSeSize, Qt::AlignCenter | Qt::TextDontClip, ">");
  147. sePainter.end();
  148. QPainter adPainter(&adImage);
  149. adPainter.setFont(painterFont);
  150. adPainter.setBrush(palette.buttonText());
  151. adPainter.setPen(palette.buttonText().color());
  152. adPainter.drawText(0, 0, makeAdSize, makeAdSize, Qt::AlignCenter | Qt::TextDontClip, "+");
  153. adPainter.end();
  154. ui->cmdMakeAv->setIconSize(avImage.size());
  155. ui->cmdMakeSe->setIconSize(seImage.size());
  156. ui->cmdMakeAd->setIconSize(adImage.size());
  157. ui->cmdMakeAv->setIcon(QIcon(QPixmap::fromImage(avImage)));
  158. ui->cmdMakeSe->setIcon(QIcon(QPixmap::fromImage(seImage)));
  159. ui->cmdMakeAd->setIcon(QIcon(QPixmap::fromImage(adImage)));
  160. }
  161. void PlayerListDialog::on_cmdCancel_clicked()
  162. {
  163. close();
  164. }
  165. void PlayerListDialog::buildInterface()
  166. {
  167. const QStringList dbPlayers = profileDB->getPlayers();
  168. for (QString sePlayer : players)
  169. {
  170. QListWidgetItem *playerItem = new QListWidgetItem(profileDB->getPlayerName(sePlayer));
  171. playerItem->setData(Qt::UserRole, sePlayer);
  172. ui->listSePlayers->addItem(playerItem);
  173. }
  174. for (QString dbPlayer : dbPlayers)
  175. {
  176. if (!players.contains(dbPlayer))
  177. {
  178. QListWidgetItem *playerItem = new QListWidgetItem(profileDB->getPlayerName(dbPlayer));
  179. playerItem->setData(Qt::UserRole, dbPlayer);
  180. ui->listAvPlayers->addItem(playerItem);
  181. }
  182. }
  183. ui->listAvPlayers->sortItems(Qt::AscendingOrder);
  184. }
  185. void PlayerListDialog::on_cmdMakeAv_clicked()
  186. {
  187. for (QListWidgetItem *item : ui->listSePlayers->selectedItems())
  188. {
  189. QString playerName = item->text();
  190. int playerID = item->data(Qt::UserRole).toInt();
  191. delete item;
  192. QListWidgetItem *playerItem = new QListWidgetItem(playerName);
  193. playerItem->setData(Qt::UserRole, playerID);
  194. ui->listAvPlayers->addItem(playerItem);
  195. ui->listAvPlayers->sortItems(Qt::AscendingOrder);
  196. }
  197. }
  198. void PlayerListDialog::on_cmdMakeSe_clicked()
  199. {
  200. int maxPlayers = 30;
  201. if (maxPlayers < ui->listSePlayers->count() + ui->listAvPlayers->selectedItems().count())
  202. {
  203. QMessageBox::warning(this, tr("Add Players..."), tr("Failed to add more Players because the limit of Players are %1!").arg(QString::number(maxPlayers)));
  204. return;
  205. }
  206. for (QListWidgetItem *item : ui->listAvPlayers->selectedItems())
  207. {
  208. QString playerName = item->text();
  209. int playerID = item->data(Qt::UserRole).toInt();
  210. delete item;
  211. QListWidgetItem *playerItem = new QListWidgetItem(playerName);
  212. playerItem->setData(Qt::UserRole, playerID);
  213. ui->listSePlayers->addItem(playerItem);
  214. }
  215. }
  216. void PlayerListDialog::on_cmdMakeAd_clicked()
  217. {
  218. bool playerOk;
  219. int playerID = QInputDialog::getInt(this, tr("Add Player..."), tr("Enter Social Club Player ID"), 1, 1, 214783647, 1, &playerOk, windowFlags());
  220. if (playerOk)
  221. {
  222. for (int i = 0; i < ui->listAvPlayers->count(); ++i)
  223. {
  224. QListWidgetItem *item = ui->listAvPlayers->item(i);
  225. QString itemPlayerName = item->text();
  226. int itemPlayerID = item->data(Qt::UserRole).toInt();
  227. if (itemPlayerID == playerID)
  228. {
  229. delete item;
  230. QListWidgetItem *playerItem = new QListWidgetItem(itemPlayerName);
  231. playerItem->setData(Qt::UserRole, playerID);
  232. ui->listSePlayers->addItem(playerItem);
  233. return;
  234. }
  235. }
  236. for (int i = 0; i < ui->listSePlayers->count(); ++i)
  237. {
  238. QListWidgetItem *item = ui->listSePlayers->item(i);
  239. int itemPlayerID = item->data(Qt::UserRole).toInt();
  240. if (itemPlayerID == playerID)
  241. {
  242. QMessageBox::warning(this, tr("Add Player..."), tr("Failed to add Player %1 because Player %1 is already added!").arg(QString::number(playerID)));
  243. return;
  244. }
  245. }
  246. QListWidgetItem *playerItem = new QListWidgetItem(QString::number(playerID));
  247. playerItem->setData(Qt::UserRole, playerID);
  248. ui->listSePlayers->addItem(playerItem);
  249. }
  250. }
  251. void PlayerListDialog::on_cmdApply_clicked()
  252. {
  253. players.clear();
  254. for (int i = 0; i < ui->listSePlayers->count(); ++i)
  255. {
  256. players += ui->listSePlayers->item(i)->data(Qt::UserRole).toString();
  257. }
  258. emit playerListUpdated(players);
  259. listUpdated = true;
  260. close();
  261. }
  262. QStringList PlayerListDialog::getPlayerList() const
  263. {
  264. return players;
  265. }
  266. bool PlayerListDialog::isListUpdated()
  267. {
  268. return listUpdated;
  269. }