Skip to content

Commit e5ce964

Browse files
committed
Fix division by zero when switching workspace
1 parent 8f624d0 commit e5ce964

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

ai_diffusion/ui/widget.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
QKeyEvent,
1717
QKeySequence,
1818
QMouseEvent,
19+
QPaintDevice,
1920
QPainter,
2021
QPaintEvent,
2122
QPalette,
@@ -924,6 +925,13 @@ def _create_action(self, name: str, workspace: Workspace):
924925
return action
925926

926927

928+
def _get_width_dip(s: QPaintDevice):
929+
# get device-indpendent width for things like QPixmap, which report their size in physical pixels
930+
if ratio := s.devicePixelRatioF():
931+
return int(s.width() / ratio)
932+
return s.width()
933+
934+
927935
class GenerateButton(QPushButton):
928936
ctrl_clicked = pyqtSignal()
929937

@@ -984,7 +992,7 @@ def paintEvent(self, a0: QPaintEvent | None) -> None:
984992
)
985993
rect = self.rect()
986994
pixmap = self.icon().pixmap(int(fm.height() * 1.3))
987-
pixmap_width = int(pixmap.width() / pixmap.devicePixelRatioF())
995+
pixmap_width = _get_width_dip(pixmap)
988996
is_hover = int(opt.state) & QStyle.StateFlag.State_MouseOver
989997
element = QStyle.PrimitiveElement.PE_PanelButtonCommand
990998
content_width = fm.width(self._operation) + 5 + pixmap_width
@@ -1173,14 +1181,13 @@ def _paint_tool_drop_down(widget: QToolButton, text: str | None = None):
11731181
)
11741182
rect = widget.rect()
11751183
pixmap = widget.icon().pixmap(int(rect.height() * 0.75))
1176-
pixmap_width = int(pixmap.width() / pixmap.devicePixelRatioF())
11771184
element = QStyle.PrimitiveElement.PE_Widget
11781185
if int(opt.state) & QStyle.StateFlag.State_MouseOver:
11791186
element = QStyle.PrimitiveElement.PE_PanelButtonCommand
11801187
style.drawPrimitive(element, opt, painter, widget)
11811188
style.drawItemPixmap(painter, rect.adjusted(4, 0, 0, 0), align, pixmap)
11821189
if text:
1183-
text_rect = rect.adjusted(pixmap_width + 4, 0, 0, 0)
1190+
text_rect = rect.adjusted(_get_width_dip(pixmap) + 4, 0, 0, 0)
11841191
style.drawItemText(painter, text_rect, align, widget.palette(), True, text)
11851192
painter.translate(int(0.5 * rect.width() - 10), 0)
11861193
style.drawPrimitive(QStyle.PrimitiveElement.PE_IndicatorArrowDown, opt, painter)

0 commit comments

Comments
 (0)