Cover grid: Add an option under Preferences->Look & feel->Cover grid->Cover size to round the corners of the displayed covers

This commit is contained in:
Kovid Goyal 2024-05-19 17:13:38 +05:30
parent 265558ee63
commit 2dd1114c5a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 105 additions and 59 deletions

View File

@ -426,6 +426,7 @@ def create_defs():
defs['cover_grid_disk_cache_size'] = 2500 defs['cover_grid_disk_cache_size'] = 2500
defs['cover_grid_show_title'] = False defs['cover_grid_show_title'] = False
defs['cover_grid_texture'] = None defs['cover_grid_texture'] = None
defs['cover_grid_corner_radius'] = 0
defs['show_vl_tabs'] = False defs['show_vl_tabs'] = False
defs['vl_tabs_closable'] = True defs['vl_tabs_closable'] = True
defs['show_highlight_toggle_button'] = False defs['show_highlight_toggle_button'] = False

View File

@ -10,6 +10,7 @@ import operator
import os import os
import weakref import weakref
from collections import namedtuple from collections import namedtuple
from contextlib import contextmanager
from functools import wraps from functools import wraps
from io import BytesIO from io import BytesIO
from textwrap import wrap from textwrap import wrap
@ -36,11 +37,13 @@ from qt.core import (
QMimeData, QMimeData,
QModelIndex, QModelIndex,
QPainter, QPainter,
QPainterPath,
QPalette, QPalette,
QPixmap, QPixmap,
QPoint, QPoint,
QPropertyAnimation, QPropertyAnimation,
QRect, QRect,
QRectF,
QSize, QSize,
QStyledItemDelegate, QStyledItemDelegate,
QStyleOptionViewItem, QStyleOptionViewItem,
@ -562,7 +565,8 @@ class CoverDelegate(QStyledItemDelegate):
return ans return ans
def paint(self, painter, option, index): def paint(self, painter, option, index):
QStyledItemDelegate.paint(self, painter, option, empty_index) # draw the hover and selection highlights with self.clip_border_radius(painter, option.rect):
QStyledItemDelegate.paint(self, painter, option, empty_index) # draw the hover and selection highlights
m = index.model() m = index.model()
db = m.db db = m.db
try: try:
@ -636,7 +640,7 @@ class CoverDelegate(QStyledItemDelegate):
dy = max(0, int((rect.height() - ch)/2.0)) dy = max(0, int((rect.height() - ch)/2.0))
right_adjust = dx right_adjust = dx
rect.adjust(dx, dy, -dx, -dy) rect.adjust(dx, dy, -dx, -dy)
painter.drawPixmap(rect, cdata) self.paint_cover(painter, rect, cdata)
if self.title_height != 0: if self.title_height != 0:
self.paint_title(painter, trect, db, book_id) self.paint_title(painter, trect, db, book_id)
if self.emblem_size > 0: if self.emblem_size > 0:
@ -658,6 +662,23 @@ class CoverDelegate(QStyledItemDelegate):
finally: finally:
painter.restore() painter.restore()
@contextmanager
def clip_border_radius(self, painter, rect):
painter.save()
r = gprefs['cover_grid_corner_radius']
if r > 0:
pp = QPainterPath()
pp.addRoundedRect(QRectF(rect), r, r)
painter.setClipPath(pp)
try:
yield
finally:
painter.restore()
def paint_cover(self, painter: QPainter, rect: QRect, pixmap: QPixmap):
with self.clip_border_radius(painter, rect):
painter.drawPixmap(rect, pixmap)
def paint_title(self, painter, rect, db, book_id): def paint_title(self, painter, rect, db, book_id):
painter.setRenderHint(QPainter.RenderHint.TextAntialiasing, True) painter.setRenderHint(QPainter.RenderHint.TextAntialiasing, True)
title, is_stars = self.render_field(db, book_id) title, is_stars = self.render_field(db, book_id)

View File

@ -626,6 +626,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
r('bd_show_cover', gprefs) r('bd_show_cover', gprefs)
r('bd_overlay_cover_size', gprefs) r('bd_overlay_cover_size', gprefs)
r('cover_grid_width', gprefs) r('cover_grid_width', gprefs)
r('cover_grid_corner_radius', gprefs)
r('cover_grid_height', gprefs) r('cover_grid_height', gprefs)
r('cover_grid_cache_size_multiple', gprefs) r('cover_grid_cache_size_multiple', gprefs)
r('cover_grid_disk_cache_size', gprefs) r('cover_grid_disk_cache_size', gprefs)

View File

@ -393,36 +393,27 @@
<string>Cover size</string> <string>Cover size</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout_4"> <layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0" colspan="5"> <item row="2" column="2">
<widget class="QLabel" name="cover_size_label"> <widget class="QPushButton" name="cover_grid_smaller_cover">
<property name="sizePolicy"> <property name="toolTip">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum"> <string>Make the covers smaller, maintaining current aspect ratio.</string>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property> </property>
<property name="text"> <property name="text">
<string>By default, calibre chooses a cover size based on your computer's screen size. You can change the cover size here:</string> <string>&amp;Smaller covers</string>
</property> </property>
<property name="wordWrap"> <property name="icon">
<bool>true</bool> <iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/minus.png</normaloff>:/images/minus.png</iconset>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="0" colspan="5"> <item row="2" column="0">
<widget class="QLabel" name="cover_grid_aspect_ratio"> <widget class="QLabel" name="label_12">
<property name="text"> <property name="text">
<string/> <string>Cover &amp;height: </string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_11">
<property name="text">
<string>Cover &amp;width: </string>
</property> </property>
<property name="buddy"> <property name="buddy">
<cstring>opt_cover_grid_width</cstring> <cstring>opt_cover_grid_height</cstring>
</property> </property>
</widget> </widget>
</item> </item>
@ -443,30 +434,6 @@ A value of zero means calculate automatically.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="2">
<widget class="QPushButton" name="cover_grid_larger_cover">
<property name="toolTip">
<string>Make the covers larger, maintaining current aspect ratio.</string>
</property>
<property name="text">
<string>&amp;Larger covers</string>
</property>
<property name="icon">
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/plus.png</normaloff>:/images/plus.png</iconset>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_12">
<property name="text">
<string>Cover &amp;height: </string>
</property>
<property name="buddy">
<cstring>opt_cover_grid_height</cstring>
</property>
</widget>
</item>
<item row="2" column="1"> <item row="2" column="1">
<widget class="QDoubleSpinBox" name="opt_cover_grid_height"> <widget class="QDoubleSpinBox" name="opt_cover_grid_height">
<property name="toolTip"> <property name="toolTip">
@ -484,27 +451,50 @@ A value of zero means calculate automatically.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="2"> <item row="0" column="0" colspan="5">
<widget class="QPushButton" name="cover_grid_smaller_cover"> <widget class="QLabel" name="cover_size_label">
<property name="toolTip"> <property name="sizePolicy">
<string>Make the covers smaller, maintaining current aspect ratio.</string> <sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;Smaller covers</string> <string>By default, calibre chooses a cover size based on your computer's screen size. You can change the cover size here:</string>
</property> </property>
<property name="icon"> <property name="wordWrap">
<iconset resource="../../../../resources/images.qrc"> <bool>true</bool>
<normaloff>:/images/minus.png</normaloff>:/images/minus.png</iconset>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="3" rowspan="2"> <item row="1" column="0">
<widget class="QPushButton" name="cover_grid_reset_size"> <widget class="QLabel" name="label_11">
<property name="text">
<string>Cover &amp;width: </string>
</property>
<property name="buddy">
<cstring>opt_cover_grid_width</cstring>
</property>
</widget>
</item>
<item row="4" column="0" colspan="5">
<widget class="QLabel" name="cover_grid_aspect_ratio">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="cover_grid_larger_cover">
<property name="toolTip"> <property name="toolTip">
<string>Reset size to automatic</string> <string>Make the covers larger, maintaining current aspect ratio.</string>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;Reset size</string> <string>&amp;Larger covers</string>
</property>
<property name="icon">
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/plus.png</normaloff>:/images/plus.png</iconset>
</property> </property>
</widget> </widget>
</item> </item>
@ -521,6 +511,39 @@ A value of zero means calculate automatically.</string>
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="1" column="3" rowspan="2">
<widget class="QPushButton" name="cover_grid_reset_size">
<property name="toolTip">
<string>Reset size to automatic</string>
</property>
<property name="text">
<string>&amp;Reset size</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QLabel" name="label_6">
<property name="text">
<string>&amp;Round the corners:</string>
</property>
<property name="buddy">
<cstring>opt_cover_grid_corner_radius</cstring>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QSpinBox" name="opt_cover_grid_corner_radius">
<property name="toolTip">
<string>The amount by which to round the corners of the covers when they are displayed in the Cover grid. A value of 10 looks good with typical cover sizes. Adjust to your preference. A value of zero disables rounding.</string>
</property>
<property name="specialValueText">
<string>no rounding</string>
</property>
<property name="suffix">
<string> px</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>