Merge pull request #650 from evandroforks/remember_last_inputs

Remember last inputs for Find and Replace and Duplicates Find
This commit is contained in:
Damien Elmes 2020-06-09 14:43:58 +10:00 committed by GitHub
commit f44597bb3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 136 additions and 14 deletions

View file

@ -36,10 +36,16 @@ from aqt.utils import (
getTag, getTag,
openHelp, openHelp,
qtMenuShortcutWorkaround, qtMenuShortcutWorkaround,
restore_combo_history,
restore_combo_index_for_session,
restore_is_checked,
restoreGeom, restoreGeom,
restoreHeader, restoreHeader,
restoreSplitter, restoreSplitter,
restoreState, restoreState,
save_combo_history,
save_combo_index_for_session,
save_is_checked,
saveGeom, saveGeom,
saveHeader, saveHeader,
saveSplitter, saveSplitter,
@ -1928,23 +1934,40 @@ update cards set usn=?, mod=?, did=? where id in """
frm = aqt.forms.findreplace.Ui_Dialog() frm = aqt.forms.findreplace.Ui_Dialog()
frm.setupUi(d) frm.setupUi(d)
d.setWindowModality(Qt.WindowModal) 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) qconnect(frm.buttonBox.helpRequested, self.onFindReplaceHelp)
restoreGeom(d, "findreplace") restoreGeom(d, "findreplace")
r = d.exec_() r = d.exec_()
saveGeom(d, "findreplace") saveGeom(d, "findreplace")
if not r: if not r:
return return
save_combo_index_for_session(frm.field, combo + "Field")
if frm.field.currentIndex() == 0: if frm.field.currentIndex() == 0:
field = None field = None
else: else:
field = fields[frm.field.currentIndex() - 1] field = fields[frm.field.currentIndex() - 1]
search = frm.find.text() search = save_combo_history(frm.find, findhistory, combo + "Find")
replace = frm.replace.text() replace = save_combo_history(frm.replace, replacehistory, combo + "Replace")
regex = frm.re.isChecked() regex = frm.re.isChecked()
nocase = frm.ignoreCase.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")) self.mw.checkpoint(_("Find and Replace"))
# starts progress dialog as well # starts progress dialog as well
self.model.beginReset() self.model.beginReset()
@ -1989,11 +2012,15 @@ update cards set usn=?, mod=?, did=? where id in """
frm = aqt.forms.finddupes.Ui_Dialog() frm = aqt.forms.finddupes.Ui_Dialog()
frm.setupUi(d) frm.setupUi(d)
restoreGeom(d, "findDupes") restoreGeom(d, "findDupes")
searchHistory = restore_combo_history(frm.search, "findDupesFind")
fields = sorted( fields = sorted(
anki.find.fieldNames(self.col, downcase=False), key=lambda x: x.lower() anki.find.fieldNames(self.col, downcase=False), key=lambda x: x.lower()
) )
frm.fields.addItems(fields) frm.fields.addItems(fields)
restore_combo_index_for_session(frm.fields, fields, "findDupesFields")
self._dupesButton = None self._dupesButton = None
# links # links
frm.webView.title = "find duplicates" frm.webView.title = "find duplicates"
web_context = FindDupesDialog(dialog=d, browser=self) web_context = FindDupesDialog(dialog=d, browser=self)
@ -2006,10 +2033,10 @@ update cards set usn=?, mod=?, did=? where id in """
qconnect(d.finished, onFin) qconnect(d.finished, onFin)
def onClick(): def onClick():
search_text = save_combo_history(frm.search, searchHistory, "findDupesFind")
save_combo_index_for_session(frm.fields, "findDupesFields")
field = fields[frm.fields.currentIndex()] field = fields[frm.fields.currentIndex()]
self.duplicatesReport( self.duplicatesReport(frm.webView, field, search_text, frm, web_context)
frm.webView, field, frm.search.text(), frm, web_context
)
search = frm.buttonBox.addButton(_("Search"), QDialogButtonBox.ActionRole) search = frm.buttonBox.addButton(_("Search"), QDialogButtonBox.ActionRole)
qconnect(search.clicked, onClick) qconnect(search.clicked, onClick)

View file

@ -76,6 +76,8 @@ class AnkiRestart(SystemExit):
class ProfileManager: class ProfileManager:
def __init__(self, base=None): def __init__(self, base=None):
## Settings which should be forgotten each Anki restart
self.session = {}
self.name = None self.name = None
self.db = None self.db = None
self.profile: Optional[Dict] = None self.profile: Optional[Dict] = None

View file

@ -8,7 +8,7 @@ import os
import re import re
import subprocess import subprocess
import sys import sys
from typing import TYPE_CHECKING, Any, Optional, Union from typing import TYPE_CHECKING, Any, List, Optional, Union
import anki import anki
import aqt import aqt
@ -409,7 +409,7 @@ def getSaveFile(parent, title, dir_description, key, ext, fname=None):
return file return file
def saveGeom(widget, key): def saveGeom(widget, key: str):
key += "Geom" key += "Geom"
if isMac and widget.windowState() & Qt.WindowFullScreen: if isMac and widget.windowState() & Qt.WindowFullScreen:
geom = None geom = None
@ -418,7 +418,7 @@ def saveGeom(widget, key):
aqt.mw.pm.profile[key] = geom 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" key += "Geom"
if aqt.mw.pm.profile.get(key): if aqt.mw.pm.profile.get(key):
widget.restoreGeometry(aqt.mw.pm.profile[key]) widget.restoreGeometry(aqt.mw.pm.profile[key])
@ -459,12 +459,12 @@ def ensureWidgetInScreenBoundaries(widget):
widget.move(x, y) widget.move(x, y)
def saveState(widget, key): def saveState(widget, key: str):
key += "State" key += "State"
aqt.mw.pm.profile[key] = widget.saveState() aqt.mw.pm.profile[key] = widget.saveState()
def restoreState(widget, key): def restoreState(widget, key: str):
key += "State" key += "State"
if aqt.mw.pm.profile.get(key): if aqt.mw.pm.profile.get(key):
widget.restoreState(aqt.mw.pm.profile[key]) widget.restoreState(aqt.mw.pm.profile[key])
@ -492,6 +492,60 @@ def restoreHeader(widget, key):
widget.restoreState(aqt.mw.pm.profile[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): def mungeQA(col, txt):
print("mungeQA() deprecated; use mw.prepare_card_text_for_display()") print("mungeQA() deprecated; use mw.prepare_card_text_for_display()")
txt = col.media.escapeImages(txt) txt = col.media.escapeImages(txt)

View file

@ -34,7 +34,20 @@
</widget> </widget>
</item> </item>
<item row="2" column="2" colspan="2"> <item row="2" column="2" colspan="2">
<widget class="QLineEdit" name="search"/> <widget class="QComboBox" name="search">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>9</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::NoInsert</enum>
</property>
</widget>
</item> </item>
</layout> </layout>
</item> </item>

View file

@ -24,7 +24,20 @@
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
<widget class="QLineEdit" name="find"/> <widget class="QComboBox" name="find">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>9</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::NoInsert</enum>
</property>
</widget>
</item> </item>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="label_2">
@ -34,7 +47,20 @@
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QLineEdit" name="replace"/> <widget class="QComboBox" name="replace">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>9</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::NoInsert</enum>
</property>
</widget>
</item> </item>
<item row="2" column="0"> <item row="2" column="0">
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="label_3">