Fix regression causing incorrect rendering of layout menu on some macOS machines

Apparently for some reason Qt is not respecting the size hint of the
items on some macOS machines. Make the rendering no longer assume that
the size hint is respected.
This commit is contained in:
Kovid Goyal 2025-03-23 08:43:39 +05:30
parent a7375ad7d4
commit 6ef70f656a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -4,12 +4,11 @@
from qt.core import QFontMetrics, QHBoxLayout, QIcon, QMenu, QPushButton, QSize, QSizePolicy, QStyle, QStyleOption, QStylePainter, Qt, QWidget from qt.core import QFontMetrics, QHBoxLayout, QIcon, QMenu, QPushButton, QSize, QSizePolicy, QStyle, QStyleOption, QStylePainter, Qt, QWidget
ICON_SZ = 64
class LayoutItem(QWidget): class LayoutItem(QWidget):
mouse_over = False mouse_over = False
VMARGIN = 4
def __init__(self, button, parent=None): def __init__(self, button, parent=None):
QWidget.__init__(self, parent) QWidget.__init__(self, parent)
@ -18,23 +17,16 @@ class LayoutItem(QWidget):
self.text = button.label self.text = button.label
self.setCursor(Qt.CursorShape.PointingHandCursor) self.setCursor(Qt.CursorShape.PointingHandCursor)
self.fm = QFontMetrics(self.font()) self.fm = QFontMetrics(self.font())
self._bi = self._di = None
def update_tips(self): def update_tips(self):
self.setToolTip(self.button.toolTip()) self.setToolTip(self.button.toolTip())
self.setStatusTip(self.button.statusTip()) self.setStatusTip(self.button.statusTip())
@property def bright_icon(self, height):
def bright_icon(self): return self.button.icon().pixmap(height, height)
if self._bi is None:
self._bi = self.button.icon().pixmap(ICON_SZ, ICON_SZ)
return self._bi
@property def dull_icon(self, height):
def dull_icon(self): return self.button.icon().pixmap(height, height, mode=QIcon.Mode.Disabled)
if self._di is None:
self._di = self.button.icon().pixmap(ICON_SZ, ICON_SZ, mode=QIcon.Mode.Disabled)
return self._di
def enterEvent(self, ev): def enterEvent(self, ev):
super().enterEvent(ev) super().enterEvent(ev)
@ -49,9 +41,10 @@ class LayoutItem(QWidget):
self.update() self.update()
def sizeHint(self): def sizeHint(self):
ICON_SZ = 64
br = self.fm.boundingRect(self.text) br = self.fm.boundingRect(self.text)
w = max(br.width(), ICON_SZ) + 10 w = max(br.width(), ICON_SZ) + 10
h = 2 * self.fm.lineSpacing() + ICON_SZ + 8 h = 2 * self.fm.lineSpacing() + ICON_SZ + 2 * self.VMARGIN
return QSize(w, h) return QSize(w, h)
def paintEvent(self, ev): def paintEvent(self, ev):
@ -64,22 +57,18 @@ class LayoutItem(QWidget):
tool.rect = self.rect() tool.rect = self.rect()
tool.state = QStyle.StateFlag.State_Raised | QStyle.StateFlag.State_Active | QStyle.StateFlag.State_MouseOver tool.state = QStyle.StateFlag.State_Raised | QStyle.StateFlag.State_Active | QStyle.StateFlag.State_MouseOver
painter.drawPrimitive(QStyle.PrimitiveElement.PE_PanelButtonTool, tool) painter.drawPrimitive(QStyle.PrimitiveElement.PE_PanelButtonTool, tool)
painter.drawText( br = painter.drawText(0, 0, self.width(), ls, Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextSingleLine, self.text)
0, 0, top = br.bottom()
self.width(), bottom = self.height() - ls
ls, Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextSingleLine, self.text)
text = _('Hide') if shown else _('Show') text = _('Hide') if shown else _('Show')
f = self.font() f = self.font()
f.setBold(True) f.setBold(True)
painter.setFont(f) painter.setFont(f)
painter.drawText( painter.drawText(0, bottom, self.width(), ls, Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextSingleLine, text)
0, self.height() - ls, height = bottom - top - 2 * self.VMARGIN
self.width(), x = (self.width() - height) // 2
ls, Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextSingleLine, text) pmap = self.bright_icon(height) if shown else self.dull_icon(height)
x = (self.width() - ICON_SZ) // 2 painter.drawPixmap(x, top + self.VMARGIN, pmap)
y = ls + (self.height() - ICON_SZ - 2 * ls) // 2
pmap = self.bright_icon if shown else self.dull_icon
painter.drawPixmap(x, y, pmap)
painter.end() painter.end()