| 1 | // Copyright 2015 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Settings/PathPane.h" |
| 5 | |
| 6 | #include <QCheckBox> |
| 7 | #include <QDir> |
| 8 | #include <QGroupBox> |
| 9 | #include <QLabel> |
| 10 | #include <QListWidget> |
| 11 | #include <QPushButton> |
| 12 | #include <QVBoxLayout> |
| 13 | |
| 14 | #include "Common/Config/Config.h" |
| 15 | #include "Common/FileUtil.h" |
| 16 | |
| 17 | #include "Core/Config/MainSettings.h" |
| 18 | #include "Core/Config/UISettings.h" |
| 19 | |
| 20 | #include "DolphinQt/Config/ConfigControls/ConfigBool.h" |
| 21 | #include "DolphinQt/Config/ConfigControls/ConfigText.h" |
| 22 | #include "DolphinQt/Config/ConfigControls/ConfigUserPath.h" |
| 23 | #include "DolphinQt/QtUtils/DolphinFileDialog.h" |
| 24 | #include "DolphinQt/QtUtils/NonDefaultQPushButton.h" |
| 25 | #include "DolphinQt/Settings.h" |
| 26 | |
| 27 | PathPane::PathPane(QWidget* parent) : QWidget(parent) |
| 28 | { |
| 29 | setWindowTitle(tr("Paths")); |
| 30 | |
| 31 | QVBoxLayout* layout = new QVBoxLayout; |
| 32 | layout->addWidget(MakeGameFolderBox()); |
| 33 | layout->addLayout(MakePathsLayout()); |
| 34 | |
| 35 | setLayout(layout); |
| 36 | } |
| 37 | |
| 38 | void PathPane::Browse() |
| 39 | { |
| 40 | QString dir = QDir::toNativeSeparators( |
| 41 | DolphinFileDialog::getExistingDirectory(this, tr("Select a Directory"), QDir::currentPath())); |
| 42 | if (!dir.isEmpty()) |
| 43 | Settings::Instance().AddPath(dir); |
| 44 | } |
| 45 | |
| 46 | void PathPane::BrowseDefaultGame() |
| 47 | { |
| 48 | QString file = QDir::toNativeSeparators(DolphinFileDialog::getOpenFileName( |
| 49 | this, tr("Select a Game"), Settings::Instance().GetDefaultGame(), |
| 50 | QStringLiteral("%1 (*.elf *.dol *.gcm *.bin *.iso *.tgc *.wbfs *.ciso *.gcz *.wia *.rvz " |
| 51 | "hif_000000.nfs *.wad *.m3u *.json);;%2 (*)") |
| 52 | .arg(tr("All GC/Wii files")) |
| 53 | .arg(tr("All Files")))); |
| 54 | |
| 55 | if (!file.isEmpty()) |
| 56 | Settings::Instance().SetDefaultGame(file); |
| 57 | } |
| 58 | |
| 59 | void PathPane::BrowseWiiNAND() |
| 60 | { |
| 61 | QString dir = QDir::toNativeSeparators(DolphinFileDialog::getExistingDirectory( |
| 62 | this, tr("Select Wii NAND Root"), QString::fromStdString(File::GetUserPath(D_WIIROOT_IDX)))); |
| 63 | if (!dir.isEmpty()) |
| 64 | m_nand_edit->SetTextAndUpdate(dir); |
| 65 | } |
| 66 | |
| 67 | void PathPane::BrowseDump() |
| 68 | { |
| 69 | QString dir = QDir::toNativeSeparators(DolphinFileDialog::getExistingDirectory( |
| 70 | this, tr("Select Dump Path"), QString::fromStdString(File::GetUserPath(D_DUMP_IDX)))); |
| 71 | if (!dir.isEmpty()) |
| 72 | m_dump_edit->SetTextAndUpdate(dir); |
| 73 | } |
| 74 | |
| 75 | void PathPane::BrowseLoad() |
| 76 | { |
| 77 | QString dir = QDir::toNativeSeparators(DolphinFileDialog::getExistingDirectory( |
| 78 | this, tr("Select Load Path"), QString::fromStdString(File::GetUserPath(D_LOAD_IDX)))); |
| 79 | if (!dir.isEmpty()) |
| 80 | { |
| 81 | m_load_edit->setText(dir); |
| 82 | Config::SetBase(Config::MAIN_LOAD_PATH, dir.toStdString()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void PathPane::BrowseResourcePack() |
| 87 | { |
| 88 | QString dir = QDir::toNativeSeparators(DolphinFileDialog::getExistingDirectory( |
| 89 | this, tr("Select Resource Pack Path"), |
| 90 | QString::fromStdString(File::GetUserPath(D_RESOURCEPACK_IDX)))); |
| 91 | if (!dir.isEmpty()) |
| 92 | { |
| 93 | m_resource_pack_edit->setText(dir); |
| 94 | Config::SetBase(Config::MAIN_RESOURCEPACK_PATH, dir.toStdString()); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void PathPane::BrowseWFS() |
| 99 | { |
| 100 | const QString dir = QDir::toNativeSeparators(DolphinFileDialog::getExistingDirectory( |
| 101 | this, tr("Select WFS Path"), QString::fromStdString(File::GetUserPath(D_WFSROOT_IDX)))); |
| 102 | if (!dir.isEmpty()) |
| 103 | { |
| 104 | m_wfs_edit->setText(dir); |
| 105 | Config::SetBase(Config::MAIN_WFS_PATH, dir.toStdString()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | QGroupBox* PathPane::MakeGameFolderBox() |
| 110 | { |
| 111 | QGroupBox* game_box = new QGroupBox(tr("Game Folders")); |
| 112 | QVBoxLayout* vlayout = new QVBoxLayout; |
| 113 | |
| 114 | m_path_list = new QListWidget; |
| 115 | m_path_list->insertItems(0, Settings::Instance().GetPaths()); |
| 116 | m_path_list->setSpacing(1); |
| 117 | connect(&Settings::Instance(), &Settings::PathAdded, this, |
| 118 | [this](const QString& dir) { m_path_list->addItem(dir); }); |
| 119 | connect(&Settings::Instance(), &Settings::PathRemoved, this, [this](const QString& dir) { |
| 120 | auto items = m_path_list->findItems(dir, Qt::MatchExactly); |
| 121 | for (auto& item : items) |
| 122 | delete item; |
| 123 | }); |
| 124 | connect(m_path_list, &QListWidget::itemSelectionChanged, this, |
| 125 | [this] { m_remove_path->setEnabled(m_path_list->selectedItems().count()); }); |
| 126 | |
| 127 | vlayout->addWidget(m_path_list); |
| 128 | |
| 129 | QHBoxLayout* hlayout = new QHBoxLayout; |
| 130 | |
| 131 | hlayout->addStretch(); |
| 132 | QPushButton* add = new NonDefaultQPushButton(tr("Add...")); |
| 133 | m_remove_path = new NonDefaultQPushButton(tr("Remove")); |
| 134 | |
| 135 | m_remove_path->setEnabled(false); |
| 136 | |
| 137 | auto* recursive_checkbox = |
| 138 | new ConfigBool(tr("Search Subfolders"), Config::MAIN_RECURSIVE_ISO_PATHS); |
| 139 | |
| 140 | auto* auto_checkbox = new QCheckBox(tr("Check for Game List Changes in the Background")); |
| 141 | auto_checkbox->setChecked(Settings::Instance().IsAutoRefreshEnabled()); |
| 142 | |
| 143 | hlayout->addWidget(add); |
| 144 | hlayout->addWidget(m_remove_path); |
| 145 | vlayout->addLayout(hlayout); |
| 146 | vlayout->addWidget(recursive_checkbox); |
| 147 | vlayout->addWidget(auto_checkbox); |
| 148 | |
| 149 | connect(recursive_checkbox, &QCheckBox::toggled, |
| 150 | [](bool checked) { Settings::Instance().RefreshGameList(); }); |
| 151 | |
| 152 | connect(auto_checkbox, &QCheckBox::toggled, &Settings::Instance(), |
| 153 | &Settings::SetAutoRefreshEnabled); |
| 154 | |
| 155 | connect(add, &QPushButton::clicked, this, &PathPane::Browse); |
| 156 | connect(m_remove_path, &QPushButton::clicked, this, &PathPane::RemovePath); |
| 157 | |
| 158 | game_box->setLayout(vlayout); |
| 159 | return game_box; |
| 160 | } |
| 161 | |
| 162 | QGridLayout* PathPane::MakePathsLayout() |
| 163 | { |
| 164 | QGridLayout* layout = new QGridLayout; |
| 165 | layout->setColumnStretch(1, 1); |
| 166 | |
| 167 | m_game_edit = new ConfigText(Config::MAIN_DEFAULT_ISO); |
| 168 | connect(&Settings::Instance(), &Settings::DefaultGameChanged, this, |
| 169 | [this](const QString& path) { m_game_edit->setText(path); }); |
| 170 | QPushButton* game_open = new NonDefaultQPushButton(QStringLiteral("...")); |
| 171 | connect(game_open, &QPushButton::clicked, this, &PathPane::BrowseDefaultGame); |
| 172 | layout->addWidget(new QLabel(tr("Default ISO:")), 0, 0); |
| 173 | layout->addWidget(m_game_edit, 0, 1); |
| 174 | layout->addWidget(game_open, 0, 2); |
| 175 | |
| 176 | m_nand_edit = new ConfigUserPath(D_WIIROOT_IDX, Config::MAIN_FS_PATH); |
| 177 | QPushButton* nand_open = new NonDefaultQPushButton(QStringLiteral("...")); |
| 178 | connect(nand_open, &QPushButton::clicked, this, &PathPane::BrowseWiiNAND); |
| 179 | layout->addWidget(new QLabel(tr("Wii NAND Root:")), 1, 0); |
| 180 | layout->addWidget(m_nand_edit, 1, 1); |
| 181 | layout->addWidget(nand_open, 1, 2); |
| 182 | |
| 183 | m_dump_edit = new ConfigUserPath(D_DUMP_IDX, Config::MAIN_DUMP_PATH); |
| 184 | QPushButton* dump_open = new NonDefaultQPushButton(QStringLiteral("...")); |
| 185 | connect(dump_open, &QPushButton::clicked, this, &PathPane::BrowseDump); |
| 186 | layout->addWidget(new QLabel(tr("Dump Path:")), 2, 0); |
| 187 | layout->addWidget(m_dump_edit, 2, 1); |
| 188 | layout->addWidget(dump_open, 2, 2); |
| 189 | |
| 190 | m_load_edit = new ConfigUserPath(D_LOAD_IDX, Config::MAIN_LOAD_PATH); |
| 191 | QPushButton* load_open = new NonDefaultQPushButton(QStringLiteral("...")); |
| 192 | connect(load_open, &QPushButton::clicked, this, &PathPane::BrowseLoad); |
| 193 | layout->addWidget(new QLabel(tr("Load Path:")), 3, 0); |
| 194 | layout->addWidget(m_load_edit, 3, 1); |
| 195 | layout->addWidget(load_open, 3, 2); |
| 196 | |
| 197 | m_resource_pack_edit = new ConfigUserPath(D_RESOURCEPACK_IDX, Config::MAIN_RESOURCEPACK_PATH); |
| 198 | QPushButton* resource_pack_open = new NonDefaultQPushButton(QStringLiteral("...")); |
| 199 | connect(resource_pack_open, &QPushButton::clicked, this, &PathPane::BrowseResourcePack); |
| 200 | layout->addWidget(new QLabel(tr("Resource Pack Path:")), 4, 0); |
| 201 | layout->addWidget(m_resource_pack_edit, 4, 1); |
| 202 | layout->addWidget(resource_pack_open, 4, 2); |
| 203 | |
| 204 | m_wfs_edit = new ConfigUserPath(D_WFSROOT_IDX, Config::MAIN_WFS_PATH); |
| 205 | QPushButton* wfs_open = new NonDefaultQPushButton(QStringLiteral("...")); |
| 206 | connect(wfs_open, &QPushButton::clicked, this, &PathPane::BrowseWFS); |
| 207 | layout->addWidget(new QLabel(tr("WFS Path:")), 5, 0); |
| 208 | layout->addWidget(m_wfs_edit, 5, 1); |
| 209 | layout->addWidget(wfs_open, 5, 2); |
| 210 | |
| 211 | return layout; |
| 212 | } |
| 213 | |
| 214 | void PathPane::RemovePath() |
| 215 | { |
| 216 | auto item = m_path_list->currentItem(); |
| 217 | if (!item) |
| 218 | return; |
| 219 | Settings::Instance().RemovePath(item->text()); |
| 220 | } |