From ad30806ba95795e81aff3df809a78feebe1f8d2e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 5 Sep 2016 14:08:11 +0530 Subject: [PATCH] Cover Browser: Allow any metadata field to be used as the sub-title, not just the rating --- src/calibre/db/backend.py | 1 + src/calibre/gui2/__init__.py | 1 - src/calibre/gui2/cover_flow.py | 50 +++++++++++++++++++---- src/calibre/gui2/preferences/look_feel.py | 6 ++- src/calibre/gui2/preferences/look_feel.ui | 26 +++++++----- 5 files changed, 63 insertions(+), 21 deletions(-) diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index d75bc17d7d..d556e8fc08 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -445,6 +445,7 @@ class DB(object): defs['update_all_last_mod_dates_on_start'] = False defs['field_under_covers_in_grid'] = 'title' defs['cover_browser_title_template'] = '{title}' + defs['cover_browser_subtitle_field'] = 'rating' # Migrate the bool tristate tweak defs['bools_are_tristate'] = \ diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 9294a1f365..39cf115a2a 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -140,7 +140,6 @@ defs['show_vl_tabs'] = False defs['show_highlight_toggle_button'] = False defs['add_comments_to_email'] = False defs['cb_preserve_aspect_ratio'] = False -defs['show_rating_in_cover_browser'] = True defs['gpm_template_editor_font_size'] = 10 defs['show_emblems'] = False defs['emblem_size'] = 32 diff --git a/src/calibre/gui2/cover_flow.py b/src/calibre/gui2/cover_flow.py index 1a22c2c685..92c3a59493 100644 --- a/src/calibre/gui2/cover_flow.py +++ b/src/calibre/gui2/cover_flow.py @@ -87,6 +87,7 @@ if pictureflow is not None: self.model.modelReset.connect(self.reset, type=Qt.QueuedConnection) self.ignore_image_requests = True self.template_inited = False + self.subtitle_error_reported = False def init_template(self, db): self.template_cache = {} @@ -97,6 +98,11 @@ if pictureflow is not None: def count(self): return self.model.count() + def render_template(self, template, index, db): + book_id = self.model.id(index) + mi = db.get_proxy_metadata(book_id) + return mi.formatter.safe_format(template, mi, _('TEMPLATE ERROR'), mi, template_cache=self.template_cache) + def caption(self, index): if self.ignore_image_requests: return '' @@ -108,10 +114,8 @@ if pictureflow is not None: if self.template_is_title: ans = self.model.title(index) else: - book_id = self.model.id(index) - mi = db.get_proxy_metadata(book_id) try: - ans = mi.formatter.safe_format(self.template, mi, _('TEMPLATE ERROR'), mi, template_cache=self.template_cache) + ans = self.render_template(self.template, index, db) except Exception: if not self.template_error_reported: self.template_error_reported = True @@ -124,11 +128,23 @@ if pictureflow is not None: return ans def subtitle(self, index): - if gprefs['show_rating_in_cover_browser']: - try: - return rating_to_stars(self.model.rating(index) * 2) - except: - pass + try: + db = self.model.db.new_api + field = db.pref('cover_browser_subtitle_field', 'rating') + if field and field != 'none': + book_id = self.model.id(index) + fm = db.field_metadata[field] + if fm['datatype'] == 'rating': + val = db.field_for(field, book_id, default_value=0) + if val: + return rating_to_stars(val, allow_half_stars=db.field_metadata[field]['display'].get('allow_half_stars')) + else: + return self.render_template('{%s}' % field, index, db).replace('&', '&&') + except Exception: + if not self.subtitle_error_reported: + self.subtitle_error_reported = True + import traceback + traceback.print_exc() return '' def reset(self): @@ -163,10 +179,15 @@ if pictureflow is not None: self.context_menu = None self.setContextMenuPolicy(Qt.DefaultContextMenu) self.setPreserveAspectRatio(gprefs['cb_preserve_aspect_ratio']) - self.setSubtitleFont(QFont(rating_font())) if not gprefs['cover_browser_reflections']: self.setShowReflections(False) + def set_subtitle_font(self, for_ratings=True): + if for_ratings: + self.setSubtitleFont(QFont(rating_font())) + else: + self.setSubtitleFont(self.font()) + def set_context_menu(self, cm): self.context_menu = cm @@ -283,6 +304,7 @@ class CoverFlowMixin(object): self.db_images = DatabaseImages(self.library_view.model(), self.is_cover_browser_visible) self.cover_flow.setImages(self.db_images) self.cover_flow.itemActivated.connect(self.iactions['View'].view_specific_book) + self.update_cover_flow_subtitle_font() else: self.cover_flow = QLabel('

'+_('Cover browser could not be loaded') + '
'+pictureflowerror) @@ -302,6 +324,16 @@ class CoverFlowMixin(object): self.cover_flow.stop.connect(self.cb_splitter.hide_side_pane) self.cb_splitter.button.toggled.connect(self.cover_browser_toggled, type=Qt.QueuedConnection) + def update_cover_flow_subtitle_font(self): + db = self.current_db.new_api + field = db.pref('cover_browser_subtitle_field', 'rating') + try: + is_rating = db.field_metadata[field]['datatype'] == 'rating' + except Exception: + is_rating = False + if hasattr(self.cover_flow, 'set_subtitle_font'): + self.cover_flow.set_subtitle_font(is_rating) + def toggle_cover_browser(self, *args): cbd = getattr(self, 'cb_dialog', None) if cbd is not None: diff --git a/src/calibre/gui2/preferences/look_feel.py b/src/calibre/gui2/preferences/look_feel.py index ba236bb42f..8759998bf7 100644 --- a/src/calibre/gui2/preferences/look_feel.py +++ b/src/calibre/gui2/preferences/look_feel.py @@ -291,8 +291,11 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): r('cover_flow_queue_length', config, restart_required=True) r('cover_browser_reflections', gprefs) - r('show_rating_in_cover_browser', gprefs) r('cover_browser_title_template', db.prefs) + fm = db.field_metadata + r('cover_browser_subtitle_field', db.prefs, choices=[(_('No subtitle'), 'none')] + sorted( + (fm[k].get('name'), k) for k in fm.all_field_keys() if fm[k].get('name') + )) r('emblem_size', gprefs) r('emblem_position', gprefs, choices=[ (_('Left'), 'left'), (_('Top'), 'top'), (_('Right'), 'right'), (_('Bottom'), 'bottom')]) @@ -636,6 +639,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): gui.library_view.refresh_book_details() gui.cover_flow.setShowReflections(gprefs['cover_browser_reflections']) gui.cover_flow.setPreserveAspectRatio(gprefs['cb_preserve_aspect_ratio']) + gui.update_cover_flow_subtitle_font() gui.cover_flow.template_inited = False gui.library_view.refresh_row_sizing() gui.grid_view.refresh_settings() diff --git a/src/calibre/gui2/preferences/look_feel.ui b/src/calibre/gui2/preferences/look_feel.ui index d7dc013629..25aedfd637 100644 --- a/src/calibre/gui2/preferences/look_feel.ui +++ b/src/calibre/gui2/preferences/look_feel.ui @@ -977,7 +977,7 @@ if you never want subcategories - + Qt::Vertical @@ -990,13 +990,6 @@ if you never want subcategories - - - - Show book &rating in cover browser - - - @@ -1017,7 +1010,7 @@ if you never want subcategories - + When showing cover browser in separate window, show it &fullscreen @@ -1049,7 +1042,7 @@ them to all have the same width and height - + margin-left: 1.5em @@ -1062,6 +1055,19 @@ them to all have the same width and height + + + + Fie&ld for sub-title: + + + opt_cover_browser_subtitle_field + + + + + +