2023-11-04 11:56:31 +01:00
|
|
|
#include "widgets/settingspages/KeyboardSettingsPage.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2021-11-21 18:46:21 +01:00
|
|
|
#include "Application.hpp"
|
|
|
|
#include "common/QLogging.hpp"
|
2022-12-31 15:41:01 +01:00
|
|
|
#include "controllers/hotkeys/Hotkey.hpp"
|
2021-11-21 18:46:21 +01:00
|
|
|
#include "controllers/hotkeys/HotkeyController.hpp"
|
|
|
|
#include "controllers/hotkeys/HotkeyModel.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "util/LayoutCreator.hpp"
|
2021-11-21 18:46:21 +01:00
|
|
|
#include "widgets/dialogs/EditHotkeyDialog.hpp"
|
2022-12-31 15:41:01 +01:00
|
|
|
#include "widgets/helper/EditableModelView.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-06-05 19:39:18 +02:00
|
|
|
#include <QFormLayout>
|
2021-11-21 18:46:21 +01:00
|
|
|
#include <QHeaderView>
|
2018-06-05 19:39:18 +02:00
|
|
|
#include <QLabel>
|
2022-12-31 15:41:01 +01:00
|
|
|
#include <QMessageBox>
|
2021-11-21 18:46:21 +01:00
|
|
|
#include <QTableView>
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2023-11-04 11:56:31 +01:00
|
|
|
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
|
|
|
|
|
2018-01-24 15:34:04 +01:00
|
|
|
namespace chatterino {
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-01-24 15:34:04 +01:00
|
|
|
KeyboardSettingsPage::KeyboardSettingsPage()
|
|
|
|
{
|
2021-11-21 18:46:21 +01:00
|
|
|
LayoutCreator<KeyboardSettingsPage> layoutCreator(this);
|
|
|
|
auto layout = layoutCreator.emplace<QVBoxLayout>();
|
|
|
|
|
2023-11-04 11:56:31 +01:00
|
|
|
auto *model = getApp()->hotkeys->createModel(nullptr);
|
2021-11-21 18:46:21 +01:00
|
|
|
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);
|
|
|
|
|
2023-09-16 13:52:51 +02:00
|
|
|
// We can safely ignore this signal connection since we own the view
|
2023-11-04 11:56:31 +01:00
|
|
|
std::ignore = view->addButtonPressed.connect([] {
|
2021-11-21 18:46:21 +01:00
|
|
|
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,
|
2023-11-04 11:56:31 +01:00
|
|
|
[view, model](const QModelIndex &clicked) {
|
|
|
|
tableCellClicked(clicked, view, model);
|
2021-11-21 18:46:21 +01:00
|
|
|
});
|
|
|
|
|
2023-11-04 11:56:31 +01:00
|
|
|
auto *resetEverything = new QPushButton("Reset to defaults");
|
2021-11-21 18:46:21 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-01-24 15:34:04 +01:00
|
|
|
} // namespace chatterino
|