1 | import wx |
---|
2 | from math import log10 |
---|
3 | from math import floor |
---|
4 | |
---|
5 | class RangeDialog(wx.Dialog): |
---|
6 | def __init__(self, parent, id, title="Set Graph Range"): |
---|
7 | wx.Dialog.__init__(self, parent, id, title, size=(350, 175)) |
---|
8 | |
---|
9 | mainbox = wx.BoxSizer(wx.VERTICAL) |
---|
10 | |
---|
11 | x_box = wx.BoxSizer(wx.HORIZONTAL) |
---|
12 | |
---|
13 | xmin_label = wx.StaticText(self, -1, 'xmin: ', size=(50,20)) |
---|
14 | x_box.Add(xmin_label) |
---|
15 | self.xmin_input = wx.TextCtrl(self, -1, size=(75, -1)) |
---|
16 | x_box.Add(self.xmin_input) |
---|
17 | |
---|
18 | x_box.AddSpacer(35) |
---|
19 | |
---|
20 | xmax_label = wx.StaticText(self, -1, "xmax: ", size=(50,20)) |
---|
21 | x_box.Add(xmax_label) |
---|
22 | self.xmax_input = wx.TextCtrl(self, -1, size=(75, -1)) |
---|
23 | x_box.Add(self.xmax_input) |
---|
24 | |
---|
25 | mainbox.Add(x_box, flag=wx.CENTER | wx.ALL, border=10) |
---|
26 | |
---|
27 | y_box = wx.BoxSizer(wx.HORIZONTAL) |
---|
28 | |
---|
29 | ymin_label = wx.StaticText(self, -1, 'ymin: ', size=(50,20)) |
---|
30 | y_box.Add(ymin_label) |
---|
31 | self.ymin_input = wx.TextCtrl(self, -1, size=(75, -1)) |
---|
32 | y_box.Add(self.ymin_input) |
---|
33 | |
---|
34 | y_box.AddSpacer(35) |
---|
35 | |
---|
36 | ymax_label = wx.StaticText(self, -1, "ymax: ", size=(50,20)) |
---|
37 | y_box.Add(ymax_label) |
---|
38 | self.ymax_input = wx.TextCtrl(self, -1, size=(75, -1)) |
---|
39 | y_box.Add(self.ymax_input) |
---|
40 | |
---|
41 | mainbox.Add(y_box, flag=wx.CENTER | wx.ALL, border=10) |
---|
42 | |
---|
43 | btn_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
44 | btn_sizer.AddSpacer(20) |
---|
45 | |
---|
46 | ok_btn = wx.Button(self, wx.ID_OK, 'Done') |
---|
47 | btn_sizer.Add(ok_btn) |
---|
48 | |
---|
49 | btn_sizer.AddSpacer(5) |
---|
50 | |
---|
51 | cancel_btn = wx.Button(self, wx.ID_CANCEL, 'Cancel') |
---|
52 | btn_sizer.Add(cancel_btn) |
---|
53 | |
---|
54 | mainbox.Add(btn_sizer, flag=wx.CENTER, border=10) |
---|
55 | |
---|
56 | self.SetSizer(mainbox) |
---|
57 | |
---|
58 | def GetXRange(self): |
---|
59 | xmin, xmax = (None, None) |
---|
60 | try: |
---|
61 | xmin = float(self.xmin_input.GetValue()) |
---|
62 | except: |
---|
63 | pass |
---|
64 | try: |
---|
65 | xmax = float(self.xmax_input.GetValue()) |
---|
66 | except: |
---|
67 | pass |
---|
68 | |
---|
69 | if xmin is not None and xmax is not None: |
---|
70 | return (xmin, xmax) |
---|
71 | return None |
---|
72 | |
---|
73 | def SetXRange(self, x_range): |
---|
74 | xmin, xmax = x_range |
---|
75 | self.xmin_input.SetValue(str(self._round_to_sigfig(xmin, 8))) |
---|
76 | self.xmax_input.SetValue(str(self._round_to_sigfig(xmax, 8))) |
---|
77 | |
---|
78 | def GetYRange(self): |
---|
79 | ymin, ymax = (None, None) |
---|
80 | try: |
---|
81 | ymin = float(self.ymin_input.GetValue()) |
---|
82 | except: |
---|
83 | pass |
---|
84 | try: |
---|
85 | ymax = float(self.ymax_input.GetValue()) |
---|
86 | except: |
---|
87 | pass |
---|
88 | |
---|
89 | if ymin is not None and ymax is not None: |
---|
90 | return (ymin, ymax) |
---|
91 | return None |
---|
92 | |
---|
93 | def SetYRange(self, y_range): |
---|
94 | ymin, ymax = y_range |
---|
95 | self.ymin_input.SetValue(str(self._round_to_sigfig(ymin, 8))) |
---|
96 | self.ymax_input.SetValue(str(self._round_to_sigfig(ymax, 8))) |
---|
97 | |
---|
98 | def _round_to_sigfig(self, x, sigfigs): |
---|
99 | if x == 0.0: |
---|
100 | return 0.0 |
---|
101 | return round(x, sigfigs-1-int(floor(log10(abs(x))))) |
---|