mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Make the customization of find similar books more flexible
This commit is contained in:
commit
6c34951988
@ -26,40 +26,56 @@ class SimilarBooksAction(InterfaceAction):
|
||||
(_('Books in this series'), 'books_in_series.png', 'series',
|
||||
_('Alt+Shift+S')),
|
||||
(_('Books by this publisher'), 'publisher.png', 'publisher', _('Alt+P')),
|
||||
(_('Books with the same tags'), 'tags.png', 'tag', _('Alt+T')),]:
|
||||
(_('Books with the same tags'), 'tags.png', 'tags', _('Alt+T')),]:
|
||||
ac = self.create_action(spec=(text, icon, None, shortcut),
|
||||
attr=target)
|
||||
m.addAction(ac)
|
||||
ac.triggered.connect(partial(self.show_similar_books, target))
|
||||
self.qaction.setMenu(m)
|
||||
|
||||
def show_similar_books(self, type, *args):
|
||||
search, join = [], ' '
|
||||
def show_similar_books(self, typ, *args):
|
||||
idx = self.gui.library_view.currentIndex()
|
||||
db = self.gui.library_view.model().db
|
||||
if not idx.isValid():
|
||||
return
|
||||
db = idx.model().db
|
||||
row = idx.row()
|
||||
if type == 'series':
|
||||
series = idx.model().db.series(row)
|
||||
if series:
|
||||
search = [db.prefs['similar_series_search_key'] + ':"'+series+'"']
|
||||
elif type == 'publisher':
|
||||
publisher = idx.model().db.publisher(row)
|
||||
if publisher:
|
||||
search = [db.prefs['similar_publisher_search_key'] + ':"'+publisher+'"']
|
||||
elif type == 'tag':
|
||||
tags = idx.model().db.tags(row)
|
||||
if tags:
|
||||
search = [db.prefs['similar_tags_search_key'] + ':"='+t+'"'
|
||||
for t in tags.split(',')]
|
||||
elif type in ('author', 'authors'):
|
||||
authors = idx.model().db.authors(row)
|
||||
if authors:
|
||||
search = [db.prefs['similar_authors_search_key'] +
|
||||
':"='+a.strip().replace('|', ',')+'"' \
|
||||
for a in authors.split(',')]
|
||||
|
||||
# Get the parameters for this search
|
||||
col = db.prefs['similar_' + typ + '_search_key']
|
||||
match = db.prefs['similar_' + typ + '_match_kind']
|
||||
if match == _('Match all'):
|
||||
join = ' and '
|
||||
else:
|
||||
join = ' or '
|
||||
|
||||
# Get all the data for the current record
|
||||
mi = db.get_metadata(row)
|
||||
|
||||
# Get the definitive field name to use for this search. If the field
|
||||
# is a grouped search term, the function returns the list of fields that
|
||||
# are to be searched, otherwise it returns the field name.
|
||||
loc = db.field_metadata.search_term_to_field_key(icu_lower(col))
|
||||
if isinstance(loc, list):
|
||||
# Grouped search terms are a list of fields. Get all the values,
|
||||
# pruning duplicates
|
||||
val = set()
|
||||
for f in loc:
|
||||
v = mi.get(f, None)
|
||||
if not v:
|
||||
continue
|
||||
if isinstance(v, list):
|
||||
val.update(v)
|
||||
else:
|
||||
val.add(v)
|
||||
else:
|
||||
# Get the value of the requested field. Can be a list or a simple val
|
||||
val = mi.get(col, None)
|
||||
if not val:
|
||||
return
|
||||
|
||||
if not isinstance(val, (list, set)):
|
||||
val = [val]
|
||||
search = [col + ':"='+t+'"' for t in val]
|
||||
if search:
|
||||
self.gui.search.set_search_string(join.join(search),
|
||||
store_in_history=True)
|
||||
|
@ -57,7 +57,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
||||
"can be useful to check for duplicates, to find which column contains "
|
||||
"a particular item, or to have hierarchical categories (categories "
|
||||
"that contain categories)."))
|
||||
self.gst = db.prefs.get('grouped_search_terms', {})
|
||||
self.gst = db.prefs.get('grouped_search_terms', {}).copy()
|
||||
self.orig_gst_keys = self.gst.keys()
|
||||
|
||||
fl = []
|
||||
@ -71,6 +71,12 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
||||
self.gst_value.update_items_cache(fl)
|
||||
self.fill_gst_box(select=None)
|
||||
|
||||
self.category_fields = fl
|
||||
ml = [_('Match any'), _('Match all')]
|
||||
r('similar_authors_match_kind', db.prefs, choices=ml)
|
||||
r('similar_tags_match_kind', db.prefs, choices=ml)
|
||||
r('similar_series_match_kind', db.prefs, choices=ml)
|
||||
r('similar_publisher_match_kind', db.prefs, choices=ml)
|
||||
self.set_similar_fields(initial=True)
|
||||
self.similar_authors_search_key.currentIndexChanged[int].connect(self.something_changed)
|
||||
self.similar_tags_search_key.currentIndexChanged[int].connect(self.something_changed)
|
||||
@ -94,12 +100,12 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
||||
self.muc_box_changed)
|
||||
|
||||
def set_similar_fields(self, initial=False):
|
||||
self.set_similar('similar_authors_search_key', first_item='author', initial=initial)
|
||||
self.set_similar('similar_tags_search_key', first_item='tags', initial=initial)
|
||||
self.set_similar('similar_series_search_key', first_item='series', initial=initial)
|
||||
self.set_similar('similar_publisher_search_key', first_item='publisher', initial=initial)
|
||||
self.set_similar('similar_authors_search_key', initial=initial)
|
||||
self.set_similar('similar_tags_search_key', initial=initial)
|
||||
self.set_similar('similar_series_search_key', initial=initial)
|
||||
self.set_similar('similar_publisher_search_key', initial=initial)
|
||||
|
||||
def set_similar(self, name, first_item, initial=False):
|
||||
def set_similar(self, name, initial=False):
|
||||
field = getattr(self, name)
|
||||
if not initial:
|
||||
val = field.currentText()
|
||||
@ -107,7 +113,8 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
||||
val = self.db.prefs[name]
|
||||
field.blockSignals(True)
|
||||
field.clear()
|
||||
choices = [first_item]
|
||||
choices = []
|
||||
choices.extend(self.category_fields)
|
||||
choices.extend(sorted(self.gst.keys(), key=sort_key))
|
||||
field.addItems(choices)
|
||||
dex = field.findText(val)
|
||||
|
@ -207,13 +207,14 @@ to be shown as user categories</string>
|
||||
<string>What to search when searching similar books</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_22">
|
||||
<item row="0" column="0" colspan="4">
|
||||
<item row="0" column="0" colspan="6">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string><p>When you search for similar books by right clicking the book and selecting "Similar books...",
|
||||
calibre constructs a search using the column lookup names defined below.
|
||||
<string><p>When you search for similar books by right clicking the
|
||||
book and selecting "Similar books...",
|
||||
calibre constructs a search using the column lookup names specified below.
|
||||
By changing the lookup name to a grouped search term you can
|
||||
search multiple columns.</p></string>
|
||||
search multiple columns at once.</p></string>
|
||||
</property>
|
||||
<property name="wordWrap">
|
||||
<bool>true</bool>
|
||||
@ -238,13 +239,17 @@ to be shown as user categories</string>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="2">
|
||||
<widget class="QComboBox" name="opt_similar_authors_match_kind">
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="3">
|
||||
<widget class="QLabel" name="label_222">
|
||||
<property name="text">
|
||||
<string>Similar series: </string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="3">
|
||||
<item row="1" column="4">
|
||||
<widget class="QComboBox" name="similar_series_search_key">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
||||
@ -254,6 +259,10 @@ to be shown as user categories</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="5">
|
||||
<widget class="QComboBox" name="opt_similar_series_match_kind">
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="label_223">
|
||||
<property name="text">
|
||||
@ -266,16 +275,24 @@ to be shown as user categories</string>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="2">
|
||||
<widget class="QComboBox" name="opt_similar_tags_match_kind">
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="3">
|
||||
<widget class="QLabel" name="label_224">
|
||||
<property name="text">
|
||||
<string>Similar publishers: </string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="3">
|
||||
<item row="2" column="4">
|
||||
<widget class="QComboBox" name="similar_publisher_search_key">
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="5">
|
||||
<widget class="QComboBox" name="opt_similar_publisher_match_kind">
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
|
@ -236,10 +236,14 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
||||
defs['categories_using_hierarchy'] = []
|
||||
defs['column_color_rules'] = []
|
||||
defs['grouped_search_make_user_categories'] = []
|
||||
defs['similar_authors_search_key'] = 'author'
|
||||
defs['similar_authors_search_key'] = 'authors'
|
||||
defs['similar_authors_match_kind'] = _('Match any')
|
||||
defs['similar_publisher_search_key'] = 'publisher'
|
||||
defs['similar_publisher_match_kind'] = _('Match any')
|
||||
defs['similar_tags_search_key'] = 'tags'
|
||||
defs['similar_tags_match_kind'] = _('Match all')
|
||||
defs['similar_series_search_key'] = 'series'
|
||||
defs['similar_series_match_kind'] = _('Match any')
|
||||
|
||||
# Migrate the bool tristate tweak
|
||||
defs['bools_are_tristate'] = \
|
||||
|
Loading…
x
Reference in New Issue
Block a user