Skip to content

Commit 83f8477

Browse files
committed
Use weakref
# Conflicts: # src/silx/gui/plot/PlotToolButtons.py
1 parent 33bbb59 commit 83f8477

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/silx/gui/plot/tools/RulerToolButton.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import logging
3434
import numpy
35+
import weakref
3536

3637
from silx.gui import icons
3738

@@ -85,14 +86,20 @@ def __init__(
8586
super().__init__(parent=parent, plot=plot)
8687
self.setCheckable(True)
8788
self._roiManager = None
88-
self._lastRoiCreated = None
89+
self.__lastRoiCreated = None
8990
self.setIcon(icons.getQIcon("ruler"))
9091
self.toggled.connect(self._callback)
9192
self._connectPlot(plot)
9293

9394
def setPlot(self, plot):
9495
return super().setPlot(plot)
9596

97+
@property
98+
def _lastRoiCreated(self):
99+
if self.__lastRoiCreated is None:
100+
return None
101+
return self.__lastRoiCreated()
102+
96103
def _callback(self, *args, **kwargs):
97104
if not self._roiManager:
98105
return
@@ -132,23 +139,25 @@ def _connectPlot(self, plot):
132139
def _disconnectPlot(self, plot):
133140
if plot and self._lastRoiCreated is not None:
134141
self._roiManager.removeRoi(self._lastRoiCreated)
135-
self._lastRoiCreated = None
142+
self.__lastRoiCreated = None
136143
return super()._disconnectPlot(plot)
137144

138145
def _registerCurrentROI(self, currentRoi):
139146
if self._lastRoiCreated is None:
140-
self._lastRoiCreated = currentRoi
147+
self.__lastRoiCreated = weakref.ref(currentRoi)
141148
self._lastRoiCreated.registerFormatFunction(self.buildDistanceText)
142-
elif currentRoi != self._lastRoiCreated and self._roiManager is not None:
149+
elif currentRoi is not self._lastRoiCreated and self._roiManager is not None:
143150
self._roiManager.removeRoi(self._lastRoiCreated)
144-
self._lastRoiCreated = currentRoi
145-
self._lastRoiCreated.registerFormatFunction(self.buildDistanceText)
151+
currentRoi.registerFormatFunction(self.buildDistanceText)
152+
self.__lastRoiCreated = weakref.ref(currentRoi)
146153

147154
def buildDistanceText(self, startPoint, endPoint):
148155
"""
149-
define the text to be displayed by the ruler.
156+
Define the text to be displayed by the ruler.
157+
150158
It can be redefine to modify precision or handle other parameters
151-
(handling pixel size to display metric distance, display distance on each distance - for non-square pixels...)
159+
(handling pixel size to display metric distance, display distance
160+
on each distance - for non-square pixels...)
152161
"""
153162
distance = numpy.linalg.norm(endPoint - startPoint)
154163
return f"{distance: .1f}px"

0 commit comments

Comments
 (0)