From e8f1d0adbdc3380ec1e7f0eaf1b155117963e8c0 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauld.mccarthy@gmail.com> Date: Sun, 6 Dec 2015 14:27:27 +0000 Subject: [PATCH] ComponentGrid and LabelGrid no longer listen to selectedOverlay/overlay list - the displayed overlay is manually specified by the MelodicClassificationPanel. This is to reduce the amount of unnecessary refreshes. --- .../controls/melodicclassificationgrid.py | 148 ++++++++---------- .../controls/melodicclassificationpanel.py | 3 + 2 files changed, 72 insertions(+), 79 deletions(-) diff --git a/fsl/fsleyes/controls/melodicclassificationgrid.py b/fsl/fsleyes/controls/melodicclassificationgrid.py index 2c7b13f47..a242bd69e 100644 --- a/fsl/fsleyes/controls/melodicclassificationgrid.py +++ b/fsl/fsleyes/controls/melodicclassificationgrid.py @@ -29,8 +29,8 @@ class ComponentGrid(fslpanel.FSLEyesPanel): """The ``ComponentGrid`` uses a :class:`.WidgetGrid`, and a set of :class:`.TextTagPanel` widgets, to display the component classifications stored in the :class:`.MelodicClassification` object that is associated - with the currently selected overlay (if this overlay is a - :class:`.MelodicImage`. + with a :class:`.MelodicImage`. The ``MelodicImage`` is specified via + the :meth:`setOverlay` method. The grid contains one row for each component, and a ``TextTagPanel`` is used to display the labels associated with each component. Each @@ -68,16 +68,9 @@ class ComponentGrid(fslpanel.FSLEyesPanel): self.__grid.Bind(widgetgrid.EVT_WG_SELECT, self.__onGridSelect) - lut .addListener('labels', self._name, self.__lutChanged) - displayCtx .addListener('selectedOverlay', - self._name, - self.__selectedOverlayChanged) - overlayList.addListener('overlays', - self._name, - self.__selectedOverlayChanged) + lut.addListener('labels', self._name, self.__lutChanged) self.__overlay = None - self.__selectedOverlayChanged() def destroy(self): @@ -86,53 +79,25 @@ class ComponentGrid(fslpanel.FSLEyesPanel): :meth:`.FSLEyesPanel.destroy`. """ - self._displayCtx .removeListener('selectedOverlay', self._name) - self._overlayList.removeListener('overlays', self._name) - self.__lut .removeListener('labels', self._name) + self.__lut.removeListener('labels', self._name) self.__deregisterCurrentOverlay() self.__lut = None fslpanel.FSLEyesPanel.destroy(self) - - def __deregisterCurrentOverlay(self): - """Called when the selected overlay changes. De-registers listeners - associated with the previously selected overlay, if necessary. - """ - - if self.__overlay is None: - return - - overlay = self.__overlay - self.__overlay = None - melclass = overlay.getICClassification() - melclass.removeListener('labels', self._name) - - try: - display = self._displayCtx.getDisplay(overlay) - opts = display.getDisplayOpts() - opts .removeListener('volume', self._name) - display.removeListener('overlayType', self._name) - - except fsldisplay.InvalidOverlayError: - pass - - - def __selectedOverlayChanged(self, *a): - """Called when the :attr:`.DisplayContext.selectedOverlay` changes. If - the overlay is a :class:`.MelodicImage`, the :class:`.WidgetGrid` is - re-populated to display the component-label mappings contained in the - associated :class:`.MelodicClassification` instance. + def setOverlay(self, overlay): + """Sets the :class:`.MelodicImage` to display component labels for. + The :class:`.WidgetGrid` is re-populated to display the + component-label mappings contained in the + :class:`.MelodicClassification` instance associated with the overlay.. """ self.__deregisterCurrentOverlay() self.__grid.ClearGrid() self.__grid.Refresh() - overlay = self._displayCtx.getSelectedOverlay() - if not isinstance(overlay, fslmelimage.MelodicImage): return @@ -151,11 +116,45 @@ class ComponentGrid(fslpanel.FSLEyesPanel): melclass.addListener('labels', self._name, self.__labelsChanged) display .addListener('overlayType', self._name, - self.__selectedOverlayChanged) + self.__overlayTypeChanged) self.__recreateTags() self.__volumeChanged() + + def __deregisterCurrentOverlay(self): + """Called when the selected overlay changes. De-registers listeners + associated with the previously selected overlay, if necessary. + """ + + if self.__overlay is None: + return + + overlay = self.__overlay + self.__overlay = None + + melclass = overlay.getICClassification() + melclass.removeListener('labels', self._name) + + try: + display = self._displayCtx.getDisplay(overlay) + opts = display.getDisplayOpts() + opts .removeListener('volume', self._name) + display.removeListener('overlayType', self._name) + + except fsldisplay.InvalidOverlayError: + pass + + + def __overlayTypeChanged(self, *a): + """Called when the :attr:`.Display.overlayType` of the currently + displayed overlay changes. When the type of an overlay changes, + a new :class:`.DisplayOpts` instance is created, so we need to + re-register various property listeners with this new + ``DisplayOpts`` instance. + """ + self.setOverlay(self.__overlay) + def __recreateTags(self): """Re-creates a :class:`.TextTagPanel` for every component in the @@ -376,8 +375,8 @@ class LabelGrid(fslpanel.FSLEyesPanel): """The ``LabelGrid`` class is the inverse of the :class:`ComponentGrid`. It uses a :class:`.WidgetGrid` to display the label-component mappings present on the :class:`.MelodicClassification` instance associated with - the currently selected overlay (if this overlay is a - :class:`.MelodicImage`. + a :class:`.MelodicImage`. The ``MelodicImage`` is specified via + the :meth:`setOverlay` method. The grid contains one row for each label, and a :class:`.TextTagPanel` is used to display the components associated with each label. Each @@ -415,17 +414,10 @@ class LabelGrid(fslpanel.FSLEyesPanel): self.__grid.Bind(widgetgrid.EVT_WG_SELECT, self.__onGridSelect) - lut .addListener('labels', self._name, self.__lutChanged) - displayCtx .addListener('selectedOverlay', - self._name, - self.__selectedOverlayChanged) - overlayList.addListener('overlays', - self._name, - self.__selectedOverlayChanged) + lut.addListener('labels', self._name, self.__lutChanged) self.__overlay = None self.__recreateGrid() - self.__selectedOverlayChanged() def destroy(self): @@ -434,37 +426,19 @@ class LabelGrid(fslpanel.FSLEyesPanel): :meth:`.FSLEyesPanel.destroy`. """ - self._displayCtx .removeListener('selectedOverlay', self._name) - self._overlayList.removeListener('overlays', self._name) - self.__lut .removeListener('labels', self._name) + self.__lut.removeListener('labels', self._name) self.__deregisterCurrentOverlay() self.__lut = None fslpanel.FSLEyesPanel.destroy(self) - - def __deregisterCurrentOverlay(self): - """Called when the selected overlay changes. De-registers property - listeners associated with the previously selected overlay, if - necessary. - """ - - if self.__overlay is None: - return - - overlay = self.__overlay - self.__overlay = None - - melclass = overlay.getICClassification() - melclass.removeListener('labels', self._name) - - def __selectedOverlayChanged(self, *a): - """Called when the :attr:`.DisplayContext.selectedOverlay` changes. - If the overlay is a :class:`.MelodicImage`, a listener is registered - with its :class:`.MelodicClassification`, and its component-label - mappings displayed on the :class:`.WidgetGrid`. + def setOverlay(self, overlay): + """Set the :class:`.MelodicImage` shown on this ``LabelGrid``. A + listener is registered with its :class:`.MelodicClassification`, + and its component-label mappings displayed on the + :class:`.WidgetGrid`. """ self.__deregisterCurrentOverlay() @@ -481,6 +455,22 @@ class LabelGrid(fslpanel.FSLEyesPanel): self.__refreshTags() + + def __deregisterCurrentOverlay(self): + """Called when the selected overlay changes. De-registers property + listeners associated with the previously selected overlay, if + necessary. + """ + + if self.__overlay is None: + return + + overlay = self.__overlay + self.__overlay = None + + melclass = overlay.getICClassification() + melclass.removeListener('labels', self._name) + def __recreateGrid(self): """Clears the :class:`.WidgetGrid`, and re-creates diff --git a/fsl/fsleyes/controls/melodicclassificationpanel.py b/fsl/fsleyes/controls/melodicclassificationpanel.py index 9e788a744..380d75769 100644 --- a/fsl/fsleyes/controls/melodicclassificationpanel.py +++ b/fsl/fsleyes/controls/melodicclassificationpanel.py @@ -161,6 +161,9 @@ class MelodicClassificationPanel(fslpanel.FSLEyesPanel): overlay = self._displayCtx.getSelectedOverlay() + self.__componentGrid.setOverlay(overlay) + self.__labelGrid .setOverlay(overlay) + if (overlay is None) or \ not isinstance(overlay, fslmelimage.MelodicImage): self.__enable(False, strings.messages[self, 'disabled']) -- GitLab