From 4db2b80d4e77e9efa3124d5e263742b271d55525 Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Sat, 4 Jan 2025 22:20:24 -0500 Subject: [PATCH] refactor: ActionDialog, Use Combo for action selection --- .../deskflow-gui/dialogs/ActionDialog.cpp | 49 ++-- src/apps/deskflow-gui/dialogs/ActionDialog.h | 16 +- src/apps/deskflow-gui/dialogs/ActionDialog.ui | 276 ++++-------------- 3 files changed, 97 insertions(+), 244 deletions(-) diff --git a/src/apps/deskflow-gui/dialogs/ActionDialog.cpp b/src/apps/deskflow-gui/dialogs/ActionDialog.cpp index d3731ce98..7da18873c 100644 --- a/src/apps/deskflow-gui/dialogs/ActionDialog.cpp +++ b/src/apps/deskflow-gui/dialogs/ActionDialog.cpp @@ -25,42 +25,28 @@ #include "KeySequence.h" #include "ServerConfig.h" -#include - ActionDialog::ActionDialog(QWidget *parent, const ServerConfig &config, Hotkey &hotkey, Action &action) : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint), ui{std::make_unique()}, m_hotkey(hotkey), - m_action(action), - m_buttonGroupType(new QButtonGroup(this)) + m_action(action) { ui->setupUi(this); connect(ui->keySequenceWidget, &KeySequenceWidget::keySequenceChanged, this, &ActionDialog::keySequenceChanged); + connect( + ui->comboActionType, QOverload::of(&QComboBox::currentIndexChanged), this, &ActionDialog::actionTypeChanged + ); connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &ActionDialog::accept); connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &ActionDialog::reject); - // work around Qt Designer's lack of a QButtonGroup; we need it to get - // at the button id of the checked radio button - QRadioButton *const typeButtons[] = { - ui->m_pRadioPress, - ui->m_pRadioRelease, - ui->m_pRadioPressAndRelease, - ui->m_pRadioSwitchToScreen, - ui->m_pRadioSwitchInDirection, - ui->m_pRadioLockCursorToScreen, - ui->m_pRadioRestartAllConnections - }; - - for (unsigned int i = 0; i < sizeof(typeButtons) / sizeof(typeButtons[0]); i++) - m_buttonGroupType->addButton(typeButtons[i], i); - ui->keySequenceWidget->setText(m_action.keySequence().toString()); ui->keySequenceWidget->setKeySequence(m_action.keySequence()); - m_buttonGroupType->button(m_action.type())->setChecked(true); + ui->m_pComboSwitchInDirection->setCurrentIndex(m_action.switchDirection()); ui->m_pComboLockCursorToScreen->setCurrentIndex(m_action.lockCursorMode()); - ui->comboTriggerOn->setCurrentIndex(m_action.haveScreens()); + ui->comboActionType->setCurrentIndex(m_action.type()); + ui->comboTriggerOn->setCurrentIndex(m_action.activeOnRelease()); ui->m_pGroupBoxScreens->setChecked(m_action.haveScreens()); @@ -80,11 +66,12 @@ ActionDialog::ActionDialog(QWidget *parent, const ServerConfig &config, Hotkey & void ActionDialog::accept() { - if (!ui->keySequenceWidget->valid() && m_buttonGroupType->checkedId() >= 0 && m_buttonGroupType->checkedId() < 3) + if (!ui->keySequenceWidget->valid() && ui->comboActionType->currentIndex() >= 0 && + ui->comboActionType->currentIndex() < 3) return; m_action.setKeySequence(ui->keySequenceWidget->keySequence()); - m_action.setType(m_buttonGroupType->checkedId()); + m_action.setType(ui->comboActionType->currentIndex()); m_action.setHaveScreens(ui->m_pGroupBoxScreens->isChecked()); m_action.typeScreenNames().clear(); @@ -97,7 +84,7 @@ void ActionDialog::accept() m_action.setSwitchDirection(ui->m_pComboSwitchInDirection->currentIndex()); m_action.setLockCursorMode(ui->m_pComboLockCursorToScreen->currentIndex()); m_action.setActiveOnRelease(ui->comboTriggerOn->currentIndex()); - m_action.setRestartServer(ui->m_pRadioRestartAllConnections->isChecked()); + m_action.setRestartServer(ui->comboActionType->currentIndex() == ActionTypes::RestartServer); QDialog::accept(); } @@ -108,4 +95,18 @@ void ActionDialog::keySequenceChanged() ui->m_pListScreens->setEnabled(!ui->keySequenceWidget->keySequence().isMouseButton()); } +void ActionDialog::actionTypeChanged(int index) +{ + ui->keySequenceWidget->setEnabled(isKeyAction(index)); + ui->m_pListScreens->setEnabled(isKeyAction(index) && ui->m_pGroupBoxScreens->isChecked()); + ui->m_pComboSwitchToScreen->setEnabled(index == ActionTypes::SwitchTo); + ui->m_pComboSwitchInDirection->setEnabled(index == ActionTypes::SwitchInDirection); + ui->m_pComboLockCursorToScreen->setEnabled(index == ActionTypes::ModifyCursorLock); +} + +bool ActionDialog::isKeyAction(int index) +{ + return ((index == ActionTypes::PressKey) || (index == ActionTypes::ReleaseKey) || (index == ActionTypes::ToggleKey)); +} + ActionDialog::~ActionDialog() = default; diff --git a/src/apps/deskflow-gui/dialogs/ActionDialog.h b/src/apps/deskflow-gui/dialogs/ActionDialog.h index 5895aeebb..4b7687c24 100644 --- a/src/apps/deskflow-gui/dialogs/ActionDialog.h +++ b/src/apps/deskflow-gui/dialogs/ActionDialog.h @@ -23,7 +23,6 @@ class Hotkey; class Action; -class QButtonGroup; class ServerConfig; class KeySequenceWidget; @@ -36,6 +35,17 @@ class ActionDialog : public QDialog Q_OBJECT public: + enum ActionTypes + { + PressKey, + ReleaseKey, + ToggleKey, + SwitchTo, + SwitchInDirection, + ModifyCursorLock, + RestartServer + }; + ActionDialog(QWidget *parent, const ServerConfig &config, Hotkey &hotkey, Action &action); ~ActionDialog() override; @@ -44,10 +54,10 @@ protected slots: private: void keySequenceChanged(); + void actionTypeChanged(int index); + bool isKeyAction(int index); std::unique_ptr ui; Hotkey &m_hotkey; Action &m_action; - - QButtonGroup *m_buttonGroupType; }; diff --git a/src/apps/deskflow-gui/dialogs/ActionDialog.ui b/src/apps/deskflow-gui/dialogs/ActionDialog.ui index 564fe02e7..2603ed4f7 100644 --- a/src/apps/deskflow-gui/dialogs/ActionDialog.ui +++ b/src/apps/deskflow-gui/dialogs/ActionDialog.ui @@ -15,54 +15,68 @@ - + - - When the hotkey is pressed - + + + + When the hotkey is pressed + + + + + When the hotkey is released + + + - - When the hotkey is released - + + + + Press key(s) + + + + + Release key(s) + + + + + Toggle key(s) + + + + + Switch to a screen + + + + + Switch to the screen in a direction + + + + + Modify the cursor lock + + + + + Restart the server + + + - + - - - true - - - Choose the action to perform + - - - - Press a hotkey - - - true - - - - - - - Release a hotkey - - - - - - - Press and release a hotkey - - - @@ -133,9 +147,9 @@ - + - Switch to screen + Switch to @@ -164,9 +178,9 @@ - + - Switch in direction + Direction to move @@ -215,9 +229,9 @@ - + - Lock cursor to screen + Cursor lock will @@ -258,17 +272,6 @@ - - - - - - Restart server - - - - - @@ -292,166 +295,5 @@ - - - m_pRadioSwitchToScreen - toggled(bool) - m_pComboSwitchToScreen - setEnabled(bool) - - - 148 - 291 - - - 350 - 290 - - - - - m_pRadioSwitchInDirection - toggled(bool) - m_pComboSwitchInDirection - setEnabled(bool) - - - 158 - 322 - - - 350 - 321 - - - - - m_pRadioLockCursorToScreen - toggled(bool) - m_pComboLockCursorToScreen - setEnabled(bool) - - - 180 - 353 - - - 350 - 352 - - - - - m_pRadioPress - toggled(bool) - m_pGroupBoxScreens - setEnabled(bool) - - - 25 - 47 - - - 33 - 155 - - - - - m_pRadioSwitchToScreen - toggled(bool) - m_pGroupBoxScreens - setDisabled(bool) - - - 48 - 278 - - - 98 - 153 - - - - - m_pRadioRelease - toggled(bool) - m_pGroupBoxScreens - setEnabled(bool) - - - 264 - 67 - - - 241 - 158 - - - - - m_pRadioPressAndRelease - toggled(bool) - m_pGroupBoxScreens - setEnabled(bool) - - - 286 - 98 - - - 290 - 156 - - - - - m_pRadioSwitchInDirection - toggled(bool) - m_pGroupBoxScreens - setDisabled(bool) - - - 38 - 313 - - - 64 - 195 - - - - - m_pRadioLockCursorToScreen - toggled(bool) - m_pGroupBoxScreens - setDisabled(bool) - - - 48 - 339 - - - 79 - 234 - - - - - m_pRadioRestartAllConnections - toggled(bool) - m_pGroupBoxScreens - setDisabled(bool) - - - 48 - 339 - - - 79 - 234 - - - - +