Skip to content

Commit 0e3d448

Browse files
committed
[feature/qt6_media_viewer] Add focus indicator
1 parent ade1752 commit 0e3d448

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

plugins/media_viewer/include/media_viewer/media_viewer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,19 @@ namespace hal
116116
/** @brief Stop playback and fully unload the current media. */
117117
void handleStopTriggered();
118118

119+
/** @brief Track focus transitions to drive the keyboard-focus indicator. */
120+
void handleFocusChanged(QWidget* old, QWidget* now);
121+
119122
private:
120123
/** @brief Return the tooltip text listing all keyboard shortcuts. */
121124
static QString shortcutHelpText();
122125

123126
/** @brief Stop playback, detach the media source, and reset all load-derived visual state. */
124127
void clearMedia();
125128

129+
/** @brief Draw the keyboard-focus outline when this widget (or a descendant) has focus. */
130+
void paintEvent(QPaintEvent* event) override;
131+
126132
/** @brief Clamp the current volume by delta into [0,100] and update slider + audio output. */
127133
void adjustVolume(int delta);
128134

@@ -166,5 +172,6 @@ namespace hal
166172
bool mUpdatingSlider = false;
167173
bool mMuted = false;
168174
bool mSeekingFirstFrame = false; // true while auto-playing to decode frame 0
175+
bool mHasFocusWithin = false;
169176
};
170177
} // namespace hal

plugins/media_viewer/src/media_viewer.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "hal_core/utilities/log.h"
3131

3232
#include <QAction>
33+
#include <QApplication>
3334
#include <QAudioOutput>
3435
#include <QComboBox>
3536
#include <QCursor>
@@ -38,6 +39,8 @@
3839
#include <QHBoxLayout>
3940
#include <QLabel>
4041
#include <QMediaPlayer>
42+
#include <QPaintEvent>
43+
#include <QPainter>
4144
#include <QShortcut>
4245
#include <QSizePolicy>
4346
#include <QSlider>
@@ -163,6 +166,7 @@ namespace hal
163166
// ---------------------------------------------------------------
164167
// Assemble content layout
165168
// ---------------------------------------------------------------
169+
mContentLayout->setContentsMargins(2, 2, 2, 2);
166170
mContentLayout->addWidget(mDisplayStack, 1);
167171
mContentLayout->addWidget(controlBar, 0);
168172

@@ -218,6 +222,7 @@ namespace hal
218222
connect(mPlayer, &QMediaPlayer::durationChanged, this, &MediaViewer::handleDurationChanged);
219223
connect(mPlayer, &QMediaPlayer::errorOccurred, this, &MediaViewer::handleMediaError);
220224
connect(mPlayer, &QMediaPlayer::sourceChanged, this, &MediaViewer::handleSourceChanged);
225+
connect(qApp, &QApplication::focusChanged, this, &MediaViewer::handleFocusChanged);
221226
}
222227

223228
MediaViewer::~MediaViewer() = default;
@@ -432,6 +437,28 @@ namespace hal
432437
mStopAction->setEnabled(!source.isEmpty());
433438
}
434439

440+
void MediaViewer::handleFocusChanged(QWidget* /*old*/, QWidget* now)
441+
{
442+
bool wantFocus = (now != nullptr) && isAncestorOf(now);
443+
if (wantFocus != mHasFocusWithin)
444+
{
445+
mHasFocusWithin = wantFocus;
446+
update();
447+
}
448+
}
449+
450+
void MediaViewer::paintEvent(QPaintEvent* event)
451+
{
452+
ExternalContentWidget::paintEvent(event);
453+
if (!mHasFocusWithin)
454+
return;
455+
QPainter painter(this);
456+
painter.setRenderHint(QPainter::Antialiasing);
457+
painter.setBrush(Qt::NoBrush);
458+
painter.setPen(QPen(QColor(40, 200, 240), 1));
459+
painter.drawRect(rect().adjusted(1, 1, -1, -1));
460+
}
461+
435462
void MediaViewer::handleStopTriggered()
436463
{
437464
log_info("media_viewer", "Stopped and unloaded.");

0 commit comments

Comments
 (0)