Rework Find & Replace dialog:

- Add option to affect whole collection
- Allow to open without selection
- Add parameter for presetting field
This commit is contained in:
RumovZ 2021-07-13 16:33:45 +02:00
parent b148b7b5e0
commit a5193339e7
6 changed files with 111 additions and 54 deletions

View File

@ -92,6 +92,7 @@ browsing-reschedule = Reschedule
browsing-search-bar-hint = Search cards/notes (type text, then press Enter) browsing-search-bar-hint = Search cards/notes (type text, then press Enter)
browsing-search-in = Search in: browsing-search-in = Search in:
browsing-search-within-formatting-slow = Search within formatting (slow) browsing-search-within-formatting-slow = Search within formatting (slow)
browsing-selected-notes-only = Selected notes only
browsing-shift-position-of-existing-cards = Shift position of existing cards browsing-shift-position-of-existing-cards = Shift position of existing cards
browsing-sidebar = Sidebar browsing-sidebar = Sidebar
browsing-sidebar-filter = Sidebar filter browsing-sidebar-filter = Sidebar filter

View File

@ -812,7 +812,6 @@ class Browser(QMainWindow):
###################################################################### ######################################################################
@no_arg_trigger @no_arg_trigger
@skip_if_selection_is_empty
@ensure_editor_saved @ensure_editor_saved
def onFindReplace(self) -> None: def onFindReplace(self) -> None:
FindAndReplaceDialog(self, mw=self.mw, note_ids=self.selected_notes()) FindAndReplaceDialog(self, mw=self.mw, note_ids=self.selected_notes())

View File

@ -3,7 +3,7 @@
from __future__ import annotations from __future__ import annotations
from typing import List, Sequence from typing import List, Optional, Sequence
import aqt import aqt
from anki.notes import NoteId from anki.notes import NoteId
@ -25,6 +25,7 @@ from aqt.utils import (
save_combo_index_for_session, save_combo_index_for_session,
save_is_checked, save_is_checked,
saveGeom, saveGeom,
tooltip,
tr, tr,
) )
@ -33,19 +34,34 @@ class FindAndReplaceDialog(QDialog):
COMBO_NAME = "BrowserFindAndReplace" COMBO_NAME = "BrowserFindAndReplace"
def __init__( def __init__(
self, parent: QWidget, *, mw: AnkiQt, note_ids: Sequence[NoteId] self,
parent: QWidget,
*,
mw: AnkiQt,
note_ids: Sequence[NoteId],
field: Optional[str] = None,
) -> None: ) -> None:
"""
If 'field' is passed, only this is added to the field selector.
Otherwise, the fields belonging to the 'note_ids' are added.
"""
super().__init__(parent) super().__init__(parent)
self.mw = mw self.mw = mw
self.note_ids = note_ids self.note_ids = note_ids
self.field_names: List[str] = [] self.field_names: List[str] = []
self._field = field
if field:
self._show([field])
elif note_ids:
# fetch field names and then show # fetch field names and then show
QueryOp( QueryOp(
parent=mw, parent=mw,
op=lambda col: col.field_names_for_note_ids(note_ids), op=lambda col: col.field_names_for_note_ids(note_ids),
success=self._show, success=self._show,
).run_in_background() ).run_in_background()
else:
self._show([])
def _show(self, field_names: Sequence[str]) -> None: def _show(self, field_names: Sequence[str]) -> None:
# add "all fields" and "tags" to the top of the list # add "all fields" and "tags" to the top of the list
@ -68,10 +84,20 @@ class FindAndReplaceDialog(QDialog):
) )
self.form.replace.completer().setCaseSensitivity(Qt.CaseSensitive) self.form.replace.completer().setCaseSensitivity(Qt.CaseSensitive)
if not self.note_ids:
# no selected notes to affect
self.form.selected_notes.setChecked(False)
self.form.selected_notes.setEnabled(False)
elif self._field:
self.form.selected_notes.setChecked(False)
restore_is_checked(self.form.re, self.COMBO_NAME + "Regex") restore_is_checked(self.form.re, self.COMBO_NAME + "Regex")
restore_is_checked(self.form.ignoreCase, self.COMBO_NAME + "ignoreCase") restore_is_checked(self.form.ignoreCase, self.COMBO_NAME + "ignoreCase")
self.form.field.addItems(self.field_names) self.form.field.addItems(self.field_names)
if self._field:
self.form.field.setCurrentIndex(self.field_names.index(self._field))
else:
restore_combo_index_for_session( restore_combo_index_for_session(
self.form.field, self.field_names, self.COMBO_NAME + "Field" self.form.field, self.field_names, self.COMBO_NAME + "Field"
) )
@ -97,16 +123,20 @@ class FindAndReplaceDialog(QDialog):
save_is_checked(self.form.re, self.COMBO_NAME + "Regex") save_is_checked(self.form.re, self.COMBO_NAME + "Regex")
save_is_checked(self.form.ignoreCase, self.COMBO_NAME + "ignoreCase") save_is_checked(self.form.ignoreCase, self.COMBO_NAME + "ignoreCase")
if not self.form.selected_notes.isChecked():
# an empty list means *all* notes
self.note_ids = []
# tags? # tags?
if self.form.field.currentIndex() == 1: if self.form.field.currentIndex() == 1:
find_and_replace_tag( op = find_and_replace_tag(
parent=self.parentWidget(), parent=self.parentWidget(),
note_ids=self.note_ids, note_ids=self.note_ids,
search=search, search=search,
replacement=replace, replacement=replace,
regex=regex, regex=regex,
match_case=match_case, match_case=match_case,
).run_in_background() )
else: else:
# fields # fields
if self.form.field.currentIndex() == 0: if self.form.field.currentIndex() == 0:
@ -114,7 +144,7 @@ class FindAndReplaceDialog(QDialog):
else: else:
field = self.field_names[self.form.field.currentIndex()] field = self.field_names[self.form.field.currentIndex()]
find_and_replace( op = find_and_replace(
parent=self.parentWidget(), parent=self.parentWidget(),
note_ids=self.note_ids, note_ids=self.note_ids,
search=search, search=search,
@ -122,7 +152,16 @@ class FindAndReplaceDialog(QDialog):
regex=regex, regex=regex,
field_name=field, field_name=field,
match_case=match_case, match_case=match_case,
).run_in_background() )
if not self.note_ids:
op.success(
lambda out: tooltip(
tr.browsing_notes_updated(count=out.count),
parent=self.parentWidget(),
)
)
op.run_in_background()
super().accept() super().accept()

View File

@ -6,7 +6,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>367</width> <width>377</width>
<height>224</height> <height>224</height>
</rect> </rect>
</property> </property>
@ -16,15 +16,8 @@
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="0" column="0"> <item row="1" column="1">
<widget class="QLabel" name="label"> <widget class="QComboBox" name="replace">
<property name="text">
<string>browsing_find</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="find">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed"> <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>9</horstretch> <horstretch>9</horstretch>
@ -49,8 +42,46 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="2" column="0">
<widget class="QComboBox" name="replace"> <widget class="QLabel" name="label_3">
<property name="text">
<string>browsing_in</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>browsing_find</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="field">
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToMinimumContentsLength</enum>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QCheckBox" name="re">
<property name="text">
<string>browsing_treat_input_as_regular_expression</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="ignoreCase">
<property name="text">
<string>browsing_ignore_case</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="find">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed"> <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>9</horstretch> <horstretch>9</horstretch>
@ -68,31 +99,10 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>browsing_in</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="field">
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToMinimumContentsLength</enum>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="re">
<property name="text">
<string>browsing_treat_input_as_regular_expression</string>
</property>
</widget>
</item>
<item row="3" column="1"> <item row="3" column="1">
<widget class="QCheckBox" name="ignoreCase"> <widget class="QCheckBox" name="selected_notes">
<property name="text"> <property name="text">
<string>browsing_ignore_case</string> <string>browsing_selected_notes_only</string>
</property> </property>
<property name="checked"> <property name="checked">
<bool>true</bool> <bool>true</bool>

View File

@ -6,7 +6,7 @@ mod search_node;
use std::{convert::TryInto, str::FromStr, sync::Arc}; use std::{convert::TryInto, str::FromStr, sync::Arc};
use super::Backend; use super::{notes::to_note_ids, Backend};
pub(super) use crate::backend_proto::search_service::Service as SearchService; pub(super) use crate::backend_proto::search_service::Service as SearchService;
use crate::{ use crate::{
backend_proto as pb, backend_proto as pb,
@ -74,7 +74,7 @@ impl SearchService for Backend {
if !input.match_case { if !input.match_case {
search = format!("(?i){}", search); search = format!("(?i){}", search);
} }
let nids = input.nids.into_iter().map(NoteId).collect(); let mut nids = to_note_ids(input.nids);
let field_name = if input.field_name.is_empty() { let field_name = if input.field_name.is_empty() {
None None
} else { } else {
@ -82,6 +82,9 @@ impl SearchService for Backend {
}; };
let repl = input.replacement; let repl = input.replacement;
self.with_col(|col| { self.with_col(|col| {
if nids.is_empty() {
nids = col.search_notes_unordered("")?
};
col.find_and_replace(nids, &search, &repl, field_name) col.find_and_replace(nids, &search, &repl, field_name)
.map(Into::into) .map(Into::into)
}) })

View File

@ -73,8 +73,13 @@ impl TagsService for Backend {
input: pb::FindAndReplaceTagRequest, input: pb::FindAndReplaceTagRequest,
) -> Result<pb::OpChangesWithCount> { ) -> Result<pb::OpChangesWithCount> {
self.with_col(|col| { self.with_col(|col| {
let note_ids = if input.note_ids.is_empty() {
col.search_notes_unordered("")?
} else {
to_note_ids(input.note_ids)
};
col.find_and_replace_tag( col.find_and_replace_tag(
&to_note_ids(input.note_ids), &note_ids,
&input.search, &input.search,
&input.replacement, &input.replacement,
input.regex, input.regex,