From ddfb8fa40411b62d281af91c35c734a187877ca1 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Sun, 8 Nov 2020 01:10:26 +0100 Subject: [PATCH] Qt/IOWindow: Apply expressions immediately so we can query the current value of the expression. --- .../DolphinQt/Config/Mapping/IOWindow.cpp | 49 +++++++++++++------ .../Core/DolphinQt/Config/Mapping/IOWindow.h | 11 +++-- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/Source/Core/DolphinQt/Config/Mapping/IOWindow.cpp b/Source/Core/DolphinQt/Config/Mapping/IOWindow.cpp index 1648175360..2f506ae7aa 100644 --- a/Source/Core/DolphinQt/Config/Mapping/IOWindow.cpp +++ b/Source/Core/DolphinQt/Config/Mapping/IOWindow.cpp @@ -93,9 +93,8 @@ QTextCharFormat GetCommentCharFormat() } } // namespace -ControlExpressionSyntaxHighlighter::ControlExpressionSyntaxHighlighter(QTextDocument* parent, - QLineEdit* result) - : QSyntaxHighlighter(parent), m_result_text(result) +ControlExpressionSyntaxHighlighter::ControlExpressionSyntaxHighlighter(QTextDocument* parent) + : QSyntaxHighlighter(parent) { } @@ -168,18 +167,11 @@ void ControlExpressionSyntaxHighlighter::highlightBlock(const QString&) } // This doesn't need to be run for every "block", but it works. - if (ciface::ExpressionParser::ParseStatus::Successful != tokenize_status) - { - m_result_text->setText(tr("Invalid Token.")); - } - else + if (ciface::ExpressionParser::ParseStatus::Successful == tokenize_status) { ciface::ExpressionParser::RemoveInertTokens(&tokens); const auto parse_status = ciface::ExpressionParser::ParseTokens(tokens); - m_result_text->setText( - QString::fromStdString(parse_status.description.value_or(_trans("Success.")))); - if (ciface::ExpressionParser::ParseStatus::Successful != parse_status.status) { const auto token = *parse_status.token; @@ -203,7 +195,8 @@ private: IOWindow::IOWindow(MappingWidget* parent, ControllerEmu::EmulatedController* controller, ControlReference* ref, IOWindow::Type type) - : QDialog(parent), m_reference(ref), m_controller(controller), m_type(type) + : QDialog(parent), m_reference(ref), m_original_expression(ref->GetExpression()), + m_controller(controller), m_type(type) { CreateMainLayout(); @@ -242,7 +235,7 @@ void IOWindow::CreateMainLayout() m_expression_text = new QPlainTextEdit(); m_expression_text->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); - new ControlExpressionSyntaxHighlighter(m_expression_text->document(), m_parse_text); + new ControlExpressionSyntaxHighlighter(m_expression_text->document()); m_operators_combo = new QComboBox(); m_operators_combo->addItem(tr("Operators")); @@ -404,6 +397,7 @@ void IOWindow::ConnectWidgets() connect(m_expression_text, &QPlainTextEdit::textChanged, [this] { m_apply_button->setText(m_apply_button->text().remove(QStringLiteral("*"))); m_apply_button->setText(m_apply_button->text() + QStringLiteral("*")); + UpdateExpression(m_expression_text->toPlainText().toStdString()); }); connect(m_operators_combo, qOverload(&QComboBox::activated), [this](int index) { @@ -423,6 +417,9 @@ void IOWindow::ConnectWidgets() m_functions_combo->setCurrentIndex(0); }); + + // revert the expression when the window closes without using the OK button + connect(this, &IOWindow::finished, [this] { UpdateExpression(m_original_expression); }); } void IOWindow::AppendSelectedOption() @@ -448,8 +445,10 @@ void IOWindow::OnDialogButtonPressed(QAbstractButton* button) return; } - m_reference->SetExpression(m_expression_text->toPlainText().toStdString()); - m_controller->UpdateSingleControlReference(g_controller_interface, m_reference); + const auto lock = m_controller->GetStateLock(); + + UpdateExpression(m_expression_text->toPlainText().toStdString()); + m_original_expression = m_reference->GetExpression(); m_apply_button->setText(m_apply_button->text().remove(QStringLiteral("*"))); @@ -532,6 +531,26 @@ void IOWindow::UpdateDeviceList() QString::fromStdString(m_controller->GetDefaultDevice().ToString())); } +void IOWindow::UpdateExpression(std::string new_expression) +{ + const auto lock = m_controller->GetStateLock(); + if (new_expression == m_reference->GetExpression()) + return; + + const auto error = m_reference->SetExpression(std::move(new_expression)); + const auto status = m_reference->GetParseStatus(); + m_controller->UpdateSingleControlReference(g_controller_interface, m_reference); + + if (error) + m_parse_text->setText(QString::fromStdString(*error)); + else if (status == ciface::ExpressionParser::ParseStatus::EmptyExpression) + m_parse_text->setText(QString()); + else if (status != ciface::ExpressionParser::ParseStatus::Successful) + m_parse_text->setText(tr("Invalid Expression.")); + else + m_parse_text->setText(tr("Success.")); +} + InputStateDelegate::InputStateDelegate(IOWindow* parent) : QItemDelegate(parent), m_parent(parent) { } diff --git a/Source/Core/DolphinQt/Config/Mapping/IOWindow.h b/Source/Core/DolphinQt/Config/Mapping/IOWindow.h index 9870a4c954..0b98487211 100644 --- a/Source/Core/DolphinQt/Config/Mapping/IOWindow.h +++ b/Source/Core/DolphinQt/Config/Mapping/IOWindow.h @@ -4,6 +4,9 @@ #pragma once +#include +#include + #include #include #include @@ -34,13 +37,10 @@ class ControlExpressionSyntaxHighlighter final : public QSyntaxHighlighter { Q_OBJECT public: - ControlExpressionSyntaxHighlighter(QTextDocument* parent, QLineEdit* result); + explicit ControlExpressionSyntaxHighlighter(QTextDocument* parent); protected: void highlightBlock(const QString& text) final override; - -private: - QLineEdit* const m_result_text; }; class IOWindow final : public QDialog @@ -74,6 +74,8 @@ private: void UpdateOptionList(); void UpdateDeviceList(); + void UpdateExpression(std::string new_expression); + // Main Layout QVBoxLayout* m_main_layout; @@ -108,6 +110,7 @@ private: QPushButton* m_apply_button; ControlReference* m_reference; + std::string m_original_expression; ControllerEmu::EmulatedController* m_controller; ciface::Core::DeviceQualifier m_devq;