Draw a border for the image preview widget even when empty

This commit is contained in:
Kovid Goyal 2023-09-06 08:47:10 +05:30
parent e26df3b529
commit 0be966db4a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 32 additions and 22 deletions

View File

@ -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()

View File

@ -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,7 +369,16 @@ class ImageView(QWidget, ImageDropMixin):
def paintEvent(self, event):
QWidget.paintEvent(self, event)
pmap = self._pixmap
p = QPainter(self)
p.setRenderHints(QPainter.RenderHint.Antialiasing | QPainter.RenderHint.SmoothPixmapTransform)
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
@ -381,8 +391,6 @@ class ImageView(QWidget, ImageDropMixin):
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()
@ -391,6 +399,7 @@ class ImageView(QWidget, ImageDropMixin):
p.drawRect(target)
if self.show_size:
draw_size(p, target, ow, oh)
finally:
p.end()
# }}}