Allow customizing the text that appears below covers in the cover grid

You can now show any metadata field below the covers in the cover grid
view, not just title. To change the displayed field go to
Preferences->Look & Feel->Cover Grid.
This commit is contained in:
Kovid Goyal 2013-12-02 19:29:53 +05:30
parent eba54de1ce
commit 6e9f48b175
4 changed files with 76 additions and 5 deletions

View File

@ -428,6 +428,7 @@ class DB(object):
defs['virtual_lib_on_startup'] = defs['cs_virtual_lib_on_startup'] = ''
defs['virt_libs_hidden'] = defs['virt_libs_order'] = ()
defs['update_all_last_mod_dates_on_start'] = False
defs['field_under_covers_in_grid'] = 'title'
# Migrate the bool tristate tweak
defs['bools_are_tristate'] = \

View File

@ -20,7 +20,8 @@ from PyQt4.Qt import (
QPropertyAnimation, QEasingCurve, pyqtSlot, QHelpEvent, QAbstractItemView,
QStyleOptionViewItem, QToolTip, QByteArray, QBuffer, QBrush)
from calibre import fit_image, prints, prepare_string_for_xml
from calibre import fit_image, prints, prepare_string_for_xml, human_readable
from calibre.constants import DEBUG
from calibre.ebooks.metadata import fmt_sidx
from calibre.utils import join_with_timeout
from calibre.gui2 import gprefs, config
@ -369,6 +370,23 @@ class CoverDelegate(QStyledItemDelegate):
def sizeHint(self, option, index):
return self.item_size
def render_field(self, db, book_id):
try:
field = db.pref('field_under_covers_in_grid', 'title')
if field == 'size':
ans = human_readable(db.field_for(field, book_id, default_value=0))
else:
mi = db.get_proxy_metadata(book_id)
display_name, ans, val, fm = mi.format_field_extended(field)
if fm and fm['datatype'] == 'rating':
ans = u'\u2605' * int(val/2.0)
return unicode(ans)
except Exception:
if DEBUG:
import traceback
traceback.print_exc()
return ''
def paint(self, painter, option, index):
QStyledItemDelegate.paint(self, painter, option, QModelIndex()) # draw the hover and selection highlights
m = index.model()
@ -418,7 +436,7 @@ class CoverDelegate(QStyledItemDelegate):
rect = trect
rect.setTop(rect.bottom() - self.title_height + 5)
painter.setRenderHint(QPainter.TextAntialiasing, True)
title = db.field_for('title', book_id, default_value='')
title = self.render_field(db, book_id)
metrics = painter.fontMetrics()
painter.drawText(rect, Qt.AlignCenter|Qt.TextSingleLine,
metrics.elidedText(title, Qt.ElideRight, rect.width()))

View File

@ -212,6 +212,11 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
r('categories_using_hierarchy', db.prefs, setting=CommaSeparatedList,
choices=sorted(list(choices), key=sort_key))
fm = db.field_metadata
choices = sorted(((fm[k]['name'], k) for k in fm.displayable_field_keys() if fm[k]['name']),
key=lambda x:sort_key(x[0]))
r('field_under_covers_in_grid', db.prefs, choices=choices)
self.current_font = self.initial_font = None
self.change_font_button.clicked.connect(self.change_font)

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>820</width>
<height>519</height>
<height>546</height>
</rect>
</property>
<property name="windowTitle">
@ -261,10 +261,40 @@
<item>
<widget class="QCheckBox" name="opt_cover_grid_show_title">
<property name="text">
<string>Show the book &amp;title below the cover</string>
<string>Show a field (such as title) &amp;under the covers</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_14">
<property name="text">
<string>&amp;Field to show under the covers:</string>
</property>
<property name="buddy">
<cstring>opt_field_under_covers_in_grid</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="opt_field_under_covers_in_grid"/>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
@ -926,5 +956,22 @@ a few top-level elements.</string>
<resources>
<include location="../../../../resources/images.qrc"/>
</resources>
<connections/>
<connections>
<connection>
<sender>opt_cover_grid_show_title</sender>
<signal>toggled(bool)</signal>
<receiver>opt_field_under_covers_in_grid</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>75</x>
<y>102</y>
</hint>
<hint type="destinationlabel">
<x>289</x>
<y>118</y>
</hint>
</hints>
</connection>
</connections>
</ui>