summaryrefslogtreecommitdiff
path: root/src/config.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.cpp')
-rw-r--r--src/config.cpp140
1 files changed, 112 insertions, 28 deletions
diff --git a/src/config.cpp b/src/config.cpp
index 1ab2b93..5ffb76e 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -33,13 +33,10 @@ Shortcuts::Shortcuts(ShortcutMap&& shortcutMap) :
{
for (const auto& [action, shortcuts] : m_actionToShortcuts) {
for (const auto& shortcut : shortcuts) {
- qDebug("Working on action %s", actionTypeToString(action));
auto bindResult = bind(action, shortcut);
if (bindResult != FudStatus::Success) {
qWarning("Error: %s", FudStatusToString(bindResult));
return;
- } else {
- qDebug("Bound %s to %s", qPrintable(shortcut.toString()), actionTypeToString(action));
}
}
}
@@ -231,7 +228,7 @@ ShortcutMap Shortcuts::fromUserConfig(const std::filesystem::path& configFileNam
auto binder = [&](ActionType action, QKeySequence keySeq) {
auto bindDefaultStatus = shortcuts.bind(action, keySeq);
if (bindDefaultStatus != FudStatus::Success) {
- qDebug("%s already bound", actionTypeToString(action));
+ qWarning("%s already bound", actionTypeToString(action));
}
};
binder(ActionType::OpenFile, QKeySequence(QKeySequence::Open));
@@ -277,30 +274,45 @@ ShortcutMap GetsuyomiConfig::shortcuts() const
return shortcuts;
}
+ShortcutDisplay::ShortcutDisplay(QWidget* parent, QKeySequence shortcut) : QWidget{parent}, m_binding{shortcut}
+{
+ auto layout = new QHBoxLayout();
+ layout->addWidget(new QLabel(shortcut.toString()));
+ auto* deleteButton = new QPushButton("Delete", this);
+ connect(deleteButton, &QPushButton::clicked, this, &ShortcutDisplay::removeOnClicked);
+ layout->addWidget(deleteButton);
+ setLayout(layout);
+}
+
+void ShortcutDisplay::removeOnClicked()
+{
+ emit removeClicked(m_binding);
+}
+
ShortcutCollector::ShortcutCollector(QWidget* parent, ActionType action, Shortcuts& shortcuts) :
- QWidget(parent), m_action{action}, m_shortcuts{shortcuts}
+ QWidget(parent), m_action{action}, m_shortcuts{shortcuts}, m_bindings{}
{
- m_layout = new QGridLayout();
+ m_layout = new QVBoxLayout();
+ auto headerLayout = new QHBoxLayout();
auto* name = new QLabel(actionTypeToString(m_action));
- m_layout->addWidget(name, 0, 0);
+ headerLayout->addWidget(name);
m_shortcutEditor = new QKeySequenceEdit(this);
- m_layout->addWidget(m_shortcutEditor, 0, 1);
- m_shortcutTable = new QTableWidget(this);
- m_layout->addWidget(m_shortcutTable);
+ m_shortcutEditor->setMaximumSequenceLength(1);
+ connect(m_shortcutEditor, &QKeySequenceEdit::editingFinished, this, &ShortcutCollector::checkBinding);
+ headerLayout->addWidget(m_shortcutEditor);
- auto shortcutOptions = m_shortcuts.shortcuts(m_action);
- if (shortcutOptions != std::nullopt) {
- m_shortcutTable->setColumnCount(2);
- m_actionList = *shortcutOptions;
+ m_acceptButton = new QPushButton("Accept", this);
+ m_acceptButton->setEnabled(false);
+ connect(m_acceptButton, &QPushButton::clicked, this, &ShortcutCollector::addBinding);
+ headerLayout->addWidget(m_acceptButton);
+ headerLayout->addStretch();
- m_shortcutTable->setRowCount(static_cast<int>(m_actionList.size()));
+ m_layout->addLayout(headerLayout);
- for (auto index = 0; index < m_actionList.size(); ++index) {
- const auto& shortcut = m_actionList[index];
- m_shortcutTable->setCellWidget(index, 0, new QLabel(shortcut.toString()));
- auto* deleteButton = new QPushButton("Delete", this);
- m_shortcutTable->setCellWidget(index, 1, deleteButton);
- connect(m_shortcutTable, &QTableWidget::cellClicked, this, &ShortcutCollector::removeShortcut);
+ auto shortcutOptions = m_shortcuts.shortcuts(m_action);
+ if (shortcutOptions != std::nullopt) {
+ for (const auto& binding : *shortcutOptions) {
+ createBinding(binding);
}
} else {
qWarning("No shortcuts found for %s", actionTypeToString(action));
@@ -309,15 +321,66 @@ ShortcutCollector::ShortcutCollector(QWidget* parent, ActionType action, Shortcu
setLayout(m_layout);
}
-void ShortcutCollector::removeShortcut(int row, int)
+void ShortcutCollector::createBinding(QKeySequence binding)
{
- auto keySequence = m_actionList[row];
- auto result = m_shortcuts.remove(keySequence);
- if (result == FudStatus::Success) {
- m_actionList.removeAt(row);
+ auto displayItem = new ShortcutDisplay(this, binding);
+ m_bindings[binding] = displayItem;
+ m_layout->addWidget(displayItem);
+ connect(displayItem, &ShortcutDisplay::removeClicked, this, &ShortcutCollector::removeBinding);
+}
+
+void ShortcutCollector::checkBinding()
+{
+ auto keySequence = m_shortcutEditor->keySequence();
+ if (keySequence == QKeySequence::UnknownKey) {
+ m_acceptButton->setEnabled(false);
+ return;
+ }
+
+ m_acceptButton->setEnabled(not m_shortcuts.contains(keySequence));
+}
+
+void ShortcutCollector::addBinding()
+{
+ auto keySequence = m_shortcutEditor->keySequence();
+ if (keySequence == QKeySequence::UnknownKey) {
+ qWarning("Invalid state - can't accept unknown key");
+ return;
+ } else if (m_shortcuts.contains(keySequence)) {
+ qWarning("Shortcut %s already bound", qPrintable(keySequence.toString()));
+ return;
+ }
+ auto result = m_shortcuts.bind(m_action, keySequence);
+ if (result != FudStatus::Success) {
+ qCritical("Error binding %s to action %s", qPrintable(keySequence.toString()), actionTypeToString(m_action));
+ return;
+ }
+ if (m_bindings.contains(keySequence)) {
+ qWarning("binding %s to action %s already exists", qPrintable(keySequence.toString()), actionTypeToString(m_action));
+ m_layout->removeWidget(m_bindings[keySequence]);
+ delete m_bindings[keySequence];
+ }
+
+ createBinding(keySequence);
+ m_shortcutEditor->clear();
+}
+
+void ShortcutCollector::removeBinding(QKeySequence binding)
+{
+ auto result = m_shortcuts.remove(binding);
+ if (result == FudStatus::NotFound) {
+ qWarning("binding %s not found", qPrintable(binding.toString()));
+ } else if (result != FudStatus::Success) {
+ qWarning("error removing binding %s: %s", qPrintable(binding.toString()), FudStatusToString(result));
+ }
+ auto bindingHandle = m_bindings.extract(binding);
+ if (bindingHandle) {
+ m_layout->removeWidget(bindingHandle.mapped());
+ delete bindingHandle.mapped();
} else {
- qCritical("What");
+ qWarning("Could not remove widget!");
}
+ checkBinding();
}
Settings::Settings(QWidget* parent, Shortcuts&& shortcuts) : QDialog{parent}, m_shortcuts{std::move(shortcuts)}
@@ -329,11 +392,32 @@ Settings::Settings(QWidget* parent, Shortcuts&& shortcuts) : QDialog{parent}, m_
return;
}
+ auto containerLayout = new QHBoxLayout();
+ auto* columnLayout = new QVBoxLayout();
+ size_t counter{0};
+ constexpr size_t maxEntriesPerColumn{4};
+
for (const auto& action : m_shortcuts.actions()) {
auto* collector = new ShortcutCollector(this, action, m_shortcuts);
- layout->addWidget(collector);
+ columnLayout->addWidget(collector);
+ counter++;
+ if (counter % (maxEntriesPerColumn + 1) == 0) {
+ containerLayout->addLayout(columnLayout);
+ columnLayout = new QVBoxLayout();
+ }
}
+ if (columnLayout->count() > 0) {
+ containerLayout->addLayout(columnLayout);
+ }
+
+ layout->addLayout(containerLayout);
+
+ auto* dialogButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
+ connect(dialogButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
+ connect(dialogButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
+ layout->addWidget(dialogButtonBox);
+
setLayout(layout);
}