From 0be966db4a3dc5833827aeff5f39f276abf96079 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 6 Sep 2023 08:47:10 +0530 Subject: [PATCH] Draw a border for the image preview widget even when empty --- .../gui2/dialogs/edit_category_notes.py | 1 + src/calibre/gui2/widgets.py | 53 +++++++++++-------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/calibre/gui2/dialogs/edit_category_notes.py b/src/calibre/gui2/dialogs/edit_category_notes.py index 3866ff1273..32ecec1ced 100644 --- a/src/calibre/gui2/dialogs/edit_category_notes.py +++ b/src/calibre/gui2/dialogs/edit_category_notes.py @@ -80,6 +80,7 @@ class AskImage(Dialog): self.image_preview = ip = ImageView(self, 'insert-image-for-notes-preview', True) ip.cover_changed.connect(self.image_pasted_or_dropped) + ip.draw_empty_border = True h.addWidget(ip) self.vr = vr = QVBoxLayout() diff --git a/src/calibre/gui2/widgets.py b/src/calibre/gui2/widgets.py index 04857e377a..16db2cd558 100644 --- a/src/calibre/gui2/widgets.py +++ b/src/calibre/gui2/widgets.py @@ -325,6 +325,7 @@ class ImageView(QWidget, ImageDropMixin): BORDER_WIDTH = 1 cover_changed = pyqtSignal(object) + draw_empty_border = False def __init__(self, parent=None, show_size_pref_name=None, default_show_size=False): QWidget.__init__(self, parent) @@ -368,30 +369,38 @@ class ImageView(QWidget, ImageDropMixin): def paintEvent(self, event): QWidget.paintEvent(self, event) pmap = self._pixmap - if pmap.isNull(): - return - w, h = pmap.width(), pmap.height() - ow, oh = w, h - cw, ch = self.rect().width(), self.rect().height() - scaled, nw, nh = fit_image(w, h, cw, ch) - if scaled: - pmap = pmap.scaled(int(nw*pmap.devicePixelRatio()), int(nh*pmap.devicePixelRatio()), Qt.AspectRatioMode.IgnoreAspectRatio, - Qt.TransformationMode.SmoothTransformation) - w, h = int(pmap.width()/pmap.devicePixelRatio()), int(pmap.height()/pmap.devicePixelRatio()) - x = int(abs(cw - w)/2) - y = int(abs(ch - h)/2) - target = QRect(x, y, w, h) p = QPainter(self) p.setRenderHints(QPainter.RenderHint.Antialiasing | QPainter.RenderHint.SmoothPixmapTransform) - p.drawPixmap(target, pmap) - if self.draw_border: - pen = QPen() - pen.setWidth(self.BORDER_WIDTH) - p.setPen(pen) - p.drawRect(target) - if self.show_size: - draw_size(p, target, ow, oh) - p.end() + try: + if pmap.isNull(): + if self.draw_empty_border: + pen = QPen() + pen.setWidth(self.BORDER_WIDTH) + p.setPen(pen) + p.drawRect(self.rect()) + p.end() + return + w, h = pmap.width(), pmap.height() + ow, oh = w, h + cw, ch = self.rect().width(), self.rect().height() + scaled, nw, nh = fit_image(w, h, cw, ch) + if scaled: + pmap = pmap.scaled(int(nw*pmap.devicePixelRatio()), int(nh*pmap.devicePixelRatio()), Qt.AspectRatioMode.IgnoreAspectRatio, + Qt.TransformationMode.SmoothTransformation) + w, h = int(pmap.width()/pmap.devicePixelRatio()), int(pmap.height()/pmap.devicePixelRatio()) + x = int(abs(cw - w)/2) + y = int(abs(ch - h)/2) + target = QRect(x, y, w, h) + p.drawPixmap(target, pmap) + if self.draw_border: + pen = QPen() + pen.setWidth(self.BORDER_WIDTH) + p.setPen(pen) + p.drawRect(target) + if self.show_size: + draw_size(p, target, ow, oh) + finally: + p.end() # }}}