aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2022-09-29 23:53:42 -0400
committerGravatar Chris Xiong <chirs241097@gmail.com> 2022-09-29 23:53:42 -0400
commit637334ba36d44239d501f13310dc4aa61fc45216 (patch)
treec8fe46a098eac8e2618bb5c5e515cde44ab67c71
parent54b0bb3e4225a301188d8518bc6d26328a7a7aa2 (diff)
downloaddeduper-637334ba36d44239d501f13310dc4aa61fc45216.tar.xz
Customizable shortcuts, maybe.
-rw-r--r--qdeduper/mingui.cpp33
-rw-r--r--qdeduper/preferencedialog.cpp68
-rw-r--r--qdeduper/preferencedialog.hpp20
3 files changed, 120 insertions, 1 deletions
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 <QInputDialog>
#include <QDesktopServices>
-using std::size_t;
const std::vector<int> 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<std::string, QKeySequence> 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 <functional>
+
+#include <QDebug>
#include <QLabel>
#include <QTabWidget>
#include <QGridLayout>
@@ -6,6 +9,10 @@
#include <QDialogButtonBox>
#include <QCheckBox>
#include <QVBoxLayout>
+#include <QAction>
+#include <QTableView>
+#include <QStandardItemModel>
+#include <QKeySequenceEdit>
#include "preferencedialog.hpp"
#include "settings.hpp"
@@ -99,6 +106,19 @@ void PreferenceDialog::setup_widgets()
}
}
+void PreferenceDialog::set_hkactions(int tab, std::map<std::string, QKeySequence> defmap, std::map<std::string, QAction*> 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<QKeySequence>(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<QKeySequence>();
+ 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<ShortcutEditorDelegate*>(this)->commitData(kse);
+ Q_EMIT const_cast<ShortcutEditorDelegate*>(this)->closeEditor(kse);
+ });
+ return kse;
+}
+
+void ShortcutEditorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
+{
+ QKeySequenceEdit *kse = qobject_cast<QKeySequenceEdit*>(editor);
+ kse->setKeySequence(index.data(Qt::ItemDataRole::UserRole).value<QKeySequence>());
+}
+
+void ShortcutEditorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
+{
+ QKeySequenceEdit *kse = qobject_cast<QKeySequenceEdit*>(editor);
+ model->setData(index, QVariant::fromValue<QKeySequence>(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 <vector>
+#include <map>
+#include <string>
#include <QDialog>
#include <QGridLayout>
+#include <QStyledItemDelegate>
#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<std::string, QKeySequence> defmap, std::map<std::string, QAction*> 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<QGridLayout*> tabs;
QDialogButtonBox *bb;
+ QTableView *hktv = nullptr;
+ QStandardItemModel *hkim = nullptr;
+ std::map<std::string, QKeySequence> defmap;
+ std::map<std::string, QAction*> 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