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