-
Notifications
You must be signed in to change notification settings - Fork 11
/
DictManager.py
75 lines (62 loc) · 2.96 KB
/
DictManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from atom.api import (Atom, List, ContainerList, Dict, observe, Callable, Typed, Unicode)
import enaml
class DictManager(Atom):
"""
Control - Presenter for a dictionary of items.
i.e. give the ability to add/delete rename items
"""
itemDict = Typed(dict)
displayFilter = Callable() # filter which items to display later
possibleItems = List() # a list of classes that can possibly be added to this list
displayList = ContainerList()
onChangeDelegate = Callable()
otherActions = Dict(Unicode(), Callable())
def __init__(self, itemDict={}, displayFilter=lambda x: True, **kwargs):
self.displayFilter = displayFilter
super(DictManager, self).__init__(itemDict=itemDict, displayFilter=displayFilter, **kwargs)
def add_item(self, parent):
"""
Create a new item dialog window and handle the result
"""
with enaml.imports():
from widgets.dialogs import AddItemDialog
dialogBox = AddItemDialog(parent, modelNames=[i.__name__ for i in self.possibleItems], objText='')
dialogBox.exec_()
if dialogBox.result:
if dialogBox.newLabel not in self.itemDict.keys():
self.itemDict[dialogBox.newLabel] = self.possibleItems[dialogBox.newModelNum](label=dialogBox.newLabel)
self.displayList.append(dialogBox.newLabel)
else:
print("WARNING: Can't use duplicate label %s"%dialogBox.newLabel)
def remove_item(self, itemLabel):
#check that the item exists before removing from the list
if itemLabel in self.itemDict.keys():
self.itemDict.pop(itemLabel)
#TODO: once ContainerDicts land see if we still need this
self.displayList.pop(self.displayList.index(itemLabel))
elif itemLabel != '':
self.displayList.pop(self.displayList.index(itemLabel))
def name_changed(self, oldLabel, newLabel):
# Add copy of changing item
self.itemDict[newLabel] = self.itemDict[oldLabel]
# update display list
idx = self.displayList.index(oldLabel)
self.displayList[idx] = newLabel
# remove old label from itemDict list
if oldLabel in self.itemDict.keys():
self.itemDict.pop(oldLabel)
else:
print("WARNING: %s is not in the list"%oldLabel)
# update label to new label list
self.itemDict[newLabel].label = newLabel
if self.onChangeDelegate:
self.onChangeDelegate(oldLabel, newLabel)
def update_enable(self, itemLabel, checkState):
self.itemDict[itemLabel].enabled = checkState
@observe('itemDict')
def update_display_list(self, change):
"""
Eventualy itemDict will be a ContainerDict and this will fire on all events.
Will have to be more careful about whether it is a "create" event or "update"
"""
self.displayList = sorted([v.label for v in self.itemDict.values() if self.displayFilter(v)])