| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/GameConfigEdit.h" |
| 5 | |
| 6 | #include <QAbstractItemView> |
| 7 | #include <QCompleter> |
| 8 | #include <QDesktopServices> |
| 9 | #include <QFile> |
| 10 | #include <QHBoxLayout> |
| 11 | #include <QKeyEvent> |
| 12 | #include <QPushButton> |
| 13 | #include <QRegularExpression> |
| 14 | #include <QScrollBar> |
| 15 | #include <QStringListModel> |
| 16 | #include <QTextCursor> |
| 17 | #include <QTextEdit> |
| 18 | #include <QVBoxLayout> |
| 19 | #include <QWhatsThis> |
| 20 | |
| 21 | #include "DolphinQt/Config/GameConfigHighlighter.h" |
| 22 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 23 | |
| 24 | GameConfigEdit::GameConfigEdit(QWidget* parent, QString path, bool read_only) |
| 25 | : QWidget{parent}, m_path(std::move(path)), m_read_only(read_only) |
| 26 | { |
| 27 | CreateWidgets(); |
| 28 | |
| 29 | LoadFile(); |
| 30 | |
| 31 | new GameConfigHighlighter(m_edit->document()); |
| 32 | |
| 33 | AddDescription(QStringLiteral("Core"), |
| 34 | tr("Section that contains most CPU and Hardware related settings.")); |
| 35 | |
| 36 | AddDescription(QStringLiteral("CPUThread"), tr("Controls whether or not Dual Core should be " |
| 37 | "enabled. Can improve performance but can also " |
| 38 | "cause issues. Defaults to <b>True</b>")); |
| 39 | |
| 40 | AddDescription(QStringLiteral("FastDiscSpeed"), |
| 41 | tr("Emulate the disc speed of real hardware. Disabling can cause instability. " |
| 42 | "Defaults to <b>True</b>")); |
| 43 | |
| 44 | AddDescription(QStringLiteral("MMU"), tr("Controls whether or not the Memory Management Unit " |
| 45 | "should be emulated fully. Few games require it.")); |
| 46 | |
| 47 | AddDescription( |
| 48 | QStringLiteral("DSPHLE"), |
| 49 | tr("Controls whether to use high or low-level DSP emulation. Defaults to <b>True</b>")); |
| 50 | |
| 51 | AddDescription( |
| 52 | QStringLiteral("JITFollowBranch"), |
| 53 | tr("Tries to translate branches ahead of time, improving performance in most cases. Defaults " |
| 54 | "to <b>True</b>")); |
| 55 | |
| 56 | AddDescription(QStringLiteral("Gecko"), tr("Section that contains all Gecko cheat codes.")); |
| 57 | |
| 58 | AddDescription(QStringLiteral("ActionReplay"), |
| 59 | tr("Section that contains all Action Replay cheat codes.")); |
| 60 | |
| 61 | AddDescription(QStringLiteral("Video_Settings"), |
| 62 | tr("Section that contains all graphics related settings.")); |
| 63 | |
| 64 | m_completer = new QCompleter(m_edit); |
| 65 | |
| 66 | auto* completion_model = new QStringListModel(m_completer); |
| 67 | completion_model->setStringList(m_completions); |
| 68 | |
| 69 | m_completer->setModel(completion_model); |
| 70 | m_completer->setModelSorting(QCompleter::UnsortedModel); |
| 71 | m_completer->setCompletionMode(QCompleter::PopupCompletion); |
| 72 | m_completer->setWidget(m_edit); |
| 73 | |
| 74 | ConnectWidgets(); |
| 75 | } |
| 76 | |
| 77 | void GameConfigEdit::CreateWidgets() |
| 78 | { |
| 79 | m_edit = new QTextEdit; |
| 80 | m_edit->setReadOnly(m_read_only); |
| 81 | m_edit->setAcceptRichText(false); |
| 82 | |
| 83 | m_refresh_button = new QPushButton(tr("Refresh")); |
| 84 | m_external_editor_button = new QPushButton(tr("Open in External Editor")); |
| 85 | |
| 86 | if (m_read_only) |
| 87 | { |
| 88 | m_refresh_button->hide(); |
| 89 | m_external_editor_button->hide(); |
| 90 | } |
| 91 | |
| 92 | auto* button_layout = new QHBoxLayout; |
| 93 | button_layout->addWidget(m_refresh_button); |
| 94 | button_layout->addWidget(m_external_editor_button); |
| 95 | |
| 96 | auto* layout = new QVBoxLayout; |
| 97 | layout->addLayout(button_layout); |
| 98 | layout->addWidget(m_edit); |
| 99 | |
| 100 | setLayout(layout); |
| 101 | } |
| 102 | |
| 103 | void GameConfigEdit::AddDescription(const QString& keyword, const QString& description) |
| 104 | { |
| 105 | m_keyword_map[keyword] = description; |
| 106 | m_completions << keyword; |
| 107 | } |
| 108 | |
| 109 | void GameConfigEdit::LoadFile() |
| 110 | { |
| 111 | QFile file(m_path); |
| 112 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 113 | return; |
| 114 | |
| 115 | m_edit->setPlainText(QString::fromStdString(file.readAll().toStdString())); |
| 116 | } |
| 117 | |
| 118 | void GameConfigEdit::SaveFile() |
| 119 | { |
| 120 | if (!isVisible() || m_read_only) |
| 121 | return; |
| 122 | |
| 123 | QFile file(m_path); |
| 124 | |
| 125 | if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) |
| 126 | { |
| 127 | ModalMessageBox::warning(this, tr("Warning"), tr("Failed to open config file!")); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | const QByteArray contents = m_edit->toPlainText().toUtf8(); |
| 132 | |
| 133 | if (file.write(contents) == -1) |
| 134 | ModalMessageBox::warning(this, tr("Warning"), tr("Failed to write config file!")); |
| 135 | } |
| 136 | |
| 137 | void GameConfigEdit::ConnectWidgets() |
| 138 | { |
| 139 | connect(m_edit, &QTextEdit::textChanged, this, &GameConfigEdit::SaveFile); |
| 140 | connect(m_edit, &QTextEdit::selectionChanged, this, &GameConfigEdit::OnSelectionChanged); |
| 141 | connect(m_completer, qOverload<const QString&>(&QCompleter::activated), this, |
| 142 | &GameConfigEdit::OnAutoComplete); |
| 143 | |
| 144 | connect(m_refresh_button, &QPushButton::clicked, this, &GameConfigEdit::LoadFile); |
| 145 | connect(m_external_editor_button, &QPushButton::clicked, this, |
| 146 | &GameConfigEdit::OpenExternalEditor); |
| 147 | } |
| 148 | |
| 149 | void GameConfigEdit::OnSelectionChanged() |
| 150 | { |
| 151 | const QString& keyword = m_edit->textCursor().selectedText(); |
| 152 | |
| 153 | if (m_keyword_map.contains(keyword)) |
| 154 | QWhatsThis::showText(QCursor::pos(), m_keyword_map[keyword], this); |
| 155 | } |
| 156 | |
| 157 | QString GameConfigEdit::GetTextUnderCursor() |
| 158 | { |
| 159 | QTextCursor tc = m_edit->textCursor(); |
| 160 | tc.select(QTextCursor::WordUnderCursor); |
| 161 | return tc.selectedText(); |
| 162 | } |
| 163 | |
| 164 | void GameConfigEdit::OnAutoComplete(const QString& completion) |
| 165 | { |
| 166 | QTextCursor cursor = m_edit->textCursor(); |
| 167 | int extra = completion.length() - m_completer->completionPrefix().length(); |
| 168 | cursor.movePosition(QTextCursor::Left); |
| 169 | cursor.movePosition(QTextCursor::EndOfWord); |
| 170 | cursor.insertText(completion.right(extra)); |
| 171 | m_edit->setTextCursor(cursor); |
| 172 | } |
| 173 | |
| 174 | void GameConfigEdit::OpenExternalEditor() |
| 175 | { |
| 176 | QFile file(m_path); |
| 177 | |
| 178 | if (!file.exists()) |
| 179 | { |
| 180 | if (m_read_only) |
| 181 | return; |
| 182 | |
| 183 | if (!file.open(QIODevice::WriteOnly)) |
| 184 | { |
| 185 | ModalMessageBox::warning(this, tr("Error"), |
| 186 | tr("Failed to create the configuration file:\n%1").arg(m_path)); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | file.close(); |
| 191 | } |
| 192 | |
| 193 | if (!QDesktopServices::openUrl(QUrl::fromLocalFile(m_path))) |
| 194 | { |
| 195 | ModalMessageBox::warning(this, tr("Error"), |
| 196 | tr("Failed to open file in external editor.\nMake sure there's an " |
| 197 | "application assigned to open INI files.")); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void GameConfigEdit::keyPressEvent(QKeyEvent* e) |
| 202 | { |
| 203 | if (m_completer->popup()->isVisible()) |
| 204 | { |
| 205 | // The following keys are forwarded by the completer to the widget |
| 206 | switch (e->key()) |
| 207 | { |
| 208 | case Qt::Key_Enter: |
| 209 | case Qt::Key_Return: |
| 210 | case Qt::Key_Escape: |
| 211 | case Qt::Key_Tab: |
| 212 | case Qt::Key_Backtab: |
| 213 | e->ignore(); |
| 214 | return; // let the completer do default behavior |
| 215 | default: |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | QWidget::keyPressEvent(e); |
| 221 | |
| 222 | const static QString end_of_word = QStringLiteral("~!@#$%^&*()_+{}|:\"<>?,./;'\\-="); |
| 223 | |
| 224 | QString completion_prefix = GetTextUnderCursor(); |
| 225 | |
| 226 | if (e->text().isEmpty() || completion_prefix.length() < 2 || |
| 227 | end_of_word.contains(e->text().right(1))) |
| 228 | { |
| 229 | m_completer->popup()->hide(); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | if (completion_prefix != m_completer->completionPrefix()) |
| 234 | { |
| 235 | m_completer->setCompletionPrefix(completion_prefix); |
| 236 | m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0)); |
| 237 | } |
| 238 | QRect cr = m_edit->cursorRect(); |
| 239 | cr.setWidth(m_completer->popup()->sizeHintForColumn(0) + |
| 240 | m_completer->popup()->verticalScrollBar()->sizeHint().width()); |
| 241 | m_completer->complete(cr); // popup it up! |
| 242 | } |
| 243 | |
| 244 | void GameConfigEdit::focusInEvent(QFocusEvent* e) |
| 245 | { |
| 246 | m_completer->setWidget(m_edit); |
| 247 | QWidget::focusInEvent(e); |
| 248 | } |