From 92010646915d5a646a8e90d5c628401e694b5848 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauld.mccarthy@gmail.com> Date: Mon, 9 Nov 2015 22:19:08 +0000 Subject: [PATCH] MelodicComponentGrid refactorings. --- fsl/data/melodicresults.py | 6 +- .../controls/melodicclassificationgrid.py | 100 ++++++++++++------ 2 files changed, 68 insertions(+), 38 deletions(-) diff --git a/fsl/data/melodicresults.py b/fsl/data/melodicresults.py index e453a7b24..bf7c66808 100644 --- a/fsl/data/melodicresults.py +++ b/fsl/data/melodicresults.py @@ -240,7 +240,7 @@ class MelodicClassification(props.HasProperties): def addLabel(self, component, label): labels = list(self.labels[component]) - comps = self.__components.get(label, []) + comps = list(self.__components.get(label, [])) if label in labels: return @@ -258,7 +258,7 @@ class MelodicClassification(props.HasProperties): def removeLabel(self, component, label): labels = list(self.labels[component]) - comps = self.__components.get(label, []) + comps = list(self.__components.get(label, [])) if label not in labels: return @@ -270,7 +270,7 @@ class MelodicClassification(props.HasProperties): self.__components[label] = comps log.debug('Label removed from component: {} <-> {}'.format(component, - label)) + label)) def clearLabels(self, component): diff --git a/fsl/fsleyes/controls/melodicclassificationgrid.py b/fsl/fsleyes/controls/melodicclassificationgrid.py index 51c66690c..632b9f8af 100644 --- a/fsl/fsleyes/controls/melodicclassificationgrid.py +++ b/fsl/fsleyes/controls/melodicclassificationgrid.py @@ -101,7 +101,6 @@ class ComponentGrid(fslpanel.FSLEyesPanel): if not isinstance(overlay, fslmelimage.MelodicImage): return - self.__overlay = overlay display = self._displayCtx.getDisplay(overlay) opts = display.getDisplayOpts() @@ -128,29 +127,8 @@ class ComponentGrid(fslpanel.FSLEyesPanel): """ overlay = self.__overlay - lut = self.__lut - melclass = overlay.getICClassification() numComps = overlay.numComponents() - labels = [l.name() for l in lut.labels] - colours = [l.colour() for l in lut.labels] - - # Compile lists of all the existing - # labels, and the colours for each one - for i in range(numComps): - - for label in melclass.getLabels(i): - if label in labels: - continue - - colour = self.__addNewLutLabel(label).colour() - - labels .append(label) - colours.append(colour) - - for i in range(len(colours)): - colours[i] = [int(round(c * 255)) for c in colours[i]] - for i in range(numComps): tags = texttag.TextTagPanel(self.__grid, @@ -159,8 +137,6 @@ class ComponentGrid(fslpanel.FSLEyesPanel): texttag.TTP_NO_DUPLICATES | texttag.TTP_KEYBOARD_NAV)) - tags.SetOptions(labels, colours) - # Store the component number on the tag # panel, so we know which component we # are dealing with in the __onTagAdded @@ -170,19 +146,54 @@ class ComponentGrid(fslpanel.FSLEyesPanel): self.__grid.SetText( i, 0, str(i)) self.__grid.SetWidget(i, 1, tags) - for label in melclass.getLabels(i): - tags.AddTag(label) - tags.Bind(texttag.EVT_TTP_TAG_ADDED_EVENT, self.__onTagAdded) tags.Bind(texttag.EVT_TTP_TAG_REMOVED_EVENT, self.__onTagRemoved) + self.__refreshTags() + self.Layout() + + def __refreshTags(self): + + overlay = self.__overlay + melclass = overlay.getICClassification() + numComps = overlay.numComponents() + lut = self.__lut + + labels = [l.name() for l in lut.labels] + colours = [l.colour() for l in lut.labels] + + # Compile lists of all the existing + # labels, and the colours for each one + for i in range(numComps): + + for label in melclass.getLabels(i): + if label in labels: + continue + + colour = self.__addNewLutLabel(label).colour() + + labels .append(label) + colours.append(colour) + + for i in range(len(colours)): + colours[i] = [int(round(c * 255)) for c in colours[i]] + + for row in range(numComps): + tags = self.__grid.GetWidget(row, 1) + + tags.ClearTags() + tags.SetOptions(labels, colours) + + for label in melclass.getLabels(row): + tags.AddTag(label) + def __addNewLutLabel(self, label, colour=None): """ """ - + lut = self.__lut value = lut.max() + 1 @@ -193,8 +204,10 @@ class ComponentGrid(fslpanel.FSLEyesPanel): lut.set(value, name=label, colour=colour) lut.enableListener('labels', self._name) - return lut.get(value) + self.__refreshTags() + return lut.get(value) + def __onTagAdded(self, ev): """ @@ -207,15 +220,25 @@ class ComponentGrid(fslpanel.FSLEyesPanel): lut = self.__lut melclass = overlay.getICClassification() + # Add the new label to the melodic component + melclass.disableListener('labels', self._name) + melclass.addLabel(component, label) + + # If the tag panel previously just contained + # the 'Unknown' tag, remove that tag + if tags.TagCount() == 2 and tags.HasTag('Unknown'): + melclass.removeLabel(component, 'Unknown') + tags.RemoveTag('Unknown') + + melclass.enableListener('labels', self._name) + + # If the newly added tag is not in + # the lookup table, add it in if lut.getByName(label) is None: colour = tags.GetTagColour(label) colour = [c / 255.0 for c in colour] self.__addNewLutLabel(label, colour) - melclass.disableListener('labels', self._name) - melclass.addLabel(component, label) - melclass.enableListener('labels', self._name) - self.__grid.FitInside() @@ -229,9 +252,16 @@ class ComponentGrid(fslpanel.FSLEyesPanel): overlay = self.__overlay melclass = overlay.getICClassification() + # Remove the label from + # the melodic component melclass.disableListener('labels', self._name) melclass.removeLabel(component, label) melclass.enableListener('labels', self._name) + + # If the tag panel now has no tags, + # add the 'Unknown' tag back in. + if tags.TagCount() == 0: + tags.AddTag('Unknown') self.__grid.FitInside() @@ -260,11 +290,11 @@ class ComponentGrid(fslpanel.FSLEyesPanel): def __labelsChanged(self, *a): - self.__recreateTags() + self.__refreshTags() def __lutChanged(self, *a): - self.__recreateTags() + self.__refreshTags() -- GitLab