Fix #1864273 [In the Customize Get books search make some columns centered](https://bugs.launchpad.net/calibre/+bug/1864273)

This commit is contained in:
Kovid Goyal 2020-03-05 09:38:08 +05:30
parent ed232acc8a
commit c4d6dd7dfe
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 39 additions and 11 deletions

View File

@ -6,27 +6,51 @@ __license__ = 'GPL 3'
__copyright__ = '2011, John Schember <john@nachtimwald.com>' __copyright__ = '2011, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
from PyQt5.Qt import (Qt, QAbstractItemModel, QIcon, QModelIndex, QSize)
from calibre.customize.ui import is_disabled, disable_plugin, enable_plugin from PyQt5.Qt import (
from calibre.db.search import _match, CONTAINS_MATCH, EQUALS_MATCH, REGEXP_MATCH QAbstractItemModel, QIcon, QModelIndex, QStyledItemDelegate, Qt
)
from calibre import fit_image
from calibre.customize.ui import disable_plugin, enable_plugin, is_disabled
from calibre.db.search import CONTAINS_MATCH, EQUALS_MATCH, REGEXP_MATCH, _match
from calibre.utils.config_base import prefs from calibre.utils.config_base import prefs
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from calibre.utils.search_query_parser import SearchQueryParser from calibre.utils.search_query_parser import SearchQueryParser
from polyglot.builtins import unicode_type, range from polyglot.builtins import range, unicode_type
class Delegate(QStyledItemDelegate):
def paint(self, painter, option, index):
icon = index.data(Qt.DecorationRole)
if icon and not icon.isNull():
QStyledItemDelegate.paint(self, painter, option, QModelIndex())
pw, ph = option.rect.width(), option.rect.height()
scaled, w, h = fit_image(option.decorationSize.width(), option.decorationSize.height(), pw, ph)
r = option.rect
if pw > w:
x = (pw - w) // 2
r = r.adjusted(x, 0, -x, 0)
if ph > h:
y = (ph - h) // 2
r = r.adjusted(0, y, 0, -y)
painter.drawPixmap(r, icon.pixmap(w, h))
else:
QStyledItemDelegate.paint(self, painter, option, index)
class Matches(QAbstractItemModel): class Matches(QAbstractItemModel):
HEADERS = [_('Enabled'), _('Name'), _('No DRM'), _('Headquarters'), _('Affiliate'), _('Formats')] HEADERS = [_('Enabled'), _('Name'), _('No DRM'), _('Headquarters'), _('Affiliate'), _('Formats')]
HTML_COLS = [1] HTML_COLS = (1,)
CENTERED_COLUMNS = (2, 3, 4)
def __init__(self, plugins): def __init__(self, plugins):
QAbstractItemModel.__init__(self) QAbstractItemModel.__init__(self)
self.NO_DRM_ICON = QIcon(I('ok.png')) self.NO_DRM_ICON = QIcon(I('ok.png'))
self.DONATE_ICON = QIcon() self.DONATE_ICON = QIcon(I('donate.png'))
self.DONATE_ICON.addFile(I('donate.png'), QSize(16, 16))
self.all_matches = plugins self.all_matches = plugins
self.matches = plugins self.matches = plugins
@ -123,6 +147,10 @@ class Matches(QAbstractItemModel):
if is_disabled(result): if is_disabled(result):
return Qt.Unchecked return Qt.Unchecked
return Qt.Checked return Qt.Checked
elif role == Qt.TextAlignmentRole:
if col in self.CENTERED_COLUMNS:
return Qt.AlignHCenter
return Qt.AlignLeft
elif role == Qt.ToolTipRole: elif role == Qt.ToolTipRole:
if col == 0: if col == 0:
if is_disabled(result): if is_disabled(result):
@ -182,9 +210,7 @@ class Matches(QAbstractItemModel):
if not self.matches: if not self.matches:
return return
descending = order == Qt.DescendingOrder descending = order == Qt.DescendingOrder
self.matches.sort(None, self.matches.sort(key=lambda x: sort_key(unicode_type(self.data_as_text(x, col))), reverse=descending)
lambda x: sort_key(unicode_type(self.data_as_text(x, col))),
descending)
if reset: if reset:
self.beginResetModel(), self.endResetModel() self.beginResetModel(), self.endResetModel()

View File

@ -12,7 +12,7 @@ from PyQt5.Qt import (Qt, QTreeView, QSize, QMenu)
from calibre.customize.ui import store_plugins from calibre.customize.ui import store_plugins
from calibre.gui2.metadata.single_download import RichTextDelegate from calibre.gui2.metadata.single_download import RichTextDelegate
from calibre.gui2.store.config.chooser.models import Matches from calibre.gui2.store.config.chooser.models import Matches, Delegate
from polyglot.builtins import range from polyglot.builtins import range
@ -27,6 +27,8 @@ class ResultsView(QTreeView):
self.setIconSize(QSize(24, 24)) self.setIconSize(QSize(24, 24))
self.rt_delegate = RichTextDelegate(self) self.rt_delegate = RichTextDelegate(self)
self.delegate = Delegate()
self.setItemDelegate(self.delegate)
for i in self._model.HTML_COLS: for i in self._model.HTML_COLS:
self.setItemDelegateForColumn(i, self.rt_delegate) self.setItemDelegateForColumn(i, self.rt_delegate)