mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
refactor: KeyboardSettingsPage (#4937)
This commit is contained in:
parent
b3ed3285ee
commit
f8b239899a
2 changed files with 59 additions and 58 deletions
|
@ -1,4 +1,4 @@
|
||||||
#include "KeyboardSettingsPage.hpp"
|
#include "widgets/settingspages/KeyboardSettingsPage.hpp"
|
||||||
|
|
||||||
#include "Application.hpp"
|
#include "Application.hpp"
|
||||||
#include "common/QLogging.hpp"
|
#include "common/QLogging.hpp"
|
||||||
|
@ -15,61 +15,12 @@
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QTableView>
|
#include <QTableView>
|
||||||
|
|
||||||
namespace chatterino {
|
namespace {
|
||||||
|
|
||||||
KeyboardSettingsPage::KeyboardSettingsPage()
|
using namespace chatterino;
|
||||||
{
|
|
||||||
LayoutCreator<KeyboardSettingsPage> layoutCreator(this);
|
|
||||||
auto layout = layoutCreator.emplace<QVBoxLayout>();
|
|
||||||
|
|
||||||
auto model = getApp()->hotkeys->createModel(nullptr);
|
void tableCellClicked(const QModelIndex &clicked, EditableModelView *view,
|
||||||
EditableModelView *view =
|
HotkeyModel *model)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
auto hotkey = getApp()->hotkeys->getHotkeyByName(
|
auto hotkey = getApp()->hotkeys->getHotkeyByName(
|
||||||
clicked.siblingAtColumn(0).data(Qt::EditRole).toString());
|
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
|
} // namespace chatterino
|
||||||
|
|
|
@ -11,10 +11,6 @@ class KeyboardSettingsPage : public SettingsPage
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
KeyboardSettingsPage();
|
KeyboardSettingsPage();
|
||||||
|
|
||||||
private:
|
|
||||||
void tableCellClicked(const QModelIndex &clicked, EditableModelView *view,
|
|
||||||
HotkeyModel *model);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace chatterino
|
} // namespace chatterino
|
||||||
|
|
Loading…
Reference in a new issue