Skip to content

Commit

Permalink
Improvement 'Replace Field'
Browse files Browse the repository at this point in the history
  • Loading branch information
un-pogaz committed Aug 6, 2022
1 parent bd8d12f commit 20965c0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 37 deletions.
73 changes: 38 additions & 35 deletions SearchReplaceCalibre.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ def _init_controls(self):
self.vargrid.addWidget(self.replace_with, 7, 1, 1, 1)
self.verticalLayout = QtWidgets.QHBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.label_41 = QtWidgets.QLabel(self.tabWidgetPage3)
self.label_41.setObjectName("label_41")
self.verticalLayout.addWidget(self.label_41)
self.replace_func_label = QtWidgets.QLabel(self.tabWidgetPage3)
self.replace_func_label.setObjectName("label_41")
self.verticalLayout.addWidget(self.replace_func_label)
self.replace_func = QtWidgets.QComboBox(self.tabWidgetPage3)
self.replace_func.setObjectName("replace_func")
self.verticalLayout.addWidget(self.replace_func)
Expand Down Expand Up @@ -353,7 +353,7 @@ def _init_controls(self):
self.template_label.setBuddy(self.s_r_template)
self.search_for_label.setBuddy(self.search_for)
self.xlabel_4.setBuddy(self.replace_with)
self.label_41.setBuddy(self.replace_func)
self.replace_func_label.setBuddy(self.replace_func)
self.destination_field_label.setBuddy(self.destination_field)
self.replace_mode_label.setBuddy(self.replace_mode)
self.s_r_dst_ident_label.setBuddy(self.s_r_dst_ident)
Expand Down Expand Up @@ -395,7 +395,7 @@ def retranslateUi(self):
self.case_sensitive.setText(_("Cas&e sensitive"))
self.xlabel_4.setText(_("&Replace with:"))
self.replace_with.setToolTip(_("The replacement text. The matched search text will be replaced with this string"))
self.label_41.setText(_("&Apply function after replace:"))
self.replace_func_label.setText(_("&Apply function after replace:"))
self.replace_func.setToolTip(_("Specify how the text is to be processed after matching and replacement. In character mode, the entire\n"
"field is processed. In regular expression mode, only the matched text is processed"))
self.destination_field_label.setText(_("&Destination field:"))
Expand Down Expand Up @@ -692,29 +692,42 @@ def s_r_destination_field_changed(self, idx):
self.destination_field_fm = self.db.metadata_for_field(txt)
self.s_r_paint_results(None)

def s_r_visible_groupe(self, groupe_1, groupe_2):
self.search_for_label.setVisible(groupe_1)
self.search_for.setVisible(groupe_1)
self.case_sensitive.setVisible(groupe_1)
self.replace_func_label.setVisible(groupe_1)
self.replace_func.setVisible(groupe_1)

self.destination_field.setVisible(groupe_2)
self.destination_field_label.setVisible(groupe_2)
self.replace_mode.setVisible(groupe_2)
self.replace_mode_label.setVisible(groupe_2)
self.comma_separated.setVisible(groupe_2)

def s_r_search_mode_changed(self, val):
search = self.search_field.currentText()
self.search_field.clear()
self.destination_field.clear()

if val != 2: ##un_pogaz Replace Field
self.search_for_label.setVisible(True)
self.search_for.setVisible(True)
self.case_sensitive.setVisible(True)
if val == 2: ##un_pogaz Replace Field
self.s_r_visible_groupe(False, False)

self.source_field_label.setText(self.destination_field_label_text)
else:
if val == 0:
self.s_r_visible_groupe(True, False)
else:
self.s_r_visible_groupe(True, True)

self.source_field_label.setText(self.source_field_label_text)
self.destination_field_label.setText(self.destination_field_label_text)

if val == 0:
for f in self.writable_fields:
self.search_field.addItem(f if f != 'sort' else 'title_sort', f)
self.destination_field.addItem(f if f != 'sort' else 'title_sort', f)
self.destination_field.setCurrentIndex(0)
self.destination_field.setVisible(False)
self.destination_field_label.setVisible(False)
self.replace_mode.setCurrentIndex(0)
self.replace_mode.setVisible(False)
self.replace_mode_label.setVisible(False)
self.comma_separated.setVisible(False)
self.s_r_heading.setText('<p>'+self.main_heading + self.character_heading)

elif val == 2: ##un_pogaz Replace Field
Expand All @@ -726,20 +739,10 @@ def s_r_search_mode_changed(self, val):
self.destination_field.setCurrentIndex(0)
self.search_field.blockSignals(False)
self.destination_field.blockSignals(False)
self.destination_field.setVisible(False)
self.destination_field_label.setVisible(False)
self.replace_mode.setCurrentIndex(0)
self.replace_mode.setVisible(False)
self.replace_mode_label.setVisible(False)
self.comma_separated.setVisible(False)
self.s_r_heading.setText('<p>'+ self.main_heading_short + CalibreText.REPLACE_HEADING)

self.search_for_label.setVisible(False)
self.search_for.setVisible(False)
self.search_for.setText(CalibreText.REPLACE_REGEX)
self.case_sensitive.setVisible(False)
self.source_field_label.setText(self.destination_field_label_text)
self.destination_field_label.setText(self.source_field_label_text)

else:
self.search_field.blockSignals(True)
Expand All @@ -750,11 +753,6 @@ def s_r_search_mode_changed(self, val):
self.destination_field.addItem(f if f != 'sort' else 'title_sort', f)
self.search_field.blockSignals(False)
self.destination_field.blockSignals(False)
self.destination_field.setVisible(True)
self.destination_field_label.setVisible(True)
self.replace_mode.setVisible(True)
self.replace_mode_label.setVisible(True)
self.comma_separated.setVisible(True)
self.s_r_heading.setText('<p>'+self.main_heading + self.regexp_heading)

self.search_field.setCurrentText(search)
Expand Down Expand Up @@ -790,11 +788,16 @@ def s_r_do_regexp(self, mi):
src = self.s_r_get_field(mi, src_field)
result = []
rfunc = S_R_FUNCTIONS[unicode_type(self.replace_func.currentText())]
for s in src:
t = self.s_r_obj.sub(self.s_r_func, s)
if self.search_mode.currentIndex() == 0:
t = rfunc(t)
result.append(t)

if self.search_mode.currentIndex() == 2: ##un_pogaz Replace Field
result.append(self.replace_with.text())
else:
for s in src:
t = self.s_r_obj.sub(self.s_r_func, s)
if self.search_mode.currentIndex() == 0:
t = rfunc(t)
result.append(t)

return result

def s_r_do_destination(self, mi, val):
Expand Down
2 changes: 1 addition & 1 deletion SearchReplaceCalibreText.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class FIELD_NAME:
IDENTIFIER_TYPE = _('Identifier type')

S_R_REPLACE = _('Replace field')
REPLACE_REGEX = '(?s)^.*$'
REPLACE_REGEX = '(?msi)^.*$'
REPLACE_HEADING = _(
'In field replacement mode, the specified field is set '
'to the text and all previous values are erased. After '
Expand Down
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ActionMassSearchReplace(InterfaceActionBase):
description = _('Easily apply a list of multiple saved Find and Replace operations to your books metadata')
supported_platforms = ['windows', 'osx', 'linux']
author = 'un_pogaz'
version = (1, 4, 4)
version = (1, 4, 5)
minimum_calibre_version = (4, 0, 0)

#: This field defines the GUI plugin class that contains all the code
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### version 1.4.5
Improvement of the 'Replace Field' mode

### version 1.4.4
Small incompatibility Calibre6/Qt6

Expand Down

0 comments on commit 20965c0

Please sign in to comment.