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) self.image_preview = ip = ImageView(self, 'insert-image-for-notes-preview', True)
ip.cover_changed.connect(self.image_pasted_or_dropped) ip.cover_changed.connect(self.image_pasted_or_dropped)
ip.draw_empty_border = True
h.addWidget(ip) h.addWidget(ip)
self.vr = vr = QVBoxLayout() self.vr = vr = QVBoxLayout()

View File

@ -325,6 +325,7 @@ class ImageView(QWidget, ImageDropMixin):
BORDER_WIDTH = 1 BORDER_WIDTH = 1
cover_changed = pyqtSignal(object) cover_changed = pyqtSignal(object)
draw_empty_border = False
def __init__(self, parent=None, show_size_pref_name=None, default_show_size=False): def __init__(self, parent=None, show_size_pref_name=None, default_show_size=False):
QWidget.__init__(self, parent) QWidget.__init__(self, parent)
@ -368,30 +369,38 @@ class ImageView(QWidget, ImageDropMixin):
def paintEvent(self, event): def paintEvent(self, event):
QWidget.paintEvent(self, event) QWidget.paintEvent(self, event)
pmap = self._pixmap 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 = QPainter(self)
p.setRenderHints(QPainter.RenderHint.Antialiasing | QPainter.RenderHint.SmoothPixmapTransform) p.setRenderHints(QPainter.RenderHint.Antialiasing | QPainter.RenderHint.SmoothPixmapTransform)
p.drawPixmap(target, pmap) try:
if self.draw_border: if pmap.isNull():
pen = QPen() if self.draw_empty_border:
pen.setWidth(self.BORDER_WIDTH) pen = QPen()
p.setPen(pen) pen.setWidth(self.BORDER_WIDTH)
p.drawRect(target) p.setPen(pen)
if self.show_size: p.drawRect(self.rect())
draw_size(p, target, ow, oh) p.end()
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()
# }}} # }}}