Skip to content

Commit

Permalink
UX: handle boxes widget key events #324
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rodlie committed Dec 13, 2024
1 parent 646ea6b commit 4855bba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/app/GUI/BoxesList/boxsinglewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/app/GUI/BoxesList/boxsinglewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
24 changes: 24 additions & 0 deletions src/app/GUI/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/app/GUI/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class MainWindow : public QMainWindow
stdsptr<void> lock();

bool processKeyEvent(QKeyEvent *event);
bool processCanvasWindowKeyEvent(QKeyEvent *event);

void installNumericFilter(QObject* const object) {
object->installEventFilter(mNumericFilter);
Expand Down

0 comments on commit 4855bba

Please sign in to comment.