refactor: actiondialog to not have ui as base of the class

This commit is contained in:
sithlord48 2024-10-18 18:14:46 -04:00 committed by Nick Bolton
parent 06aa41e8f5
commit 7b3d8c3dc3
3 changed files with 53 additions and 45 deletions

View file

@ -17,6 +17,7 @@
*/ */
#include "ActionDialog.h" #include "ActionDialog.h"
#include "ui_ActionDialog.h"
#include "Action.h" #include "Action.h"
#include "Hotkey.h" #include "Hotkey.h"
@ -24,58 +25,56 @@
#include "ServerConfig.h" #include "ServerConfig.h"
#include <QButtonGroup> #include <QButtonGroup>
#include <QtCore>
#include <QtGui>
ActionDialog::ActionDialog(QWidget *parent, ServerConfig &config, Hotkey &hotkey, Action &action) ActionDialog::ActionDialog(QWidget *parent, ServerConfig &config, Hotkey &hotkey, Action &action)
: QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint), : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint),
Ui::ActionDialogBase(), ui{std::make_unique<Ui::ActionDialog>()},
m_ServerConfig(config), m_ServerConfig(config),
m_Hotkey(hotkey), m_Hotkey(hotkey),
m_Action(action), m_Action(action),
m_pButtonGroupType(new QButtonGroup(this)) m_pButtonGroupType(new QButtonGroup(this))
{ {
setupUi(this); ui->setupUi(this);
// work around Qt Designer's lack of a QButtonGroup; we need it to get // work around Qt Designer's lack of a QButtonGroup; we need it to get
// at the button id of the checked radio button // at the button id of the checked radio button
QRadioButton *const typeButtons[] = { QRadioButton *const typeButtons[] = {
m_pRadioPress, ui->m_pRadioPress,
m_pRadioRelease, ui->m_pRadioRelease,
m_pRadioPressAndRelease, ui->m_pRadioPressAndRelease,
m_pRadioSwitchToScreen, ui->m_pRadioSwitchToScreen,
m_pRadioSwitchInDirection, ui->m_pRadioSwitchInDirection,
m_pRadioLockCursorToScreen, ui->m_pRadioLockCursorToScreen,
m_pRadioRestartAllConnections ui->m_pRadioRestartAllConnections
}; };
for (unsigned int i = 0; i < sizeof(typeButtons) / sizeof(typeButtons[0]); i++) for (unsigned int i = 0; i < sizeof(typeButtons) / sizeof(typeButtons[0]); i++)
m_pButtonGroupType->addButton(typeButtons[i], i); m_pButtonGroupType->addButton(typeButtons[i], i);
m_pKeySequenceWidgetHotkey->setText(m_Action.keySequence().toString()); ui->m_pKeySequenceWidgetHotkey->setText(m_Action.keySequence().toString());
m_pKeySequenceWidgetHotkey->setKeySequence(m_Action.keySequence()); ui->m_pKeySequenceWidgetHotkey->setKeySequence(m_Action.keySequence());
m_pButtonGroupType->button(m_Action.type())->setChecked(true); m_pButtonGroupType->button(m_Action.type())->setChecked(true);
m_pComboSwitchInDirection->setCurrentIndex(m_Action.switchDirection()); ui->m_pComboSwitchInDirection->setCurrentIndex(m_Action.switchDirection());
m_pComboLockCursorToScreen->setCurrentIndex(m_Action.lockCursorMode()); ui->m_pComboLockCursorToScreen->setCurrentIndex(m_Action.lockCursorMode());
if (m_Action.activeOnRelease()) if (m_Action.activeOnRelease())
m_pRadioHotkeyReleased->setChecked(true); ui->m_pRadioHotkeyReleased->setChecked(true);
else else
m_pRadioHotkeyPressed->setChecked(true); ui->m_pRadioHotkeyPressed->setChecked(true);
m_pGroupBoxScreens->setChecked(m_Action.haveScreens()); ui->m_pGroupBoxScreens->setChecked(m_Action.haveScreens());
int idx = 0; int idx = 0;
foreach (const Screen &screen, serverConfig().screens()) foreach (const Screen &screen, serverConfig().screens())
if (!screen.isNull()) { if (!screen.isNull()) {
QListWidgetItem *pListItem = new QListWidgetItem(screen.name()); QListWidgetItem *pListItem = new QListWidgetItem(screen.name());
m_pListScreens->addItem(pListItem); ui->m_pListScreens->addItem(pListItem);
if (m_Action.typeScreenNames().indexOf(screen.name()) != -1) if (m_Action.typeScreenNames().indexOf(screen.name()) != -1)
m_pListScreens->setCurrentItem(pListItem); ui->m_pListScreens->setCurrentItem(pListItem);
m_pComboSwitchToScreen->addItem(screen.name()); ui->m_pComboSwitchToScreen->addItem(screen.name());
if (screen.name() == m_Action.switchScreenName()) if (screen.name() == m_Action.switchScreenName())
m_pComboSwitchToScreen->setCurrentIndex(idx); ui->m_pComboSwitchToScreen->setCurrentIndex(idx);
idx++; idx++;
} }
@ -88,17 +87,17 @@ void ActionDialog::accept()
m_Action.setKeySequence(sequenceWidget()->keySequence()); m_Action.setKeySequence(sequenceWidget()->keySequence());
m_Action.setType(m_pButtonGroupType->checkedId()); m_Action.setType(m_pButtonGroupType->checkedId());
m_Action.setHaveScreens(m_pGroupBoxScreens->isChecked()); m_Action.setHaveScreens(ui->m_pGroupBoxScreens->isChecked());
m_Action.typeScreenNames().clear(); m_Action.typeScreenNames().clear();
foreach (const QListWidgetItem *pItem, m_pListScreens->selectedItems()) foreach (const QListWidgetItem *pItem, ui->m_pListScreens->selectedItems())
m_Action.typeScreenNames().append(pItem->text()); m_Action.typeScreenNames().append(pItem->text());
m_Action.setSwitchScreenName(m_pComboSwitchToScreen->currentText()); m_Action.setSwitchScreenName(ui->m_pComboSwitchToScreen->currentText());
m_Action.setSwitchDirection(m_pComboSwitchInDirection->currentIndex()); m_Action.setSwitchDirection(ui->m_pComboSwitchInDirection->currentIndex());
m_Action.setLockCursorMode(m_pComboLockCursorToScreen->currentIndex()); m_Action.setLockCursorMode(ui->m_pComboLockCursorToScreen->currentIndex());
m_Action.setActiveOnRelease(m_pRadioHotkeyReleased->isChecked()); m_Action.setActiveOnRelease(ui->m_pRadioHotkeyReleased->isChecked());
m_Action.setRestartServer(m_pRadioRestartAllConnections->isChecked()); m_Action.setRestartServer(ui->m_pRadioRestartAllConnections->isChecked());
QDialog::accept(); QDialog::accept();
} }
@ -106,10 +105,17 @@ void ActionDialog::accept()
void ActionDialog::on_m_pKeySequenceWidgetHotkey_keySequenceChanged() void ActionDialog::on_m_pKeySequenceWidgetHotkey_keySequenceChanged()
{ {
if (sequenceWidget()->keySequence().isMouseButton()) { if (sequenceWidget()->keySequence().isMouseButton()) {
m_pGroupBoxScreens->setEnabled(false); ui->m_pGroupBoxScreens->setEnabled(false);
m_pListScreens->setEnabled(false); ui->m_pListScreens->setEnabled(false);
} else { } else {
m_pGroupBoxScreens->setEnabled(true); ui->m_pGroupBoxScreens->setEnabled(true);
m_pListScreens->setEnabled(true); ui->m_pListScreens->setEnabled(true);
} }
} }
const KeySequenceWidget *ActionDialog::sequenceWidget() const
{
return ui->m_pKeySequenceWidgetHotkey;
}
ActionDialog::~ActionDialog() = default;

View file

@ -20,36 +20,38 @@
#include <QDialog> #include <QDialog>
#include "ui_ActionDialogBase.h"
class Hotkey; class Hotkey;
class Action; class Action;
class QRadioButton; class QRadioButton;
class QButtonGroup; class QButtonGroup;
class ServerConfig; class ServerConfig;
class KeySequenceWidget;
class ActionDialog : public QDialog, public Ui::ActionDialogBase namespace Ui {
class ActionDialog;
}
class ActionDialog : public QDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
ActionDialog(QWidget *parent, ServerConfig &config, Hotkey &hotkey, Action &action); ActionDialog(QWidget *parent, ServerConfig &config, Hotkey &hotkey, Action &action);
~ActionDialog() override;
protected slots: protected slots:
void accept(); void accept() override;
void on_m_pKeySequenceWidgetHotkey_keySequenceChanged(); void on_m_pKeySequenceWidgetHotkey_keySequenceChanged();
protected: protected:
const KeySequenceWidget *sequenceWidget() const const KeySequenceWidget *sequenceWidget() const;
{
return m_pKeySequenceWidgetHotkey;
}
const ServerConfig &serverConfig() const const ServerConfig &serverConfig() const
{ {
return m_ServerConfig; return m_ServerConfig;
} }
private: private:
std::unique_ptr<Ui::ActionDialog> ui;
const ServerConfig &m_ServerConfig; const ServerConfig &m_ServerConfig;
Hotkey &m_Hotkey; Hotkey &m_Hotkey;
Action &m_Action; Action &m_Action;

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>ActionDialogBase</class> <class>ActionDialog</class>
<widget class="QDialog" name="ActionDialogBase"> <widget class="QDialog" name="ActionDialog">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
@ -313,7 +313,7 @@
<connection> <connection>
<sender>buttonBox</sender> <sender>buttonBox</sender>
<signal>accepted()</signal> <signal>accepted()</signal>
<receiver>ActionDialogBase</receiver> <receiver>ActionDialog</receiver>
<slot>accept()</slot> <slot>accept()</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
@ -329,7 +329,7 @@
<connection> <connection>
<sender>buttonBox</sender> <sender>buttonBox</sender>
<signal>rejected()</signal> <signal>rejected()</signal>
<receiver>ActionDialogBase</receiver> <receiver>ActionDialog</receiver>
<slot>reject()</slot> <slot>reject()</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">