@@ -12,45 +12,153 @@ Created on Jul 12, 2015
12
12
"""
13
13
import textwrap
14
14
from inkcut.core.utils import load_icon
15
- from enaml.layout.api import hbox, align, spacer
15
+ from enaml.layout.api import hbox, vbox, align, spacer
16
16
from enaml.qt.QtWidgets import QApplication
17
- from enaml.widgets.api import Container, Form, Label, ObjectCombo, SpinBox, CheckBox, PushButton
17
+ from enaml.widgets.api import Container, Form, Label, ObjectCombo, SpinBox, CheckBox, PushButton, Menu, Action, Field
18
+ from enaml.validator import IntValidator
19
+ from inkcut.core.api import log
20
+ from enaml.stdlib.message_box import question
18
21
import serial
19
22
23
+ enamldef HexPair(Field):
24
+ attr number = 0
25
+ mask = "HHHH"
26
+ validator = IntValidator(minimum=0, maximum=0x888ffff, base=16)
27
+ text << "{:0>4x}".format(number)
28
+ text :: self.number = int(change['value'], 16)
29
+ max_length = 4
30
+
20
31
enamldef SerialPortSettingsView(Container):
21
32
attr model
22
33
attr exclusive = False
34
+ attr show_all_ports = False
23
35
padding = 0
24
36
25
37
alias dsrdtr
26
38
func selected_port(port, ports):
27
- matches = [p for p in ports if p.device_path == port]
39
+ matches = [p for p in ports if p.device_path == port and model.port_matches(p) ]
28
40
return matches[0] if matches else None
29
41
42
+ func get_ports(candidates):
43
+ if show_all_ports:
44
+ return candidates
45
+ else:
46
+ return [port for port in candidates if model.port_matches(port)]
47
+
48
+ func port_string(port):
49
+ if model.port_matches(port):
50
+ return str(port)
51
+ return "{}{}".format(QApplication.translate("serialport", "(not compatible): "), (str(port)))
52
+
53
+ func filter_update_confirm(result):
54
+ if result and result.action == 'accept':
55
+ return True
56
+ return False
57
+
58
+ func refresh_filter_clear():
59
+ filter_clear.enabled = model.has_port_filter()
60
+
30
61
Form:
31
62
Label:
32
63
text = QApplication.translate("serialport", "Port")
33
64
Container:
34
65
padding = 0
35
66
constraints = [
36
- hbox(cb, pb),
37
- align('v_center', cb, pb)
67
+ vbox(
68
+ hbox(cb, pb),
69
+ hbox(filter_label, filter_descr, filter_label_vid, filter_vid,
70
+ filter_label_pid, filter_pid, clear_group),
71
+ ),
72
+ align('v_center', cb, pb),
73
+ align('v_center', filter_label, filter_label_vid, filter_label_pid, filter_descr),
74
+ (filter_vid.width == 50) | 'medium',
75
+ (filter_pid.width == 50) | 'medium',
76
+ filter_vid.width == filter_pid.width
38
77
]
39
78
ObjectCombo: cb:
40
- items << model.ports
79
+ items << get_ports( model.ports)
41
80
selected << selected_port(model.port, model.ports)
81
+ to_string = port_string
42
82
selected ::
43
83
port = change['value']
44
84
if port:
45
- model.port = port.device_path
85
+ if not model.port_matches(port):
86
+ if filter_update_confirm(question(
87
+ self, '', QApplication.translate("serialport",
88
+ "Port does not match the filter, do you want to update filter?"))):
89
+ model.clear_filter()
90
+ model.make_filter(port)
91
+ model.port = port.device_path
92
+ refresh_filter_clear()
93
+ else:
94
+ model.port = ""
95
+ cb.selected = None
96
+ cb._refresh_proxy(dict(type='update'))
97
+ else:
98
+ old_port = change['oldvalue']
99
+ if not model.has_port_filter() and\
100
+ (old_port and old_port.device_path != port.device_path):
101
+ model.make_filter(port)
102
+ model.port = port.device_path
46
103
tool_tip = textwrap.dedent("""
47
104
List of serial ports detected by the system. If nothing is here you
48
105
must install the device driver for your machine.
49
106
""").strip()
50
- PushButton: pb:
51
- text = QApplication.translate("serialport", "Refresh")
52
- icon = load_icon("arrow_refresh")
53
- clicked :: model.refresh()
107
+ Container: pb:
108
+ padding = 0
109
+ constraints = [
110
+ hbox(pb2, pb3, spacing=0),
111
+ pb2.height == pb3.height,
112
+ pb3.width == pb3.height
113
+ ]
114
+ hug_width = 'medium'
115
+ PushButton: pb2:
116
+ text = QApplication.translate("serialport", "Refresh")
117
+ icon = load_icon("arrow_refresh")
118
+ clicked :: model.refresh()
119
+ PushButton: pb3:
120
+ Menu:
121
+ Action:
122
+ text = QApplication.translate("serialport", "Show all")
123
+ checkable = True
124
+ checked := show_all_ports
125
+ checked :: model.refresh()
126
+ Label: filter_label:
127
+ text = QApplication.translate("serialport", "Filter: ")
128
+ Field: filter_descr:
129
+ text := model.port_filter_name
130
+ text :: refresh_filter_clear()
131
+ Label: filter_label_vid:
132
+ text = QApplication.translate("serialport", "VID")
133
+ HexPair: filter_vid:
134
+ number := model.port_filter_vid
135
+ number :: refresh_filter_clear()
136
+ resist_width = 'weak'
137
+ Label: filter_label_pid:
138
+ text = QApplication.translate("serialport", "PID")
139
+ HexPair: filter_pid:
140
+ number := model.port_filter_pid
141
+ number :: refresh_filter_clear()
142
+ resist_width = 'weak'
143
+ Container: clear_group:
144
+ padding = 0
145
+ constraints = [
146
+ hbox(filter_clear, pb4, spacing=0),
147
+ pb4.height == filter_clear.height,
148
+ pb4.width == pb4.height
149
+ ]
150
+ PushButton: filter_clear:
151
+ text = QApplication.translate("serialport", "Clear")
152
+ icon = load_icon("cancel")
153
+ clicked ::
154
+ model.clear_filter()
155
+ model.refresh()
156
+ refresh_filter_clear()
157
+ PushButton: pb4:
158
+ Menu:
159
+ Action:
160
+ text = QApplication.translate("serialport", "Make filter")
161
+ triggered :: model.make_filter(cb.selected)
54
162
Label:
55
163
text = QApplication.translate("serialport", "Baudrate")
56
164
SpinBox:
0 commit comments