Skip to content

Commit

Permalink
Add a guard around check_states update.
Browse files Browse the repository at this point in the history
The order up enaml subscription updates is undefined, so we cannot rely upon the
QtListStrWidget's `checked_states` list being the same length as `items` when
on_edit() fires.
  • Loading branch information
blakejohnson committed Jun 15, 2016
1 parent bb04a33 commit 94e635b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
7 changes: 3 additions & 4 deletions DictManagerView.enaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ enamldef DictManagerView(Container): dictView:
QtListStrWidget: itemList:
items := dictManager.displayList
validator = labelValidator
checked_states = [dictManager.itemDict[i].enabled for i in dictManager.displayList]
initialized ::
checked_states << [dictManager.itemDict[i].enabled for i in dictManager.displayList]
initialized ::
itemList.item_changed.connect(dictManager.name_changed)
itemList.enable_changed.connect(dictManager.update_enable)

Expand All @@ -47,9 +47,8 @@ enamldef DictManagerView(Container): dictView:
text = "Remove"
clicked :: dictManager.remove_item(itemList.selected_item)
Container: selectedItemView:
MappedView:
MappedView:
model << dictManager.itemDict[itemList.selected_item] if itemList.selected_item else None
typemap = dictView.viewMap
modelkey = dictView.modelName
kwargs = dictView.viewkwargs if dictView.viewkwargs else {}

1 change: 0 additions & 1 deletion ExpSettingsGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ def populate_physical_channels(self):
pc.translator = instr.translator
pc.samplingRate = instr.samplingRate
self.channels[label] = pc
print self.channels.channelDict
self.physicalChannelManager.update_display_list(None)


Expand Down
50 changes: 23 additions & 27 deletions widgets/qt_list_str_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class QtListStrWidget(RawWidget):
#: The list of str being viewed
items = d_(List(Unicode()))

checked_states = d_(ContainerList(Bool()))
checked_states = d_(List(Bool()))

#: The index of the currently selected str
selected_index = d_(Int(-1))

#: The currently selected str
selected_item = d_(Unicode())

Expand All @@ -35,16 +35,12 @@ class QtListStrWidget(RawWidget):
#: Whether or not the items should be editable
editable = d_(Bool(True))

#
validator = d_(Callable())

#: .
hug_width = set_default('weak')

item_changed = Signal()
enable_changed = Signal()


#--------------------------------------------------------------------------
# Initialization API
#--------------------------------------------------------------------------
Expand All @@ -63,7 +59,7 @@ def create_widget(self, parent):
if self.items:
self.selected_index = 0
self.selected_item = self.items[0]
widget.setCurrentRow(0)
widget.setCurrentRow(0)

widget.itemSelectionChanged.connect(self.on_selection)
widget.itemChanged.connect(self.on_edit)
Expand All @@ -84,34 +80,35 @@ def add_item(self, widget, item, checked=True):
# Signal Handlers
#--------------------------------------------------------------------------
def on_selection(self):
"""
"""
The signal handler for the index changed signal.
"""
widget = self.get_widget()
self.selected_index = widget.currentRow()
self.selected_item = self.items[widget.currentRow()] if self.selected_index >= 0 else u''
self.selected_item = self.items[widget.currentRow()] if self.selected_index >= 0 else u''

def on_edit(self, item):
"""
"""
The signal handler for the item changed signal.
"""
widget = self.get_widget()
itemRow = widget.indexFromItem(item).row()
oldLabel = self.items[itemRow]
newLabel = item.text()
#only signal the enable change when the labels are the same and is in
#the item list, also only signal a name change when the labels are not
#the same and the newlabel is not in the item list

# only signal the enable change when the labels are the same and is in
# the item list, also only signal a name change when the labels are not
# the same and the newlabel is not in the item list
if newLabel == oldLabel and newLabel in self.items:
self.checked_states[itemRow] = True if item.checkState() == Qt.Checked else False
self.enable_changed(item.text(), self.checked_states[itemRow])
if itemRow < len(self.checked_states):
self.checked_states[itemRow] = True if item.checkState() == Qt.Checked else False
self.enable_changed(item.text(), self.checked_states[itemRow])
elif oldLabel != newLabel and newLabel not in self.items:
self.item_changed(oldLabel, newLabel)
self.selected_item = item.text()
self.items[itemRow] = item.text()
self.apply_validator(item, newLabel)

#--------------------------------------------------------------------------
# ProxyListStrView API
#--------------------------------------------------------------------------
Expand All @@ -124,7 +121,7 @@ def set_items(self, items, widget = None):
nitems = len(items)
for idx, item in enumerate(items[:count]):
itemWidget = widget.item(idx)
#Update checked state before the text so that we can distinguish a checked state change from a label change
# Update checked state before the text so that we can distinguish a checked state change from a label change
itemWidget.setCheckState(Qt.Checked if self.checked_states[idx] else Qt.Unchecked)
itemWidget.setText(item)
self.apply_validator(itemWidget, item)
Expand Down Expand Up @@ -154,24 +151,23 @@ def _update_proxy(self, change):
"""
# The superclass handler implementation is sufficient.
widget =self.get_widget()
widget = self.get_widget()
if widget != None:
if change["name"] == "items":
if change["type"] == "update":
if len(change["oldvalue"]) > len(change["value"]):
#We've lost an item
# We've lost an item
removedKey = set(change["oldvalue"]) - set(change["value"])
removedIndex = change["oldvalue"].index(list(removedKey)[0])
del self.checked_states[removedIndex]
elif len(change["oldvalue"]) < len(change["value"]):
self.checked_states.append(True)

self.set_items(self.items)


#update the selected item because the current row has changed
self.selected_item = self.items[widget.currentRow()] if self.selected_index >= 0 else u''


# update the selected item because the current row has changed
self.selected_item = self.items[widget.currentRow()] if self.selected_index >= 0 else u''


# Helper methods
def _set_item_flag(item, flag, enabled):
Expand Down

0 comments on commit 94e635b

Please sign in to comment.