Qt: Normalize line endings in cheat edit dialog

This commit is contained in:
Stenzek
2025-10-12 12:15:42 +10:00
parent f8c720a5ef
commit 08556f3143
4 changed files with 20 additions and 3 deletions

View File

@@ -32,6 +32,9 @@
<property name="text">
<string>Description:</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignTop</set>
</property>
</widget>
</item>
<item row="1" column="1">

View File

@@ -358,7 +358,8 @@ void GameCheatSettingsWidget::onCheatListContextMenuRequested(const QPoint& pos)
connect(remove, &QAction::triggered, this, [this, &selected_code]() { removeCode(selected_code, true); });
context_menu.addSeparator();
QAction* disable_all = context_menu.addAction(QIcon::fromTheme(QStringLiteral("chat-off-line")), tr("Disable All Cheats"));
QAction* disable_all =
context_menu.addAction(QIcon::fromTheme(QStringLiteral("chat-off-line")), tr("Disable All Cheats"));
connect(disable_all, &QAction::triggered, this, &GameCheatSettingsWidget::disableAllCheats);
QAction* reload = context_menu.addAction(QIcon::fromTheme(QStringLiteral("refresh-line")), tr("Reload Cheats"));
@@ -856,7 +857,7 @@ void CheatCodeEditorDialog::saveClicked()
return;
}
std::string new_body = m_ui.instructions->toPlainText().toStdString();
std::string new_body = QtUtils::NormalizeLineEndings(m_ui.instructions->toPlainText()).trimmed().toStdString();
if (new_body.empty())
{
QMessageBox::critical(this, tr("Error"), tr("Instructions cannot be empty."));
@@ -889,7 +890,10 @@ void CheatCodeEditorDialog::saveClicked()
}
m_code->name = std::move(new_name);
m_code->description = m_ui.description->toPlainText().replace('\n', ' ').toStdString();
m_code->description = QtUtils::NormalizeLineEndings(m_ui.description->toPlainText())
.replace(QChar('\n'), QChar(' '))
.trimmed()
.toStdString();
m_code->type = static_cast<Cheats::CodeType>(m_ui.type->currentIndex());
m_code->activation = static_cast<Cheats::CodeActivation>(m_ui.activation->currentIndex());
m_code->body = std::move(new_body);

View File

@@ -177,6 +177,13 @@ QString QtUtils::StringViewToQString(std::string_view str)
return str.empty() ? QString() : QString::fromUtf8(str.data(), str.size());
}
QString QtUtils::NormalizeLineEndings(QString str)
{
str.replace(QStringLiteral("\r\n"), QStringLiteral("\n"));
str.replace(QChar('\r'), QChar('\n'));
return str;
}
void QtUtils::SetWidgetFontForInheritedSetting(QWidget* widget, bool inherited)
{
if (widget->font().italic() != inherited)

View File

@@ -91,6 +91,9 @@ std::optional<unsigned> PromptForAddress(QWidget* parent, const QString& title,
/// Converts a std::string_view to a QString safely.
QString StringViewToQString(std::string_view str);
/// Ensures line endings are normalized in \n format.
QString NormalizeLineEndings(QString str);
/// Sets a widget to italics if the setting value is inherited.
void SetWidgetFontForInheritedSetting(QWidget* widget, bool inherited);