ResourcePackManager.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Copyright 2018 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DolphinQt/ResourcePackManager.h"
  4. #include <QDesktopServices>
  5. #include <QDialogButtonBox>
  6. #include <QGridLayout>
  7. #include <QHeaderView>
  8. #include <QPushButton>
  9. #include <QTableWidget>
  10. #include <QUrl>
  11. #include "Common/FileUtil.h"
  12. #include "DolphinQt/QtUtils/ModalMessageBox.h"
  13. #include "DolphinQt/QtUtils/NonDefaultQPushButton.h"
  14. #include "DolphinQt/QtUtils/SetWindowDecorations.h"
  15. #include "UICommon/ResourcePack/Manager.h"
  16. ResourcePackManager::ResourcePackManager(QWidget* widget) : QDialog(widget)
  17. {
  18. CreateWidgets();
  19. ConnectWidgets();
  20. RepopulateTable();
  21. setWindowTitle(tr("Resource Pack Manager"));
  22. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  23. resize(QSize(900, 600));
  24. }
  25. void ResourcePackManager::CreateWidgets()
  26. {
  27. auto* layout = new QGridLayout;
  28. m_table_widget = new QTableWidget;
  29. m_table_widget->setTabKeyNavigation(false);
  30. m_open_directory_button = new NonDefaultQPushButton(tr("Open Directory..."));
  31. m_change_button = new NonDefaultQPushButton(tr("Install"));
  32. m_remove_button = new NonDefaultQPushButton(tr("Remove"));
  33. m_refresh_button = new NonDefaultQPushButton(tr("Refresh"));
  34. m_priority_up_button = new NonDefaultQPushButton(tr("Up"));
  35. m_priority_down_button = new NonDefaultQPushButton(tr("Down"));
  36. auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok);
  37. connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
  38. layout->addWidget(m_table_widget, 0, 0, 7, 1);
  39. layout->addWidget(m_open_directory_button, 0, 1);
  40. layout->addWidget(m_change_button, 1, 1);
  41. layout->addWidget(m_remove_button, 2, 1);
  42. layout->addWidget(m_refresh_button, 3, 1);
  43. layout->addWidget(m_priority_up_button, 4, 1);
  44. layout->addWidget(m_priority_down_button, 5, 1);
  45. layout->addWidget(buttons, 7, 1, Qt::AlignRight);
  46. setLayout(layout);
  47. setLayout(layout);
  48. }
  49. void ResourcePackManager::ConnectWidgets()
  50. {
  51. connect(m_open_directory_button, &QPushButton::clicked, this,
  52. &ResourcePackManager::OpenResourcePackDir);
  53. connect(m_refresh_button, &QPushButton::clicked, this, &ResourcePackManager::Refresh);
  54. connect(m_change_button, &QPushButton::clicked, this, &ResourcePackManager::Change);
  55. connect(m_remove_button, &QPushButton::clicked, this, &ResourcePackManager::Remove);
  56. connect(m_priority_up_button, &QPushButton::clicked, this, &ResourcePackManager::PriorityUp);
  57. connect(m_priority_down_button, &QPushButton::clicked, this, &ResourcePackManager::PriorityDown);
  58. connect(m_table_widget, &QTableWidget::itemSelectionChanged, this,
  59. &ResourcePackManager::SelectionChanged);
  60. connect(m_table_widget, &QTableWidget::itemDoubleClicked, this,
  61. &ResourcePackManager::ItemDoubleClicked);
  62. }
  63. void ResourcePackManager::OpenResourcePackDir()
  64. {
  65. QDesktopServices::openUrl(
  66. QUrl::fromLocalFile(QString::fromStdString(File::GetUserPath(D_RESOURCEPACK_IDX))));
  67. }
  68. void ResourcePackManager::RepopulateTable()
  69. {
  70. m_table_widget->clear();
  71. m_table_widget->setColumnCount(6);
  72. m_table_widget->setHorizontalHeaderLabels(
  73. {QString{}, tr("Name"), tr("Version"), tr("Description"), tr("Author"), tr("Website")});
  74. auto* header = m_table_widget->horizontalHeader();
  75. for (int i = 0; i < 4; i++)
  76. header->setSectionResizeMode(i, QHeaderView::ResizeToContents);
  77. header->setStretchLastSection(true);
  78. header->setHighlightSections(false);
  79. int size = static_cast<int>(ResourcePack::GetPacks().size());
  80. m_table_widget->setSelectionBehavior(QAbstractItemView::SelectRows);
  81. m_table_widget->setSelectionMode(QAbstractItemView::SingleSelection);
  82. m_table_widget->setRowCount(size);
  83. m_table_widget->setIconSize(QSize(32, 32));
  84. for (int i = 0; i < size; i++)
  85. {
  86. const auto& pack = ResourcePack::GetPacks()[size - 1 - i];
  87. const auto* manifest = pack.GetManifest();
  88. const auto& authors = manifest->GetAuthors();
  89. auto* logo_item = new QTableWidgetItem;
  90. auto* name_item = new QTableWidgetItem(QString::fromStdString(manifest->GetName()));
  91. auto* version_item = new QTableWidgetItem(QString::fromStdString(manifest->GetVersion()));
  92. auto* author_item =
  93. new QTableWidgetItem(authors ? QString::fromStdString(*authors) : tr("Unknown author"));
  94. auto* description_item =
  95. new QTableWidgetItem(QString::fromStdString(manifest->GetDescription().value_or("")));
  96. auto* website_item =
  97. new QTableWidgetItem(QString::fromStdString(manifest->GetWebsite().value_or("")));
  98. QPixmap logo;
  99. logo.loadFromData(reinterpret_cast<const uchar*>(pack.GetLogo().data()),
  100. (int)pack.GetLogo().size());
  101. logo_item->setIcon(QIcon(logo));
  102. QFont link_font = website_item->font();
  103. link_font.setUnderline(true);
  104. website_item->setFont(link_font);
  105. website_item->setForeground(QBrush(Qt::blue));
  106. website_item->setData(Qt::UserRole, website_item->text());
  107. for (auto* item :
  108. {logo_item, name_item, version_item, author_item, description_item, website_item})
  109. {
  110. item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  111. if (ResourcePack::IsInstalled(pack))
  112. {
  113. item->setBackground(QColor(Qt::green));
  114. auto font = item->font();
  115. font.setBold(true);
  116. item->setFont(font);
  117. }
  118. }
  119. m_table_widget->setItem(i, 0, logo_item);
  120. m_table_widget->setItem(i, 1, name_item);
  121. m_table_widget->setItem(i, 2, version_item);
  122. m_table_widget->setItem(i, 3, description_item);
  123. m_table_widget->setItem(i, 4, author_item);
  124. m_table_widget->setItem(i, 5, website_item);
  125. }
  126. SelectionChanged();
  127. }
  128. // Revert the indicies as to be more intuitive for users
  129. int ResourcePackManager::GetResourcePackIndex(QTableWidgetItem* item) const
  130. {
  131. return m_table_widget->rowCount() - 1 - item->row();
  132. }
  133. void ResourcePackManager::Change()
  134. {
  135. auto items = m_table_widget->selectedItems();
  136. if (items.empty())
  137. return;
  138. if (ResourcePack::IsInstalled(ResourcePack::GetPacks()[GetResourcePackIndex(items[0])]))
  139. {
  140. Uninstall();
  141. }
  142. else
  143. {
  144. Install();
  145. }
  146. }
  147. void ResourcePackManager::Install()
  148. {
  149. auto items = m_table_widget->selectedItems();
  150. if (items.empty())
  151. return;
  152. auto& item = ResourcePack::GetPacks()[GetResourcePackIndex(items[0])];
  153. bool success = item.Install(File::GetUserPath(D_LOAD_IDX));
  154. if (!success)
  155. {
  156. ModalMessageBox::critical(
  157. this, tr("Error"),
  158. tr("Failed to install pack: %1").arg(QString::fromStdString(item.GetError())));
  159. }
  160. RepopulateTable();
  161. }
  162. void ResourcePackManager::Uninstall()
  163. {
  164. auto items = m_table_widget->selectedItems();
  165. if (items.empty())
  166. return;
  167. auto& item = ResourcePack::GetPacks()[GetResourcePackIndex(items[0])];
  168. bool success = item.Uninstall(File::GetUserPath(D_LOAD_IDX));
  169. if (!success)
  170. {
  171. ModalMessageBox::critical(
  172. this, tr("Error"),
  173. tr("Failed to uninstall pack: %1").arg(QString::fromStdString(item.GetError())));
  174. }
  175. RepopulateTable();
  176. }
  177. void ResourcePackManager::Remove()
  178. {
  179. auto items = m_table_widget->selectedItems();
  180. if (items.empty())
  181. return;
  182. ModalMessageBox box(this);
  183. box.setWindowTitle(tr("Confirmation"));
  184. box.setText(tr("Are you sure you want to delete this pack?"));
  185. box.setIcon(QMessageBox::Warning);
  186. box.setStandardButtons(QMessageBox::Yes | QMessageBox::Abort);
  187. SetQWidgetWindowDecorations(&box);
  188. if (box.exec() != QMessageBox::Yes)
  189. return;
  190. Uninstall();
  191. File::Delete(ResourcePack::GetPacks()[GetResourcePackIndex(items[0])].GetPath());
  192. RepopulateTable();
  193. }
  194. void ResourcePackManager::PriorityDown()
  195. {
  196. auto items = m_table_widget->selectedItems();
  197. if (items.empty())
  198. return;
  199. auto row = GetResourcePackIndex(items[0]);
  200. if (items[0]->row() >= m_table_widget->rowCount())
  201. return;
  202. auto& pack = ResourcePack::GetPacks()[row];
  203. std::string path = pack.GetPath();
  204. row--;
  205. ResourcePack::Remove(pack);
  206. ResourcePack::Add(path, row);
  207. RepopulateTable();
  208. m_table_widget->selectRow(row == 0 ? m_table_widget->rowCount() - 1 : row);
  209. }
  210. void ResourcePackManager::PriorityUp()
  211. {
  212. auto items = m_table_widget->selectedItems();
  213. if (items.empty())
  214. return;
  215. auto row = GetResourcePackIndex(items[0]);
  216. if (items[0]->row() == 0)
  217. return;
  218. auto& pack = ResourcePack::GetPacks()[row];
  219. std::string path = pack.GetPath();
  220. row++;
  221. ResourcePack::Remove(pack);
  222. ResourcePack::Add(path, items[0]->row() == m_table_widget->rowCount() ? -1 : row);
  223. RepopulateTable();
  224. m_table_widget->selectRow(row == m_table_widget->rowCount() - 1 ? 0 : row);
  225. }
  226. void ResourcePackManager::Refresh()
  227. {
  228. ResourcePack::Init();
  229. RepopulateTable();
  230. }
  231. void ResourcePackManager::SelectionChanged()
  232. {
  233. auto items = m_table_widget->selectedItems();
  234. const bool has_selection = !items.empty();
  235. if (has_selection)
  236. {
  237. m_change_button->setText(
  238. ResourcePack::IsInstalled(ResourcePack::GetPacks()[GetResourcePackIndex(items[0])]) ?
  239. tr("Uninstall") :
  240. tr("Install"));
  241. }
  242. for (auto* item : {m_change_button, m_remove_button})
  243. item->setEnabled(has_selection);
  244. m_priority_down_button->setEnabled(has_selection &&
  245. items[0]->row() < m_table_widget->rowCount() - 1);
  246. m_priority_up_button->setEnabled(has_selection && items[0]->row() != 0);
  247. }
  248. void ResourcePackManager::ItemDoubleClicked(QTableWidgetItem* item)
  249. {
  250. auto item_data = item->data(Qt::UserRole);
  251. if (item_data.isNull())
  252. return;
  253. QDesktopServices::openUrl(QUrl(item_data.toString()));
  254. }