Skip to content

Commit

Permalink
fix: allow to switch focus using keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhd1701 committed Dec 31, 2021
1 parent c438983 commit f441f88
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 23 deletions.
2 changes: 2 additions & 0 deletions gridplayer/params_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
PLAYER_INITIAL_SIZE = (640, 360)
PLAYER_MIN_VIDEO_SIZE = (100, 90)

OVERLAY_ACTIVITY_EVENT = 2000

if platform.system() == "Darwin":
PLAYER_INFO_TEXT_SIZE = 24
else:
Expand Down
12 changes: 11 additions & 1 deletion gridplayer/player/managers/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,20 @@
},
"Next Video": {
"title": translate("Actions", "Next Video"),
"key": Qt.Key_Tab,
"key": "N",
"icon": "next-video",
"func": "next_single_video",
},
"Next Active": {
"title": "Next Active",
"key": Qt.Key_Tab,
"func": "next_active",
},
"Previous Active": {
"title": "Previous Active",
"key": Qt.SHIFT + Qt.Key_Tab,
"func": "previous_active",
},
}
)

Expand Down
44 changes: 34 additions & 10 deletions gridplayer/player/managers/active_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def commands(self):
return {
"active": self.cmd_active,
"is_active_param_set_to": self.is_active_param_set_to,
"next_active": self.next_active,
"previous_active": self.previous_active,
}

def cmd_active(self, command, *args):
Expand All @@ -50,26 +52,48 @@ def is_active_param_set_to(self, param_name, param_value):
return active_video_param == param_value

def update_active_under_mouse(self):
self._update_active_block(self._get_current_cursor_pos())
if is_modal_open():
return

self._update_active_block(self._get_hover_video_block())
self.cmd_active("show_overlay")

def update_active_reset(self):
self._update_active_block(None)

def _update_active_block(self, pos):
if is_modal_open():
return

old_active_block = self._ctx.active_block
self._update_active_block(None)

def next_active(self):
if self._ctx.active_block is None:
next_active = self._ctx.video_blocks[0]
else:
next_block_index = (
self._ctx.video_blocks.index(self._ctx.active_block) + 1
) % len(self._ctx.video_blocks)
next_active = self._ctx.video_blocks[next_block_index]

if pos is None:
self._ctx.active_block = None
self._update_active_block(next_active)
self.cmd_active("show_overlay")

def previous_active(self):
if self._ctx.active_block is None:
next_active = self._ctx.video_blocks[-1]
else:
self._ctx.active_block = self._get_hover_video_block()
next_block_index = (
self._ctx.video_blocks.index(self._ctx.active_block) - 1
) % len(self._ctx.video_blocks)
next_active = self._ctx.video_blocks[next_block_index]

self._update_active_block(next_active)
self.cmd_active("show_overlay")

def _update_active_block(self, new_active_block):
old_active_block = self._ctx.active_block
self._ctx.active_block = new_active_block

if self._ctx.active_block is not None:
self._ctx.active_block.is_active = True
if self._ctx.active_block is not None:
self._ctx.active_block.is_active = True

if self._ctx.active_block != old_active_block:
if old_active_block is not None:
Expand Down
5 changes: 1 addition & 4 deletions gridplayer/player/player.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import platform

from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import QEvent, pyqtSignal
from PyQt5.QtWidgets import QWidget

from gridplayer.player.manager import ManagersManager
Expand Down Expand Up @@ -67,9 +67,6 @@ def __init__(self, **kwargs):
],
"mouse_hide": [
("video_blocks.video_count_changed", "show_cursor"),
("mouse_hidden", "active_block.update_active_reset"),
("mouse_hidden", "video_blocks.hide_overlay"),
("mouse_shown", "active_block.update_active_under_mouse"),
],
"active_block": [
("video_blocks.video_count_changed", "update_active_under_mouse")
Expand Down
16 changes: 10 additions & 6 deletions gridplayer/widgets/video_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
from typing import Optional

from pydantic.color import Color
from PyQt5.QtCore import Qt, QTimer, pyqtSignal
from PyQt5.QtCore import QEvent, Qt, QTimer, pyqtSignal
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QStackedLayout, QWidget

from gridplayer.dialogs.rename_dialog import QVideoRenameDialog
from gridplayer.exceptions import PlayerException
from gridplayer.params_static import PLAYER_ID_LENGTH, VideoRepeat
from gridplayer.params_static import (
OVERLAY_ACTIVITY_EVENT,
PLAYER_ID_LENGTH,
VideoRepeat,
)
from gridplayer.settings import Settings
from gridplayer.utils.misc import qt_connect
from gridplayer.utils.next_file import next_video_file
Expand Down Expand Up @@ -219,6 +223,10 @@ def showEvent(self, event):
if self.is_video_initialized and not Settings().get("misc/overlay_hide"):
self.show_overlay()

def customEvent(self, event):
if event.type() == OVERLAY_ACTIVITY_EVENT:
self.show_overlay()

def is_under_cursor(self):
return self.rect().contains(self.mapFromGlobal(QCursor.pos()))

Expand Down Expand Up @@ -269,10 +277,6 @@ def hide_overlay(self):
if not Settings().get("misc/overlay_hide"):
return

if self.is_active:
self.overlay_hide_timer.start(1000 * Settings().get("misc/overlay_timeout"))
return

self.overlay.hide()

def time_changed(self, new_time):
Expand Down
7 changes: 6 additions & 1 deletion gridplayer/widgets/video_overlay.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from PyQt5.QtCore import QEvent, QPoint, QRect, Qt, QTimer, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QPalette, QRegion
from PyQt5.QtGui import QGuiApplication, QPalette, QRegion
from PyQt5.QtWidgets import (
QApplication,
QGraphicsOpacityEffect,
Expand All @@ -8,6 +8,7 @@
QWidget,
)

from gridplayer.params_static import OVERLAY_ACTIVITY_EVENT
from gridplayer.settings import Settings
from gridplayer.utils.misc import qt_connect
from gridplayer.utils.time_txt import get_time_txt
Expand Down Expand Up @@ -132,6 +133,10 @@ def ui_setup(self): # noqa: WPS213

self.floating_progress = OverlayShortLabelFloating(parent=self)

def customEvent(self, event):
if event.type() == OVERLAY_ACTIVITY_EVENT:
QGuiApplication.sendEvent(self.parent(), QEvent(2000))

def resizeEvent(self, event):
too_narrow_to_fit = 250
is_wide = event.size().width() > too_narrow_to_fit
Expand Down
10 changes: 9 additions & 1 deletion gridplayer/widgets/video_overlay_elements.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import math

from PyQt5.QtCore import QPoint, Qt, pyqtSignal
from PyQt5.QtCore import QEvent, QPoint, Qt, pyqtSignal
from PyQt5.QtGui import (
QColor,
QFontMetrics,
QGuiApplication,
QMouseEvent,
QPainter,
QPainterPath,
QRegion,
)
from PyQt5.QtWidgets import QSizePolicy, QWidget

from gridplayer.params_static import OVERLAY_ACTIVITY_EVENT
from gridplayer.utils.time_txt import get_time_txt_short


Expand Down Expand Up @@ -270,6 +272,9 @@ def mouseMoveEvent(self, event):
if QGuiApplication.mouseButtons() == Qt.LeftButton:
self._update_position(self.progress_select_x)
event.accept()

# Send activity event to keep overlay visible
QGuiApplication.sendEvent(self.parent(), QEvent(OVERLAY_ACTIVITY_EVENT))
else:
event.ignore()

Expand Down Expand Up @@ -403,6 +408,9 @@ def mouseMoveEvent(self, event):
if QGuiApplication.mouseButtons() == Qt.LeftButton:
self._update_position(self.progress_select_y)
event.accept()

# Send activity event to keep overlay visible
QGuiApplication.sendEvent(self.parent(), QEvent(OVERLAY_ACTIVITY_EVENT))
else:
event.ignore()

Expand Down

0 comments on commit f441f88

Please sign in to comment.