remove scale, rotate and translate shims

This commit is contained in:
KIU Shueng Chuan 2021-01-31 23:13:54 +08:00
parent 7192df7032
commit 185f9c7dad
21 changed files with 50 additions and 75 deletions

View File

@ -41,7 +41,7 @@ v.addItem(im1)
v.addItem(im2)
im2.moveBy(110, 20)
v.setRange(QtCore.QRectF(0, 0, 200, 120))
im1.scale(0.8, 0.5)
im1.setTransform(QtGui.QTransform.fromScale(0.8, 0.5))
im3 = pg.ImageItem()
v2 = w.addViewBox(1,0)

View File

@ -17,7 +17,7 @@ vb.setAspectLocked()
img = pg.ImageItem()
img.setImage(np.random.normal(size=(100,100)))
img.scale(0.01, 0.01)
img.setScale(0.01)
vb.addItem(img)
scale = pg.ScaleBar(size=0.1)

View File

@ -133,7 +133,7 @@ view.addItem(box1)
box2 = MenuBox("Menu Box #2")
box2.setParentItem(box1)
box2.setPos(5, 5)
box2.scale(0.2, 0.2)
box2.setScale(0.2)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':

View File

@ -71,8 +71,8 @@ hist.setLevels(data.min(), data.max())
iso.setData(pg.gaussianFilter(data, (2, 2)))
# set position and scale of image
img.scale(0.2, 0.2)
img.translate(-50, 0)
tr = QtGui.QTransform()
img.setTransform(tr.scale(0.2, 0.2).translate(-50, 0))
# zoom to fit imageo
p1.autoRange()

View File

@ -147,10 +147,9 @@ class Optic(pg.GraphicsObject, ParamObj):
self.setParams(**defaults)
def updateTransform(self):
self.resetTransform()
self.setPos(0, 0)
self.translate(Point(self['pos']))
self.rotate(self['angle'])
tr = QtGui.QTransform()
self.setTransform(tr.translate(Point(self['pos'])).rotate(self['angle']))
def setParam(self, param, val):
ParamObj.setParam(self, param, val)
@ -160,7 +159,7 @@ class Optic(pg.GraphicsObject, ParamObj):
# Move graphics item
self.gitem.setPos(Point(self['pos']))
self.gitem.resetTransform()
self.gitem.rotate(self['angle'])
self.gitem.setRotation(self['angle'])
# Move ROI to match
try:
@ -179,7 +178,7 @@ class Optic(pg.GraphicsObject, ParamObj):
pos = self.roi.pos()
# rotate gitem temporarily so we can decide where it will need to move
self.gitem.resetTransform()
self.gitem.rotate(self.roi.angle())
self.gitem.setRotation(self.roi.angle())
br = self.gitem.boundingRect()
o1 = self.gitem.mapToParent(br.topLeft())
self.setParams(angle=self.roi.angle(), pos=pos + (self.gitem.pos() - o1))

View File

@ -678,7 +678,8 @@ class ClockItem(pg.ItemGroup):
pg.ItemGroup.__init__(self)
self.size = clock.size
self.item = QtGui.QGraphicsEllipseItem(QtCore.QRectF(0, 0, self.size, self.size))
self.item.translate(-self.size*0.5, -self.size*0.5)
tr = QtGui.QTransform.fromTranslate(-self.size*0.5, -self.size*0.5)
self.item.setTransform(tr)
self.item.setPen(pg.mkPen(100,100,100))
self.item.setBrush(clock.brush)
self.hand = QtGui.QGraphicsLineItem(0, 0, 0, self.size*0.5)
@ -722,19 +723,19 @@ class ClockItem(pg.ItemGroup):
t = data['pt'][self.i]
self.hand.setRotation(-0.25 * t * 360.)
self.resetTransform()
v = data['v'][self.i]
gam = (1.0 - v**2)**0.5
self.scale(gam, 1.0)
self.setTransform(QtGui.QTransform.fromScale(gam, 1.0))
f = data['f'][self.i]
self.flare.resetTransform()
tr = QtGui.QTransform()
if f < 0:
self.flare.translate(self.size*0.4, 0)
tr.translate(self.size*0.4, 0)
else:
self.flare.translate(-self.size*0.4, 0)
tr.translate(-self.size*0.4, 0)
self.flare.scale(-f * (0.5+np.random.random()*0.1), 1.0)
tr.scale(-f * (0.5+np.random.random()*0.1), 1.0)
self.flare.setTransform(tr)
if self._spaceline is not None:
self._spaceline.setPos(pg.Point(data['x'][self.i], data['t'][self.i]))

View File

@ -295,32 +295,6 @@ if QT_LIB in [PYQT5, PYQT6, PYSIDE2, PYSIDE6]:
# We're using Qt5 which has a different structure so we're going to use a shim to
# recreate the Qt4 structure
__QGraphicsItem_scale = QtWidgets.QGraphicsItem.scale
def scale(self, *args):
if args:
sx, sy = args
tr = self.transform()
tr.scale(sx, sy)
self.setTransform(tr)
else:
return __QGraphicsItem_scale(self)
QtWidgets.QGraphicsItem.scale = scale
def rotate(self, angle):
tr = self.transform()
tr.rotate(angle)
self.setTransform(tr)
QtWidgets.QGraphicsItem.rotate = rotate
def translate(self, dx, dy):
tr = self.transform()
tr.translate(dx, dy)
self.setTransform(tr)
QtWidgets.QGraphicsItem.translate = translate
QtGui.QApplication = QtWidgets.QApplication
QtGui.QGraphicsScene = QtWidgets.QGraphicsScene
QtGui.QGraphicsObject = QtWidgets.QGraphicsObject

View File

@ -42,37 +42,36 @@ def test_simple():
rect = pg.QtGui.QGraphicsRectItem(0, 0, 100, 100)
scene.addItem(rect)
rect.setPos(20,20)
rect.translate(50, 50)
rect.rotate(30)
rect.scale(0.5, 0.5)
tr = pg.QtGui.QTransform()
rect.setTransform(tr.translate(50, 50).rotate(30).scale(0.5, 0.5))
rect1 = pg.QtGui.QGraphicsRectItem(0, 0, 100, 100)
rect1.setParentItem(rect)
rect1.setFlag(rect1.ItemIgnoresTransformations)
rect1.setPos(20, 20)
rect1.scale(2,2)
rect1.setScale(2)
el1 = pg.QtGui.QGraphicsEllipseItem(0, 0, 100, 100)
el1.setParentItem(rect1)
grp = pg.ItemGroup()
grp.setParentItem(rect)
grp.translate(200,0)
grp.rotate(30)
tr = pg.QtGui.QTransform()
grp.setTransform(tr.translate(200, 0).rotate(30))
rect2 = pg.QtGui.QGraphicsRectItem(0, 0, 100, 25)
rect2.setFlag(rect2.ItemClipsChildrenToShape)
rect2.setParentItem(grp)
rect2.setPos(0,25)
rect2.rotate(30)
rect2.setRotation(30)
el = pg.QtGui.QGraphicsEllipseItem(0, 0, 100, 50)
el.translate(10,-5)
el.scale(0.5,2)
tr = pg.QtGui.QTransform()
el.setTransform(tr.translate(10, -5).scale(0.5, 2))
el.setParentItem(rect2)
grp2 = pg.ItemGroup()
scene.addItem(grp2)
grp2.scale(100,100)
grp2.setScale(100)
rect3 = pg.QtGui.QGraphicsRectItem(0,0,2,2)
rect3.setPen(pg.mkPen(width=1, cosmetic=False))

View File

@ -302,7 +302,7 @@ class TerminalGraphicsItem(GraphicsObject):
self.box = QtGui.QGraphicsRectItem(0, 0, 10, 10, self)
on_update = self.labelChanged if self.term.isRenamable() else None
self.label = TextItem(self.term.name(), self, on_update)
self.label.scale(0.7, 0.7)
self.label.setScale(0.7)
self.newConnection = None
self.setFiltersChildEvents(True) ## to pick up mouse events on the rectitem
if self.term.isRenamable():

View File

@ -50,7 +50,7 @@ class AxisItem(GraphicsWidget):
if orientation not in ['left', 'right', 'top', 'bottom']:
raise Exception("Orientation argument must be one of 'left', 'right', 'top', or 'bottom'.")
if orientation in ['left', 'right']:
self.label.rotate(-90)
self.label.setRotation(-90)
self.style = {
'tickTextOffset': [5, 2], ## (horizontal, vertical) spacing between text and axis

View File

@ -17,7 +17,7 @@ class ButtonItem(GraphicsObject):
if width is not None:
s = float(width) / self.pixmap.width()
self.scale(s, s)
self.setScale(s)
if parentItem is not None:
self.setParentItem(parentItem)
self.setOpacity(0.7)

View File

@ -80,7 +80,7 @@ class CurvePoint(GraphicsObject):
ang = np.arctan2(p2.y()-p1.y(), p2.x()-p1.x()) ## returns radians
self.resetTransform()
if self._rotate:
self.rotate(180+ ang * 180 / np.pi) ## takes degrees
self.setRotation(180 + np.rad2deg(ang)) ## takes degrees
QtGui.QGraphicsItem.setPos(self, *newPos)
return True

View File

@ -151,7 +151,8 @@ class TickSliderItem(GraphicsWidget):
elif ort != 'bottom':
raise Exception("%s is not a valid orientation. Options are 'left', 'right', 'top', and 'bottom'" %str(ort))
self.translate(self.tickSize/2., 0)
tr = QtGui.QTransform.fromTranslate(self.tickSize/2., 0)
self.setTransform(tr, True)
def addTick(self, x, color=None, movable=True, finish=True):
## public
@ -521,7 +522,8 @@ class GradientEditorItem(TickSliderItem):
============== ===================================================================
"""
TickSliderItem.setOrientation(self, orientation)
self.translate(0, self.rectSize)
tr = QtGui.QTransform.fromTranslate(0, self.rectSize)
self.setTransform(tr, True)
def showMenu(self, ev):
#private

View File

@ -112,7 +112,7 @@ class HistogramLUTItem(GraphicsWidget):
self.plot = self.plots[0] # for backward compatibility.
for plot in self.plots:
plot.rotate(90)
plot.setRotation(90)
self.vb.addItem(plot)
self.fillHistogram(fillHistogram)

View File

@ -203,9 +203,10 @@ class ImageItem(GraphicsObject):
def setRect(self, rect):
"""Scale and translate the image to fit within rect (must be a QRect or QRectF)."""
self.resetTransform()
self.translate(rect.left(), rect.top())
self.scale(rect.width() / self.width(), rect.height() / self.height())
tr = QtGui.QTransform()
tr.translate(rect.left(), rect.top())
tr.scale(rect.width() / self.width(), rect.height() / self.height())
self.setTransform(tr)
def clear(self):
self.image = None

View File

@ -220,7 +220,7 @@ class InfiniteLine(GraphicsObject):
"""
self.angle = angle #((angle+45) % 180) - 45 ## -45 <= angle < 135
self.resetTransform()
self.rotate(self.angle)
self.setRotation(self.angle)
self.update()
def setPos(self, pos):

View File

@ -109,7 +109,7 @@ class LabelItem(GraphicsWidget, GraphicsWidgetAnchor):
def setAngle(self, angle):
self.angle = angle
self.item.resetTransform()
self.item.rotate(angle)
self.item.setRotation(angle)
self.updateMin()

View File

@ -97,8 +97,9 @@ class LinearRegionItem(GraphicsObject):
# and down in horizontal mode.
InfiniteLine(QtCore.QPointF(0, values[0]), angle=0, **lineKwds),
InfiniteLine(QtCore.QPointF(0, values[1]), angle=0, **lineKwds)]
self.lines[0].scale(1, -1)
self.lines[1].scale(1, -1)
tr = QtGui.QTransform.fromScale(1, -1)
self.lines[0].setTransform(tr, True)
self.lines[1].setTransform(tr, True)
elif orientation in ('vertical', LinearRegionItem.Vertical):
self.lines = [
InfiniteLine(QtCore.QPointF(values[0], 0), angle=90, **lineKwds),

View File

@ -1321,8 +1321,8 @@ class ViewBox(GraphicsWidget):
r = QtCore.QRectF(p1, p2)
r = self.childGroup.mapRectFromParent(r)
self.rbScaleBox.setPos(r.topLeft())
self.rbScaleBox.resetTransform()
self.rbScaleBox.scale(r.width(), r.height())
tr = QtGui.QTransform.fromScale(r.width(), r.height())
self.rbScaleBox.setTransform(tr)
self.rbScaleBox.show()
def showAxRect(self, ax, **kwargs):

View File

@ -92,7 +92,7 @@ def test_ImageItem(transpose=False):
img2 = TransposedImageItem(transpose=transpose)
img2.setImage(np.fromfunction(lambda x,y: (x+y)%2, (10, 10)), levels=[-1,2])
view.addItem(img2)
img2.scale(10, 10)
img2.setScale(10)
img2.setZValue(-10)
data[..., 0] *= 1e-9

View File

@ -3,7 +3,7 @@ import sys
import numpy as np
import pytest
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtTest
from pyqtgraph.Qt import QtCore, QtGui, QtTest
from pyqtgraph.tests import assertImageApproved, mouseMove, mouseDrag, mouseClick, TransposedImageItem, resizeWindow
import pytest
@ -122,9 +122,8 @@ def check_getArrayRegion(roi, name, testResize=True, transpose=False):
app.processEvents()
assertImageApproved(win, name+'/roi_getarrayregion_resize', 'Simple ROI region selection, resized.')
img1.scale(1, -1)
img1.setPos(0, img1.height())
img1.rotate(20)
img1.setTransform(QtGui.QTransform().scale(1, -1).rotate(20), True)
rgn = roi.getArrayRegion(data, img1, axes=(1, 2))
img2.setImage(rgn[0, ..., 0])
app.processEvents()
@ -142,9 +141,8 @@ def check_getArrayRegion(roi, name, testResize=True, transpose=False):
assertImageApproved(win, name+'/roi_getarrayregion_inverty', 'Simple ROI region selection, view inverted.', pxCount=pxCount)
roi.setState(initState)
img1.resetTransform()
img1.setPos(0, 0)
img1.scale(1, 0.5)
img1.setTransform(QtGui.QTransform.fromScale(1, 0.5))
rgn = roi.getArrayRegion(data, img1, axes=(1, 2))
img2.setImage(rgn[0, ..., 0])
app.processEvents()