Skip to content

Potential issues with GraphicsItem.pixelVectors caching #1189

@erikmansson

Description

@erikmansson

I found some potential issues in the caching in GraphicsItem.pixelVectors.

In both the local cache lookup:

if direction is None and dt == self._pixelVectorCache[0]:
return tuple(map(Point, self._pixelVectorCache[1])) ## return a *copy*

and global cache lookup:

key = (dt.m11(), dt.m21(), dt.m12(), dt.m22())
pv = self._pixelVectorGlobalCache.get(key, None)
if direction is None and pv is not None:
self._pixelVectorCache = [dt, pv]
return tuple(map(Point,pv)) ## return a *copy*

the cached value is used if direction is None (implicitly QtCore.QPointF(1, 0)). However, the cached value may have been calculated for any direction, and the direction does seem not affect the key obtained from dt. This may result in returning an incorrect value in when pixelVectors has been called with an explicit direction prior to calling it with direction=None.

Furthermore, when pixelVectors is called with direction=Point([1, 0]), as done here:

pxl = self.pixelLength(Point([1, 0]))

and here

px = self.pixelLength(direction=Point(1,0), ortho=True) ## get pixel length orthogonal to the line

through here:

def pixelLength(self, direction, ortho=False):
"""Return the length of one pixel in the direction indicated (in local coordinates)
If ortho=True, then return the length of one pixel orthogonal to the direction indicated.
Return None if pixel size is not yet defined (usually because the item has not yet been displayed).
"""
normV, orthoV = self.pixelVectors(direction)

No cache lookup is done since direction is not None, but should be since it's set to that value implicitly if direction is None. This slows down updating e.g. InfiniteLines a lot, which is an issue for a project I'm currently working on.

I'd be happy to help out fixing this, but I'd like to know what the intended behavior is first. Is including the direction in the cache key and then not checking if direction is None a potential good fix/improvement?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions