Skip to content

Commit 38f20d4

Browse files
committed
Fix custom painted widgets for right-to-left layouts
1 parent abf2404 commit 38f20d4

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

ai_diffusion/ui/widget.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -775,17 +775,21 @@ def paintEvent(self, a0: QPaintEvent | None) -> None:
775775
painter = QPainter(self)
776776
fm = self.fontMetrics()
777777
style = ensure(self.style())
778+
align = (
779+
Qt.AlignmentFlag.AlignLeft
780+
| Qt.AlignmentFlag.AlignVCenter
781+
| Qt.AlignmentFlag.AlignAbsolute
782+
)
778783
rect = self.rect()
779784
pixmap = self.icon().pixmap(int(fm.height() * 1.3))
780785
is_hover = int(opt.state) & QStyle.StateFlag.State_MouseOver
781786
element = QStyle.PrimitiveElement.PE_PanelButtonCommand
782-
vcenter = Qt.AlignmentFlag.AlignVCenter
783787
content_width = fm.width(self._operation) + 5 + pixmap.width()
784788
content_rect = rect.adjusted(int(0.5 * (rect.width() - content_width)), 0, 0, 0)
785789
style.drawPrimitive(element, opt, painter, self)
786-
style.drawItemPixmap(painter, content_rect, vcenter, pixmap)
790+
style.drawItemPixmap(painter, content_rect, align, pixmap)
787791
content_rect = content_rect.adjusted(pixmap.width() + 5, 0, 0, 0)
788-
style.drawItemText(painter, content_rect, vcenter, self.palette(), True, self._operation)
792+
style.drawItemText(painter, content_rect, align, self.palette(), True, self._operation)
789793

790794
cost_width = 0
791795
if is_hover and self._cost > 0:
@@ -799,23 +803,23 @@ def paintEvent(self, a0: QPaintEvent | None) -> None:
799803
)
800804
painter.setOpacity(0.7)
801805
cost_rect = cost_rect.adjusted(6, 0, 0, 0)
802-
style.drawItemText(painter, cost_rect, vcenter, self.palette(), True, str(self._cost))
806+
style.drawItemText(painter, cost_rect, align, self.palette(), True, str(self._cost))
803807
cost_rect = cost_rect.adjusted(text_width + 4, 0, 0, 0)
804-
style.drawItemPixmap(painter, cost_rect, vcenter, pixmap)
808+
style.drawItemPixmap(painter, cost_rect, align, pixmap)
805809

806810
seed_width = 0
807811
if is_hover and self.model.fixed_seed:
808812
pixmap = self._seed_icon.pixmap(fm.height())
809813
seed_width = pixmap.width() + 4
810814
seed_rect = rect.adjusted(rect.width() - cost_width - seed_width, 0, 0, 0)
811-
style.drawItemPixmap(painter, seed_rect, vcenter, pixmap)
815+
style.drawItemPixmap(painter, seed_rect, align, pixmap)
812816

813817
if is_hover and self.model.resolution_multiplier != 1.0:
814818
pixmap = self._resolution_icon.pixmap(fm.height())
815819
resolution_rect = rect.adjusted(
816820
rect.width() - cost_width - seed_width - pixmap.width() - 4, 0, 0, 0
817821
)
818-
style.drawItemPixmap(painter, resolution_rect, vcenter, pixmap)
822+
style.drawItemPixmap(painter, resolution_rect, align, pixmap)
819823

820824

821825
class ErrorBox(QFrame):
@@ -942,22 +946,18 @@ def _paint_tool_drop_down(widget: QToolButton, text: str | None = None):
942946
opt.initFrom(widget)
943947
painter = QPainter(widget)
944948
style = ensure(widget.style())
949+
align = (
950+
Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignAbsolute
951+
)
945952
rect = widget.rect()
946953
pixmap = widget.icon().pixmap(int(rect.height() * 0.75))
947954
element = QStyle.PrimitiveElement.PE_Widget
948955
if int(opt.state) & QStyle.StateFlag.State_MouseOver:
949956
element = QStyle.PrimitiveElement.PE_PanelButtonCommand
950957
style.drawPrimitive(element, opt, painter, widget)
951-
style.drawItemPixmap(
952-
painter,
953-
rect.adjusted(4, 0, 0, 0),
954-
Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter,
955-
pixmap,
956-
)
958+
style.drawItemPixmap(painter, rect.adjusted(4, 0, 0, 0), align, pixmap)
957959
if text:
958960
text_rect = rect.adjusted(pixmap.width() + 4, 0, 0, 0)
959-
style.drawItemText(
960-
painter, text_rect, Qt.AlignmentFlag.AlignVCenter, widget.palette(), True, text
961-
)
961+
style.drawItemText(painter, text_rect, align, widget.palette(), True, text)
962962
painter.translate(int(0.5 * rect.width() - 10), 0)
963963
style.drawPrimitive(QStyle.PrimitiveElement.PE_IndicatorArrowDown, opt, painter)

0 commit comments

Comments
 (0)