DolphinQT/IOWindow: Add combo boxes to insert the new operators and functions so people are semi-aware of them. Fixed the "Apply" button. Display an error message on expression parse error.

This commit is contained in:
Jordan Woyak 2019-01-27 10:35:29 -06:00
parent 5cb1248612
commit e3cf2ae0d4
2 changed files with 84 additions and 36 deletions

View File

@ -12,6 +12,7 @@
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
#include <QListWidget> #include <QListWidget>
#include <QMessageBox>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QPushButton> #include <QPushButton>
#include <QSlider> #include <QSlider>
@ -35,12 +36,13 @@ IOWindow::IOWindow(QWidget* parent, ControllerEmu::EmulatedController* controlle
: QDialog(parent), m_reference(ref), m_controller(controller), m_type(type) : QDialog(parent), m_reference(ref), m_controller(controller), m_type(type)
{ {
CreateMainLayout(); CreateMainLayout();
ConnectWidgets();
setWindowTitle(type == IOWindow::Type::Input ? tr("Configure Input") : tr("Configure Output")); setWindowTitle(type == IOWindow::Type::Input ? tr("Configure Input") : tr("Configure Output"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
ConfigChanged(); ConfigChanged();
ConnectWidgets();
} }
void IOWindow::CreateMainLayout() void IOWindow::CreateMainLayout()
@ -51,10 +53,6 @@ void IOWindow::CreateMainLayout()
m_option_list = new QListWidget(); m_option_list = new QListWidget();
m_select_button = new QPushButton(tr("Select")); m_select_button = new QPushButton(tr("Select"));
m_detect_button = new QPushButton(tr("Detect")); m_detect_button = new QPushButton(tr("Detect"));
m_or_button = new QPushButton(tr("| OR"));
m_and_button = new QPushButton(tr("&& AND"));
m_add_button = new QPushButton(tr("+ ADD"));
m_not_button = new QPushButton(tr("! NOT"));
m_test_button = new QPushButton(tr("Test")); m_test_button = new QPushButton(tr("Test"));
m_expression_text = new QPlainTextEdit(); m_expression_text = new QPlainTextEdit();
m_button_box = new QDialogButtonBox(); m_button_box = new QDialogButtonBox();
@ -63,6 +61,42 @@ void IOWindow::CreateMainLayout()
m_range_slider = new QSlider(Qt::Horizontal); m_range_slider = new QSlider(Qt::Horizontal);
m_range_spinbox = new QSpinBox(); m_range_spinbox = new QSpinBox();
m_operators_combo = new QComboBox();
m_operators_combo->addItem(tr("Operators"));
m_operators_combo->insertSeparator(1);
if (m_type == Type::Input)
{
m_operators_combo->addItem(tr("! Not"));
m_operators_combo->addItem(tr("* Multiply"));
m_operators_combo->addItem(tr("/ Divide"));
m_operators_combo->addItem(tr("% Modulo"));
m_operators_combo->addItem(tr("+ Add"));
m_operators_combo->addItem(tr("- Subtract"));
m_operators_combo->addItem(tr("> Greater-than"));
m_operators_combo->addItem(tr("< Less-than"));
m_operators_combo->addItem(tr("& And"));
}
m_operators_combo->addItem(tr("| Or"));
if (m_type == Type::Input)
{
m_operators_combo->addItem(tr(", Comma"));
}
m_functions_combo = new QComboBox();
m_functions_combo->addItem(tr("Functions"));
m_functions_combo->insertSeparator(1);
m_functions_combo->addItem(QStringLiteral("!if"));
m_functions_combo->addItem(QStringLiteral("!sin"));
m_functions_combo->addItem(QStringLiteral("!timer"));
m_functions_combo->addItem(QStringLiteral("!toggle"));
m_functions_combo->addItem(QStringLiteral("!while"));
m_functions_combo->addItem(QStringLiteral("!deadzone"));
m_functions_combo->addItem(QStringLiteral("!smooth"));
m_functions_combo->addItem(QStringLiteral("!hold"));
m_functions_combo->addItem(QStringLiteral("!tap"));
m_functions_combo->addItem(QStringLiteral("!relative"));
m_functions_combo->addItem(QStringLiteral("!pulse"));
// Devices // Devices
m_main_layout->addWidget(m_devices_combo); m_main_layout->addWidget(m_devices_combo);
@ -77,16 +111,6 @@ void IOWindow::CreateMainLayout()
m_range_spinbox->setMaximum(500); m_range_spinbox->setMaximum(500);
m_main_layout->addLayout(range_hbox); m_main_layout->addLayout(range_hbox);
// Options (Buttons, Outputs) and action buttons
// macOS style doesn't support expanding buttons
#ifndef __APPLE__
for (QPushButton* button : {m_select_button, m_detect_button, m_or_button, m_and_button,
m_add_button, m_not_button, m_test_button})
{
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
#endif
auto* hbox = new QHBoxLayout(); auto* hbox = new QHBoxLayout();
auto* button_vbox = new QVBoxLayout(); auto* button_vbox = new QVBoxLayout();
hbox->addWidget(m_option_list, 8); hbox->addWidget(m_option_list, 8);
@ -94,13 +118,10 @@ void IOWindow::CreateMainLayout()
button_vbox->addWidget(m_select_button); button_vbox->addWidget(m_select_button);
button_vbox->addWidget(m_type == Type::Input ? m_detect_button : m_test_button); button_vbox->addWidget(m_type == Type::Input ? m_detect_button : m_test_button);
button_vbox->addWidget(m_or_button); button_vbox->addWidget(m_operators_combo);
if (m_type == Type::Input) if (m_type == Type::Input)
{ {
button_vbox->addWidget(m_and_button); button_vbox->addWidget(m_functions_combo);
button_vbox->addWidget(m_add_button);
button_vbox->addWidget(m_not_button);
} }
m_main_layout->addLayout(hbox, 2); m_main_layout->addLayout(hbox, 2);
@ -132,11 +153,7 @@ void IOWindow::ConfigChanged()
void IOWindow::ConnectWidgets() void IOWindow::ConnectWidgets()
{ {
connect(m_select_button, &QPushButton::clicked, [this] { AppendSelectedOption(""); }); connect(m_select_button, &QPushButton::clicked, [this] { AppendSelectedOption(); });
connect(m_add_button, &QPushButton::clicked, [this] { AppendSelectedOption(" + "); });
connect(m_and_button, &QPushButton::clicked, [this] { AppendSelectedOption(" & "); });
connect(m_or_button, &QPushButton::clicked, [this] { AppendSelectedOption(" | "); });
connect(m_not_button, &QPushButton::clicked, [this] { AppendSelectedOption("!"); });
connect(m_detect_button, &QPushButton::clicked, this, &IOWindow::OnDetectButtonPressed); connect(m_detect_button, &QPushButton::clicked, this, &IOWindow::OnDetectButtonPressed);
connect(m_test_button, &QPushButton::clicked, this, &IOWindow::OnTestButtonPressed); connect(m_test_button, &QPushButton::clicked, this, &IOWindow::OnTestButtonPressed);
@ -147,17 +164,38 @@ void IOWindow::ConnectWidgets()
this, &IOWindow::OnRangeChanged); this, &IOWindow::OnRangeChanged);
connect(m_range_slider, static_cast<void (QSlider::*)(int value)>(&QSlider::valueChanged), this, connect(m_range_slider, static_cast<void (QSlider::*)(int value)>(&QSlider::valueChanged), this,
&IOWindow::OnRangeChanged); &IOWindow::OnRangeChanged);
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("*"));
});
connect(m_operators_combo, QOverload<int>::of(&QComboBox::activated), [this](int index) {
if (0 == index)
return;
m_expression_text->insertPlainText(m_operators_combo->currentText().left(1));
m_operators_combo->setCurrentIndex(0);
});
connect(m_functions_combo, QOverload<int>::of(&QComboBox::activated), [this](int index) {
if (0 == index)
return;
m_expression_text->insertPlainText(m_functions_combo->currentText() + QStringLiteral("()"));
m_functions_combo->setCurrentIndex(0);
});
} }
void IOWindow::AppendSelectedOption(const std::string& prefix) void IOWindow::AppendSelectedOption()
{ {
if (m_option_list->currentItem() == nullptr) if (m_option_list->currentItem() == nullptr)
return; return;
m_expression_text->insertPlainText( m_expression_text->insertPlainText(MappingCommon::GetExpressionForControl(
QString::fromStdString(prefix) + m_option_list->currentItem()->text(), m_devq, m_controller->GetDefaultDevice()));
MappingCommon::GetExpressionForControl(m_option_list->currentItem()->text(), m_devq,
m_controller->GetDefaultDevice()));
} }
void IOWindow::OnDeviceChanged(const QString& device) void IOWindow::OnDeviceChanged(const QString& device)
@ -175,7 +213,19 @@ void IOWindow::OnDialogButtonPressed(QAbstractButton* button)
} }
m_reference->SetExpression(m_expression_text->toPlainText().toStdString()); m_reference->SetExpression(m_expression_text->toPlainText().toStdString());
m_controller->UpdateReferences(g_controller_interface); m_controller->UpdateSingleControlReference(g_controller_interface, m_reference);
m_apply_button->setText(m_apply_button->text().remove(QStringLiteral("*")));
if (ciface::ExpressionParser::ParseStatus::SyntaxError == m_reference->GetParseStatus())
{
QMessageBox error(this);
error.setIcon(QMessageBox::Critical);
error.setWindowTitle(tr("Error"));
error.setText(tr("The expression contains a syntax error."));
error.setWindowModality(Qt::WindowModal);
error.exec();
}
if (button != m_apply_button) if (button != m_apply_button)
accept(); accept();

View File

@ -51,7 +51,7 @@ private:
void OnTestButtonPressed(); void OnTestButtonPressed();
void OnRangeChanged(int range); void OnRangeChanged(int range);
void AppendSelectedOption(const std::string& prefix); void AppendSelectedOption();
void UpdateOptionList(); void UpdateOptionList();
void UpdateDeviceList(); void UpdateDeviceList();
@ -70,13 +70,11 @@ private:
// Shared actions // Shared actions
QPushButton* m_select_button; QPushButton* m_select_button;
QPushButton* m_or_button; QComboBox* m_operators_combo;
// Input actions // Input actions
QPushButton* m_detect_button; QPushButton* m_detect_button;
QPushButton* m_and_button; QComboBox* m_functions_combo;
QPushButton* m_not_button;
QPushButton* m_add_button;
// Output actions // Output actions
QPushButton* m_test_button; QPushButton* m_test_button;