forked from onp/gmcr-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets_f02a_02_infeasTreeview.py
124 lines (97 loc) · 4.34 KB
/
widgets_f02a_02_infeasTreeview.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright: (c) Oskar Petersons 2013
"""Treeview widget for displaying misperceived states removed.
Loaded by the frame_02_infeasibles module.
Copied from a very similar widget used on the infeasibles screen.
"""
from tkinter import Tk, N, S, E, W, VERTICAL
from tkinter import ttk
from data_01_conflictModel import ConflictModel
NSEW = (N, S, E, W)
class TreeInfeas(ttk.Frame):
"""Treeview widget for displaying infeasible states removed."""
def __init__(self, master, conflict=None, *args):
"""Initialize the widget."""
ttk.Frame.__init__(self, master, padding=(5))
self.conflict = conflict
self.tDisp = ttk.Treeview(self, columns=('state', 'stDes', 'stRem'),
selectmode='browse')
self.scrl = ttk.Scrollbar(self, orient=VERTICAL,
command=self.tDisp.yview)
self.upBtn = ttk.Button(self, width=10, text='Up', command=self.upCmd)
self.downBtn = ttk.Button(self, width=10, text='Down',
command=self.downCmd)
self.delBtn = ttk.Button(self, width=10, text='Delete',
command=self.delCmd)
# ##########
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.tDisp.heading('state', text='Misperceived State')
self.tDisp.heading('stDes', text='# of States Described')
self.tDisp.heading('stRem', text='# of States Removed')
self.tDisp['show'] = 'headings'
self.tDisp.grid(column=0, row=0, columnspan=5, sticky=NSEW)
self.scrl.grid(column=5, row=0, sticky=NSEW)
self.tDisp.configure(yscrollcommand=self.scrl.set)
self.upBtn.grid(column=2, row=2, sticky=NSEW)
self.downBtn.grid(column=3, row=2, sticky=NSEW)
self.delBtn.grid(column=4, row=2, sticky=NSEW)
self.tDisp.bind('<<TreeviewSelect>>', self.selChgCmd)
def refresh(self):
"""Fully refreshes the list displayed."""
chldn = self.tDisp.get_children()
for chld in chldn:
self.tDisp.delete(chld)
for misp in self.activeDM.misperceptions:
key = misp.name
self.tDisp.insert('', 'end', key, text=key)
self.tDisp.set(key, 'state', key)
self.tDisp.set(key, 'stDes', str(2**(key.count('-'))))
self.tDisp.set(key, 'stRem', str(misp.statesRemoved))
def selChgCmd(self, *args):
"""Called whenever the selection changes."""
self.tDisp.selId = self.tDisp.selection()
self.tDisp.selIdx = self.tDisp.index(self.tDisp.selId)
self.event_generate('<<SelItem>>', x=self.tDisp.selIdx)
def upCmd(self, *args):
"""Called whenever an item is moved upwards."""
idx = self.tDisp.selIdx
if idx != 0:
self.activeDM.misperceptions.moveCondition(idx, idx - 1)
self.activeDM.calculatePerceived()
self.event_generate('<<ValueChange>>')
self.tDisp.selection_set(self.tDisp.selId)
self.selChgCmd()
def downCmd(self, *args):
"""Called whenever an item is moved downwards."""
idx = self.tDisp.selIdx
if idx != len(self.conflict.infeasibles) - 1:
self.activeDM.misperceptions.moveCondition(idx, idx + 1)
self.activeDM.calculatePerceived()
self.event_generate('<<ValueChange>>')
self.tDisp.selection_set(self.tDisp.selId)
self.selChgCmd()
def delCmd(self, *args):
"""Called when an item is deleted."""
idx = self.tDisp.selIdx
self.activeDM.misperceptions.removeCondition(idx)
self.activeDM.calculatePerceived()
self.event_generate('<<ValueChange>>')
if len(self.activeDM.misperceptions) > 0:
try:
self.tDisp.selection_set(
self.activeDM.misperceptions[idx].name)
except IndexError:
self.tDisp.selection_set(
self.activeDM.misperceptions[idx - 1].name)
def main():
"""Run widget in test window."""
root = Tk()
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
g1 = ConflictModel()
g1.load_from_file('Examples/SyriaIraq.gmcr')
theTree = TreeInfeas(root, g1)
theTree.grid(column=0, row=0, sticky=NSEW)
root.mainloop()
if __name__ == '__main__':
main()