From 54d7f136cb7e0979d993d20a2a5d8528250da17d Mon Sep 17 00:00:00 2001 From: evandrocoan Date: Sun, 31 May 2020 00:39:00 -0300 Subject: [PATCH] Created the setupComboBoxHistory and saveComboBoxHistory to remove duplicated/common code. https://anki.tenderapp.com/discussions/ankidesktop/39468-find-and-replace-does-not-remember-the-input-from-last-time --- qt/aqt/browser.py | 35 ++++++++--------------------------- qt/aqt/utils.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index 47225681a..8f77afcd7 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -40,10 +40,12 @@ from aqt.utils import ( restoreHeader, restoreSplitter, restoreState, + saveComboBoxHistory, saveGeom, saveHeader, saveSplitter, saveState, + setupComboBoxHistory, shortcut, showInfo, showWarning, @@ -1930,17 +1932,11 @@ update cards set usn=?, mod=?, did=? where id in """ frm.setupUi(d) d.setWindowModality(Qt.WindowModal) - findhistory = self.mw.pm.profile.get("FindAndReplaceFindHistory", []) - frm.find.addItems(findhistory) - frm.find.lineEdit().setText(findhistory[0] if findhistory else "") - frm.find.lineEdit().selectAll() + combo = "BrowserFindAndReplace" + findhistory = setupComboBoxHistory(frm.find, combo + "Find") + replacehistory = setupComboBoxHistory(frm.replace, combo + "Replace") + frm.find.setFocus() - - replacehistory = self.mw.pm.profile.get("FindAndReplaceReplaceHistory", []) - frm.replace.addItems(replacehistory) - frm.replace.lineEdit().setText(replacehistory[0] if replacehistory else "") - frm.replace.lineEdit().selectAll() - frm.field.addItems([_("All Fields")] + fields) qconnect(frm.buttonBox.helpRequested, self.onFindReplaceHelp) restoreGeom(d, "findreplace") @@ -1953,23 +1949,8 @@ update cards set usn=?, mod=?, did=? where id in """ else: field = fields[frm.field.currentIndex() - 1] - search = frm.find.lineEdit().text() - if search in findhistory: - findhistory.remove(search) - findhistory.insert(0, search) - findhistory = findhistory[:30] - frm.find.clear() - frm.find.addItems(findhistory) - self.mw.pm.profile["FindAndReplaceFindHistory"] = findhistory - - replace = frm.replace.lineEdit().text() - if replace in replacehistory: - replacehistory.remove(replace) - replacehistory.insert(0, replace) - replacehistory = replacehistory[:30] - frm.replace.clear() - frm.replace.addItems(replacehistory) - self.mw.pm.profile["FindAndReplaceReplaceHistory"] = replacehistory + search = saveComboBoxHistory(frm.find, findhistory, combo + "Find") + replace = saveComboBoxHistory(frm.replace, replacehistory, combo + "Replace") regex = frm.re.isChecked() nocase = frm.ignoreCase.isChecked() diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 1acc57d54..d772ebf76 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -338,6 +338,28 @@ def getTag(parent, deck, question, tags="user", **kwargs): return ret +def setupComboBoxHistory(comboBox, name): + name += "BoxHistory" + history = aqt.mw.pm.profile.get(name, []) + comboBox.addItems(history) + comboBox.lineEdit().setText(history[0] if history else "") + comboBox.lineEdit().selectAll() + return history + + +def saveComboBoxHistory(comboBox, history, name): + 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.profile[name] = history + return text_input + + # File handling ######################################################################