Improve drawing performance by caching the line and bounding rect.

This commit is contained in:
Alessandro Bacchini 2016-02-02 15:31:48 +01:00
parent fd76443458
commit 98ff70e8a0

View File

@ -64,6 +64,9 @@ class InfiniteLine(GraphicsObject):
self.setHoverPen(color=(255,0,0), width=self.pen.width()) self.setHoverPen(color=(255,0,0), width=self.pen.width())
self.currentPen = self.pen self.currentPen = self.pen
self._boundingRect = None
self._line = None
def setMovable(self, m): def setMovable(self, m):
"""Set whether the line is movable by the user.""" """Set whether the line is movable by the user."""
self.movable = m self.movable = m
@ -135,6 +138,10 @@ class InfiniteLine(GraphicsObject):
newPos[1] = min(newPos[1], self.maxRange[1]) newPos[1] = min(newPos[1], self.maxRange[1])
if self.p != newPos: if self.p != newPos:
# Invalidate bounding rect and line
self._boundingRect = None
self._line = None
self.p = newPos self.p = newPos
GraphicsObject.setPos(self, Point(self.p)) GraphicsObject.setPos(self, Point(self.p))
self.update() self.update()
@ -175,7 +182,18 @@ class InfiniteLine(GraphicsObject):
#print "ignore", change #print "ignore", change
#return GraphicsObject.itemChange(self, change, val) #return GraphicsObject.itemChange(self, change, val)
def viewTransformChanged(self):
self._boundingRect = None
self._line = None
GraphicsObject.viewTransformChanged(self)
def viewChanged(self, view, oldView):
self._boundingRect = None
self._line = None
GraphicsObject.viewChanged(self, view, oldView)
def boundingRect(self): def boundingRect(self):
if self._boundingRect is None:
#br = UIGraphicsItem.boundingRect(self) #br = UIGraphicsItem.boundingRect(self)
br = self.viewRect() br = self.viewRect()
## add a 4-pixel radius around the line for mouse interaction. ## add a 4-pixel radius around the line for mouse interaction.
@ -186,12 +204,14 @@ class InfiniteLine(GraphicsObject):
w = (max(4, self.pen.width()/2, self.hoverPen.width()/2)+1) * px w = (max(4, self.pen.width()/2, self.hoverPen.width()/2)+1) * px
br.setBottom(-w) br.setBottom(-w)
br.setTop(w) br.setTop(w)
return br.normalized() br = br.normalized()
self._boundingRect = br
self._line = QtCore.QLineF(br.right(), 0, br.left(), 0)
return self._boundingRect
def paint(self, p, *args): def paint(self, p, *args):
br = self.boundingRect()
p.setPen(self.currentPen) p.setPen(self.currentPen)
p.drawLine(Point(br.right(), 0), Point(br.left(), 0)) p.drawLine(self._line)
def dataBounds(self, axis, frac=1.0, orthoRange=None): def dataBounds(self, axis, frac=1.0, orthoRange=None):
if axis == 0: if axis == 0: