Add decorator to save editor in browser
This commit is contained in:
parent
71789eb51a
commit
9bf93573da
@ -47,6 +47,7 @@ from aqt.utils import (
|
|||||||
restoreState,
|
restoreState,
|
||||||
save_combo_history,
|
save_combo_history,
|
||||||
save_combo_index_for_session,
|
save_combo_index_for_session,
|
||||||
|
save_editor,
|
||||||
save_is_checked,
|
save_is_checked,
|
||||||
saveGeom,
|
saveGeom,
|
||||||
saveHeader,
|
saveHeader,
|
||||||
@ -581,13 +582,11 @@ class Browser(QMainWindow):
|
|||||||
self.mw.deferred_delete_and_garbage_collect(self)
|
self.mw.deferred_delete_and_garbage_collect(self)
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def closeWithCallback(self, onsuccess: Callable) -> None:
|
def closeWithCallback(self, onsuccess: Callable) -> None:
|
||||||
def callback() -> None:
|
|
||||||
self._closeWindow()
|
self._closeWindow()
|
||||||
onsuccess()
|
onsuccess()
|
||||||
|
|
||||||
self.editor.saveNow(callback)
|
|
||||||
|
|
||||||
def keyPressEvent(self, evt: QKeyEvent) -> None:
|
def keyPressEvent(self, evt: QKeyEvent) -> None:
|
||||||
if evt.key() == Qt.Key_Escape:
|
if evt.key() == Qt.Key_Escape:
|
||||||
self.close()
|
self.close()
|
||||||
@ -652,10 +651,8 @@ class Browser(QMainWindow):
|
|||||||
self.form.searchEdit.setFocus()
|
self.form.searchEdit.setFocus()
|
||||||
|
|
||||||
# search triggered by user
|
# search triggered by user
|
||||||
|
@save_editor
|
||||||
def onSearchActivated(self) -> None:
|
def onSearchActivated(self) -> None:
|
||||||
self.editor.saveNow(self._onSearchActivated)
|
|
||||||
|
|
||||||
def _onSearchActivated(self) -> None:
|
|
||||||
text = self.form.searchEdit.lineEdit().text()
|
text = self.form.searchEdit.lineEdit().text()
|
||||||
try:
|
try:
|
||||||
normed = self.col.build_search_string(text)
|
normed = self.col.build_search_string(text)
|
||||||
@ -785,11 +782,9 @@ QTableView {{ gridline-color: {grid} }}
|
|||||||
self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self)
|
self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self)
|
||||||
gui_hooks.editor_did_init_left_buttons.remove(add_preview_button)
|
gui_hooks.editor_did_init_left_buttons.remove(add_preview_button)
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def onRowChanged(self, current: QItemSelection, previous: QItemSelection) -> None:
|
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:
|
if self._closeEventHasCleanedUp:
|
||||||
return
|
return
|
||||||
update = self.updateTitle()
|
update = self.updateTitle()
|
||||||
@ -842,11 +837,9 @@ QTableView {{ gridline-color: {grid} }}
|
|||||||
qconnect(hh.sortIndicatorChanged, self.onSortChanged)
|
qconnect(hh.sortIndicatorChanged, self.onSortChanged)
|
||||||
qconnect(hh.sectionMoved, self.onColumnMoved)
|
qconnect(hh.sectionMoved, self.onColumnMoved)
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def onSortChanged(self, idx: int, ord: int) -> None:
|
def onSortChanged(self, idx: int, ord: int) -> None:
|
||||||
ord_bool = bool(ord)
|
ord = bool(ord)
|
||||||
self.editor.saveNow(lambda: self._onSortChanged(idx, ord_bool))
|
|
||||||
|
|
||||||
def _onSortChanged(self, idx: int, ord: bool) -> None:
|
|
||||||
type = self.model.activeCols[idx]
|
type = self.model.activeCols[idx]
|
||||||
noSort = ("question", "answer")
|
noSort = ("question", "answer")
|
||||||
if type in noSort:
|
if type in noSort:
|
||||||
@ -894,10 +887,8 @@ QTableView {{ gridline-color: {grid} }}
|
|||||||
gui_hooks.browser_header_will_show_context_menu(self, m)
|
gui_hooks.browser_header_will_show_context_menu(self, m)
|
||||||
m.exec_(gpos)
|
m.exec_(gpos)
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def toggleField(self, type: str) -> None:
|
def toggleField(self, type: str) -> None:
|
||||||
self.editor.saveNow(lambda: self._toggleField(type))
|
|
||||||
|
|
||||||
def _toggleField(self, type: str) -> None:
|
|
||||||
self.model.beginReset()
|
self.model.beginReset()
|
||||||
if type in self.model.activeCols:
|
if type in self.model.activeCols:
|
||||||
if len(self.model.activeCols) < 2:
|
if len(self.model.activeCols) < 2:
|
||||||
@ -1074,10 +1065,8 @@ where id in %s"""
|
|||||||
# Misc menu options
|
# Misc menu options
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def onChangeModel(self) -> None:
|
def onChangeModel(self) -> None:
|
||||||
self.editor.saveNow(self._onChangeModel)
|
|
||||||
|
|
||||||
def _onChangeModel(self) -> None:
|
|
||||||
nids = self.oneModelNotes()
|
nids = self.oneModelNotes()
|
||||||
if nids:
|
if nids:
|
||||||
ChangeModel(self, nids)
|
ChangeModel(self, nids)
|
||||||
@ -1163,10 +1152,8 @@ where id in %s"""
|
|||||||
# Deck change
|
# Deck change
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def setDeck(self) -> None:
|
def setDeck(self) -> None:
|
||||||
self.editor.saveNow(self._setDeck)
|
|
||||||
|
|
||||||
def _setDeck(self) -> None:
|
|
||||||
from aqt.studydeck import StudyDeck
|
from aqt.studydeck import StudyDeck
|
||||||
|
|
||||||
cids = self.selectedCards()
|
cids = self.selectedCards()
|
||||||
@ -1198,28 +1185,26 @@ where id in %s"""
|
|||||||
# Tags
|
# Tags
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def add_tags_to_selected_notes(
|
def add_tags_to_selected_notes(
|
||||||
self,
|
self,
|
||||||
tags: Optional[str] = None,
|
tags: Optional[str] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"Shows prompt if tags not provided."
|
"Shows prompt if tags not provided."
|
||||||
self.editor.saveNow(
|
self._update_tags_of_selected_notes(
|
||||||
lambda: self._update_tags_of_selected_notes(
|
|
||||||
func=self.col.tags.bulk_add,
|
func=self.col.tags.bulk_add,
|
||||||
tags=tags,
|
tags=tags,
|
||||||
prompt=tr(TR.BROWSING_ENTER_TAGS_TO_ADD),
|
prompt=tr(TR.BROWSING_ENTER_TAGS_TO_ADD),
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def remove_tags_from_selected_notes(self, tags: Optional[str] = None) -> None:
|
def remove_tags_from_selected_notes(self, tags: Optional[str] = None) -> None:
|
||||||
"Shows prompt if tags not provided."
|
"Shows prompt if tags not provided."
|
||||||
self.editor.saveNow(
|
self._update_tags_of_selected_notes(
|
||||||
lambda: self._update_tags_of_selected_notes(
|
|
||||||
func=self.col.tags.bulk_remove,
|
func=self.col.tags.bulk_remove,
|
||||||
tags=tags,
|
tags=tags,
|
||||||
prompt=tr(TR.BROWSING_ENTER_TAGS_TO_DELETE),
|
prompt=tr(TR.BROWSING_ENTER_TAGS_TO_DELETE),
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
def _update_tags_of_selected_notes(
|
def _update_tags_of_selected_notes(
|
||||||
self,
|
self,
|
||||||
@ -1238,10 +1223,8 @@ where id in %s"""
|
|||||||
self.model.endReset()
|
self.model.endReset()
|
||||||
self.mw.requireReset(reason=ResetReason.BrowserAddTags, context=self)
|
self.mw.requireReset(reason=ResetReason.BrowserAddTags, context=self)
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def clearUnusedTags(self) -> None:
|
def clearUnusedTags(self) -> None:
|
||||||
self.editor.saveNow(self._clearUnusedTags)
|
|
||||||
|
|
||||||
def _clearUnusedTags(self) -> None:
|
|
||||||
def on_done(fut: Future) -> None:
|
def on_done(fut: Future) -> None:
|
||||||
fut.result()
|
fut.result()
|
||||||
self.on_tag_list_update()
|
self.on_tag_list_update()
|
||||||
@ -1257,10 +1240,8 @@ where id in %s"""
|
|||||||
def current_card_is_suspended(self) -> bool:
|
def current_card_is_suspended(self) -> bool:
|
||||||
return bool(self.card and self.card.queue == QUEUE_TYPE_SUSPENDED)
|
return bool(self.card and self.card.queue == QUEUE_TYPE_SUSPENDED)
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def suspend_selected_cards(self) -> None:
|
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()
|
want_suspend = not self.current_card_is_suspended()
|
||||||
c = self.selectedCards()
|
c = self.selectedCards()
|
||||||
if want_suspend:
|
if want_suspend:
|
||||||
@ -1324,10 +1305,8 @@ where id in %s"""
|
|||||||
# Repositioning
|
# Repositioning
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def reposition(self) -> None:
|
def reposition(self) -> None:
|
||||||
self.editor.saveNow(self._reposition)
|
|
||||||
|
|
||||||
def _reposition(self) -> None:
|
|
||||||
cids = self.selectedCards()
|
cids = self.selectedCards()
|
||||||
cids2 = self.col.db.list(
|
cids2 = self.col.db.list(
|
||||||
f"select id from cards where type = {CARD_TYPE_NEW} and id in "
|
f"select id from cards where type = {CARD_TYPE_NEW} and id in "
|
||||||
@ -1372,34 +1351,30 @@ where id in %s"""
|
|||||||
self.model.reset()
|
self.model.reset()
|
||||||
self.mw.requireReset(reason=ResetReason.BrowserReschedule, context=self)
|
self.mw.requireReset(reason=ResetReason.BrowserReschedule, context=self)
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def set_due_date(self) -> None:
|
def set_due_date(self) -> None:
|
||||||
self.editor.saveNow(
|
set_due_date_dialog(
|
||||||
lambda: set_due_date_dialog(
|
|
||||||
mw=self.mw,
|
mw=self.mw,
|
||||||
parent=self,
|
parent=self,
|
||||||
card_ids=self.selectedCards(),
|
card_ids=self.selectedCards(),
|
||||||
default_key=Config.String.SET_DUE_BROWSER,
|
default_key=Config.String.SET_DUE_BROWSER,
|
||||||
on_done=self._after_schedule,
|
on_done=self._after_schedule,
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def forget_cards(self) -> None:
|
def forget_cards(self) -> None:
|
||||||
self.editor.saveNow(
|
forget_cards(
|
||||||
lambda: forget_cards(
|
|
||||||
mw=self.mw,
|
mw=self.mw,
|
||||||
parent=self,
|
parent=self,
|
||||||
card_ids=self.selectedCards(),
|
card_ids=self.selectedCards(),
|
||||||
on_done=self._after_schedule,
|
on_done=self._after_schedule,
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
# Edit: selection
|
# Edit: selection
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def selectNotes(self) -> None:
|
def selectNotes(self) -> None:
|
||||||
self.editor.saveNow(self._selectNotes)
|
|
||||||
|
|
||||||
def _selectNotes(self) -> None:
|
|
||||||
nids = self.selectedNotes()
|
nids = self.selectedNotes()
|
||||||
# clear the selection so we don't waste energy preserving it
|
# clear the selection so we don't waste energy preserving it
|
||||||
tv = self.form.tableView
|
tv = self.form.tableView
|
||||||
@ -1457,10 +1432,8 @@ where id in %s"""
|
|||||||
# Edit: replacing
|
# Edit: replacing
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def onFindReplace(self) -> None:
|
def onFindReplace(self) -> None:
|
||||||
self.editor.saveNow(self._onFindReplace)
|
|
||||||
|
|
||||||
def _onFindReplace(self) -> None:
|
|
||||||
nids = self.selectedNotes()
|
nids = self.selectedNotes()
|
||||||
if not nids:
|
if not nids:
|
||||||
return
|
return
|
||||||
@ -1550,10 +1523,8 @@ where id in %s"""
|
|||||||
# Edit: finding dupes
|
# Edit: finding dupes
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
@save_editor
|
||||||
def onFindDupes(self) -> None:
|
def onFindDupes(self) -> None:
|
||||||
self.editor.saveNow(self._onFindDupes)
|
|
||||||
|
|
||||||
def _onFindDupes(self) -> None:
|
|
||||||
d = QDialog(self)
|
d = QDialog(self)
|
||||||
self.mw.garbage_collect_on_dialog_finish(d)
|
self.mw.garbage_collect_on_dialog_finish(d)
|
||||||
frm = aqt.forms.finddupes.Ui_Dialog()
|
frm = aqt.forms.finddupes.Ui_Dialog()
|
||||||
|
@ -974,3 +974,15 @@ def startup_info() -> Any:
|
|||||||
si = subprocess.STARTUPINFO() # pytype: disable=module-attr
|
si = subprocess.STARTUPINFO() # pytype: disable=module-attr
|
||||||
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW # pytype: disable=module-attr
|
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW # pytype: disable=module-attr
|
||||||
return si
|
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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user