refactor: KeyboardSettingsPage (#4937)

This commit is contained in:
pajlada 2023-11-04 11:56:31 +01:00 committed by GitHub
parent b3ed3285ee
commit f8b239899a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 58 deletions

View file

@ -1,4 +1,4 @@
#include "KeyboardSettingsPage.hpp"
#include "widgets/settingspages/KeyboardSettingsPage.hpp"
#include "Application.hpp"
#include "common/QLogging.hpp"
@ -15,61 +15,12 @@
#include <QMessageBox>
#include <QTableView>
namespace chatterino {
namespace {
KeyboardSettingsPage::KeyboardSettingsPage()
{
LayoutCreator<KeyboardSettingsPage> layoutCreator(this);
auto layout = layoutCreator.emplace<QVBoxLayout>();
using namespace chatterino;
auto model = getApp()->hotkeys->createModel(nullptr);
EditableModelView *view =
layout.emplace<EditableModelView>(model).getElement();
view->setTitles({"Hotkey name", "Keybinding"});
view->getTableView()->horizontalHeader()->setVisible(true);
view->getTableView()->horizontalHeader()->setStretchLastSection(false);
view->getTableView()->horizontalHeader()->setSectionResizeMode(
QHeaderView::ResizeToContents);
view->getTableView()->horizontalHeader()->setSectionResizeMode(
1, QHeaderView::Stretch);
// We can safely ignore this signal connection since we own the view
std::ignore = view->addButtonPressed.connect([view, model] {
EditHotkeyDialog dialog(nullptr);
bool wasAccepted = dialog.exec() == 1;
if (wasAccepted)
{
auto newHotkey = dialog.data();
int vectorIndex = getApp()->hotkeys->hotkeys_.append(newHotkey);
getApp()->hotkeys->save();
}
});
QObject::connect(view->getTableView(), &QTableView::doubleClicked,
[this, view, model](const QModelIndex &clicked) {
this->tableCellClicked(clicked, view, model);
});
QPushButton *resetEverything = new QPushButton("Reset to defaults");
QObject::connect(resetEverything, &QPushButton::clicked, [this]() {
auto reply = QMessageBox::question(
this, "Reset hotkeys",
"Are you sure you want to reset hotkeys to defaults?",
QMessageBox::Yes | QMessageBox::Cancel);
if (reply == QMessageBox::Yes)
{
getApp()->hotkeys->resetToDefaults();
}
});
view->addCustomButton(resetEverything);
}
void KeyboardSettingsPage::tableCellClicked(const QModelIndex &clicked,
EditableModelView *view,
HotkeyModel *model)
void tableCellClicked(const QModelIndex &clicked, EditableModelView *view,
HotkeyModel *model)
{
auto hotkey = getApp()->hotkeys->getHotkeyByName(
clicked.siblingAtColumn(0).data(Qt::EditRole).toString());
@ -89,4 +40,58 @@ void KeyboardSettingsPage::tableCellClicked(const QModelIndex &clicked,
}
}
} // namespace
namespace chatterino {
KeyboardSettingsPage::KeyboardSettingsPage()
{
LayoutCreator<KeyboardSettingsPage> layoutCreator(this);
auto layout = layoutCreator.emplace<QVBoxLayout>();
auto *model = getApp()->hotkeys->createModel(nullptr);
EditableModelView *view =
layout.emplace<EditableModelView>(model).getElement();
view->setTitles({"Hotkey name", "Keybinding"});
view->getTableView()->horizontalHeader()->setVisible(true);
view->getTableView()->horizontalHeader()->setStretchLastSection(false);
view->getTableView()->horizontalHeader()->setSectionResizeMode(
QHeaderView::ResizeToContents);
view->getTableView()->horizontalHeader()->setSectionResizeMode(
1, QHeaderView::Stretch);
// We can safely ignore this signal connection since we own the view
std::ignore = view->addButtonPressed.connect([] {
EditHotkeyDialog dialog(nullptr);
bool wasAccepted = dialog.exec() == 1;
if (wasAccepted)
{
auto newHotkey = dialog.data();
int vectorIndex = getApp()->hotkeys->hotkeys_.append(newHotkey);
getApp()->hotkeys->save();
}
});
QObject::connect(view->getTableView(), &QTableView::doubleClicked,
[view, model](const QModelIndex &clicked) {
tableCellClicked(clicked, view, model);
});
auto *resetEverything = new QPushButton("Reset to defaults");
QObject::connect(resetEverything, &QPushButton::clicked, [this]() {
auto reply = QMessageBox::question(
this, "Reset hotkeys",
"Are you sure you want to reset hotkeys to defaults?",
QMessageBox::Yes | QMessageBox::Cancel);
if (reply == QMessageBox::Yes)
{
getApp()->hotkeys->resetToDefaults();
}
});
view->addCustomButton(resetEverything);
}
} // namespace chatterino

View file

@ -11,10 +11,6 @@ class KeyboardSettingsPage : public SettingsPage
{
public:
KeyboardSettingsPage();
private:
void tableCellClicked(const QModelIndex &clicked, EditableModelView *view,
HotkeyModel *model);
};
} // namespace chatterino