game_list.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QHeaderView>
  5. #include <QThreadPool>
  6. #include <QVBoxLayout>
  7. #include "common/common_paths.h"
  8. #include "common/logging/log.h"
  9. #include "common/string_util.h"
  10. #include "core/loader/loader.h"
  11. #include "game_list.h"
  12. #include "game_list_p.h"
  13. #include "ui_settings.h"
  14. GameList::GameList(QWidget* parent) : QWidget{parent} {
  15. QVBoxLayout* layout = new QVBoxLayout;
  16. tree_view = new QTreeView;
  17. item_model = new QStandardItemModel(tree_view);
  18. tree_view->setModel(item_model);
  19. tree_view->setAlternatingRowColors(true);
  20. tree_view->setSelectionMode(QHeaderView::SingleSelection);
  21. tree_view->setSelectionBehavior(QHeaderView::SelectRows);
  22. tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
  23. tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
  24. tree_view->setSortingEnabled(true);
  25. tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
  26. tree_view->setUniformRowHeights(true);
  27. item_model->insertColumns(0, COLUMN_COUNT);
  28. item_model->setHeaderData(COLUMN_NAME, Qt::Horizontal, "Name");
  29. item_model->setHeaderData(COLUMN_FILE_TYPE, Qt::Horizontal, "File type");
  30. item_model->setHeaderData(COLUMN_SIZE, Qt::Horizontal, "Size");
  31. connect(tree_view, &QTreeView::activated, this, &GameList::ValidateEntry);
  32. // We must register all custom types with the Qt Automoc system so that we are able to use it
  33. // with
  34. // signals/slots. In this case, QList falls under the umbrells of custom types.
  35. qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
  36. layout->addWidget(tree_view);
  37. setLayout(layout);
  38. }
  39. GameList::~GameList() {
  40. emit ShouldCancelWorker();
  41. }
  42. void GameList::AddEntry(const QList<QStandardItem*>& entry_items) {
  43. item_model->invisibleRootItem()->appendRow(entry_items);
  44. }
  45. void GameList::ValidateEntry(const QModelIndex& item) {
  46. // We don't care about the individual QStandardItem that was selected, but its row.
  47. int row = item_model->itemFromIndex(item)->row();
  48. QStandardItem* child_file = item_model->invisibleRootItem()->child(row, COLUMN_NAME);
  49. QString file_path = child_file->data(GameListItemPath::FullPathRole).toString();
  50. if (file_path.isEmpty())
  51. return;
  52. std::string std_file_path(file_path.toStdString());
  53. if (!FileUtil::Exists(std_file_path) || FileUtil::IsDirectory(std_file_path))
  54. return;
  55. emit GameChosen(file_path);
  56. }
  57. void GameList::DonePopulating() {
  58. tree_view->setEnabled(true);
  59. }
  60. void GameList::PopulateAsync(const QString& dir_path, bool deep_scan) {
  61. if (!FileUtil::Exists(dir_path.toStdString()) ||
  62. !FileUtil::IsDirectory(dir_path.toStdString())) {
  63. LOG_ERROR(Frontend, "Could not find game list folder at %s", dir_path.toLocal8Bit().data());
  64. return;
  65. }
  66. tree_view->setEnabled(false);
  67. // Delete any rows that might already exist if we're repopulating
  68. item_model->removeRows(0, item_model->rowCount());
  69. emit ShouldCancelWorker();
  70. GameListWorker* worker = new GameListWorker(dir_path, deep_scan);
  71. connect(worker, &GameListWorker::EntryReady, this, &GameList::AddEntry, Qt::QueuedConnection);
  72. connect(worker, &GameListWorker::Finished, this, &GameList::DonePopulating,
  73. Qt::QueuedConnection);
  74. // Use DirectConnection here because worker->Cancel() is thread-safe and we want it to cancel
  75. // without delay.
  76. connect(this, &GameList::ShouldCancelWorker, worker, &GameListWorker::Cancel,
  77. Qt::DirectConnection);
  78. QThreadPool::globalInstance()->start(worker);
  79. current_worker = std::move(worker);
  80. }
  81. void GameList::SaveInterfaceLayout() {
  82. UISettings::values.gamelist_header_state = tree_view->header()->saveState();
  83. }
  84. void GameList::LoadInterfaceLayout() {
  85. auto header = tree_view->header();
  86. if (!header->restoreState(UISettings::values.gamelist_header_state)) {
  87. // We are using the name column to display icons and titles
  88. // so make it as large as possible as default.
  89. header->resizeSection(COLUMN_NAME, header->width());
  90. }
  91. item_model->sort(header->sortIndicatorSection(), header->sortIndicatorOrder());
  92. }
  93. void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, unsigned int recursion) {
  94. const auto callback = [this, recursion](unsigned* num_entries_out, const std::string& directory,
  95. const std::string& virtual_name) -> bool {
  96. std::string physical_name = directory + DIR_SEP + virtual_name;
  97. if (stop_processing)
  98. return false; // Breaks the callback loop.
  99. if (!FileUtil::IsDirectory(physical_name)) {
  100. std::unique_ptr<Loader::AppLoader> loader = Loader::GetLoader(physical_name);
  101. if (!loader)
  102. return true;
  103. std::vector<u8> smdh;
  104. loader->ReadIcon(smdh);
  105. emit EntryReady({
  106. new GameListItemPath(QString::fromStdString(physical_name), smdh),
  107. new GameListItem(
  108. QString::fromStdString(Loader::GetFileTypeString(loader->GetFileType()))),
  109. new GameListItemSize(FileUtil::GetSize(physical_name)),
  110. });
  111. } else if (recursion > 0) {
  112. AddFstEntriesToGameList(physical_name, recursion - 1);
  113. }
  114. return true;
  115. };
  116. FileUtil::ForeachDirectoryEntry(nullptr, dir_path, callback);
  117. }
  118. void GameListWorker::run() {
  119. stop_processing = false;
  120. AddFstEntriesToGameList(dir_path.toStdString(), deep_scan ? 256 : 0);
  121. emit Finished();
  122. }
  123. void GameListWorker::Cancel() {
  124. disconnect(this, nullptr, nullptr, nullptr);
  125. stop_processing = true;
  126. }