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,
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)

View File

@ -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

View File

@ -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)

View File

@ -34,7 +34,20 @@
</widget>
</item>
<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>
</layout>
</item>

View File

@ -24,7 +24,20 @@
</widget>
</item>
<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 row="1" column="0">
<widget class="QLabel" name="label_2">
@ -34,7 +47,20 @@
</widget>
</item>
<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 row="2" column="0">
<widget class="QLabel" name="label_3">