From 637334ba36d44239d501f13310dc4aa61fc45216 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Thu, 29 Sep 2022 23:53:42 -0400 Subject: Customizable shortcuts, maybe. --- qdeduper/mingui.cpp | 33 ++++++++++++++++++++- qdeduper/preferencedialog.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++ qdeduper/preferencedialog.hpp | 20 +++++++++++++ 3 files changed, 120 insertions(+), 1 deletion(-) diff --git a/qdeduper/mingui.cpp b/qdeduper/mingui.cpp index 8d5d10a..774aa1c 100644 --- a/qdeduper/mingui.cpp +++ b/qdeduper/mingui.cpp @@ -48,13 +48,31 @@ #include #include -using std::size_t; const std::vector keys = { Qt::Key::Key_A, Qt::Key::Key_S, Qt::Key::Key_D, Qt::Key::Key_F, Qt::Key::Key_G, Qt::Key::Key_H, Qt::Key::Key_J, Qt::Key::Key_K, Qt::Key::Key_L, Qt::Key::Key_Semicolon, Qt::Key::Key_T, Qt::Key::Key_Y, Qt::Key::Key_U, Qt::Key::Key_I, Qt::Key::Key_O, Qt::Key::Key_P }; +const std::map defhk = { + {"00_create_db", QKeySequence(Qt::Modifier::CTRL | Qt::Key::Key_T)}, + {"01_load_db", QKeySequence(Qt::Modifier::CTRL | Qt::Key::Key_O)}, + {"02_save_db", QKeySequence(Qt::Modifier::CTRL | Qt::Key::Key_W)}, + {"03_save_list", QKeySequence(Qt::Modifier::CTRL | Qt::Key::Key_E)}, + {"04_load_list", QKeySequence(Qt::Modifier::CTRL | Qt::Key::Key_R)}, + {"05_search_image", QKeySequence(Qt::Modifier::CTRL | Qt::Key::Key_Slash)}, + {"06_preferences", QKeySequence(Qt::Modifier::CTRL | Qt::Key::Key_P)}, + {"07_exit", QKeySequence(Qt::Modifier::CTRL | Qt::Key::Key_Q)}, + {"08_next_group", QKeySequence(Qt::Key::Key_M)}, + {"09_prev_group", QKeySequence(Qt::Key::Key_Z)}, + {"10_skip_group", QKeySequence(Qt::Key::Key_B)}, + {"11_single_mode_toggle", QKeySequence()}, + {"12_mark_all", QKeySequence(Qt::Key::Key_X)}, + {"13_mark_all_dir", QKeySequence()}, + {"14_mark_all_dir_rec", QKeySequence()}, + {"15_view_marked", QKeySequence()}, +}; + QString fsstr_to_qstring(const fs::path::string_type &s) { @@ -149,9 +167,16 @@ DeduperMainWindow::DeduperMainWindow() sr->register_bool_option(generalt, "show_memory_usage", "Show Database Engine Memory Usage", false); int sigt = sr->register_tab("Signature"); sr->register_double_option(sigt, "signature/threshold", "Distance Threshold", 0, 1, 0.3); + int hkt = sr->register_tab("Shortcuts"); + for (auto &hkp : defhk) + { + std::string hkn = hkp.first.substr(3); + sr->register_keyseq_option(hkt, "hotkey/" + hkn, QString(), hkp.second); + } prefdlg = new PreferenceDialog(sr, this); prefdlg->setModal(true); prefdlg->close(); + prefdlg->set_hkactions(hkt, defhk, menuact); QObject::connect(menuact["preferences"], &QAction::triggered, prefdlg, &PreferenceDialog::open); QObject::connect(prefdlg, &PreferenceDialog::accepted, this, &DeduperMainWindow::apply_prefs); apply_prefs(); @@ -784,6 +809,12 @@ void DeduperMainWindow::apply_prefs() this->rampupd->stop(); this->dbramusg->setText(QString()); } + for (auto &hkp : defhk) + { + std::string hkn = hkp.first.substr(3); + QKeySequence ks = sr->get_option_keyseq("hotkey/" + hkn); + menuact[hkn]->setShortcut(ks); + } } void DeduperMainWindow::update_memusg() diff --git a/qdeduper/preferencedialog.cpp b/qdeduper/preferencedialog.cpp index 6851634..28ccbea 100644 --- a/qdeduper/preferencedialog.cpp +++ b/qdeduper/preferencedialog.cpp @@ -1,3 +1,6 @@ +#include + +#include #include #include #include @@ -6,6 +9,10 @@ #include #include #include +#include +#include +#include +#include #include "preferencedialog.hpp" #include "settings.hpp" @@ -99,6 +106,19 @@ void PreferenceDialog::setup_widgets() } } +void PreferenceDialog::set_hkactions(int tab, std::map defmap, std::map actmap) +{ + this->defmap = defmap; + this->actmap = actmap; + this->hktv = new QTableView(); + this->hkim = new QStandardItemModel(); + this->tabs[tab]->addWidget(hktv, 0, 0); + this->hktv->setModel(hkim); + this->hktv->setSortingEnabled(false); + this->hktv->setSelectionMode(QAbstractItemView::SelectionMode::NoSelection); + this->hktv->setItemDelegateForColumn(1, new ShortcutEditorDelegate); +} + void PreferenceDialog::load_widget_status() { for (auto &k : sr->klist) @@ -132,6 +152,24 @@ void PreferenceDialog::load_widget_status() break; } } + this->hkim->clear(); + this->hkim->setHorizontalHeaderLabels({"Menu Item", "Hotkey"}); + for (auto &hkp : this->defmap) + { + std::string actn = hkp.first.substr(3); + QKeySequence ks = sr->get_option_keyseq("hotkey/" + actn); + if (this->actmap.find(actn) == this->actmap.end()) + continue; + QAction *act = this->actmap[actn]; + QStandardItem *itma = new QStandardItem(act->text()); + QStandardItem *itmk = new QStandardItem(act->shortcut().toString()); + itma->setIcon(act->icon()); + itma->setEditable(false); + itma->setData(QString::fromStdString(actn), Qt::ItemDataRole::UserRole); + itmk->setData(QVariant::fromValue(ks), Qt::ItemDataRole::UserRole); + this->hkim->appendRow({itma, itmk}); + } + this->hktv->resizeColumnsToContents(); } void PreferenceDialog::save_widget_status() @@ -167,4 +205,34 @@ void PreferenceDialog::save_widget_status() break; } } + for (int i = 0; i < hkim->rowCount(); ++i) + { + std::string actn = hkim->item(i, 0)->data(Qt::ItemDataRole::UserRole).toString().toStdString(); + QKeySequence ks = hkim->item(i, 1)->data(Qt::ItemDataRole::UserRole).value(); + sr->set_option_keyseq("hotkey/" + actn, ks); + } +} +QWidget* ShortcutEditorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + Q_UNUSED(option); + Q_UNUSED(index); + QKeySequenceEdit *kse = new QKeySequenceEdit(parent); + QObject::connect(kse, &QKeySequenceEdit::editingFinished, [this, kse] { + Q_EMIT const_cast(this)->commitData(kse); + Q_EMIT const_cast(this)->closeEditor(kse); + }); + return kse; +} + +void ShortcutEditorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const +{ + QKeySequenceEdit *kse = qobject_cast(editor); + kse->setKeySequence(index.data(Qt::ItemDataRole::UserRole).value()); +} + +void ShortcutEditorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const +{ + QKeySequenceEdit *kse = qobject_cast(editor); + model->setData(index, QVariant::fromValue(kse->keySequence()), Qt::ItemDataRole::UserRole); + model->setData(index, kse->keySequence().toString(), Qt::ItemDataRole::DisplayRole); } diff --git a/qdeduper/preferencedialog.hpp b/qdeduper/preferencedialog.hpp index 06977fd..8efefde 100644 --- a/qdeduper/preferencedialog.hpp +++ b/qdeduper/preferencedialog.hpp @@ -2,15 +2,20 @@ #define PREFERENCEDIALOG_HPP #include +#include +#include #include #include +#include #include "settings.hpp" class QTabWidget; class QGridLayout; class QDialogButtonBox; +class QTableView; +class QStandardItemModel; class PreferenceDialog : public QDialog { @@ -18,8 +23,10 @@ class PreferenceDialog : public QDialog public: PreferenceDialog(SettingsRegistry *sr, QWidget *parent = nullptr); void setup_widgets(); + void set_hkactions(int tab, std::map defmap, std::map actmap); void load_widget_status(); void save_widget_status(); + void open() override; void accept() override; private: @@ -27,6 +34,19 @@ private: QTabWidget *tw; std::vector tabs; QDialogButtonBox *bb; + QTableView *hktv = nullptr; + QStandardItemModel *hkim = nullptr; + std::map defmap; + std::map actmap; +}; + +class ShortcutEditorDelegate : public QStyledItemDelegate +{ + Q_OBJECT +public: + QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + void setEditorData(QWidget *editor, const QModelIndex &index) const override; + void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; }; #endif -- cgit v1.2.3