From e124f935a5f75e19d896670aa3c7e58e00067af4 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 1 Jun 2021 15:16:53 +1000 Subject: [PATCH] fix timebox causing crash When a modal was created with another window as its parent, the other window was being returned, when it was the current window that we actually wanted. This caused nextCard() to be called again when it presented the timebox modal, leading to a stack overflow. https://forums.ankiweb.net/t/anki-2-1-45-alpha/10061/71 --- qt/aqt/browser/browser.py | 6 +++--- qt/aqt/main.py | 7 +++---- qt/aqt/utils.py | 11 ++--------- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/qt/aqt/browser/browser.py b/qt/aqt/browser/browser.py index ef607e357..86b7fb50d 100644 --- a/qt/aqt/browser/browser.py +++ b/qt/aqt/browser/browser.py @@ -40,7 +40,7 @@ from aqt.undo import UndoActionsInfo from aqt.utils import ( HelpPage, KeyboardModifiersPressed, - current_top_level_widget, + current_window, ensure_editor_saved, getTag, no_arg_trigger, @@ -112,7 +112,7 @@ class Browser(QMainWindow): def on_operation_did_execute( self, changes: OpChanges, handler: Optional[object] ) -> None: - focused = current_top_level_widget() == self + focused = current_window() == self self.table.op_executed(changes, handler, focused) self.sidebar.op_executed(changes, handler, focused) if changes.editor: @@ -135,7 +135,7 @@ class Browser(QMainWindow): self._update_context_actions() def on_focus_change(self, new: Optional[QWidget], old: Optional[QWidget]) -> None: - if current_top_level_widget() == self: + if current_window() == self: self.setUpdatesEnabled(True) self.table.redraw_cells() self.sidebar.refresh_if_needed() diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 004de77b4..296ef23f4 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -66,7 +66,7 @@ from aqt.utils import ( KeyboardModifiersPressed, askUser, checkInvalidFilename, - current_top_level_widget, + current_window, disable_help_button, getFile, getOnlyText, @@ -80,7 +80,6 @@ from aqt.utils import ( showInfo, showWarning, tooltip, - top_level_widget, tr, ) @@ -725,7 +724,7 @@ class AnkiQt(QMainWindow): self, changes: OpChanges, handler: Optional[object] ) -> None: "Notify current screen of changes." - focused = current_top_level_widget() == self + focused = current_window() == self if self.state == "review": dirty = self.reviewer.op_executed(changes, handler, focused) elif self.state == "overview": @@ -745,7 +744,7 @@ class AnkiQt(QMainWindow): self, new_focus: Optional[QWidget], _old: Optional[QWidget] ) -> None: "If main window has received focus, ensure current UI state is updated." - if new_focus and top_level_widget(new_focus) == self: + if new_focus and new_focus.window() == self: if self.state == "review": self.reviewer.refresh_if_needed() elif self.state == "overview": diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 5d5ddbcdf..3870ee55e 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -721,16 +721,9 @@ def downArrow() -> str: return "▾" -def top_level_widget(widget: QWidget) -> QWidget: - window = None - while widget := widget.parentWidget(): - window = widget - return window - - -def current_top_level_widget() -> Optional[QWidget]: +def current_window() -> Optional[QWidget]: if widget := QApplication.focusWidget(): - return top_level_widget(widget) + return widget.window() else: return None