|
32 | 32 |
|
33 | 33 | import logging |
34 | 34 | import numpy |
| 35 | +import weakref |
35 | 36 |
|
36 | 37 | from silx.gui import icons |
37 | 38 |
|
@@ -85,14 +86,20 @@ def __init__( |
85 | 86 | super().__init__(parent=parent, plot=plot) |
86 | 87 | self.setCheckable(True) |
87 | 88 | self._roiManager = None |
88 | | - self._lastRoiCreated = None |
| 89 | + self.__lastRoiCreated = None |
89 | 90 | self.setIcon(icons.getQIcon("ruler")) |
90 | 91 | self.toggled.connect(self._callback) |
91 | 92 | self._connectPlot(plot) |
92 | 93 |
|
93 | 94 | def setPlot(self, plot): |
94 | 95 | return super().setPlot(plot) |
95 | 96 |
|
| 97 | + @property |
| 98 | + def _lastRoiCreated(self): |
| 99 | + if self.__lastRoiCreated is None: |
| 100 | + return None |
| 101 | + return self.__lastRoiCreated() |
| 102 | + |
96 | 103 | def _callback(self, *args, **kwargs): |
97 | 104 | if not self._roiManager: |
98 | 105 | return |
@@ -132,23 +139,25 @@ def _connectPlot(self, plot): |
132 | 139 | def _disconnectPlot(self, plot): |
133 | 140 | if plot and self._lastRoiCreated is not None: |
134 | 141 | self._roiManager.removeRoi(self._lastRoiCreated) |
135 | | - self._lastRoiCreated = None |
| 142 | + self.__lastRoiCreated = None |
136 | 143 | return super()._disconnectPlot(plot) |
137 | 144 |
|
138 | 145 | def _registerCurrentROI(self, currentRoi): |
139 | 146 | if self._lastRoiCreated is None: |
140 | | - self._lastRoiCreated = currentRoi |
| 147 | + self.__lastRoiCreated = weakref.ref(currentRoi) |
141 | 148 | 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: |
143 | 150 | 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) |
146 | 153 |
|
147 | 154 | def buildDistanceText(self, startPoint, endPoint): |
148 | 155 | """ |
149 | | - define the text to be displayed by the ruler. |
| 156 | + Define the text to be displayed by the ruler. |
| 157 | +
|
150 | 158 | 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...) |
152 | 161 | """ |
153 | 162 | distance = numpy.linalg.norm(endPoint - startPoint) |
154 | 163 | return f"{distance: .1f}px" |
0 commit comments