-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialogs.py
More file actions
232 lines (192 loc) · 8.65 KB
/
dialogs.py
File metadata and controls
232 lines (192 loc) · 8.65 KB
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
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
from __future__ import print_function
import traceback
import wx
import wx.lib.agw.floatspin
import wx.lib.dialogs
import wx.xrc as xrc
try:
from typing import Tuple
except:
pass
import config
from rd60xx import rdwrap
import rdgui_xrc
from utils import appendlistitem
# assume main module added our XmlResourceHandler
_ = wx.GetTranslation
class WrappedMultiMessageDialog(wx.lib.dialogs.MultiMessageDialog):
def __init__(self, parent, message, caption = "Message Box", msg2="",
style = wx.OK | wx.CANCEL, pos = wx.DefaultPosition, icon=None,
btnLabels=None):
super(WrappedMultiMessageDialog, self).__init__(parent, message, caption,
msg2, style, pos, icon, btnLabels)
# this is evil
for w in self.Children:
if isinstance(w, wx.TextCtrl) and (w.WindowStyle & wx.TE_DONTWRAP) != 0:
new = wx.TextCtrl(self, value=w.Value,
style=(w.WindowStyle & ~(wx.TE_DONTWRAP|wx.HSCROLL)) | wx.TE_BESTWRAP)
new.SetMinSize(w.GetMinSize())
self.Sizer.Replace(w, new, True)
w.Destroy()
self.Layout()
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnClose(self, evt):
self.EndModal(wx.ID_CLOSE)
class DlgPortSelector(rdgui_xrc.xrcdlgPortSelector):
def __init__(self, parent):
super(DlgPortSelector, self).__init__(parent)
self.ctlComportList = self.ctlComportList # type: wx.ListCtrl
self.wxID_OK = self.wxID_OK # type: wx.Button
if wx.GetApp().config.mock_data:
appendlistitem(self.ctlComportList, "port", "desc", "hwid")
import serial.tools.list_ports
for port, desc, hwid in serial.tools.list_ports.comports():
appendlistitem(self.ctlComportList, port, desc, hwid)
self.wxID_OK.Enable(False)
def OnButton_wxID_CANCEL(self, evt):
# type: (wx.CommandEvent) -> None
self.EndModal(evt.Id)
def OnButton_wxID_OK(self, evt):
# type: (wx.CommandEvent) -> None
sel = self.ctlComportList.GetFirstSelected()
if (sel != -1):
self.port = self.ctlComportList.GetItemText(sel) # type: str
self.EndModal(evt.Id)
def OnList_item_deselected_ctlComportList(self, evt):
# type: (wx.ListEvent) -> None
self.wxID_OK.Enable(False)
def OnList_item_selected_ctlComportList(self, evt):
# type: (wx.ListEvent) -> None
self.wxID_OK.Enable(True)
def OnList_item_activated_ctlComportList(self, evt):
# type: (wx.ListEvent) -> None
self.port = evt.Text # type: str
self.EndModal(wx.ID_OK)
class DlgSettings(rdgui_xrc.xrcdlgSettings, config.ConfigChangeHandler):
def __init__(self, parent):
super(DlgSettings, self).__init__(parent)
self.config = wx.GetApp().config # type: config.Config
self.config.Subscribe(self)
self.ctlGraphSeconds = self.ctlGraphSeconds # type: wx.lib.agw.floatspin.FloatSpin
self.ctlPollingInterval = self.ctlPollingInterval # type: wx.lib.agw.floatspin.FloatSpin
self.ctlVoltageRange = self.ctlVoltageRange # type: wx.lib.agw.floatspin.FloatSpin
self.ctlAmperageRange = self.ctlAmperageRange # type: wx.lib.agw.floatspin.FloatSpin
self.wxID_APPLY = self.wxID_APPLY # type: wx.Button
self.ctlGraphSeconds.SetDefaultValue(self.config.graph_seconds)
self.ctlGraphSeconds.SetToDefaultValue()
self.ctlPollingInterval.SetDefaultValue(self.config.polling_interval)
self.ctlPollingInterval.SetToDefaultValue()
self.ctlVoltageRange.SetDefaultValue(self.config.voltage_range)
self.ctlVoltageRange.SetToDefaultValue()
self.ctlAmperageRange.SetDefaultValue(self.config.amperage_range)
self.ctlAmperageRange.SetToDefaultValue()
def OnClose(self, evt):
# type: (wx.CloseEvent) -> None
self.config.Unsubscribe(self)
evt.Skip()
def OnSpinctrl(self, evt):
# type: (wx.CommandEvent) -> None
self.wxID_APPLY.Enable()
OnSpinctrl_ctlGraphSeconds = OnSpinctrl
OnSpinctrl_ctlPollingInterval = OnSpinctrl
OnSpinctrl_ctlVoltageRange = OnSpinctrl
OnSpinctrl_ctlAmperageRange = OnSpinctrl
def OnButton_wxID_OK(self, evt):
# type: (wx.CommandEvent) -> None
self.OnApply()
self.EndModal(evt.Id)
def OnButton_wxID_CANCEL(self, evt):
# type: (wx.CommandEvent) -> None
self.EndModal(evt.Id)
def OnButton_wxID_APPLY(self, evt):
# type: (wx.CommandEvent) -> None
self.OnApply()
def OnApply(self):
dirty = False
if not self.ctlGraphSeconds.IsDefaultValue():
self.config.graph_seconds = self.ctlGraphSeconds.GetValue()
dirty = True
if not self.ctlPollingInterval.IsDefaultValue():
self.config.polling_interval = self.ctlPollingInterval.GetValue()
dirty = True
if not self.ctlVoltageRange.IsDefaultValue():
self.config.voltage_range = self.ctlVoltageRange.GetValue()
dirty = True
if not self.ctlAmperageRange.IsDefaultValue():
self.config.amperage_range = self.ctlAmperageRange.GetValue()
dirty = True
if dirty:
self.config.Save()
self.wxID_APPLY.Enable(False)
def OnConfigChangeEnd(self, updates):
if 'graph_seconds' in updates:
self.ctlGraphSeconds.SetDefaultValue(updates['graph_seconds'])
if 'polling_interval' in updates:
self.ctlPollingInterval.SetDefaultValue(updates['polling_interval'])
if 'voltage_range' in updates:
self.ctlVoltageRange.SetDefaultValue(updates['voltage_range'])
if 'amperage_range' in updates:
self.ctlAmperageRange.SetDefaultValue(updates['amperage_range'])
class DlgCalibration(rdgui_xrc.xrcdlgCalibration):
def __init__(self, parent):
super(DlgCalibration, self).__init__(parent)
self.spinctrls = (
xrc.XRCCTRL(self, "ctlVOutputZero"),
xrc.XRCCTRL(self, "ctlVOutputScale"),
xrc.XRCCTRL(self, "ctlVReadbackZero"),
xrc.XRCCTRL(self, "ctlVReadbackScale"),
xrc.XRCCTRL(self, "ctlAOutputZero"),
xrc.XRCCTRL(self, "ctlAOutputScale"),
xrc.XRCCTRL(self, "ctlAReadbackZero"),
xrc.XRCCTRL(self, "ctlAReadbackScale"),
) # type: Tuple[wx.SpinCtrl, ...]
if wx.GetApp().config.mock_data:
self.initial_regs = [18, 22802, 22, 17585, 276, 21458, 77, 17418]
else:
with rdwrap.lock:
self.initial_regs = rdwrap.rd._read_registers(0x37, 8) # type: list[int]
for ctrl, reg in zip(self.spinctrls, self.initial_regs):
ctrl.SetValue(reg)
def OnButton_wxID_OK(self, evt):
# type: (wx.CommandEvent) -> None
ans = wx.MessageBox(
"{}\n\n{}".format(
_("Are you sure you want to commit calibration data?"),
_("Note: calibration data can be reset to default by holding \"1\" while powering on the unit.")
),
_("Commit calibration data"),
wx.YES_NO|wx.NO_DEFAULT|wx.ICON_WARNING,
self)
if ans == wx.YES:
regs = [ctrl.GetValue() for ctrl in self.spinctrls]
with rdwrap.lock:
# TODO: should we do this or rely on the SpinEvents?
rdwrap.rd._write_registers(0x37, regs)
rdwrap.rd._write_register(0x36, 0x1501)
self.EndModal(evt.Id)
def OnButton_wxID_CANCEL(self, evt):
# type: (wx.CommandEvent) -> None
try:
with rdwrap.lock:
rdwrap.rd._write_registers(0x37, self.initial_regs)
except:
wx.lib.dialogs.MultiMessageBox(
_("An error occurred while attempting to restore calibration data"),
_("Error restoring calibration data"),
traceback.format_exc(), wx.OK|wx.ICON_ERROR, self)
self.EndModal(evt.Id)
def OnSpinctrl(self, evt):
# type: (wx.SpinEvent) -> None
i = self.spinctrls.index(evt.EventObject)
with rdwrap.lock:
rdwrap.rd._write_register(0x37 + i, evt.GetInt())
OnSpinctrl_ctlVOutputZero = OnSpinctrl
OnSpinctrl_ctlVOutputScale = OnSpinctrl
OnSpinctrl_ctlVReadbackZero = OnSpinctrl
OnSpinctrl_ctlVReadbackScale = OnSpinctrl
OnSpinctrl_ctlAOutputZero = OnSpinctrl
OnSpinctrl_ctlAOutputScale = OnSpinctrl
OnSpinctrl_ctlAReadbackZero = OnSpinctrl
OnSpinctrl_ctlAReadbackScale = OnSpinctrl