Debian Codegraph @

Source/Core/DolphinQt/Config/PatchesWidget.cpp

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/Config/PatchesWidget.h"
5
6 #include <QGridLayout>
7 #include <QListWidget>
8 #include <QPushButton>
9
10 #include "Common/FileUtil.h"
11 #include "Common/IniFile.h"
12
13 #include "Core/ConfigManager.h"
14 #include "Core/PatchEngine.h"
15
16 #include "DolphinQt/Config/HardcoreWarningWidget.h"
17 #include "DolphinQt/Config/NewPatchDialog.h"
18
19 #include "UICommon/GameFile.h"
20
21 PatchesWidget::PatchesWidget(const UICommon::GameFile& game)
22 : m_game_id(game.GetGameID()), m_game_revision(game.GetRevision())
23 {
24 Common::IniFile game_ini_local;
25 game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini");
26
27 Common::IniFile game_ini_default =
28 SConfig::GetInstance().LoadDefaultGameIni(m_game_id, m_game_revision);
29
30 PatchEngine::LoadPatchSection("OnFrame", &m_patches, game_ini_default, game_ini_local);
31
32 CreateWidgets();
33 ConnectWidgets();
34
35 Update();
36
37 UpdateActions();
38 }
39
40 void PatchesWidget::CreateWidgets()
41 {
42 #ifdef USE_RETRO_ACHIEVEMENTS
43 m_hc_warning = new HardcoreWarningWidget(this);
44 #endif // USE_RETRO_ACHIEVEMENTS
45 m_list = new QListWidget;
46 m_add_button = new QPushButton(tr("&Add..."));
47 m_edit_button = new QPushButton();
48 m_remove_button = new QPushButton(tr("&Remove"));
49
50 auto* grid_layout = new QGridLayout;
51
52 grid_layout->addWidget(m_list, 0, 0, 1, -1);
53 grid_layout->addWidget(m_add_button, 1, 0);
54 grid_layout->addWidget(m_edit_button, 1, 2);
55 grid_layout->addWidget(m_remove_button, 1, 1);
56
57 auto* layout = new QVBoxLayout;
58
59 #ifdef USE_RETRO_ACHIEVEMENTS
60 layout->addWidget(m_hc_warning);
61 #endif // USE_RETRO_ACHIEVEMENTS
62 layout->addLayout(grid_layout);
63
64 setLayout(layout);
65 }
66
67 void PatchesWidget::ConnectWidgets()
68 {
69 #ifdef USE_RETRO_ACHIEVEMENTS
70 connect(m_hc_warning, &HardcoreWarningWidget::OpenAchievementSettings, this,
71 &PatchesWidget::OpenAchievementSettings);
72 #endif // USE_RETRO_ACHIEVEMENTS
73
74 connect(m_list, &QListWidget::itemSelectionChanged, this, &PatchesWidget::UpdateActions);
75 connect(m_list, &QListWidget::itemChanged, this, &PatchesWidget::OnItemChanged);
76 connect(m_remove_button, &QPushButton::clicked, this, &PatchesWidget::OnRemove);
77 connect(m_add_button, &QPushButton::clicked, this, &PatchesWidget::OnAdd);
78 connect(m_edit_button, &QPushButton::clicked, this, &PatchesWidget::OnEdit);
79 }
80
81 void PatchesWidget::OnItemChanged(QListWidgetItem* item)
82 {
83 m_patches[m_list->row(item)].enabled = (item->checkState() == Qt::Checked);
84 SavePatches();
85 }
86
87 void PatchesWidget::OnAdd()
88 {
89 PatchEngine::Patch patch;
90 patch.user_defined = true;
91
92 bool new_patch_confirmed = false;
93 {
94 NewPatchDialog dialog(this, patch);
95 new_patch_confirmed = dialog.exec();
96 }
97 if (new_patch_confirmed)
98 {
99 m_patches.push_back(patch);
100 SavePatches();
101 Update();
102 }
103 }
104
105 void PatchesWidget::OnEdit()
106 {
107 if (m_list->selectedItems().isEmpty())
108 return;
109
110 auto* item = m_list->selectedItems()[0];
111
112 auto patch = m_patches[m_list->row(item)];
113
114 if (!patch.user_defined)
115 {
116 // i18n: If there is a pre-defined patch with the name %1 and the user wants to edit it,
117 // a copy of it gets created with this name
118 patch.name = tr("%1 (Copy)").arg(QString::fromStdString(patch.name)).toStdString();
119 }
120
121 bool new_patch_confirmed = false;
122 {
123 NewPatchDialog dialog(this, patch);
124 new_patch_confirmed = dialog.exec();
125 }
126 if (new_patch_confirmed)
127 {
128 if (patch.user_defined)
129 {
130 m_patches[m_list->row(item)] = patch;
131 }
132 else
133 {
134 patch.user_defined = true;
135 m_patches.push_back(patch);
136 }
137
138 SavePatches();
139 Update();
140 }
141 }
142
143 void PatchesWidget::OnRemove()
144 {
145 if (m_list->selectedItems().isEmpty())
146 return;
147
148 m_patches.erase(m_patches.begin() + m_list->row(m_list->selectedItems()[0]));
149 SavePatches();
150 Update();
151 }
152
153 void PatchesWidget::SavePatches()
154 {
155 const std::string ini_path = File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini";
156
157 Common::IniFile game_ini_local;
158 game_ini_local.Load(ini_path);
159 PatchEngine::SavePatchSection(&game_ini_local, m_patches);
160 game_ini_local.Save(ini_path);
161 }
162
163 void PatchesWidget::Update()
164 {
165 m_list->clear();
166
167 for (const auto& patch : m_patches)
168 {
169 auto* item = new QListWidgetItem(QString::fromStdString(patch.name));
170 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
171 item->setCheckState(patch.enabled ? Qt::Checked : Qt::Unchecked);
172 item->setData(Qt::UserRole, patch.user_defined);
173
174 m_list->addItem(item);
175 }
176 }
177
178 void PatchesWidget::UpdateActions()
179 {
180 bool selected = !m_list->selectedItems().isEmpty();
181
182 auto* item = selected ? m_list->selectedItems()[0] : nullptr;
183
184 bool user_defined = selected ? item->data(Qt::UserRole).toBool() : true;
185
186 m_edit_button->setEnabled(selected);
187 m_edit_button->setText(user_defined ? tr("&Edit...") : tr("&Clone..."));
188 m_remove_button->setEnabled(selected && user_defined);
189 }