| 1 | // Copyright 2022 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/GraphicsModListWidget.h" |
| 5 | |
| 6 | #include <QCheckBox> |
| 7 | #include <QDesktopServices> |
| 8 | #include <QHBoxLayout> |
| 9 | #include <QLabel> |
| 10 | #include <QListWidget> |
| 11 | #include <QPushButton> |
| 12 | #include <QUrl> |
| 13 | #include <QVBoxLayout> |
| 14 | #include <QWidget> |
| 15 | |
| 16 | #include <set> |
| 17 | |
| 18 | #include "Common/FileUtil.h" |
| 19 | #include "Core/ConfigManager.h" |
| 20 | #include "Core/Core.h" |
| 21 | #include "Core/System.h" |
| 22 | #include "DolphinQt/Config/GraphicsModWarningWidget.h" |
| 23 | #include "DolphinQt/QtUtils/ClearLayoutRecursively.h" |
| 24 | #include "DolphinQt/Settings.h" |
| 25 | #include "UICommon/GameFile.h" |
| 26 | #include "VideoCommon/GraphicsModSystem/Config/GraphicsMod.h" |
| 27 | #include "VideoCommon/VideoConfig.h" |
| 28 | |
| 29 | GraphicsModListWidget::GraphicsModListWidget(const UICommon::GameFile& game) |
| 30 | : m_game_id(game.GetGameID()), m_mod_group(m_game_id) |
| 31 | { |
| 32 | CalculateGameRunning(Core::GetState(Core::System::GetInstance())); |
| 33 | if (m_loaded_game_is_running && g_Config.graphics_mod_config) |
| 34 | { |
| 35 | m_mod_group.SetChangeCount(g_Config.graphics_mod_config->GetChangeCount()); |
| 36 | } |
| 37 | CreateWidgets(); |
| 38 | ConnectWidgets(); |
| 39 | |
| 40 | RefreshModList(); |
| 41 | OnModChanged(std::nullopt); |
| 42 | } |
| 43 | |
| 44 | GraphicsModListWidget::~GraphicsModListWidget() |
| 45 | { |
| 46 | if (m_needs_save) |
| 47 | { |
| 48 | m_mod_group.Save(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void GraphicsModListWidget::CreateWidgets() |
| 53 | { |
| 54 | auto* main_v_layout = new QVBoxLayout(this); |
| 55 | |
| 56 | auto* main_layout = new QHBoxLayout; |
| 57 | |
| 58 | auto* left_v_layout = new QVBoxLayout; |
| 59 | |
| 60 | m_mod_list = new QListWidget; |
| 61 | m_mod_list->setSortingEnabled(false); |
| 62 | m_mod_list->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectItems); |
| 63 | m_mod_list->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection); |
| 64 | m_mod_list->setSelectionRectVisible(true); |
| 65 | m_mod_list->setDragDropMode(QAbstractItemView::InternalMove); |
| 66 | |
| 67 | m_open_directory_button = new QPushButton(tr("Open Directory...")); |
| 68 | m_refresh = new QPushButton(tr("&Refresh List")); |
| 69 | QHBoxLayout* hlayout = new QHBoxLayout; |
| 70 | hlayout->addWidget(m_open_directory_button); |
| 71 | hlayout->addWidget(m_refresh); |
| 72 | |
| 73 | left_v_layout->addWidget(m_mod_list); |
| 74 | left_v_layout->addLayout(hlayout); |
| 75 | |
| 76 | auto* right_v_layout = new QVBoxLayout; |
| 77 | |
| 78 | m_selected_mod_name = new QLabel(); |
| 79 | right_v_layout->addWidget(m_selected_mod_name); |
| 80 | |
| 81 | m_mod_meta_layout = new QVBoxLayout; |
| 82 | right_v_layout->addLayout(m_mod_meta_layout); |
| 83 | right_v_layout->addStretch(); |
| 84 | |
| 85 | main_layout->addLayout(left_v_layout); |
| 86 | main_layout->addLayout(right_v_layout, 1); |
| 87 | |
| 88 | m_warning = new GraphicsModWarningWidget(this); |
| 89 | main_v_layout->addWidget(m_warning); |
| 90 | main_v_layout->addLayout(main_layout); |
| 91 | |
| 92 | setLayout(main_v_layout); |
| 93 | } |
| 94 | |
| 95 | void GraphicsModListWidget::ConnectWidgets() |
| 96 | { |
| 97 | connect(m_warning, &GraphicsModWarningWidget::GraphicsModEnableSettings, this, |
| 98 | &GraphicsModListWidget::OpenGraphicsSettings); |
| 99 | |
| 100 | connect(m_mod_list, &QListWidget::itemSelectionChanged, this, |
| 101 | &GraphicsModListWidget::ModSelectionChanged); |
| 102 | |
| 103 | connect(m_mod_list, &QListWidget::itemChanged, this, &GraphicsModListWidget::ModItemChanged); |
| 104 | |
| 105 | connect(m_mod_list->model(), &QAbstractItemModel::rowsMoved, this, |
| 106 | &GraphicsModListWidget::SaveModList); |
| 107 | |
| 108 | connect(m_open_directory_button, &QPushButton::clicked, this, |
| 109 | &GraphicsModListWidget::OpenGraphicsModDir); |
| 110 | |
| 111 | connect(m_refresh, &QPushButton::clicked, this, &GraphicsModListWidget::RefreshModList); |
| 112 | |
| 113 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, |
| 114 | &GraphicsModListWidget::CalculateGameRunning); |
| 115 | } |
| 116 | |
| 117 | void GraphicsModListWidget::RefreshModList() |
| 118 | { |
| 119 | if (m_needs_save) |
| 120 | { |
| 121 | SaveToDisk(); |
| 122 | } |
| 123 | |
| 124 | m_mod_list->setCurrentItem(nullptr); |
| 125 | m_mod_list->clear(); |
| 126 | |
| 127 | m_mod_group = GraphicsModGroupConfig(m_game_id); |
| 128 | m_mod_group.Load(); |
| 129 | |
| 130 | std::set<std::string> groups; |
| 131 | |
| 132 | for (const GraphicsModConfig& mod : m_mod_group.GetMods()) |
| 133 | { |
| 134 | for (const GraphicsTargetGroupConfig& group : mod.m_groups) |
| 135 | groups.insert(group.m_name); |
| 136 | } |
| 137 | |
| 138 | for (const GraphicsModConfig& mod : m_mod_group.GetMods()) |
| 139 | { |
| 140 | // If no group matches the mod's features, or if the mod has no features, skip it |
| 141 | if (std::ranges::none_of(mod.m_features, [&groups](const GraphicsModFeatureConfig& feature) { |
| 142 | return groups.contains(feature.m_group); |
| 143 | })) |
| 144 | { |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(mod.m_title)); |
| 149 | item->setFlags(item->flags() | Qt::ItemIsUserCheckable); |
| 150 | item->setData(Qt::UserRole, QString::fromStdString(mod.GetAbsolutePath())); |
| 151 | item->setCheckState(mod.m_enabled ? Qt::Checked : Qt::Unchecked); |
| 152 | |
| 153 | m_mod_list->addItem(item); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void GraphicsModListWidget::ModSelectionChanged() |
| 158 | { |
| 159 | if (m_mod_list->currentItem() == nullptr) |
| 160 | return; |
| 161 | if (m_mod_list->count() == 0) |
| 162 | return; |
| 163 | const auto absolute_path = m_mod_list->currentItem()->data(Qt::UserRole).toString().toStdString(); |
| 164 | OnModChanged(absolute_path); |
| 165 | } |
| 166 | |
| 167 | void GraphicsModListWidget::ModItemChanged(QListWidgetItem* item) |
| 168 | { |
| 169 | const auto absolute_path = item->data(Qt::UserRole).toString(); |
| 170 | GraphicsModConfig* mod = m_mod_group.GetMod(absolute_path.toStdString()); |
| 171 | if (!mod) |
| 172 | return; |
| 173 | |
| 174 | const bool was_enabled = mod->m_enabled; |
| 175 | const bool should_enable = item->checkState() == Qt::Checked; |
| 176 | mod->m_enabled = should_enable; |
| 177 | if (was_enabled == should_enable) |
| 178 | return; |
| 179 | |
| 180 | m_mod_group.SetChangeCount(m_mod_group.GetChangeCount() + 1); |
| 181 | if (m_loaded_game_is_running) |
| 182 | { |
| 183 | g_Config.graphics_mod_config = m_mod_group; |
| 184 | } |
| 185 | m_needs_save = true; |
| 186 | } |
| 187 | |
| 188 | void GraphicsModListWidget::OnModChanged(const std::optional<std::string>& absolute_path) |
| 189 | { |
| 190 | ClearLayoutRecursively(m_mod_meta_layout); |
| 191 | |
| 192 | adjustSize(); |
| 193 | |
| 194 | if (!absolute_path) |
| 195 | { |
| 196 | m_selected_mod_name->setText(tr("No graphics mod selected")); |
| 197 | m_selected_mod_name->setAlignment(Qt::AlignCenter); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | const GraphicsModConfig* mod = m_mod_group.GetMod(*absolute_path); |
| 202 | if (!mod) |
| 203 | return; |
| 204 | |
| 205 | m_selected_mod_name->setText(QString::fromStdString(mod->m_title)); |
| 206 | m_selected_mod_name->setAlignment(Qt::AlignLeft); |
| 207 | QFont font = m_selected_mod_name->font(); |
| 208 | font.setWeight(QFont::Bold); |
| 209 | m_selected_mod_name->setFont(font); |
| 210 | |
| 211 | if (!mod->m_author.empty()) |
| 212 | { |
| 213 | auto* author_label = new QLabel(tr("By: %1").arg(QString::fromStdString(mod->m_author))); |
| 214 | m_mod_meta_layout->addWidget(author_label); |
| 215 | } |
| 216 | |
| 217 | if (!mod->m_description.empty()) |
| 218 | { |
| 219 | auto* description_label = |
| 220 | new QLabel(tr("Description: %1").arg(QString::fromStdString(mod->m_description))); |
| 221 | description_label->setWordWrap(true); |
| 222 | m_mod_meta_layout->addWidget(description_label); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void GraphicsModListWidget::SaveModList() |
| 227 | { |
| 228 | for (int i = 0; i < m_mod_list->count(); i++) |
| 229 | { |
| 230 | const auto absolute_path = m_mod_list->model() |
| 231 | ->data(m_mod_list->model()->index(i, 0), Qt::UserRole) |
| 232 | .toString() |
| 233 | .toStdString(); |
| 234 | m_mod_group.GetMod(absolute_path)->m_weight = i; |
| 235 | } |
| 236 | |
| 237 | if (m_loaded_game_is_running) |
| 238 | { |
| 239 | g_Config.graphics_mod_config = m_mod_group; |
| 240 | } |
| 241 | m_needs_save = true; |
| 242 | } |
| 243 | |
| 244 | void GraphicsModListWidget::SaveToDisk() |
| 245 | { |
| 246 | m_needs_save = false; |
| 247 | m_mod_group.Save(); |
| 248 | } |
| 249 | |
| 250 | const GraphicsModGroupConfig& GraphicsModListWidget::GetGraphicsModConfig() const |
| 251 | { |
| 252 | return m_mod_group; |
| 253 | } |
| 254 | |
| 255 | void GraphicsModListWidget::CalculateGameRunning(Core::State state) |
| 256 | { |
| 257 | m_loaded_game_is_running = |
| 258 | state == Core::State::Running ? m_game_id == SConfig::GetInstance().GetGameID() : false; |
| 259 | } |
| 260 | |
| 261 | void GraphicsModListWidget::OpenGraphicsModDir() |
| 262 | { |
| 263 | QDesktopServices::openUrl( |
| 264 | QUrl::fromLocalFile(QString::fromStdString(File::GetUserPath(D_GRAPHICSMOD_IDX)))); |
| 265 | } |