From 67c8f5c5cf21779a4d84d3a05e659d818c151599 Mon Sep 17 00:00:00 2001 From: ArsenArsen Date: Tue, 27 Jun 2017 12:46:35 +0200 Subject: [PATCH] Better hotkey input --- KShare.pro | 9 ++++-- hotkeyinputdialog.cpp | 37 ++++++++++++++++++++++ hotkeyinputdialog.hpp | 31 ++++++++++++++++++ hotkeyinputdialog.ui | 74 +++++++++++++++++++++++++++++++++++++++++++ main.cpp | 2 +- settingsdialog.cpp | 9 +++--- 6 files changed, 154 insertions(+), 8 deletions(-) create mode 100644 hotkeyinputdialog.cpp create mode 100644 hotkeyinputdialog.hpp create mode 100644 hotkeyinputdialog.ui diff --git a/KShare.pro b/KShare.pro index 6ed64ca..6f081a4 100644 --- a/KShare.pro +++ b/KShare.pro @@ -62,7 +62,8 @@ SOURCES += main.cpp\ aboutbox.cpp \ cropeditor/drawing/eraseritem.cpp \ cropeditor/drawing/rectitem.cpp \ - cropeditor/drawing/ellipseitem.cpp + cropeditor/drawing/ellipseitem.cpp \ + hotkeyinputdialog.cpp HEADERS += mainwindow.hpp \ cropeditor/cropeditor.hpp \ @@ -103,7 +104,8 @@ HEADERS += mainwindow.hpp \ aboutbox.hpp \ cropeditor/drawing/eraseritem.hpp \ cropeditor/drawing/rectitem.hpp \ - cropeditor/drawing/ellipseitem.hpp + cropeditor/drawing/ellipseitem.hpp \ + hotkeyinputdialog.hpp LIBS += -lavcodec -lavformat -lavutil -lswscale -lavutil @@ -131,7 +133,8 @@ FORMS += mainwindow.ui \ cropeditor/settings/blurdialog.ui \ recording/encoders/encodersettingsdialog.ui \ settingsdialog.ui \ - aboutbox.ui + aboutbox.ui \ + hotkeyinputdialog.ui DISTFILES += \ README.md \ diff --git a/hotkeyinputdialog.cpp b/hotkeyinputdialog.cpp new file mode 100644 index 0000000..deb9103 --- /dev/null +++ b/hotkeyinputdialog.cpp @@ -0,0 +1,37 @@ +#include "hotkeyinputdialog.hpp" +#include "ui_hotkeyinputdialog.h" +#include +#include +#include + +HotkeyInputDialog::HotkeyInputDialog(QString hotkeyName, QKeySequence currentSeq, QWidget *parent) +: QDialog(parent), ui(new Ui::HotkeyInputDialog) { + ui->setupUi(this); + ui->keySeq->setText(currentSeq.toString()); + setWindowTitle(hotkeyName); + connect(this, &QDialog::accepted, [&] { + QKeySequence s(ui->keySeq->text()); + if (!s.toString().isEmpty()) emit sequenceSelected(s, windowTitle()); + }); +} + +HotkeyInputDialog::~HotkeyInputDialog() { + delete ui; +} + +void HotkeyInputDialog::keyPressEvent(QKeyEvent *e) { + if (e->key() == Qt::Key_Shift || e->key() == Qt::Key_Control || e->key() == Qt::Key_Alt || e->key() == Qt::Key_AltGr + || e->key() == Qt::Key_Context1 || e->key() == Qt::Key_Context2 || e->key() == Qt::Key_Context3 || e->key() == Qt::Key_Context4) + return; + if (recording) { + QKeySequence seq(e->modifiers() + e->key()); + ui->keySeq->setText(seq.toString()); + recording = false; + ui->recordButton->setText("Record"); + } +} + +void HotkeyInputDialog::on_recordButton_clicked() { + recording = !recording; + ui->recordButton->setText(recording ? "Stop recording" : "Record"); +} diff --git a/hotkeyinputdialog.hpp b/hotkeyinputdialog.hpp new file mode 100644 index 0000000..155aca4 --- /dev/null +++ b/hotkeyinputdialog.hpp @@ -0,0 +1,31 @@ +#ifndef HOTKEYINPUTDIALOG_HPP +#define HOTKEYINPUTDIALOG_HPP + +#include + +namespace Ui { +class HotkeyInputDialog; +} + +class HotkeyInputDialog : public QDialog { + Q_OBJECT + +public: + explicit HotkeyInputDialog(QString hotkeyName, QKeySequence currentSeq, QWidget *parent = 0); + ~HotkeyInputDialog(); + +signals: + void sequenceSelected(QKeySequence seq, QString name); + +protected: + void keyPressEvent(QKeyEvent *e) override; + +private slots: + void on_recordButton_clicked(); + +private: + bool recording = false; + Ui::HotkeyInputDialog *ui; +}; + +#endif // HOTKEYINPUTDIALOG_HPP diff --git a/hotkeyinputdialog.ui b/hotkeyinputdialog.ui new file mode 100644 index 0000000..859ec05 --- /dev/null +++ b/hotkeyinputdialog.ui @@ -0,0 +1,74 @@ + + + HotkeyInputDialog + + + + 0 + 0 + 255 + 80 + + + + Dialog + + + + + + + + + Record + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + HotkeyInputDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + HotkeyInputDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/main.cpp b/main.cpp index 5e870dd..d2d8c0f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include "mainwindow.hpp" #include "screenshotutil.hpp" +#include "ui_mainwindow.h" #include #include #include @@ -11,7 +12,6 @@ extern "C" { #include #include } -#include "ui_mainwindow.h" #include #include #include diff --git a/settingsdialog.cpp b/settingsdialog.cpp index 062990d..a00ec11 100644 --- a/settingsdialog.cpp +++ b/settingsdialog.cpp @@ -1,4 +1,5 @@ #include "settingsdialog.hpp" +#include "hotkeyinputdialog.hpp" #include "mainwindow.hpp" #include "ui_settingsdialog.h" @@ -111,10 +112,10 @@ void SettingsDialog::on_hotkeys_doubleClicked(const QModelIndex &) { if (ui->hotkeys->selectedItems().length() == 1) { QListWidgetItem *i = ui->hotkeys->selectedItems().at(0); QString str = i->data(Qt::UserRole + 1).toString(); - bool ok; - QString seq = QInputDialog::getText(MainWindow::inst(), "Hotkey Input", "Insert hotkey:", QLineEdit::Normal, - hotkeying::sequence(str), &ok); - if (ok && hotkeying::valid(seq)) hotkeying::hotkey(str, QKeySequence(seq), fncs.value(str)); + HotkeyInputDialog *hotkey = new HotkeyInputDialog(str, hotkeying::sequence(str), this); + connect(hotkey, &HotkeyInputDialog::sequenceSelected, + [&](QKeySequence seq, QString name) { hotkeying::hotkey(name, seq, fncs.value(name)); }); + hotkey->show(); } }