diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index e75e5a0eb..81115b400 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -36,10 +36,16 @@ from aqt.utils import ( getTag, openHelp, qtMenuShortcutWorkaround, + restore_combo_history, + restore_combo_index_for_session, + restore_is_checked, restoreGeom, restoreHeader, restoreSplitter, restoreState, + save_combo_history, + save_combo_index_for_session, + save_is_checked, saveGeom, saveHeader, saveSplitter, @@ -1928,23 +1934,40 @@ update cards set usn=?, mod=?, did=? where id in """ frm = aqt.forms.findreplace.Ui_Dialog() frm.setupUi(d) d.setWindowModality(Qt.WindowModal) - frm.field.addItems([_("All Fields")] + fields) + + combo = "BrowserFindAndReplace" + findhistory = restore_combo_history(frm.find, combo + "Find") + replacehistory = restore_combo_history(frm.replace, combo + "Replace") + + restore_is_checked(frm.re, combo + "Regex") + restore_is_checked(frm.ignoreCase, combo + "ignoreCase") + + frm.find.setFocus() + allfields = [_("All Fields")] + fields + frm.field.addItems(allfields) + restore_combo_index_for_session(frm.field, allfields, combo + "Field") qconnect(frm.buttonBox.helpRequested, self.onFindReplaceHelp) restoreGeom(d, "findreplace") r = d.exec_() saveGeom(d, "findreplace") if not r: return + + save_combo_index_for_session(frm.field, combo + "Field") if frm.field.currentIndex() == 0: field = None else: field = fields[frm.field.currentIndex() - 1] - search = frm.find.text() - replace = frm.replace.text() + search = save_combo_history(frm.find, findhistory, combo + "Find") + replace = save_combo_history(frm.replace, replacehistory, combo + "Replace") + regex = frm.re.isChecked() nocase = frm.ignoreCase.isChecked() + save_is_checked(frm.re, combo + "Regex") + save_is_checked(frm.ignoreCase, combo + "ignoreCase") + self.mw.checkpoint(_("Find and Replace")) # starts progress dialog as well self.model.beginReset() @@ -1989,11 +2012,15 @@ update cards set usn=?, mod=?, did=? where id in """ frm = aqt.forms.finddupes.Ui_Dialog() frm.setupUi(d) restoreGeom(d, "findDupes") + searchHistory = restore_combo_history(frm.search, "findDupesFind") + fields = sorted( anki.find.fieldNames(self.col, downcase=False), key=lambda x: x.lower() ) frm.fields.addItems(fields) + restore_combo_index_for_session(frm.fields, fields, "findDupesFields") self._dupesButton = None + # links frm.webView.title = "find duplicates" web_context = FindDupesDialog(dialog=d, browser=self) @@ -2006,10 +2033,10 @@ update cards set usn=?, mod=?, did=? where id in """ qconnect(d.finished, onFin) def onClick(): + search_text = save_combo_history(frm.search, searchHistory, "findDupesFind") + save_combo_index_for_session(frm.fields, "findDupesFields") field = fields[frm.fields.currentIndex()] - self.duplicatesReport( - frm.webView, field, frm.search.text(), frm, web_context - ) + self.duplicatesReport(frm.webView, field, search_text, frm, web_context) search = frm.buttonBox.addButton(_("Search"), QDialogButtonBox.ActionRole) qconnect(search.clicked, onClick) diff --git a/qt/aqt/profiles.py b/qt/aqt/profiles.py index 9fc13a0bd..8ca096c3f 100644 --- a/qt/aqt/profiles.py +++ b/qt/aqt/profiles.py @@ -76,6 +76,8 @@ class AnkiRestart(SystemExit): class ProfileManager: def __init__(self, base=None): + ## Settings which should be forgotten each Anki restart + self.session = {} self.name = None self.db = None self.profile: Optional[Dict] = None diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 1acc57d54..59e936bff 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -8,7 +8,7 @@ import os import re import subprocess import sys -from typing import TYPE_CHECKING, Any, Optional, Union +from typing import TYPE_CHECKING, Any, List, Optional, Union import anki import aqt @@ -409,7 +409,7 @@ def getSaveFile(parent, title, dir_description, key, ext, fname=None): return file -def saveGeom(widget, key): +def saveGeom(widget, key: str): key += "Geom" if isMac and widget.windowState() & Qt.WindowFullScreen: geom = None @@ -418,7 +418,7 @@ def saveGeom(widget, key): aqt.mw.pm.profile[key] = geom -def restoreGeom(widget, key, offset=None, adjustSize=False): +def restoreGeom(widget, key: str, offset=None, adjustSize=False): key += "Geom" if aqt.mw.pm.profile.get(key): widget.restoreGeometry(aqt.mw.pm.profile[key]) @@ -459,12 +459,12 @@ def ensureWidgetInScreenBoundaries(widget): widget.move(x, y) -def saveState(widget, key): +def saveState(widget, key: str): key += "State" aqt.mw.pm.profile[key] = widget.saveState() -def restoreState(widget, key): +def restoreState(widget, key: str): key += "State" if aqt.mw.pm.profile.get(key): widget.restoreState(aqt.mw.pm.profile[key]) @@ -492,6 +492,60 @@ def restoreHeader(widget, key): widget.restoreState(aqt.mw.pm.profile[key]) +def save_is_checked(widget, key: str): + key += "IsChecked" + aqt.mw.pm.profile[key] = widget.isChecked() + + +def restore_is_checked(widget, key: str): + key += "IsChecked" + if aqt.mw.pm.profile.get(key) is not None: + widget.setChecked(aqt.mw.pm.profile[key]) + + +def save_combo_index_for_session(widget: QComboBox, key: str): + textKey = key + "ComboActiveText" + indexKey = key + "ComboActiveIndex" + aqt.mw.pm.session[textKey] = widget.currentText() + aqt.mw.pm.session[indexKey] = widget.currentIndex() + + +def restore_combo_index_for_session(widget: QComboBox, history: List[str], key: str): + textKey = key + "ComboActiveText" + indexKey = key + "ComboActiveIndex" + text = aqt.mw.pm.session.get(textKey) + index = aqt.mw.pm.session.get(indexKey) + if text is not None and index is not None: + if index < len(history) and history[index] == text: + widget.setCurrentIndex(index) + + +def save_combo_history(comboBox: QComboBox, history: List[str], name: str): + name += "BoxHistory" + text_input = comboBox.lineEdit().text() + if text_input in history: + history.remove(text_input) + history.insert(0, text_input) + history = history[:50] + comboBox.clear() + comboBox.addItems(history) + aqt.mw.pm.session[name] = text_input + aqt.mw.pm.profile[name] = history + return text_input + + +def restore_combo_history(comboBox: QComboBox, name: str): + name += "BoxHistory" + history = aqt.mw.pm.profile.get(name, []) + comboBox.addItems([""] + history) + if history: + session_input = aqt.mw.pm.session.get(name) + if session_input and session_input == history[0]: + comboBox.lineEdit().setText(session_input) + comboBox.lineEdit().selectAll() + return history + + def mungeQA(col, txt): print("mungeQA() deprecated; use mw.prepare_card_text_for_display()") txt = col.media.escapeImages(txt) diff --git a/qt/designer/finddupes.ui b/qt/designer/finddupes.ui index fccba5f63..2884364c5 100644 --- a/qt/designer/finddupes.ui +++ b/qt/designer/finddupes.ui @@ -34,7 +34,20 @@ - + + + + 9 + 0 + + + + true + + + QComboBox::NoInsert + + diff --git a/qt/designer/findreplace.ui b/qt/designer/findreplace.ui index 4b94cf51f..e39103381 100644 --- a/qt/designer/findreplace.ui +++ b/qt/designer/findreplace.ui @@ -24,7 +24,20 @@ - + + + + 9 + 0 + + + + true + + + QComboBox::NoInsert + + @@ -34,7 +47,20 @@ - + + + + 9 + 0 + + + + true + + + QComboBox::NoInsert + +