1 | import wx |
---|
2 | import wx.lib.newevent |
---|
3 | from parameters_panel_slicer import SlicerParameterPanel |
---|
4 | from sas.sasgui.guiframe.utils import format_number |
---|
5 | from sas.sasgui.guiframe.panel_base import PanelBase |
---|
6 | from sas.sasgui.guiframe.events import (SlicerParameterEvent, EVT_SLICER_PARS, |
---|
7 | EVT_SLICER) |
---|
8 | |
---|
9 | |
---|
10 | class SlicerPanel(wx.Panel, PanelBase): |
---|
11 | """ |
---|
12 | Panel class to show the slicer parameters |
---|
13 | """ |
---|
14 | # Internal name for the AUI manager |
---|
15 | window_name = "Slicer panel" |
---|
16 | # Title to appear on top of the window |
---|
17 | window_caption = "Slicer Panel" |
---|
18 | CENTER_PANE = False |
---|
19 | |
---|
20 | def __init__(self, parent, id=-1, type=None, base=None, |
---|
21 | params=None, *args, **kwargs): |
---|
22 | wx.Panel.__init__(self, parent, id, *args, **kwargs) |
---|
23 | PanelBase.__init__(self) |
---|
24 | # Initialization of the class |
---|
25 | self.base = base |
---|
26 | if params is None: |
---|
27 | params = {} |
---|
28 | self.params = params |
---|
29 | self.parent = base.parent |
---|
30 | self.frame = None |
---|
31 | self.type = type |
---|
32 | self.listeners = [] |
---|
33 | self.parameters = [] |
---|
34 | self.bck = wx.GridBagSizer(5, 5) |
---|
35 | self.SetSizer(self.bck) |
---|
36 | if type is None and params is None: |
---|
37 | label = "Right-click on 2D plot for slicer options" |
---|
38 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
39 | self.bck.Add(title, (0, 0), (1, 2), |
---|
40 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
41 | else: |
---|
42 | self.set_slicer(type, params) |
---|
43 | # Bindings |
---|
44 | self.parent.Bind(EVT_SLICER, SlicerParameterPanel.on_evt_slicer) |
---|
45 | self.parent.Bind(EVT_SLICER_PARS, SlicerParameterPanel.on_param_change) |
---|
46 | |
---|
47 | def set_slicer(self, type, params): |
---|
48 | """ |
---|
49 | Rebuild the panel |
---|
50 | """ |
---|
51 | self.bck.Clear(True) |
---|
52 | self.type = type |
---|
53 | if type is None: |
---|
54 | label = "Right-click on 2D plot for slicer options" |
---|
55 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
56 | self.bck.Add(title, (0, 0), (1, 2), |
---|
57 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
58 | else: |
---|
59 | title_text = str(type) + "Parameters" |
---|
60 | title = wx.StaticText(self, -1, title_text, |
---|
61 | style=wx.ALIGN_LEFT) |
---|
62 | self.bck.Add(title, (0, 0), (1, 2), |
---|
63 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
64 | n = 1 |
---|
65 | self.parameters = [] |
---|
66 | keys = params.keys() |
---|
67 | keys.sort() |
---|
68 | for item in keys: |
---|
69 | if not item.lower() in ["num_points", "avg", "avg_error", "sum", |
---|
70 | "sum_error"]: |
---|
71 | n += 1 |
---|
72 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
73 | self.bck.Add(text, (n - 1, 0), |
---|
74 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, |
---|
75 | border=15) |
---|
76 | ctl = wx.TextCtrl(self, -1, size=(80, 20), |
---|
77 | style=wx.TE_PROCESS_ENTER) |
---|
78 | hint_msg = "Modify the value of %s to change " % item |
---|
79 | hint_msg += "the 2D slicer" |
---|
80 | ctl.SetToolTipString(hint_msg) |
---|
81 | ctl.SetValue(str(format_number(params[item]))) |
---|
82 | self.Bind(wx.EVT_TEXT_ENTER, self.on_text_enter) |
---|
83 | ctl.Bind(wx.EVT_SET_FOCUS, self.on_set_focus) |
---|
84 | ctl.Bind(wx.EVT_KILL_FOCUS, self.on_text_enter) |
---|
85 | self.parameters.append([item, ctl]) |
---|
86 | self.bck.Add(ctl, (n - 1, 1), flag=wx.TOP | wx.BOTTOM, |
---|
87 | border=0) |
---|
88 | for item in keys: |
---|
89 | if item.lower() in ["num_points", "avg", "avg_error", "sum", |
---|
90 | "sum_error"]: |
---|
91 | n += 1 |
---|
92 | text = wx.StaticText(self, -1, item + ": ", |
---|
93 | style=wx.ALIGN_LEFT) |
---|
94 | self.bck.Add(text, (n - 1, 0), |
---|
95 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, |
---|
96 | border=15) |
---|
97 | ctl = wx.StaticText(self, -1, |
---|
98 | str(format_number(params[item])), |
---|
99 | style=wx.ALIGN_LEFT) |
---|
100 | ctl.SetToolTipString("Result %s" % item) |
---|
101 | self.bck.Add(ctl, (n - 1, 1), flag=wx.TOP | wx.BOTTOM, |
---|
102 | border=0) |
---|
103 | self.bck.Layout() |
---|
104 | self.Layout() |
---|
105 | p_sizer = self.parent.GetSizer() |
---|
106 | if p_sizer is not None: |
---|
107 | p_sizer.Layout() |
---|
108 | |
---|
109 | def on_set_focus(self, evt): |
---|
110 | """ |
---|
111 | Highlight the txtcrtl |
---|
112 | """ |
---|
113 | evt.Skip() |
---|
114 | # Get a handle to the TextCtrl |
---|
115 | widget = evt.GetEventObject() |
---|
116 | # Select the whole control, after this event resolves |
---|
117 | wx.CallAfter(widget.SetSelection, -1, -1) |
---|
118 | |
---|
119 | def on_text_enter(self, evt): |
---|
120 | """ |
---|
121 | Parameters have changed |
---|
122 | """ |
---|
123 | evt.Skip() |
---|
124 | params = {} |
---|
125 | has_error = False |
---|
126 | for item in self.parameters: |
---|
127 | try: |
---|
128 | params[item[0]] = float(item[1].GetValue()) |
---|
129 | item[1].SetBackgroundColour(wx.SystemSettings_GetColour( |
---|
130 | wx.SYS_COLOUR_WINDOW)) |
---|
131 | item[1].Refresh() |
---|
132 | except: |
---|
133 | has_error = True |
---|
134 | item[1].SetBackgroundColour("pink") |
---|
135 | item[1].Refresh() |
---|
136 | if not has_error: |
---|
137 | # Post parameter event |
---|
138 | # base is guiframe is this case |
---|
139 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
140 | wx.PostEvent(self.base, event) |
---|
141 | |
---|
142 | def on_close(self, event): |
---|
143 | """ |
---|
144 | On Close Event |
---|
145 | """ |
---|
146 | uid = self.uid |
---|
147 | self.parent.delete_panel(uid) |
---|
148 | self.frame.Destroy() |
---|