refactor: ActionDialog, Use Combo for action selection

This commit is contained in:
sithlord48
2025-01-04 22:20:24 -05:00
committed by Nick Bolton
parent 04f73cfcb7
commit 4db2b80d4e
3 changed files with 97 additions and 244 deletions

View File

@ -25,42 +25,28 @@
#include "KeySequence.h"
#include "ServerConfig.h"
#include <QButtonGroup>
ActionDialog::ActionDialog(QWidget *parent, const ServerConfig &config, Hotkey &hotkey, Action &action)
: QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint),
ui{std::make_unique<Ui::ActionDialog>()},
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<int>::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;

View File

@ -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::ActionDialog> ui;
Hotkey &m_hotkey;
Action &m_action;
QButtonGroup *m_buttonGroupType;
};

View File

@ -15,54 +15,68 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QComboBox" name="comboTriggerOn">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<property name="text">
<string>When the hotkey is pressed</string>
</property>
<widget class="QComboBox" name="comboTriggerOn">
<item>
<property name="text">
<string>When the hotkey is pressed</string>
</property>
</item>
<item>
<property name="text">
<string>When the hotkey is released</string>
</property>
</item>
</widget>
</item>
<item>
<property name="text">
<string>When the hotkey is released</string>
</property>
<widget class="QComboBox" name="comboActionType">
<item>
<property name="text">
<string>Press key(s)</string>
</property>
</item>
<item>
<property name="text">
<string>Release key(s)</string>
</property>
</item>
<item>
<property name="text">
<string>Toggle key(s)</string>
</property>
</item>
<item>
<property name="text">
<string>Switch to a screen</string>
</property>
</item>
<item>
<property name="text">
<string>Switch to the screen in a direction</string>
</property>
</item>
<item>
<property name="text">
<string>Modify the cursor lock</string>
</property>
</item>
<item>
<property name="text">
<string>Restart the server</string>
</property>
</item>
</widget>
</item>
</widget>
</layout>
</item>
<item>
<widget class="QGroupBox" name="m_pGroupType">
<property name="font">
<font>
<kerning>true</kerning>
</font>
</property>
<property name="title">
<string>Choose the action to perform</string>
<string/>
</property>
<layout class="QVBoxLayout">
<item>
<widget class="QRadioButton" name="m_pRadioPress">
<property name="text">
<string>Press a hotkey</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="m_pRadioRelease">
<property name="text">
<string>Release a hotkey</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="m_pRadioPressAndRelease">
<property name="text">
<string>Press and release a hotkey</string>
</property>
</widget>
</item>
<item>
<widget class="KeySequenceWidget" name="keySequenceWidget">
<property name="sizePolicy">
@ -133,9 +147,9 @@
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QRadioButton" name="m_pRadioSwitchToScreen">
<widget class="QLabel" name="label">
<property name="text">
<string>Switch to screen</string>
<string>Switch to</string>
</property>
</widget>
</item>
@ -164,9 +178,9 @@
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QRadioButton" name="m_pRadioSwitchInDirection">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Switch in direction</string>
<string>Direction to move</string>
</property>
</widget>
</item>
@ -215,9 +229,9 @@
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QRadioButton" name="m_pRadioLockCursorToScreen">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Lock cursor to screen</string>
<string>Cursor lock will</string>
</property>
</widget>
</item>
@ -258,17 +272,6 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QRadioButton" name="m_pRadioRestartAllConnections">
<property name="text">
<string>Restart server</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
@ -292,166 +295,5 @@
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>m_pRadioSwitchToScreen</sender>
<signal>toggled(bool)</signal>
<receiver>m_pComboSwitchToScreen</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>148</x>
<y>291</y>
</hint>
<hint type="destinationlabel">
<x>350</x>
<y>290</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_pRadioSwitchInDirection</sender>
<signal>toggled(bool)</signal>
<receiver>m_pComboSwitchInDirection</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>158</x>
<y>322</y>
</hint>
<hint type="destinationlabel">
<x>350</x>
<y>321</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_pRadioLockCursorToScreen</sender>
<signal>toggled(bool)</signal>
<receiver>m_pComboLockCursorToScreen</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>180</x>
<y>353</y>
</hint>
<hint type="destinationlabel">
<x>350</x>
<y>352</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_pRadioPress</sender>
<signal>toggled(bool)</signal>
<receiver>m_pGroupBoxScreens</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>25</x>
<y>47</y>
</hint>
<hint type="destinationlabel">
<x>33</x>
<y>155</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_pRadioSwitchToScreen</sender>
<signal>toggled(bool)</signal>
<receiver>m_pGroupBoxScreens</receiver>
<slot>setDisabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>48</x>
<y>278</y>
</hint>
<hint type="destinationlabel">
<x>98</x>
<y>153</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_pRadioRelease</sender>
<signal>toggled(bool)</signal>
<receiver>m_pGroupBoxScreens</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>264</x>
<y>67</y>
</hint>
<hint type="destinationlabel">
<x>241</x>
<y>158</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_pRadioPressAndRelease</sender>
<signal>toggled(bool)</signal>
<receiver>m_pGroupBoxScreens</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>286</x>
<y>98</y>
</hint>
<hint type="destinationlabel">
<x>290</x>
<y>156</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_pRadioSwitchInDirection</sender>
<signal>toggled(bool)</signal>
<receiver>m_pGroupBoxScreens</receiver>
<slot>setDisabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>38</x>
<y>313</y>
</hint>
<hint type="destinationlabel">
<x>64</x>
<y>195</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_pRadioLockCursorToScreen</sender>
<signal>toggled(bool)</signal>
<receiver>m_pGroupBoxScreens</receiver>
<slot>setDisabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>48</x>
<y>339</y>
</hint>
<hint type="destinationlabel">
<x>79</x>
<y>234</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_pRadioRestartAllConnections</sender>
<signal>toggled(bool)</signal>
<receiver>m_pGroupBoxScreens</receiver>
<slot>setDisabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>48</x>
<y>339</y>
</hint>
<hint type="destinationlabel">
<x>79</x>
<y>234</y>
</hint>
</hints>
</connection>
</connections>
<connections/>
</ui>