Qt/IOWindow: Apply expressions immediately so we can query the current value of the expression.

This commit is contained in:
Admiral H. Curtiss 2020-11-08 01:10:26 +01:00
parent cae741584b
commit ddfb8fa404
2 changed files with 41 additions and 19 deletions

View File

@ -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<int>(&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)
{
}

View File

@ -4,6 +4,9 @@
#pragma once
#include <memory>
#include <string>
#include <QDialog>
#include <QString>
#include <QSyntaxHighlighter>
@ -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;