From 6e0e17b2b9c28405db9f8644e0d445a662f84b13 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 12 Mar 2021 15:44:19 +1000 Subject: [PATCH] Revert "Merge pull request #1066 from RumovZ/editor-save-dec" This reverts commit a0c47243b648a42f0f1a4a2c10c8b8495a0cfee6, reversing changes made to 0ab87b733958138ae726b7c7a0c46647ca05edb6. @RumoVZ this broke a bunch of operations like 'select notes' and 'set due date'. When the triggered signal is connected to a function, PyQt looks at the function signature to decide what arguments to pass it. The wrapper was using *args, so PyQt passes in an extra argument, which the underlying function didn't expect. I tried settting __signature__ on the wrapper, but PyQT seems to ignore it, so we may either need to check all of the existing calls and add the ignored extra arguments, or create a separate wrapper for such cases. --- qt/aqt/browser.py | 111 +++++++++++++++++++++++++++++----------------- qt/aqt/sidebar.py | 11 +++-- qt/aqt/utils.py | 22 --------- 3 files changed, 77 insertions(+), 67 deletions(-) diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index c65636f51..bc72d432c 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -47,7 +47,6 @@ from aqt.utils import ( restoreState, save_combo_history, save_combo_index_for_session, - save_editor, save_is_checked, saveGeom, saveHeader, @@ -582,10 +581,12 @@ class Browser(QMainWindow): self.mw.deferred_delete_and_garbage_collect(self) self.close() - @save_editor def closeWithCallback(self, onsuccess: Callable) -> None: - self._closeWindow() - onsuccess() + def callback() -> None: + self._closeWindow() + onsuccess() + + self.editor.saveNow(callback) def keyPressEvent(self, evt: QKeyEvent) -> None: if evt.key() == Qt.Key_Escape: @@ -651,8 +652,10 @@ class Browser(QMainWindow): self.form.searchEdit.setFocus() # search triggered by user - @save_editor def onSearchActivated(self) -> None: + self.editor.saveNow(self._onSearchActivated) + + def _onSearchActivated(self) -> None: text = self.form.searchEdit.lineEdit().text() try: normed = self.col.build_search_string(text) @@ -782,9 +785,11 @@ QTableView {{ gridline-color: {grid} }} self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self) gui_hooks.editor_did_init_left_buttons.remove(add_preview_button) - @save_editor def onRowChanged(self, current: QItemSelection, previous: QItemSelection) -> None: - """Update current note and hide/show editor.""" + "Update current note and hide/show editor." + self.editor.saveNow(lambda: self._onRowChanged(current, previous)) + + def _onRowChanged(self, current: QItemSelection, previous: QItemSelection) -> None: if self._closeEventHasCleanedUp: return update = self.updateTitle() @@ -837,9 +842,11 @@ QTableView {{ gridline-color: {grid} }} qconnect(hh.sortIndicatorChanged, self.onSortChanged) qconnect(hh.sectionMoved, self.onColumnMoved) - @save_editor def onSortChanged(self, idx: int, ord: int) -> None: - ord = bool(ord) + ord_bool = bool(ord) + self.editor.saveNow(lambda: self._onSortChanged(idx, ord_bool)) + + def _onSortChanged(self, idx: int, ord: bool) -> None: type = self.model.activeCols[idx] noSort = ("question", "answer") if type in noSort: @@ -887,8 +894,10 @@ QTableView {{ gridline-color: {grid} }} gui_hooks.browser_header_will_show_context_menu(self, m) m.exec_(gpos) - @save_editor def toggleField(self, type: str) -> None: + self.editor.saveNow(lambda: self._toggleField(type)) + + def _toggleField(self, type: str) -> None: self.model.beginReset() if type in self.model.activeCols: if len(self.model.activeCols) < 2: @@ -1065,8 +1074,10 @@ where id in %s""" # Misc menu options ###################################################################### - @save_editor def onChangeModel(self) -> None: + self.editor.saveNow(self._onChangeModel) + + def _onChangeModel(self) -> None: nids = self.oneModelNotes() if nids: ChangeModel(self, nids) @@ -1152,8 +1163,10 @@ where id in %s""" # Deck change ###################################################################### - @save_editor def setDeck(self) -> None: + self.editor.saveNow(self._setDeck) + + def _setDeck(self) -> None: from aqt.studydeck import StudyDeck cids = self.selectedCards() @@ -1185,25 +1198,27 @@ where id in %s""" # Tags ###################################################################### - @save_editor def add_tags_to_selected_notes( self, tags: Optional[str] = None, ) -> None: "Shows prompt if tags not provided." - self._update_tags_of_selected_notes( - func=self.col.tags.bulk_add, - tags=tags, - prompt=tr(TR.BROWSING_ENTER_TAGS_TO_ADD), + self.editor.saveNow( + lambda: self._update_tags_of_selected_notes( + func=self.col.tags.bulk_add, + tags=tags, + prompt=tr(TR.BROWSING_ENTER_TAGS_TO_ADD), + ) ) - @save_editor def remove_tags_from_selected_notes(self, tags: Optional[str] = None) -> None: "Shows prompt if tags not provided." - self._update_tags_of_selected_notes( - func=self.col.tags.bulk_remove, - tags=tags, - prompt=tr(TR.BROWSING_ENTER_TAGS_TO_DELETE), + self.editor.saveNow( + lambda: self._update_tags_of_selected_notes( + func=self.col.tags.bulk_remove, + tags=tags, + prompt=tr(TR.BROWSING_ENTER_TAGS_TO_DELETE), + ) ) def _update_tags_of_selected_notes( @@ -1223,8 +1238,10 @@ where id in %s""" self.model.endReset() self.mw.requireReset(reason=ResetReason.BrowserAddTags, context=self) - @save_editor def clearUnusedTags(self) -> None: + self.editor.saveNow(self._clearUnusedTags) + + def _clearUnusedTags(self) -> None: def on_done(fut: Future) -> None: fut.result() self.on_tag_list_update() @@ -1240,8 +1257,10 @@ where id in %s""" def current_card_is_suspended(self) -> bool: return bool(self.card and self.card.queue == QUEUE_TYPE_SUSPENDED) - @save_editor def suspend_selected_cards(self) -> None: + self.editor.saveNow(self._suspend_selected_cards) + + def _suspend_selected_cards(self) -> None: want_suspend = not self.current_card_is_suspended() c = self.selectedCards() if want_suspend: @@ -1305,8 +1324,10 @@ where id in %s""" # Repositioning ###################################################################### - @save_editor def reposition(self) -> None: + self.editor.saveNow(self._reposition) + + def _reposition(self) -> None: cids = self.selectedCards() cids2 = self.col.db.list( f"select id from cards where type = {CARD_TYPE_NEW} and id in " @@ -1352,30 +1373,34 @@ where id in %s""" # updates undo status self.mw.requireReset(reason=ResetReason.BrowserReschedule, context=self) - @save_editor def set_due_date(self) -> None: - set_due_date_dialog( - mw=self.mw, - parent=self, - card_ids=self.selectedCards(), - config_key=Config.String.SET_DUE_BROWSER, - on_done=self._after_schedule, + self.editor.saveNow( + lambda: set_due_date_dialog( + mw=self.mw, + parent=self, + card_ids=self.selectedCards(), + config_key=Config.String.SET_DUE_BROWSER, + on_done=self._after_schedule, + ) ) - @save_editor def forget_cards(self) -> None: - forget_cards( - mw=self.mw, - parent=self, - card_ids=self.selectedCards(), - on_done=self._after_schedule, + self.editor.saveNow( + lambda: forget_cards( + mw=self.mw, + parent=self, + card_ids=self.selectedCards(), + on_done=self._after_schedule, + ) ) # Edit: selection ###################################################################### - @save_editor def selectNotes(self) -> None: + self.editor.saveNow(self._selectNotes) + + def _selectNotes(self) -> None: nids = self.selectedNotes() # clear the selection so we don't waste energy preserving it tv = self.form.tableView @@ -1433,8 +1458,10 @@ where id in %s""" # Edit: replacing ###################################################################### - @save_editor def onFindReplace(self) -> None: + self.editor.saveNow(self._onFindReplace) + + def _onFindReplace(self) -> None: nids = self.selectedNotes() if not nids: return @@ -1524,8 +1551,10 @@ where id in %s""" # Edit: finding dupes ###################################################################### - @save_editor def onFindDupes(self) -> None: + self.editor.saveNow(self._onFindDupes) + + def _onFindDupes(self) -> None: d = QDialog(self) self.mw.garbage_collect_on_dialog_finish(d) frm = aqt.forms.finddupes.Ui_Dialog() diff --git a/qt/aqt/sidebar.py b/qt/aqt/sidebar.py index 5cc38715f..da4905b20 100644 --- a/qt/aqt/sidebar.py +++ b/qt/aqt/sidebar.py @@ -24,7 +24,6 @@ from aqt.utils import ( TR, askUser, getOnlyText, - save_browser_editor, show_invalid_search_error, showInfo, showWarning, @@ -1143,8 +1142,10 @@ class SidebarTreeView(QTreeView): self.mw.deckBrowser.refresh() self.mw.update_undo_actions() - @save_browser_editor - def remove_tags(self, _item: SidebarItem) -> None: + def remove_tags(self, item: SidebarItem) -> None: + self.browser.editor.saveNow(lambda: self._remove_tags(item)) + + def _remove_tags(self, _item: SidebarItem) -> None: tags = self._selected_tags() def do_remove() -> int: @@ -1194,8 +1195,10 @@ class SidebarTreeView(QTreeView): self.browser.model.beginReset() self.mw.taskman.with_progress(do_rename, on_done) - @save_browser_editor def delete_decks(self, _item: SidebarItem) -> None: + self.browser.editor.saveNow(self._delete_decks) + + def _delete_decks(self) -> None: def do_delete() -> int: return self.mw.col.decks.remove(dids) diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index d12960c1e..8d94aa5c7 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -974,25 +974,3 @@ def startup_info() -> Any: si = subprocess.STARTUPINFO() # pytype: disable=module-attr si.dwFlags |= subprocess.STARTF_USESHOWWINDOW # pytype: disable=module-attr return si - - -def save_editor(func: Callable) -> Callable: - """Call editor.saveNow with the decorated method as callback. The method's owner - must hold a reference to the Editor. - """ - - def decorated(self: Any, *args: Any, **kwargs: Any) -> None: - self.editor.saveNow(lambda: func(self, *args, **kwargs)) - - return decorated - - -def save_browser_editor(func: Callable) -> Callable: - """Call editor.saveNow with the decorated method as callback. The method's owner - must hold a reference to the Editor's Browser. - """ - - def decorated(self: Any, *args: Any, **kwargs: Any) -> None: - self.browser.editor.saveNow(lambda: func(self, *args, **kwargs)) - - return decorated