Skip to content

Commit

Permalink
src/gui/definition/definitionwidget: add shortcuts to jump definitions
Browse files Browse the repository at this point in the history
Add Ctrl+Up and Ctrl+Down to jump to the previous and next definitions
respectively. This gives the definition widget focus so it may capture
some key events instead of mpv doing it.

Closes #263
  • Loading branch information
ripose-jp committed Feb 23, 2025
1 parent 514dd0c commit 22a4cce
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 5 deletions.
89 changes: 89 additions & 0 deletions src/gui/widgets/definition/definitionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ DefinitionWidget::DefinitionWidget(bool showNavigation, QWidget *parent)
initTheme();
initSearch();
initAudioSources();
initShortcuts();
initSignals();
}

Expand Down Expand Up @@ -211,6 +212,27 @@ void DefinitionWidget::initAudioSources()
);
}

void DefinitionWidget::initShortcuts()
{
m_shortcutSkipPrev =
new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Up), this);
m_shortcutSkipPrev->setContext(Qt::WidgetWithChildrenShortcut);
connect(
m_shortcutSkipPrev, &QShortcut::activated,
this, &DefinitionWidget::skipPrev,
Qt::QueuedConnection
);

m_shortcutSkipNext =
new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Down), this);
m_shortcutSkipNext->setContext(Qt::WidgetWithChildrenShortcut);
connect(
m_shortcutSkipNext, &QShortcut::activated,
this, &DefinitionWidget::skipNext,
Qt::QueuedConnection
);
}

void DefinitionWidget::initSignals()
{
GlobalMediator *mediator = GlobalMediator::getGlobalMediator();
Expand Down Expand Up @@ -469,6 +491,8 @@ void DefinitionWidget::showKanji(QSharedPointer<const Kanji> kanji)
);
m_ui->scrollAreaContents->layout()->addWidget(kanjiWidget);
m_ui->scrollArea->verticalScrollBar()->setValue(0);

m_kanjiShown = true;
}

void DefinitionWidget::hideKanji()
Expand All @@ -488,6 +512,8 @@ void DefinitionWidget::hideKanji()
}
QApplication::processEvents();
m_ui->scrollArea->verticalScrollBar()->setValue(m_savedScroll);

m_kanjiShown = false;
}

/* End Kanji Helpers */
Expand Down Expand Up @@ -571,3 +597,66 @@ bool DefinitionWidget::positionChild(const QPoint &pos)
#undef VERTICAL_OFFSET

/* End Child Handlers */
/* Begin Shortcut Helpers */

void DefinitionWidget::skipPrev()
{
constexpr int ITER_SKIP = 2;

if (m_kanjiShown)
{
return;
}

int scrollY = m_ui->scrollArea->verticalScrollBar()->value();
QLayout *scrollLayout = m_ui->scrollAreaContents->layout();
int prevY = 0;
for (int i = 1; i < scrollLayout->count(); i += ITER_SKIP)
{
QWidget *widget = scrollLayout->itemAt(i)->widget();
if (widget == nullptr)
{
break;
}
int currY = widget->mapToParent(QPoint(0, 0)).y();
if (prevY <= scrollY && scrollY <= currY)
{
m_ui->scrollArea->verticalScrollBar()->setValue(prevY);
return;
}
prevY = currY;
}

m_ui->scrollArea->verticalScrollBar()->setValue(prevY);
}

void DefinitionWidget::skipNext()
{
constexpr int ITER_SKIP = 2;

if (m_kanjiShown)
{
return;
}

int scrollY = m_ui->scrollArea->verticalScrollBar()->value();
QLayout *scrollLayout = m_ui->scrollAreaContents->layout();
int prevY = 0;
for (int i = 1; i < scrollLayout->count(); i += ITER_SKIP)
{
QWidget *widget = scrollLayout->itemAt(i)->widget();
if (widget == nullptr)
{
break;
}
int currY = widget->mapToParent(QPoint(0, 0)).y();
if (prevY <= scrollY && scrollY < currY)
{
m_ui->scrollArea->verticalScrollBar()->setValue(currY);
return;
}
prevY = currY;
}
}

/* End Shortcut Helpers */
25 changes: 25 additions & 0 deletions src/gui/widgets/definition/definitionwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QWidget>

#include <QPointer>
#include <QShortcut>
#include <QWheelEvent>

#include "definitionstate.h"
Expand Down Expand Up @@ -100,6 +101,11 @@ private Q_SLOTS:
*/
void initAudioSources();

/**
* Initializes keyboard shortcuts.
*/
void initShortcuts();

/**
* Initializes settings signals.
*/
Expand Down Expand Up @@ -157,6 +163,16 @@ private Q_SLOTS:
*/
bool positionChild(const QPoint &pos);

/**
* Skips to the previous entry in the scroll area.
*/
void skipPrev();

/**
* Skips to the next entry in the scroll area.
*/
void skipNext();

protected:
/**
* Emits a signal that the Definition Widget has been hidden.
Expand Down Expand Up @@ -217,11 +233,20 @@ private Q_SLOTS:
*/
int m_savedScroll = 0;

/* True if a subsearch kanji is shown, false otherwise */
bool m_kanjiShown = false;

/* Current search ID. Used to prevent erroneous signals */
int m_searchId = 0;

/* The child definition widget */
QPointer<DefinitionWidget> m_child = nullptr;

/* Shortcut to skip to the previous entry */
QShortcut *m_shortcutSkipPrev = nullptr;

/* Shortcut to skip to the next entry */
QShortcut *m_shortcutSkipNext = nullptr;
};

#endif // DEFINITIONWIDGET_H
13 changes: 8 additions & 5 deletions src/gui/widgets/definition/definitionwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::FocusPolicy::ClickFocus</enum>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
Expand All @@ -26,7 +29,7 @@
<string notr="true"/>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item alignment="Qt::AlignRight">
<item alignment="Qt::AlignmentFlag::AlignRight">
<widget class="QWidget" name="widgetNav" native="true">
<property name="toolTip">
<string>Close</string>
Expand Down Expand Up @@ -75,13 +78,13 @@
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
<enum>Qt::FocusPolicy::NoFocus</enum>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
<enum>QFrame::Shape::StyledPanel</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
<enum>Qt::ScrollBarPolicy::ScrollBarAlwaysOff</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
Expand All @@ -91,7 +94,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>484</width>
<width>480</width>
<height>344</height>
</rect>
</property>
Expand Down
6 changes: 6 additions & 0 deletions src/gui/widgets/definition/glossarylabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,12 @@ void GlossaryLabel::mousePressEvent(QMouseEvent *event)
event->ignore();
}

void GlossaryLabel::keyPressEvent(QKeyEvent *event)
{
QTextEdit::keyPressEvent(event);
event->ignore();
}

/* End Event Handlers */
/* Begin Worker Implementation */

Expand Down
6 changes: 6 additions & 0 deletions src/gui/widgets/definition/glossarylabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public Q_SLOTS:
*/
void mousePressEvent(QMouseEvent *event) override;

/**
* Ignore the key press event.
* @param event The key event.
*/
void keyPressEvent(QKeyEvent *event) override;

private Q_SLOTS:
/**
* Adjust the size of the label.
Expand Down
9 changes: 9 additions & 0 deletions src/gui/widgets/overlay/playeroverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ PlayerOverlay::PlayerOverlay(QWidget *parent) : QStackedLayout(parent)
mediator, &GlobalMediator::definitionsShown,
Qt::QueuedConnection
);
connect(
m_definition,
&DefinitionWidget::widgetShown,
m_definition,
[this] () {
m_definition->setFocus(Qt::FocusReason::PopupFocusReason);
},
Qt::QueuedConnection
);
connect(
m_definition, &DefinitionWidget::widgetHidden,
&m_hideTimer, qOverload<>(&QTimer::start),
Expand Down

0 comments on commit 22a4cce

Please sign in to comment.