Skip to content

Commit

Permalink
refactor: KeyboardSettingsPage (#4937)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada authored Nov 4, 2023
1 parent b3ed328 commit f8b2398
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
61 changes: 33 additions & 28 deletions src/widgets/settingspages/KeyboardSettingsPage.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "KeyboardSettingsPage.hpp"
#include "widgets/settingspages/KeyboardSettingsPage.hpp"

#include "Application.hpp"
#include "common/QLogging.hpp"
Expand All @@ -15,14 +15,41 @@
#include <QMessageBox>
#include <QTableView>

namespace {

using namespace chatterino;

void tableCellClicked(const QModelIndex &clicked, EditableModelView *view,
HotkeyModel *model)
{
auto hotkey = getApp()->hotkeys->getHotkeyByName(
clicked.siblingAtColumn(0).data(Qt::EditRole).toString());
if (!hotkey)
{
return; // clicked on header or invalid hotkey
}
EditHotkeyDialog dialog(hotkey);
bool wasAccepted = dialog.exec() == 1;

if (wasAccepted)
{
auto newHotkey = dialog.data();
auto vectorIndex =
getApp()->hotkeys->replaceHotkey(hotkey->name(), newHotkey);
getApp()->hotkeys->save();
}
}

} // namespace

namespace chatterino {

KeyboardSettingsPage::KeyboardSettingsPage()
{
LayoutCreator<KeyboardSettingsPage> layoutCreator(this);
auto layout = layoutCreator.emplace<QVBoxLayout>();

auto model = getApp()->hotkeys->createModel(nullptr);
auto *model = getApp()->hotkeys->createModel(nullptr);
EditableModelView *view =
layout.emplace<EditableModelView>(model).getElement();

Expand All @@ -35,7 +62,7 @@ KeyboardSettingsPage::KeyboardSettingsPage()
1, QHeaderView::Stretch);

// We can safely ignore this signal connection since we own the view
std::ignore = view->addButtonPressed.connect([view, model] {
std::ignore = view->addButtonPressed.connect([] {
EditHotkeyDialog dialog(nullptr);
bool wasAccepted = dialog.exec() == 1;

Expand All @@ -48,11 +75,11 @@ KeyboardSettingsPage::KeyboardSettingsPage()
});

QObject::connect(view->getTableView(), &QTableView::doubleClicked,
[this, view, model](const QModelIndex &clicked) {
this->tableCellClicked(clicked, view, model);
[view, model](const QModelIndex &clicked) {
tableCellClicked(clicked, view, model);
});

QPushButton *resetEverything = new QPushButton("Reset to defaults");
auto *resetEverything = new QPushButton("Reset to defaults");
QObject::connect(resetEverything, &QPushButton::clicked, [this]() {
auto reply = QMessageBox::question(
this, "Reset hotkeys",
Expand All @@ -67,26 +94,4 @@ KeyboardSettingsPage::KeyboardSettingsPage()
view->addCustomButton(resetEverything);
}

void KeyboardSettingsPage::tableCellClicked(const QModelIndex &clicked,
EditableModelView *view,
HotkeyModel *model)
{
auto hotkey = getApp()->hotkeys->getHotkeyByName(
clicked.siblingAtColumn(0).data(Qt::EditRole).toString());
if (!hotkey)
{
return; // clicked on header or invalid hotkey
}
EditHotkeyDialog dialog(hotkey);
bool wasAccepted = dialog.exec() == 1;

if (wasAccepted)
{
auto newHotkey = dialog.data();
auto vectorIndex =
getApp()->hotkeys->replaceHotkey(hotkey->name(), newHotkey);
getApp()->hotkeys->save();
}
}

} // namespace chatterino
4 changes: 0 additions & 4 deletions src/widgets/settingspages/KeyboardSettingsPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class KeyboardSettingsPage : public SettingsPage
{
public:
KeyboardSettingsPage();

private:
void tableCellClicked(const QModelIndex &clicked, EditableModelView *view,
HotkeyModel *model);
};

} // namespace chatterino

0 comments on commit f8b2398

Please sign in to comment.