summaryrefslogtreecommitdiff
path: root/src/citra_qt/game_list.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2015-10-01 23:35:19 -0400
committerGravatar bunnei2015-10-01 23:35:19 -0400
commit11a64acf236851f88a023bcfa1eecb02991bdccc (patch)
tree814484ca15050312eec965740d5f6d2b6c814a80 /src/citra_qt/game_list.cpp
parentMerge pull request #1180 from lioncash/symbol (diff)
parentGame list: save and load column sizes, sort order, to QSettings (diff)
downloadyuzu-11a64acf236851f88a023bcfa1eecb02991bdccc.tar.gz
yuzu-11a64acf236851f88a023bcfa1eecb02991bdccc.tar.xz
yuzu-11a64acf236851f88a023bcfa1eecb02991bdccc.zip
Merge pull request #1095 from archshift/game-list
Initial implementation of a game list
Diffstat (limited to 'src/citra_qt/game_list.cpp')
-rw-r--r--src/citra_qt/game_list.cpp171
1 files changed, 171 insertions, 0 deletions
diff --git a/src/citra_qt/game_list.cpp b/src/citra_qt/game_list.cpp
new file mode 100644
index 000000000..dade3c212
--- /dev/null
+++ b/src/citra_qt/game_list.cpp
@@ -0,0 +1,171 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <QHeaderView>
6#include <QThreadPool>
7#include <QVBoxLayout>
8
9#include "game_list.h"
10#include "game_list_p.h"
11
12#include "core/loader/loader.h"
13
14#include "common/common_paths.h"
15#include "common/logging/log.h"
16#include "common/string_util.h"
17
18GameList::GameList(QWidget* parent)
19{
20 QVBoxLayout* layout = new QVBoxLayout;
21
22 tree_view = new QTreeView;
23 item_model = new QStandardItemModel(tree_view);
24 tree_view->setModel(item_model);
25
26 tree_view->setAlternatingRowColors(true);
27 tree_view->setSelectionMode(QHeaderView::SingleSelection);
28 tree_view->setSelectionBehavior(QHeaderView::SelectRows);
29 tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
30 tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
31 tree_view->setSortingEnabled(true);
32 tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
33 tree_view->setUniformRowHeights(true);
34
35 item_model->insertColumns(0, COLUMN_COUNT);
36 item_model->setHeaderData(COLUMN_FILE_TYPE, Qt::Horizontal, "File type");
37 item_model->setHeaderData(COLUMN_NAME, Qt::Horizontal, "Name");
38 item_model->setHeaderData(COLUMN_SIZE, Qt::Horizontal, "Size");
39
40 connect(tree_view, SIGNAL(activated(const QModelIndex&)), this, SLOT(ValidateEntry(const QModelIndex&)));
41
42 // We must register all custom types with the Qt Automoc system so that we are able to use it with
43 // signals/slots. In this case, QList falls under the umbrells of custom types.
44 qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
45
46 layout->addWidget(tree_view);
47 setLayout(layout);
48}
49
50GameList::~GameList()
51{
52 emit ShouldCancelWorker();
53}
54
55void GameList::AddEntry(QList<QStandardItem*> entry_items)
56{
57 item_model->invisibleRootItem()->appendRow(entry_items);
58}
59
60void GameList::ValidateEntry(const QModelIndex& item)
61{
62 // We don't care about the individual QStandardItem that was selected, but its row.
63 int row = item_model->itemFromIndex(item)->row();
64 QStandardItem* child_file = item_model->invisibleRootItem()->child(row, COLUMN_NAME);
65 QString file_path = child_file->data(GameListItemPath::FullPathRole).toString();
66
67 if (file_path.isEmpty())
68 return;
69 std::string std_file_path = file_path.toStdString();
70 if (!FileUtil::Exists(std_file_path) || FileUtil::IsDirectory(std_file_path))
71 return;
72 emit GameChosen(file_path);
73}
74
75void GameList::DonePopulating()
76{
77 tree_view->setEnabled(true);
78}
79
80void GameList::PopulateAsync(const QString& dir_path, bool deep_scan)
81{
82 if (!FileUtil::Exists(dir_path.toStdString()) || !FileUtil::IsDirectory(dir_path.toStdString())) {
83 LOG_ERROR(Frontend, "Could not find game list folder at %s", dir_path.toLatin1().data());
84 return;
85 }
86
87 tree_view->setEnabled(false);
88 // Delete any rows that might already exist if we're repopulating
89 item_model->removeRows(0, item_model->rowCount());
90
91 emit ShouldCancelWorker();
92 GameListWorker* worker = new GameListWorker(dir_path, deep_scan);
93
94 connect(worker, SIGNAL(EntryReady(QList<QStandardItem*>)), this, SLOT(AddEntry(QList<QStandardItem*>)), Qt::QueuedConnection);
95 connect(worker, SIGNAL(Finished()), this, SLOT(DonePopulating()), Qt::QueuedConnection);
96 // Use DirectConnection here because worker->Cancel() is thread-safe and we want it to cancel without delay.
97 connect(this, SIGNAL(ShouldCancelWorker()), worker, SLOT(Cancel()), Qt::DirectConnection);
98
99 QThreadPool::globalInstance()->start(worker);
100 current_worker = std::move(worker);
101}
102
103void GameList::SaveInterfaceLayout(QSettings& settings)
104{
105 settings.beginGroup("UILayout");
106 settings.setValue("gameListHeaderState", tree_view->header()->saveState());
107 settings.endGroup();
108}
109
110void GameList::LoadInterfaceLayout(QSettings& settings)
111{
112 auto header = tree_view->header();
113 settings.beginGroup("UILayout");
114 header->restoreState(settings.value("gameListHeaderState").toByteArray());
115 settings.endGroup();
116
117 item_model->sort(header->sortIndicatorSection(), header->sortIndicatorOrder());
118}
119
120void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, bool deep_scan)
121{
122 const auto callback = [&](const std::string& directory,
123 const std::string& virtual_name) -> int {
124
125 std::string physical_name = directory + DIR_SEP + virtual_name;
126
127 if (stop_processing)
128 return -1; // A negative return value breaks the callback loop.
129
130 if (deep_scan && FileUtil::IsDirectory(physical_name)) {
131 AddFstEntriesToGameList(physical_name, true);
132 } else {
133 std::string filename_filename, filename_extension;
134 Common::SplitPath(physical_name, nullptr, &filename_filename, &filename_extension);
135
136 Loader::FileType guessed_filetype = Loader::GuessFromExtension(filename_extension);
137 if (guessed_filetype == Loader::FileType::Unknown)
138 return 0;
139 Loader::FileType filetype = Loader::IdentifyFile(physical_name);
140 if (filetype == Loader::FileType::Unknown) {
141 LOG_WARNING(Frontend, "File %s is of indeterminate type and is possibly corrupted.", physical_name.c_str());
142 return 0;
143 }
144 if (guessed_filetype != filetype) {
145 LOG_WARNING(Frontend, "Filetype and extension of file %s do not match.", physical_name.c_str());
146 }
147
148 emit EntryReady({
149 new GameListItem(QString::fromStdString(Loader::GetFileTypeString(filetype))),
150 new GameListItemPath(QString::fromStdString(physical_name)),
151 new GameListItemSize(FileUtil::GetSize(physical_name)),
152 });
153 }
154
155 return 0; // We don't care about the found entries
156 };
157 FileUtil::ScanDirectoryTreeAndCallback(dir_path, callback);
158}
159
160void GameListWorker::run()
161{
162 stop_processing = false;
163 AddFstEntriesToGameList(dir_path.toStdString(), deep_scan);
164 emit Finished();
165}
166
167void GameListWorker::Cancel()
168{
169 disconnect(this, 0, 0, 0);
170 stop_processing = true;
171}