Cover Browser: Allow any metadata field to be used as the sub-title, not just the rating

This commit is contained in:
Kovid Goyal 2016-09-05 14:08:11 +05:30
parent 33b9f5e520
commit ad30806ba9
5 changed files with 63 additions and 21 deletions

View File

@ -445,6 +445,7 @@ class DB(object):
defs['update_all_last_mod_dates_on_start'] = False defs['update_all_last_mod_dates_on_start'] = False
defs['field_under_covers_in_grid'] = 'title' defs['field_under_covers_in_grid'] = 'title'
defs['cover_browser_title_template'] = '{title}' defs['cover_browser_title_template'] = '{title}'
defs['cover_browser_subtitle_field'] = 'rating'
# Migrate the bool tristate tweak # Migrate the bool tristate tweak
defs['bools_are_tristate'] = \ defs['bools_are_tristate'] = \

View File

@ -140,7 +140,6 @@ defs['show_vl_tabs'] = False
defs['show_highlight_toggle_button'] = False defs['show_highlight_toggle_button'] = False
defs['add_comments_to_email'] = False defs['add_comments_to_email'] = False
defs['cb_preserve_aspect_ratio'] = False defs['cb_preserve_aspect_ratio'] = False
defs['show_rating_in_cover_browser'] = True
defs['gpm_template_editor_font_size'] = 10 defs['gpm_template_editor_font_size'] = 10
defs['show_emblems'] = False defs['show_emblems'] = False
defs['emblem_size'] = 32 defs['emblem_size'] = 32

View File

@ -87,6 +87,7 @@ if pictureflow is not None:
self.model.modelReset.connect(self.reset, type=Qt.QueuedConnection) self.model.modelReset.connect(self.reset, type=Qt.QueuedConnection)
self.ignore_image_requests = True self.ignore_image_requests = True
self.template_inited = False self.template_inited = False
self.subtitle_error_reported = False
def init_template(self, db): def init_template(self, db):
self.template_cache = {} self.template_cache = {}
@ -97,6 +98,11 @@ if pictureflow is not None:
def count(self): def count(self):
return self.model.count() 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): def caption(self, index):
if self.ignore_image_requests: if self.ignore_image_requests:
return '' return ''
@ -108,10 +114,8 @@ if pictureflow is not None:
if self.template_is_title: if self.template_is_title:
ans = self.model.title(index) ans = self.model.title(index)
else: else:
book_id = self.model.id(index)
mi = db.get_proxy_metadata(book_id)
try: 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: except Exception:
if not self.template_error_reported: if not self.template_error_reported:
self.template_error_reported = True self.template_error_reported = True
@ -124,11 +128,23 @@ if pictureflow is not None:
return ans return ans
def subtitle(self, index): def subtitle(self, index):
if gprefs['show_rating_in_cover_browser']:
try: try:
return rating_to_stars(self.model.rating(index) * 2) db = self.model.db.new_api
except: field = db.pref('cover_browser_subtitle_field', 'rating')
pass 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 '' return ''
def reset(self): def reset(self):
@ -163,10 +179,15 @@ if pictureflow is not None:
self.context_menu = None self.context_menu = None
self.setContextMenuPolicy(Qt.DefaultContextMenu) self.setContextMenuPolicy(Qt.DefaultContextMenu)
self.setPreserveAspectRatio(gprefs['cb_preserve_aspect_ratio']) self.setPreserveAspectRatio(gprefs['cb_preserve_aspect_ratio'])
self.setSubtitleFont(QFont(rating_font()))
if not gprefs['cover_browser_reflections']: if not gprefs['cover_browser_reflections']:
self.setShowReflections(False) 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): def set_context_menu(self, cm):
self.context_menu = 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.db_images = DatabaseImages(self.library_view.model(), self.is_cover_browser_visible)
self.cover_flow.setImages(self.db_images) self.cover_flow.setImages(self.db_images)
self.cover_flow.itemActivated.connect(self.iactions['View'].view_specific_book) self.cover_flow.itemActivated.connect(self.iactions['View'].view_specific_book)
self.update_cover_flow_subtitle_font()
else: else:
self.cover_flow = QLabel('<p>'+_('Cover browser could not be loaded') + self.cover_flow = QLabel('<p>'+_('Cover browser could not be loaded') +
'<br>'+pictureflowerror) '<br>'+pictureflowerror)
@ -302,6 +324,16 @@ class CoverFlowMixin(object):
self.cover_flow.stop.connect(self.cb_splitter.hide_side_pane) self.cover_flow.stop.connect(self.cb_splitter.hide_side_pane)
self.cb_splitter.button.toggled.connect(self.cover_browser_toggled, type=Qt.QueuedConnection) 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): def toggle_cover_browser(self, *args):
cbd = getattr(self, 'cb_dialog', None) cbd = getattr(self, 'cb_dialog', None)
if cbd is not None: if cbd is not None:

View File

@ -291,8 +291,11 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
r('cover_flow_queue_length', config, restart_required=True) r('cover_flow_queue_length', config, restart_required=True)
r('cover_browser_reflections', gprefs) r('cover_browser_reflections', gprefs)
r('show_rating_in_cover_browser', gprefs)
r('cover_browser_title_template', db.prefs) 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_size', gprefs)
r('emblem_position', gprefs, choices=[ r('emblem_position', gprefs, choices=[
(_('Left'), 'left'), (_('Top'), 'top'), (_('Right'), 'right'), (_('Bottom'), 'bottom')]) (_('Left'), 'left'), (_('Top'), 'top'), (_('Right'), 'right'), (_('Bottom'), 'bottom')])
@ -636,6 +639,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
gui.library_view.refresh_book_details() gui.library_view.refresh_book_details()
gui.cover_flow.setShowReflections(gprefs['cover_browser_reflections']) gui.cover_flow.setShowReflections(gprefs['cover_browser_reflections'])
gui.cover_flow.setPreserveAspectRatio(gprefs['cb_preserve_aspect_ratio']) gui.cover_flow.setPreserveAspectRatio(gprefs['cb_preserve_aspect_ratio'])
gui.update_cover_flow_subtitle_font()
gui.cover_flow.template_inited = False gui.cover_flow.template_inited = False
gui.library_view.refresh_row_sizing() gui.library_view.refresh_row_sizing()
gui.grid_view.refresh_settings() gui.grid_view.refresh_settings()

View File

@ -977,7 +977,7 @@ if you never want subcategories</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="8" column="0" colspan="2"> <item row="9" column="0" colspan="2">
<spacer name="verticalSpacer_4"> <spacer name="verticalSpacer_4">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
@ -990,13 +990,6 @@ if you never want subcategories</string>
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="4" column="0">
<widget class="QCheckBox" name="opt_show_rating_in_cover_browser">
<property name="text">
<string>Show book &amp;rating in cover browser</string>
</property>
</widget>
</item>
<item row="5" column="0"> <item row="5" column="0">
<widget class="QLabel" name="label_23"> <widget class="QLabel" name="label_23">
<property name="text"> <property name="text">
@ -1017,7 +1010,7 @@ if you never want subcategories</string>
<item row="1" column="1" colspan="2"> <item row="1" column="1" colspan="2">
<widget class="QSpinBox" name="opt_cover_flow_queue_length"/> <widget class="QSpinBox" name="opt_cover_flow_queue_length"/>
</item> </item>
<item row="6" column="0" colspan="3"> <item row="7" column="0" colspan="3">
<widget class="QCheckBox" name="opt_cb_fullscreen"> <widget class="QCheckBox" name="opt_cb_fullscreen">
<property name="text"> <property name="text">
<string>When showing cover browser in separate window, show it &amp;fullscreen</string> <string>When showing cover browser in separate window, show it &amp;fullscreen</string>
@ -1049,7 +1042,7 @@ them to all have the same width and height</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="7" column="0" colspan="3"> <item row="8" column="0" colspan="3">
<widget class="QLabel" name="fs_help_msg"> <widget class="QLabel" name="fs_help_msg">
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">margin-left: 1.5em</string> <string notr="true">margin-left: 1.5em</string>
@ -1062,6 +1055,19 @@ them to all have the same width and height</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="6" column="0">
<widget class="QLabel" name="label_24">
<property name="text">
<string>Fie&amp;ld for sub-title:</string>
</property>
<property name="buddy">
<cstring>opt_cover_browser_subtitle_field</cstring>
</property>
</widget>
</item>
<item row="6" column="1" colspan="2">
<widget class="QComboBox" name="opt_cover_browser_subtitle_field"/>
</item>
</layout> </layout>
</widget> </widget>
</widget> </widget>