1+ from fractions import Fraction
12from typing import Optional
23
34from AnyQt .QtCore import Qt , QPointF , QRectF , QSizeF , QMarginsF
@@ -38,6 +39,7 @@ class SimpleLayoutItem(QGraphicsLayoutItem):
3839 "__resizeContents" ,
3940 "__aspectMode" ,
4041 "__transform" ,
42+ "__scale" ,
4143 )
4244
4345 def __init__ (
@@ -57,6 +59,8 @@ def __init__(
5759 self .__resizeContents = resizeContents
5860 self .__aspectMode = aspectMode
5961 self .__transform = None # type: Optional[QGraphicsScale]
62+ self .__scale = (Fraction (1 ), Fraction (1 ))
63+
6064 if resizeContents :
6165 self .__transform = QGraphicsScale ()
6266 trs = item .transformations ()
@@ -82,8 +86,9 @@ def sizeHint(self, which: Qt.SizeHint, constraint=QSizeF(-1, -1)) -> QSizeF:
8286 size = brect .size ()
8387 if scale is not None :
8488 # undo the scaling
85- sx , sy = scale .xScale (), scale .yScale ()
86- size = QSizeF (size .width () / sx , size .height () / sy )
89+ sx , sy = self .__scale
90+ size = QSizeF (float (Fraction (size .width ()) / sx ),
91+ float (Fraction (size .height ()) / sy ))
8792 if constraint != QSizeF (- 1 , - 1 ):
8893 size = scaled (size , constraint , self .__aspectMode )
8994 return size
@@ -104,10 +109,14 @@ def __updateScale(self):
104109 return
105110 itemsize = self .sizeHint (Qt .PreferredSize )
106111 scaledsize = scaled (itemsize , geom .size (), self .__aspectMode )
107- sx = scaledsize .width () / itemsize .width ()
108- sy = scaledsize .height () / itemsize .height ()
109- self .__transform .setXScale (sx )
110- self .__transform .setYScale (sy )
112+ if not itemsize .isEmpty ():
113+ sx = Fraction (scaledsize .width ()) / Fraction (itemsize .width ())
114+ sy = Fraction (scaledsize .height ()) / Fraction (itemsize .height ())
115+ else :
116+ sx = sy = Fraction (1 )
117+ self .__scale = (sx , sy )
118+ self .__transform .setXScale (float (sx ))
119+ self .__transform .setYScale (float (sy ))
111120
112121 def __layout (self ):
113122 item = self .item
0 commit comments