-
Notifications
You must be signed in to change notification settings - Fork 1
/
state_widgets.py
258 lines (208 loc) · 8.05 KB
/
state_widgets.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import multiprocessing as mp
import sys
import tkinter as tk
from threading import Thread
from tkinter import messagebox as msgBx
from tkinter import scrolledtext
from tkinter import ttk
# iconlocation = 'C:\\Users\\Jeff\\Documents\\School\\rams_icon_dark_2.ico'
done = False
class visualizer():
def done_func(self):
self.winRoot.destroy()
global done
done = True
def __init__(self, inputArray):
self.iarray = inputArray
self.winRoot = tk.Tk()
self.winRoot.title("Machine System States")
self.winRoot.protocol("WM_DELETE_WINDOW", self.done_func)
self.started = True
# winRoot.iconbitmap(iconlocation)
# ~ mainButton = ttk.Button(winRoot,text = "Start", command=self.processActions({})).grid(column=0, row = 5)
self.new_state = BuildStateFrame(self.winRoot, "System States", inputArray)
self.winRoot.update_idletasks()
self.winRoot.update()
# ~ def processActions(self,valsToSend):
# ~ runProcess = True
# ~ if self.started:
# ~ self.createThread(valsToSend)
# ~ def createThread(self, valsToSend):
# ~ runT = Thread(target=self.startProcessing(valsToSend)) ## example threading program
# ~ runT.setDaemon(True)
# ~ runT.start()
## Read the binary strings here and pass them on
def startProcessing(self, valsToSend):
self.new_state.setFrameState(valsToSend)
self.winRoot.update_idletasks()
self.winRoot.update()
# ~ if not self.started:
# ~ started = True
class BuildStateFrame(ttk.LabelFrame):
def __init__(self, parent, systemName, stateDescData):
ttk.LabelFrame.__init__(self, parent, text=systemName)
self.grid(column=0, row=0, sticky='ew') ##position of the frame in the window
# ~ self.MAX_STATES = 60 #### needs determining jve
# state colors
colorPallet = ["grey", "green", "red", "cyan"]
self.normalColorPallet = []
self.normalColorPallet.append(colorPallet[0]) # off
self.normalColorPallet.append(colorPallet[1]) # green
self.normalColorPallet.append(colorPallet[3]) # cyan
self.abnormalColorPallet = []
self.abnormalColorPallet.append(colorPallet[0]) # off
self.abnormalColorPallet.append(colorPallet[2]) # red
self.abnormalColorPallet.append(colorPallet[3]) # cyan
# Determine the number of columns that will be created to determine
# the number of rows to set
numberStates = len(stateDescData)
# ~ if numberStates > self.MAX_STATES:
# ~ print("TOO MANY STATES FOR SYSTEM")
# ~ # ERROR DIALOG
# ~ msg = "System is unable to handle " + str(numberStates) + " states. \nMaximum is " + str(self.MAX_STATES) + "."
# ~ msgBx.showerror("TOO MANY STATES FOR SYSTEM", msg)
# ~ sys.exit(1)
if numberStates < 11:
numColsDisp = numberStates
elif numberStates < 31:
numColsDisp = numberStates // 2
else:
numColsDisp = 15
# track for row and column position used in button defs
rowNum = 1
colNum = 0
# create an array for the buttons
statePos = 0
self.stateBtnArray = [] * numberStates
dict = stateDescData
for key in dict.keys():
values = dict[key]
arrayPos = key[1]
stateLabelName = key[0]
stateType = values[0]
stateDesc = "." # not passing in the initial string - will be updated
activeValue = values[2]
if stateType.lower() == 'abnormal':
stateBtn = CircleButton(self, 32, 32, self.abnormalColorPallet, stateLabelName, stateDesc, activeValue)
elif stateType.lower() == 'normal':
stateBtn = CircleButton(self, 32, 32, self.normalColorPallet, stateLabelName, stateDesc, activeValue)
else:
print("BAD SUBSYSTEM TYPE DEFINITION")
# ERROR DIALOG
msg = "System definition, " + stateType + ", is an invalid type."
msgBx.showerror("Invalid state definition", msg)
sys.exit(1)
stateBtn.grid(column=colNum, row=rowNum)
self.stateBtnArray.insert(arrayPos, stateBtn)
statePos += 1
# k += 1
# label
self.topLabel = ttk.Label(self, text=stateLabelName).grid(column=colNum, row=rowNum - 1)
colNum += 1
if (colNum == numColsDisp):
self.spacerLabel = " \n "
self.topLeftLabel = ttk.Label(self, text=self.spacerLabel).grid(column=0, row=rowNum + 1)
colNum = 0
rowNum += 3
# set a minimum width of all columns for display uniformity
col_count, row_count = self.grid_size()
for col in range(col_count):
self.grid_columnconfigure(col, minsize=85)
############ UPDATING STATES ################################
def setFrameState(self, newStates):
self.createDThread(newStates)
def createDThread(self, newStates):
runT = Thread(target=self.makeSettings(newStates)) ## example threading program
runT.setDaemon(True)
runT.start()
def makeSettings(self, newStates):
for key in newStates.keys():
arrayPos = key[1] # the position in the button array
stateLabelName = key[0] # unneeded - just for checking
stateDesc = newStates[key][0] # active value to be sent to the button
activeValue = newStates[key][1] # new description text for the button
# perform the update
state = self.stateBtnArray[arrayPos]
state.setStateColor(activeValue)
state.updateDescription(stateDesc)
# repaint
self.update_idletasks()
###############################################################################
class CircleButton(tk.Canvas):
def __init__(self, parent, width, height, colors, stateName, description, activeValue):
tk.Canvas.__init__(self, parent, borderwidth=1, relief="flat", highlightthickness=0)
self.colors = colors
self.stateName = stateName
self.description = description
self.lastSetValue = 0
self.activationValue = activeValue
self.myColor = 0
# Create the button and actions
spacePd = 4
self.id = self.create_oval((spacePd, spacePd, width + spacePd, height + spacePd), outline=colors[0],
fill=colors[0])
(x0, y0, x1, y1) = self.bbox("all")
width = (x1 - x0) + spacePd
height = (y1 - y0) + spacePd
self.configure(width=width, height=height)
self.bind("<ButtonPress-1>", self._on_press)
self.bind("<ButtonRelease-1>", self._on_release)
self.itemconfig(self.id, fill=self.colors[0])
def updateDescription(self, inString):
self.description = inString
# Button actions
def _on_press(self, event):
self.configure(relief="sunken")
def _on_release(self, event):
self.configure(relief="flat")
self.lclClick()
# just an int value is incoming
def setStateColor(self, inStateValue):
if (int(
inStateValue) != self.activationValue): # != implies less than, because the max value inStateValue can attend is activatonBValue
if int(self.lastSetValue) == 1: # was active, goto transition
self.itemconfig(self.id, fill=self.colors[2])
self.lastSetValue = 2
elif int(self.lastSetValue) == 2: # in transition
self.itemconfig(self.id, fill=self.colors[0])
self.lastSetValue = 0
else:
# if int(self.lastSetValue) == 0 or int(self.lastSetValue) == 2: #refer to the paper state diagram
self.itemconfig(self.id, fill=self.colors[1])
self.lastSetValue = 1
############ Data Section ############
def showData(self):
self.infoWindow = tk.Toplevel(self)
# self.infoWindow.wm_iconbitmap(iconlocation)
self.infoWindow.wm_title("SUBSYSTEM: " + self.stateName)
self.sytemLabel = ttk.Label(self.infoWindow, text="Description:").grid(column=0, row=0, sticky=tk.W)
self.systemList = scrolledtext.ScrolledText(self.infoWindow, width=45, height=10, wrap=tk.WORD)
self.systemList.grid(column=0, row=1)
self.disMissBtn = ttk.Button(self.infoWindow, text="Dismiss", command=self.dismissAction)
self.disMissBtn.grid(column=0, row=2)
self.textToScrolledText(self.description)
# repaint
self.update_idletasks()
def createThread(self):
self.runT = mp.Process(target=self.showData())
# ~ runT.setDaemon(True)
self.runT.start()
def lclClick(self):
self.createThread()
def textToScrolledText(self, inText):
self.systemList.insert(tk.INSERT, inText)
def dismissAction(self):
self.infoWindow.destroy()
self.runT.terminate()
viz = None
def process(val):
if not done:
viz.startProcessing(val)
return None
def start_up(args):
global viz
viz = visualizer(args)
def communicate(in_q, out_q, args): # args is a dictionary of whatever this module needs
start_up(args)
import lib.process as proc
proc.io(in_q, out_q, process, "visualizer")