Skip to content

Commit

Permalink
allow global keyboard shortcuts in all widgets by default
Browse files Browse the repository at this point in the history
Master filters events in all widgets. For key press events, it is
checked whether the widget handles the event itself. If not, Master
handles it.
  • Loading branch information
stiglers-eponym committed Sep 2, 2022
1 parent 8c4f5d5 commit 50a33d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/gui/thumbnailbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void ThumbnailButton::mouseReleaseEvent(QMouseEvent *event)

void ThumbnailButton::focusInEvent(QFocusEvent*)
{
setFrameStyle(QFrame::Panel);
setFrameStyle(QFrame::Panel | QFrame::Plain);
setLineWidth(2);
QPalette palette;
palette.setColor(QPalette::WindowText, Qt::red);
Expand Down
19 changes: 14 additions & 5 deletions src/master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@ QWidget* Master::createWidget(QJsonObject &object, QWidget *parent)
// Mute slides by default, except if they are marked as master.
if (!object.value("mute").toBool(!object.value("master").toBool(false)))
scene->flags() &= ~SlideScene::MuteSlide;
connect(slide, &SlideView::sendKeyEvent, this, &Master::receiveKeyEvent);
connect(slide, &SlideView::sendAction, this, &Master::handleAction);
connect(scene, &SlideScene::navigationToViews, slide, &SlideView::pageChanged, Qt::DirectConnection);
widget = slide;
Expand Down Expand Up @@ -605,6 +604,7 @@ QWidget* Master::createWidget(QJsonObject &object, QWidget *parent)
}
if (widget)
{
widget->installEventFilter(this);
// Add keyboard shortcut.
if (object.contains("keys"))
{
Expand Down Expand Up @@ -641,17 +641,24 @@ void Master::showAll() const
}
}

void Master::receiveKeyEvent(const QKeyEvent* event)
bool Master::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() != QEvent::KeyPress)
return QObject::eventFilter(obj, event);
event->ignore();
obj->event(event);
if (event->isAccepted())
return true;
const auto *kevent = static_cast<QKeyEvent*>(event);
#if (QT_VERSION_MAJOR >= 6)
const QKeySequence key_code(event->keyCombination());
const QKeySequence key_code(kevent->keyCombination());
#else
const quint32 key_code = event->key() | (event->modifiers() & ~Qt::KeypadModifier);
const quint32 key_code = kevent->key() | (kevent->modifiers() & ~Qt::KeypadModifier);
#endif
// Search shortcuts for given key sequence.
{
QWidget* widget = shortcuts.value(key_code);
debug_msg(DebugKeyInput, "Key action:" << widget << event << (event->key() | (event->modifiers() & ~Qt::KeypadModifier)));
debug_msg(DebugKeyInput, "Key action:" << widget << kevent << (kevent->key() | (kevent->modifiers() & ~Qt::KeypadModifier)));
if (widget)
{
widget->show();
Expand Down Expand Up @@ -692,6 +699,8 @@ void Master::receiveKeyEvent(const QKeyEvent* event)
setTool(new Tool(*tool));
}
}
event->accept();
return true;
}

void Master::nextSlide() const noexcept
Expand Down
8 changes: 4 additions & 4 deletions src/master.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ class Master : public QObject
/// Get open file name from QFileDialog
static QString getOpenFileName();

public slots:
/// Handle key events collected by any other object.
/// By default all key events are redirected to this and handled here.
void receiveKeyEvent(const QKeyEvent* event);
protected:
/// Filter key input events from other widgets
bool eventFilter(QObject *obj, QEvent *event) override;

public slots:
/// Read memory size restriction from preferences and distribute memory to pixcaches.
void distributeMemory();

Expand Down

0 comments on commit 50a33d5

Please sign in to comment.