From 4855bba981f93171c89fc96b8932be26e53bdc1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole-Andr=C3=A9=20Rodlie?= Date: Fri, 13 Dec 2024 23:05:51 +0100 Subject: [PATCH] UX: handle boxes widget key events #324 1. when entering boxes widget we set focus to get key events (clear focus when leaving) 2. if hover state is true we forward key events to main window 3. main window will process supported key events for current canvas --- src/app/GUI/BoxesList/boxsinglewidget.cpp | 10 ++++++++++ src/app/GUI/BoxesList/boxsinglewidget.h | 2 ++ src/app/GUI/mainwindow.cpp | 24 +++++++++++++++++++++++ src/app/GUI/mainwindow.h | 1 + 4 files changed, 37 insertions(+) diff --git a/src/app/GUI/BoxesList/boxsinglewidget.cpp b/src/app/GUI/BoxesList/boxsinglewidget.cpp index 90490ef9a..002f054e3 100644 --- a/src/app/GUI/BoxesList/boxsinglewidget.cpp +++ b/src/app/GUI/BoxesList/boxsinglewidget.cpp @@ -736,16 +736,26 @@ void BoxSingleWidget::mouseReleaseEvent(QMouseEvent *event) void BoxSingleWidget::enterEvent(QEvent *) { + setFocus(); mHover = true; update(); } void BoxSingleWidget::leaveEvent(QEvent *) { + clearFocus(); mHover = false; update(); } +void BoxSingleWidget::keyPressEvent(QKeyEvent *event) +{ + if (mHover) { + MainWindow::sGetInstance()->processCanvasWindowKeyEvent(event); + } + SingleWidget::keyPressEvent(event); +} + void BoxSingleWidget::mouseDoubleClickEvent(QMouseEvent *e) { Q_UNUSED(e) diff --git a/src/app/GUI/BoxesList/boxsinglewidget.h b/src/app/GUI/BoxesList/boxsinglewidget.h index ae411ed21..634f2d8d5 100644 --- a/src/app/GUI/BoxesList/boxsinglewidget.h +++ b/src/app/GUI/BoxesList/boxsinglewidget.h @@ -104,6 +104,8 @@ class BoxSingleWidget : public SingleWidget { void enterEvent(QEvent *); void leaveEvent(QEvent *); + void keyPressEvent(QKeyEvent *event); + void paintEvent(QPaintEvent *); void mouseDoubleClickEvent(QMouseEvent *e); void resizeEvent(QResizeEvent *); diff --git a/src/app/GUI/mainwindow.cpp b/src/app/GUI/mainwindow.cpp index c87d37c78..6c8ffe9d1 100644 --- a/src/app/GUI/mainwindow.cpp +++ b/src/app/GUI/mainwindow.cpp @@ -1644,6 +1644,30 @@ bool MainWindow::processKeyEvent(QKeyEvent *event) return false; } +bool MainWindow::processCanvasWindowKeyEvent(QKeyEvent *event) +{ +#ifdef Q_OS_MAC + if (event->type() == QEvent::ShortcutOverride) { return false; } +#endif + const bool ctrl = event->modifiers() & Qt::ControlModifier; + if (ctrl && event->key() == Qt::Key_V) { + if (event->isAutoRepeat()) { return false; } + (*mActions.pasteAction)(); + } else if (ctrl && event->key() == Qt::Key_C) { + if (event->isAutoRepeat()) { return false; } + (*mActions.copyAction)(); + } else if (ctrl && event->key() == Qt::Key_D) { + if (event->isAutoRepeat()) { return false; } + (*mActions.duplicateAction)(); + } else if (ctrl && event->key() == Qt::Key_X) { + if (event->isAutoRepeat()) { return false; } + (*mActions.cutAction)(); + } else if (event->key() == Qt::Key_Delete) { + (*mActions.deleteAction)(); + } else { return false; } + return true; +} + void MainWindow::readSettings(const QString &openProject) { mUI->readSettings(); diff --git a/src/app/GUI/mainwindow.h b/src/app/GUI/mainwindow.h index cb21268c2..a2797f36b 100644 --- a/src/app/GUI/mainwindow.h +++ b/src/app/GUI/mainwindow.h @@ -129,6 +129,7 @@ class MainWindow : public QMainWindow stdsptr lock(); bool processKeyEvent(QKeyEvent *event); + bool processCanvasWindowKeyEvent(QKeyEvent *event); void installNumericFilter(QObject* const object) { object->installEventFilter(mNumericFilter);