-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_switches_sidebar.py
112 lines (92 loc) · 3.61 KB
/
gui_switches_sidebar.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
"""Create GUI switches sidebar panel.
Used in gui.py to make instance of the GUI monitors sidebar panel.
Classes
-------
MonitorsSidebarPanel - creates wx panel for GUI monitors sidebar panel
"""
import wx
import wx.lib.scrolledpanel
class SwitchesSidebarPanel(wx.Panel):
"""Create wx.Panel for switches sidebar."""
def __init__(
self, parent, names, devices, network, monitors, push_status, input_cmd
):
"""Initialise SwitchesSidebarPanel panel.
Parameters
----------
parent
parent panel
names
instance of names class
devices
instance of devices class
network
instance of network class
monitors
instance of monitors class
push_status
GUI statusbar pushstatus function
input_cmd
GUI command line input_cmd function to output to GUI command line
"""
wx.Panel.__init__(self, parent)
# self.SetBackgroundColour(wx.WHITE)
self.parent = parent
self.names = names
self.devices = devices
self.network = network
self.monitors = monitors
self.push_status = push_status
self.input_cmd = input_cmd
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.scroll_sizer = wx.BoxSizer(wx.VERTICAL)
info_text = wx.StaticText(
self, wx.ID_ANY, _("Change state of switches:")
)
self.scroll_panel = wx.lib.scrolledpanel.ScrolledPanel(
self, -1 # , style=wx.SIMPLE_BORDER
)
self.scroll_panel.SetupScrolling()
self.scroll_panel.SetSizer(self.scroll_sizer)
self.sizer.Add(info_text, 0, wx.ALL, 3)
self.sizer.Add(self.scroll_panel, 1, wx.ALL | wx.EXPAND, 0)
self.SetSizer(self.sizer)
def update_list(self):
"""Update list of switches."""
self.switches_list = self.devices.find_devices(self.devices.SWITCH)
for i in range(len(self.switches_list)):
device_id = self.switches_list[i]
name = self.devices.get_signal_name(device_id, None)
init_state = self.devices.get_device(device_id).switch_state
self.create_switch(name, device_id, init_state)
self.SetSizer(self.sizer)
def create_switch(self, name, device_id, init_state):
"""Create text and button inside a horizontal sizer.
with text and switch state initialised to correct value
"""
text = wx.StaticText(self.scroll_panel, wx.ID_ANY, name)
toggle = wx.ToggleButton(
self.scroll_panel, device_id, label=str(init_state)
)
if init_state == 1:
toggle.SetValue(True)
switch_sizer = wx.BoxSizer(wx.HORIZONTAL)
switch_sizer.Add(text, 1, wx.ALIGN_CENTER | wx.ALL, 1)
switch_sizer.Add(toggle, 1, wx.EXPAND | wx.ALL, 1)
toggle.Bind(wx.EVT_TOGGLEBUTTON, self.on_toggle_switch)
self.scroll_sizer.Add(switch_sizer, 0, wx.ALL | wx.EXPAND, 0)
def on_toggle_switch(self, event):
"""Handle the event where the user toggles one of the switches."""
toggle = event.GetEventObject()
id = toggle.GetId()
switch_name = self.devices.get_signal_name(id, None)
current_state = int(toggle.GetLabel())
if current_state == 1:
new_state = 0
elif current_state == 0:
new_state = 1
toggle.SetLabel(str(new_state))
command = "s {} {}".format(switch_name, new_state)
self.input_cmd(command)
text = _("Switch {} toggled to {}").format(switch_name, new_state)
self.push_status(text)