-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_connections_sidebar.py
173 lines (151 loc) · 5.94 KB
/
gui_connections_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
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
"""Create GUI connections sidebar panel.
Used in gui.py to make instance of the GUI connections sidebar panel.
Classes
-------
ConnectionsSidebarPanel - creates wx panel for GUI connections sidebar panel
"""
import wx
from wx.core import Command, SingleChoiceDialog
class ConnectionsSidebarPanel(wx.Panel):
"""Create wx.Panel for connections sidebar."""
def __init__(
self,
parent,
names,
devices,
network,
monitors,
push_status,
output_cmd,
):
"""Initialise ConnectionsSidebarPanel 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
output_cmd
GUI command line output_cmd function
"""
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.output_cmd = output_cmd
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.current_connections_text_list = []
self.new_connections_text_list = []
# Create widgets
info_text1 = wx.StaticText(
self, wx.ID_ANY, _("Choose a connection to replace:")
)
info_text2 = wx.StaticText(self, wx.ID_ANY, _("Replace ..."))
self.dropdown_find = wx.Choice(
self,
wx.CB_DROPDOWN | wx.CB_READONLY,
choices=self.current_connections_text_list,
name="find",
)
info_text3 = wx.StaticText(self, wx.ID_ANY, _("with ..."))
self.dropdown_replace = wx.Choice(
self,
wx.CB_DROPDOWN | wx.CB_READONLY,
choices=self.new_connections_text_list,
name="replace",
)
self.replace_button = wx.Button(self, wx.ID_ANY, _("Replace"))
self.sizer.Add(info_text1, 0, wx.ALL, 3)
self.sizer.Add(info_text2, 0, wx.ALL, 3)
self.sizer.Add(self.dropdown_find, 0, wx.ALL | wx.EXPAND, 3)
self.sizer.Add(info_text3, 0, wx.ALL, 3)
self.sizer.Add(self.dropdown_replace, 0, wx.ALL | wx.EXPAND, 3)
self.sizer.Add(self.replace_button, 0, wx.ALL | wx.EXPAND, 3)
# Bind events to event handlers
self.dropdown_find.Bind(wx.EVT_CHOICE, self.on_dropdown_find)
self.replace_button.Bind(wx.EVT_BUTTON, self.on_click_replace)
self.SetSizer(self.sizer)
def update_dropdown_find(self):
"""Update the dropdown to choose the first connection.
(The connection that will be replaced)
"""
self.current_connections_text_list = []
self.dropdown_find.Clear()
self.dropdown_replace.Clear()
self.connections_dict = self.network.get_all_connections()
self.dropdown_find.Append("")
self.current_connections_text_list = [""]
for key in self.connections_dict:
second_device_id = key[0]
second_port_id = key[1]
first_device_id = self.connections_dict[key][0]
first_port_id = self.connections_dict[key][1]
text = ""
text += self.devices.get_signal_name(
first_device_id, first_port_id
)
text += " > "
text += self.devices.get_signal_name(
second_device_id, second_port_id
)
self.current_connections_text_list.append(text)
self.dropdown_find.Append(text)
def on_dropdown_find(self, event):
"""Handle the event when the user uses the first (find) dropdown."""
self.new_connections_text_list = []
self.dropdown_replace.Clear()
selection_index = self.dropdown_find.GetCurrentSelection()
selected_text = self.dropdown_find.GetString(selection_index)
signal_names = selected_text.split(" > ")
first_signal_name = signal_names[0]
second_signal_name = signal_names[1]
[monitored, not_monitored] = self.monitors.get_signal_names()
self.new_connections_text_list = [*monitored, *not_monitored]
for i in range(len(self.new_connections_text_list)):
third_signal_name = self.new_connections_text_list[i]
text = "{} > {}".format(third_signal_name, second_signal_name)
if text != selected_text:
self.new_connections_text_list.append(text)
self.dropdown_replace.Append(text)
def on_click_replace(self, event):
"""Handle the event when the user clicks the Replace button."""
selection_index = self.dropdown_replace.GetCurrentSelection()
selected_text = self.dropdown_replace.GetString(selection_index)
signal_names = selected_text.split(" > ")
third_signal_name = signal_names[0]
second_signal_name = signal_names[1]
third_device_id, third_port_id = self.devices.get_signal_ids(
third_signal_name
)
second_device_id, second_port_id = self.devices.get_signal_ids(
second_signal_name
)
selection_index = self.dropdown_find.GetCurrentSelection()
selected_text = self.dropdown_find.GetString(selection_index)
first_signal_name = selected_text.split(" > ")[0]
self.output_cmd(
_(
"\nReplacing connection\n" "{} > {}" " with " "{} > {}"
).format(
first_signal_name,
second_signal_name,
third_signal_name,
second_signal_name,
)
)
self.network.replace_connection(
second_device_id, second_port_id, third_device_id, third_port_id
)
self.update_dropdown_find()