Skip to content

Commit

Permalink
Merge pull request #4 from un-pogaz/1.4.0
Browse files Browse the repository at this point in the history
merge 1.4.0
  • Loading branch information
un-pogaz authored Feb 25, 2022
2 parents 3d03d53 + e318dd3 commit 2ab04e4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 20 deletions.
24 changes: 19 additions & 5 deletions SearchReplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def operation_isFullValid(operation, all_fields=None, writable_fields=None, poss


def operation_para_list(operation):
name = operation.get(KEY_OPERATION.NAME, '')
column = operation.get(KEY_OPERATION.SEARCH_FIELD, '')
field = operation.get(KEY_OPERATION.DESTINATION_FIELD, '')
if (field and field != column):
Expand All @@ -172,7 +173,7 @@ def operation_para_list(operation):
search_for = operation.get(KEY_OPERATION.SEARCH_FOR, '')
replace_with = operation.get(KEY_OPERATION.REPLACE_WITH, '')

return [ column, template, search_mode, search_for, replace_with ]
return [ name, column, template, search_mode, search_for, replace_with ]

def operation_string(operation):
val = [operation_para_list(operation)[0]]
Expand Down Expand Up @@ -219,9 +220,7 @@ def search_replace(self, book_id, operation=None):

class SearchReplaceDialog(Dialog):
def __init__(self, operation=None, book_ids=[]):
if not operation:
operation = get_default_operation()
self.operation = operation
self.operation = operation or get_default_operation()
self.widget = SearchReplaceWidget(book_ids[:10])
Dialog.__init__(self, _('Configuration of a Search/Replace operation'), 'config_query_SearchReplace')

Expand All @@ -248,6 +247,21 @@ def accept(self):
else:
return

self.operation = self.widget.save_settings()
new_operation = self.widget.save_settings()
new_operation_name = new_operation.get(KEY_OPERATION.NAME, None)
if new_operation_name and new_operation_name == self.operation.get(KEY_OPERATION.NAME, None):
different = False
for k in new_operation:
if k in self.operation and new_operation[k] != self.operation[k]:
different = True
break

if different:
new_operation[KEY_OPERATION.NAME] = ''
self.operation = new_operation

else:
self.operation = new_operation

debug_print('Saved operation > {0}\n{1}\n'.format(operation_string(self.operation), self.operation))
Dialog.accept(self)
12 changes: 11 additions & 1 deletion SearchReplaceCalibre.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,9 @@ def s_r_save_query(self, *args):
self.query_field.setCurrentIndex(self.query_field.findText(name))

def s_r_query_change(self, idx):
item_name = self.query_field.currentText()
self.s_r_query_load(self.query_field.currentText())

def s_r_query_load(self, item_name):
if not item_name:
self.s_r_reset_query_fields()
self.saved_search_name = ''
Expand Down Expand Up @@ -1123,6 +1125,14 @@ def s_r_reset_query_fields(self):

def load_query(self, query):
if query:

item_name = query.get(KEY.NAME, None)
if item_name and self.saved_search_name != item_name and unicode_type(item_name) in self.queries:
idx = self.query_field.findText(item_name)
if idx:
self.query_field.setCurrentIndex(idx)
return

def set_text(attr, key):
try:
attr.setText(query[key])
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, 3, 3)
version = (1, 4, 0)
minimum_calibre_version = (4, 0, 0)

#: This field defines the GUI plugin class that contains all the code
Expand Down
8 changes: 6 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
### version 1.4.0
Shared Search/Replace operation: set in once, used where you want, edit them and all reference has edited
Uses and compatible with Calibre saved Search/Replace system

### version 1.3.3
[fix] Fix the fix of saved Calibre Search/Replace operation
[fix] Fix the fix of Calibre saved Search/Replace operation

### version 1.3.2
The saved Calibre Search/Replace operation could not be loaded
The Calibre saved Search/Replace operation could not be loaded

### version 1.3.1
Various technical improvement
Expand Down
37 changes: 26 additions & 11 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from calibre.gui2.ui import get_gui
from calibre.gui2.widgets2 import Dialog
from calibre.utils.zipfile import ZipFile
from polyglot.builtins import unicode_type

from .SearchReplace import SearchReplaceDialog, KEY_OPERATION, TEMPLATE_FIELD, operation_is_active, get_default_operation, operation_ConvertError, operation_string, operation_para_list, operation_isFullValid, operation_testFullError, operation_testGetError, clean_empty_operation
from .common_utils import (debug_print, get_icon, PREFS_json, KeyboardConfigDialog, get_selected_BookIds,
Expand Down Expand Up @@ -880,7 +881,7 @@ def ok_clicked(self):



COL_CONFIG = ['', _('Columns'), _('Template'), _('Search mode'), _('Search'), _('Replace')]
COL_CONFIG = ['', _('Name'), _('Columns'), _('Template'), _('Search mode'), _('Search'), _('Replace')]
class ConfigOperationListDialog(Dialog):
def __init__(self, parent, menu):
menu = menu or get_default_menu()
Expand Down Expand Up @@ -1096,9 +1097,13 @@ def populate_table(self, operation_list=None):
self.verticalHeader().setDefaultSectionSize(24)

operation_list = clean_empty_operation(operation_list)
calibre_queries = JSONConfig("search_replace_queries")

self.setRowCount(len(operation_list))
for row, operation in enumerate(operation_list):
self.populate_table_row(row, operation)
self.populate_table_row(row, calibre_queries.get(unicode_type(operation.get(KEY_OPERATION.NAME, None)), operation))

self.test_column_hidden()

self.selectRow(0)

Expand All @@ -1111,20 +1116,26 @@ def populate_table_row(self, row, operation):
item = ReadOnlyTableWidgetItem('')
self.setItem(row, i, item)

as_template = False
for i_row in range(self.rowCount()):
item = self.item(i_row, 0)
if item and item.getOperation()[KEY_OPERATION.SEARCH_FIELD] == TEMPLATE_FIELD:
as_template = True
break

self.setColumnHidden(2, not as_template)

self.update_row(row)

self.resizeColumnsToContents()
self.blockSignals(False)

def test_column_hidden(self):
no_name = True
no_template = True
for i_row in range(self.rowCount()):
item = self.item(i_row, 0)
operation = item.getOperation() if item else {}
if no_name and operation.get(KEY_OPERATION.NAME, ''):
no_name = False
if no_template and operation.get(KEY_OPERATION.SEARCH_FIELD, '') == TEMPLATE_FIELD:
no_template = False

self.setColumnHidden(1, no_name)
self.setColumnHidden(3, no_template)
self.resizeColumnsToContents()

def update_row(self, row):
operation = self.convert_row_to_operation(row)

Expand Down Expand Up @@ -1256,6 +1267,8 @@ def append_operation_list(self, operation_list):
row = self.currentRow() + 1
self.insertRow(row)
self.populate_table_row(row, operation)

self.test_column_hidden()


def settingsDoubleClicked(self):
Expand All @@ -1267,6 +1280,8 @@ def settingsDoubleClicked(self):
if d.exec_() == d.Accepted:
d.operation[KEY_OPERATION.ACTIVE] = operation_is_active(src_operation)
self.populate_table_row(row, d.operation)

self.test_column_hidden()

class OperationWidgetItem(QTableWidgetItem):
def __init__(self, table, operation):
Expand Down

0 comments on commit 2ab04e4

Please sign in to comment.