game_list.cpp 5.6 KB

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