1 | import wx |
---|
2 | import wx.lib.newevent |
---|
3 | |
---|
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 | from .parameters_panel_slicer import SlicerParameterPanel |
---|
10 | |
---|
11 | class SlicerPanel(wx.Panel, PanelBase): |
---|
12 | """ |
---|
13 | Panel class to show the slicer parameters |
---|
14 | """ |
---|
15 | # Internal name for the AUI manager |
---|
16 | window_name = "Slicer panel" |
---|
17 | # Title to appear on top of the window |
---|
18 | window_caption = "Slicer Panel" |
---|
19 | CENTER_PANE = False |
---|
20 | |
---|
21 | def __init__(self, parent, id=-1, type=None, base=None, |
---|
22 | params=None, *args, **kwargs): |
---|
23 | wx.Panel.__init__(self, parent, id, *args, **kwargs) |
---|
24 | PanelBase.__init__(self) |
---|
25 | # Initialization of the class |
---|
26 | self.base = base |
---|
27 | if params is None: |
---|
28 | params = {} |
---|
29 | self.params = params |
---|
30 | self.parent = base.parent |
---|
31 | self.frame = None |
---|
32 | self.type = type |
---|
33 | self.listeners = [] |
---|
34 | self.parameters = [] |
---|
35 | self.bck = wx.GridBagSizer(5, 5) |
---|
36 | self.SetSizer(self.bck) |
---|
37 | if type is None and params is None: |
---|
38 | label = "Right-click on 2D plot for slicer options" |
---|
39 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
40 | self.bck.Add(title, (0, 0), (1, 2), |
---|
41 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
42 | else: |
---|
43 | self.set_slicer(type, params) |
---|
44 | # Bindings |
---|
45 | self.parent.Bind(EVT_SLICER, SlicerParameterPanel.on_evt_slicer) |
---|
46 | self.parent.Bind(EVT_SLICER_PARS, SlicerParameterPanel.on_param_change) |
---|
47 | |
---|
48 | def set_slicer(self, type, params): |
---|
49 | """ |
---|
50 | Rebuild the panel |
---|
51 | """ |
---|
52 | self.bck.Clear(True) |
---|
53 | self.type = type |
---|
54 | if type is None: |
---|
55 | label = "Right-click on 2D plot for slicer options" |
---|
56 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
57 | self.bck.Add(title, (0, 0), (1, 2), |
---|
58 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
59 | else: |
---|
60 | title_text = str(type) + "Parameters" |
---|
61 | title = wx.StaticText(self, -1, title_text, |
---|
62 | style=wx.ALIGN_LEFT) |
---|
63 | self.bck.Add(title, (0, 0), (1, 2), |
---|
64 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
65 | n = 1 |
---|
66 | self.parameters = [] |
---|
67 | keys = list(sorted(params.keys())) |
---|
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() |
---|