Always update transform when setting angle of a TextItem (#970)

* Always update transform when setting angle of a TextItem

* Add test to check TextItem.setAngle

* Relax test a bit but still check that setAngle has an effect

* Add docstring to setAngle

* Remove unneeded numpy testing function imports
This commit is contained in:
Kenneth Lyons 2019-09-13 22:04:48 -07:00 committed by Ogi Moore
parent fc9768be3c
commit c94b1cb99e
2 changed files with 34 additions and 4 deletions

View File

@ -110,9 +110,16 @@ class TextItem(GraphicsObject):
self.updateTextPos()
def setAngle(self, angle):
"""
Set the angle of the text in degrees.
This sets the rotation angle of the text as a whole, measured
counter-clockwise from the x axis of the parent. Note that this rotation
angle does not depend on horizontal/vertical scaling of the parent.
"""
self.angle = angle
self.updateTransform()
self.updateTransform(force=True)
def setAnchor(self, anchor):
self.anchor = Point(anchor)
self.updateTextPos()
@ -169,7 +176,7 @@ class TextItem(GraphicsObject):
p.setRenderHint(p.Antialiasing, True)
p.drawPolygon(self.textItem.mapToParent(self.textItem.boundingRect()))
def updateTransform(self):
def updateTransform(self, force=False):
# update transform such that this item has the correct orientation
# and scaling relative to the scene, but inherits its position from its
# parent.
@ -181,7 +188,7 @@ class TextItem(GraphicsObject):
else:
pt = p.sceneTransform()
if pt == self._lastTransform:
if not force and pt == self._lastTransform:
return
t = pt.inverted()[0]

View File

@ -0,0 +1,23 @@
import pytest
import pyqtgraph as pg
app = pg.mkQApp()
def test_TextItem_setAngle():
plt = pg.plot()
plt.setXRange(-10, 10)
plt.setYRange(-20, 20)
item = pg.TextItem(text="test")
plt.addItem(item)
t1 = item.transform()
item.setAngle(30)
app.processEvents()
t2 = item.transform()
assert t1 != t2
assert not t1.isRotating()
assert t2.isRotating()