1 | |
---|
2 | import wx |
---|
3 | import sys |
---|
4 | import colorsys |
---|
5 | import numpy |
---|
6 | from sans.guiframe.utils import format_number, check_float |
---|
7 | from invariant_widgets import OutputTextCtrl |
---|
8 | # Dimensions related to chart |
---|
9 | RECTANGLE_WIDTH = 400.0 |
---|
10 | RECTANGLE_HEIGHT = 20 |
---|
11 | RECTANGLE_SCALE = 0.0001 |
---|
12 | DEFAULT_QSTAR = 1.0 |
---|
13 | #Invariant panel size |
---|
14 | _BOX_WIDTH = 76 |
---|
15 | _BOX_PERCENT_WIDTH = 100 |
---|
16 | |
---|
17 | if sys.platform.count("win32")>0: |
---|
18 | _STATICBOX_WIDTH = 450 |
---|
19 | PANEL_WIDTH = 500 |
---|
20 | PANEL_HEIGHT = 700 |
---|
21 | FONT_VARIANT = 0 |
---|
22 | else: |
---|
23 | _STATICBOX_WIDTH = 480 |
---|
24 | PANEL_WIDTH = 530 |
---|
25 | PANEL_HEIGHT = 700 |
---|
26 | FONT_VARIANT = 1 |
---|
27 | |
---|
28 | |
---|
29 | class InvariantDetailsPanel(wx.Dialog): |
---|
30 | """ |
---|
31 | This panel describes proportion of invariants |
---|
32 | """ |
---|
33 | def __init__(self, parent=None, id=-1, qstar_container=None, title="", |
---|
34 | size=(PANEL_WIDTH, 450)): |
---|
35 | wx.Dialog.__init__(self, parent, id=id, title=title, size=size) |
---|
36 | |
---|
37 | #Font size |
---|
38 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
39 | self.parent = parent |
---|
40 | #self.qstar_container |
---|
41 | self.qstar_container = qstar_container |
---|
42 | self.compute_scale() |
---|
43 | #warning message |
---|
44 | self.warning_msg = "" |
---|
45 | #Define scale of each bar |
---|
46 | low_inv_scale = self.qstar_container.qstar_low_scale |
---|
47 | self.low_scale = self.check_scale(scale=low_inv_scale, scale_name="Low") |
---|
48 | inv_scale = self.qstar_container.qstar_scale |
---|
49 | self.inv_scale = self.check_scale(scale=inv_scale, scale_name="Inv") |
---|
50 | high_inv_scale = self.qstar_container.qstar_high_scale |
---|
51 | self.high_scale = self.check_scale(scale=high_inv_scale, scale_name="High") |
---|
52 | #Default color the extrapolation bar is grey |
---|
53 | self.extrapolation_color_low = wx.Colour(169, 169, 168, 128) |
---|
54 | self.extrapolation_color_high = wx.Colour(169, 169, 168, 128) |
---|
55 | #change color of high and low bar when necessary |
---|
56 | self.set_color_bar() |
---|
57 | #draw the panel itself |
---|
58 | self._do_layout() |
---|
59 | self.set_values() |
---|
60 | |
---|
61 | def _define_structure(self): |
---|
62 | """ |
---|
63 | Define main sizers needed for this panel |
---|
64 | """ |
---|
65 | #Box sizers must be defined first before defining buttons/textctrls (MAC). |
---|
66 | self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
67 | #Sizer related to chart |
---|
68 | chart_box = wx.StaticBox(self, -1, "Invariant Chart") |
---|
69 | self.chart_sizer = wx.StaticBoxSizer(chart_box, wx.VERTICAL) |
---|
70 | self.chart_sizer.SetMinSize((PANEL_WIDTH - 50,-1)) |
---|
71 | #Sizer related to invariant values |
---|
72 | self.invariant_sizer = wx.GridBagSizer(4, 4) |
---|
73 | #Sizer related to warning message |
---|
74 | warning_box = wx.StaticBox(self, -1, "Warning") |
---|
75 | self.warning_sizer = wx.StaticBoxSizer(warning_box, wx.VERTICAL) |
---|
76 | self.warning_sizer.SetMinSize((PANEL_WIDTH-50,-1)) |
---|
77 | #Sizer related to button |
---|
78 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
79 | |
---|
80 | def _layout_shart(self): |
---|
81 | """ |
---|
82 | Draw widgets related to chart |
---|
83 | """ |
---|
84 | self.panel_chart = wx.Panel(self) |
---|
85 | self.panel_chart.Bind(wx.EVT_PAINT, self.on_paint) |
---|
86 | self.chart_sizer.Add(self.panel_chart, 1, wx.EXPAND|wx.ALL, 0) |
---|
87 | |
---|
88 | def _layout_invariant(self): |
---|
89 | """ |
---|
90 | Draw widgets related to invariant |
---|
91 | """ |
---|
92 | uncertainty = "+/-" |
---|
93 | unit_invariant = '[1/cm 1/A]' |
---|
94 | |
---|
95 | invariant_txt = wx.StaticText(self, -1, 'Invariant') |
---|
96 | self.invariant_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
97 | self.invariant_tcl.SetToolTipString("Invariant in the data set's Q range.") |
---|
98 | self.invariant_err_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
99 | self.invariant_err_tcl.SetToolTipString("Uncertainty on the invariant.") |
---|
100 | invariant_units_txt = wx.StaticText(self, -1, unit_invariant) |
---|
101 | |
---|
102 | invariant_low_txt = wx.StaticText(self, -1, 'Invariant in low-Q region') |
---|
103 | self.invariant_low_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
104 | self.invariant_low_tcl.SetToolTipString("Invariant computed with the extrapolated low-Q data.") |
---|
105 | self.invariant_low_err_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
106 | self.invariant_low_err_tcl.SetToolTipString("Uncertainty on the invariant.") |
---|
107 | invariant_low_units_txt = wx.StaticText(self, -1, unit_invariant) |
---|
108 | |
---|
109 | invariant_high_txt = wx.StaticText(self, -1, 'Invariant in high-Q region') |
---|
110 | self.invariant_high_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
111 | self.invariant_high_tcl.SetToolTipString("Invariant computed with the extrapolated high-Q data") |
---|
112 | self.invariant_high_err_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
113 | self.invariant_high_err_tcl.SetToolTipString("Uncertainty on the invariant.") |
---|
114 | invariant_high_units_txt = wx.StaticText(self, -1, unit_invariant) |
---|
115 | |
---|
116 | #Invariant low |
---|
117 | iy = 1 |
---|
118 | ix = 0 |
---|
119 | self.invariant_sizer.Add(invariant_low_txt, (iy, ix), (1,1), |
---|
120 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
121 | ix += 1 |
---|
122 | self.invariant_sizer.Add(self.invariant_low_tcl, (iy, ix), (1,1), |
---|
123 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
124 | ix += 1 |
---|
125 | self.invariant_sizer.Add( wx.StaticText(self, -1, uncertainty), |
---|
126 | (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
127 | ix += 1 |
---|
128 | self.invariant_sizer.Add(self.invariant_low_err_tcl, (iy, ix), (1,1), |
---|
129 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
130 | ix += 1 |
---|
131 | self.invariant_sizer.Add(invariant_low_units_txt |
---|
132 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
133 | #Invariant |
---|
134 | iy += 1 |
---|
135 | ix = 0 |
---|
136 | self.invariant_sizer.Add(invariant_txt, (iy, ix), (1,1), |
---|
137 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
138 | ix += 1 |
---|
139 | self.invariant_sizer.Add(self.invariant_tcl, (iy, ix), (1,1), |
---|
140 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
141 | ix += 1 |
---|
142 | self.invariant_sizer.Add(wx.StaticText(self, -1, uncertainty), |
---|
143 | (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
144 | ix += 1 |
---|
145 | self.invariant_sizer.Add(self.invariant_err_tcl, (iy, ix), (1,1), |
---|
146 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
147 | ix +=1 |
---|
148 | self.invariant_sizer.Add(invariant_units_txt |
---|
149 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
150 | #Invariant high |
---|
151 | iy += 1 |
---|
152 | ix = 0 |
---|
153 | self.invariant_sizer.Add(invariant_high_txt, (iy, ix), (1,1), |
---|
154 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
155 | ix += 1 |
---|
156 | self.invariant_sizer.Add(self.invariant_high_tcl, (iy, ix), (1,1), |
---|
157 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
158 | ix += 1 |
---|
159 | self.invariant_sizer.Add(wx.StaticText(self, -1, uncertainty), |
---|
160 | (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
161 | ix += 1 |
---|
162 | self.invariant_sizer.Add(self.invariant_high_err_tcl, (iy, ix), (1,1), |
---|
163 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
164 | ix += 1 |
---|
165 | self.invariant_sizer.Add(invariant_high_units_txt |
---|
166 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
167 | |
---|
168 | def _layout_warning(self): |
---|
169 | """ |
---|
170 | Draw widgets related to warning |
---|
171 | """ |
---|
172 | #Warning [string] |
---|
173 | self.warning_msg_txt = wx.StaticText(self, -1,self.warning_msg) |
---|
174 | self.warning_msg_txt.SetForegroundColour('red') |
---|
175 | self.warning_sizer.AddMany([(self.warning_msg_txt, 0, |
---|
176 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)]) |
---|
177 | |
---|
178 | def _layout_button(self): |
---|
179 | """ |
---|
180 | Draw widgets related to button |
---|
181 | """ |
---|
182 | #Close button |
---|
183 | id = wx.NewId() |
---|
184 | button_ok = wx.Button(self, id, "Ok") |
---|
185 | button_ok.SetToolTipString("Give Details on Computation") |
---|
186 | self.Bind(wx.EVT_BUTTON, self.on_close, id=id) |
---|
187 | self.button_sizer.AddMany([((20,20), 0 , wx.LEFT, 350), |
---|
188 | (button_ok, 0 , wx.RIGHT, 10)]) |
---|
189 | def _do_layout(self): |
---|
190 | """ |
---|
191 | Draw window content |
---|
192 | """ |
---|
193 | self._define_structure() |
---|
194 | self._layout_shart() |
---|
195 | self._layout_invariant() |
---|
196 | self._layout_warning() |
---|
197 | self._layout_button() |
---|
198 | self.main_sizer.AddMany([(self.chart_sizer, 1, wx.ALL, 10), |
---|
199 | (self.invariant_sizer, 0, wx.ALL, 10), |
---|
200 | (self.warning_sizer, 0, wx.ALL, 10), |
---|
201 | (self.button_sizer, 0, wx.ALL, 10)]) |
---|
202 | self.SetSizer(self.main_sizer) |
---|
203 | |
---|
204 | |
---|
205 | def set_values(self): |
---|
206 | """ |
---|
207 | Set value of txtcrtl |
---|
208 | """ |
---|
209 | self.invariant_tcl.SetValue(format_number(self.qstar_container.qstar)) |
---|
210 | self.invariant_err_tcl.SetValue(format_number(self.qstar_container.qstar_err)) |
---|
211 | self.invariant_low_tcl.SetValue(format_number(self.qstar_container.qstar_low)) |
---|
212 | self.invariant_low_err_tcl.SetValue(format_number(self.qstar_container.qstar_low_err)) |
---|
213 | self.invariant_high_tcl.SetValue(format_number(self.qstar_container.qstar_high)) |
---|
214 | self.invariant_high_err_tcl.SetValue(format_number(self.qstar_container.qstar_high_err)) |
---|
215 | |
---|
216 | def compute_scale(self): |
---|
217 | """ |
---|
218 | Compute scale |
---|
219 | """ |
---|
220 | qstar_total = self.qstar_container.qstar_total |
---|
221 | if qstar_total is None: |
---|
222 | qstar_total = DEFAULT_QSTAR |
---|
223 | #compute the percentage of invariant |
---|
224 | inv = self.qstar_container.qstar |
---|
225 | if inv is None: |
---|
226 | inv = 0.0 |
---|
227 | inv_scale = inv/qstar_total |
---|
228 | self.qstar_container.qstar_scale = inv_scale |
---|
229 | #compute the percentage of low q invariant |
---|
230 | inv_low = self.qstar_container.qstar_low |
---|
231 | if inv_low is None: |
---|
232 | inv_low = 0.0 |
---|
233 | inv_scale = inv_low/qstar_total |
---|
234 | self.qstar_container.qstar_low_scale = inv_scale |
---|
235 | #compute the percentage of high q invariant |
---|
236 | inv_high = self.qstar_container.qstar_high |
---|
237 | if inv_high is None: |
---|
238 | inv_high = 0.0 |
---|
239 | inv_scale = inv_high/qstar_total |
---|
240 | self.qstar_container.qstar_high_scale = inv_scale |
---|
241 | |
---|
242 | def check_scale(self, scale, scale_name='scale'): |
---|
243 | """ |
---|
244 | Check scale receive in this panel. i |
---|
245 | """ |
---|
246 | try: |
---|
247 | if scale is None: |
---|
248 | scale = 0.0 |
---|
249 | scale = float(scale) |
---|
250 | if scale == 0.0: |
---|
251 | scale = RECTANGLE_SCALE |
---|
252 | except: |
---|
253 | scale = RECTANGLE_SCALE |
---|
254 | self.warning_msg += "Receive an invalid scale for %s\n" |
---|
255 | self.warning_msg += "check this value : %s\n"%(str(scale_name),str(scale)) |
---|
256 | return scale |
---|
257 | |
---|
258 | def set_color_bar(self): |
---|
259 | """ |
---|
260 | Change the color for low and high bar when necessary |
---|
261 | """ |
---|
262 | #warning to the user when the extrapolated invariant is greater than %5 |
---|
263 | if self.low_scale >= 0.05: |
---|
264 | self.extrapolation_color_low = wx.Colour(255, 0, 0, 128) |
---|
265 | self.warning_msg += "The value of invariant extrapolated at \n" |
---|
266 | self.warning_msg += "low q range is %s percent \n"%(str(self.low_scale*100)) |
---|
267 | if self.high_scale >= 0.05: |
---|
268 | self.extrapolation_color_high = wx.Colour(255, 0, 0, 128) |
---|
269 | self.warning_msg += "The value of invariant extrapolated at \n" |
---|
270 | self.warning_msg += "high q range is %s percent \n"%(str(self.high_scale*100)) |
---|
271 | |
---|
272 | def on_close(self, event): |
---|
273 | """ |
---|
274 | Close the current window |
---|
275 | """ |
---|
276 | self.Close() |
---|
277 | |
---|
278 | def on_paint(self, event): |
---|
279 | """ |
---|
280 | Draw the chart |
---|
281 | """ |
---|
282 | dc = wx.PaintDC(self.panel_chart) |
---|
283 | try: |
---|
284 | gc = wx.GraphicsContext.Create(dc) |
---|
285 | except NotImplementedError: |
---|
286 | dc.DrawText("This build of wxPython does not support the wx.GraphicsContext " |
---|
287 | "family of classes.", 25, 25) |
---|
288 | return |
---|
289 | #Start the drawing |
---|
290 | font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) |
---|
291 | font.SetWeight(wx.BOLD) |
---|
292 | gc.SetFont(font) |
---|
293 | # Draw a rectangle |
---|
294 | path = gc.CreatePath() |
---|
295 | path.AddRectangle(-RECTANGLE_WIDTH/2,-RECTANGLE_HEIGHT/2, |
---|
296 | RECTANGLE_WIDTH/2,RECTANGLE_HEIGHT/2) |
---|
297 | x_origine = 50 |
---|
298 | y_origine = 15 |
---|
299 | #Draw low rectangle |
---|
300 | gc.PushState() |
---|
301 | label = "Low" |
---|
302 | PathFunc = gc.DrawPath |
---|
303 | w, h = gc.GetTextExtent(label) |
---|
304 | gc.DrawText(label, x_origine, y_origine) |
---|
305 | #Translate the rectangle |
---|
306 | x_center = x_origine + RECTANGLE_WIDTH * self.low_scale/2 + w +10 |
---|
307 | y_center = y_origine + h |
---|
308 | gc.Translate(x_center, y_center) |
---|
309 | gc.SetPen(wx.Pen("black", 1)) |
---|
310 | gc.SetBrush(wx.Brush(self.extrapolation_color_low)) |
---|
311 | # Increase width by self.low_scale |
---|
312 | gc.Scale(self.low_scale, 1.0) |
---|
313 | PathFunc(path) |
---|
314 | gc.PopState() |
---|
315 | #Draw rectangle for invariant |
---|
316 | gc.PushState() # save it again |
---|
317 | y_origine += 20 |
---|
318 | gc.DrawText("Inv", x_origine, y_origine) |
---|
319 | # offset to the lower part of the window |
---|
320 | x_center = x_origine + RECTANGLE_WIDTH * self.inv_scale/2 + w + 10 |
---|
321 | y_center = y_origine + h |
---|
322 | gc.Translate(x_center, y_center) |
---|
323 | # 128 == half transparent |
---|
324 | gc.SetBrush(wx.Brush(wx.Colour(67, 208, 128, 128))) |
---|
325 | # Increase width by self.inv_scale |
---|
326 | gc.Scale(self.inv_scale, 1.0) |
---|
327 | gc.DrawPath(path) |
---|
328 | gc.PopState() |
---|
329 | # restore saved state |
---|
330 | #Draw rectangle for high invariant |
---|
331 | gc.PushState() |
---|
332 | y_origine += 20 |
---|
333 | gc.DrawText("High", x_origine, y_origine) |
---|
334 | #define the position of the new rectangle |
---|
335 | x_center = x_origine + RECTANGLE_WIDTH * self.high_scale/2 + w + 10 |
---|
336 | y_center = y_origine + h |
---|
337 | gc.Translate(x_center, y_center) |
---|
338 | gc.SetBrush(wx.Brush(self.extrapolation_color_high)) |
---|
339 | # increase scale by self.high_scale |
---|
340 | gc.Scale(self.high_scale, 1.0) |
---|
341 | gc.DrawPath(path) |
---|
342 | gc.PopState() |
---|
343 | |
---|
344 | class InvariantDetailsWindow(wx.Dialog): |
---|
345 | def __init__(self, parent, qstar_container=None, *args, **kwds): |
---|
346 | kwds["size"]= (PANEL_WIDTH +100, 450) |
---|
347 | wx.Dialog.__init__(self, parent, *args, **kwds) |
---|
348 | self.container = qstar_container |
---|
349 | if self.container is None: |
---|
350 | from invariant_panel import InvariantContainer |
---|
351 | self.container = InvariantContainer() |
---|
352 | self.container.qstar_total = 1 |
---|
353 | self.container.qstar = 0.75 |
---|
354 | self.container.qstar_low = 0.60 |
---|
355 | self.container.qstar_high = 0.0049 |
---|
356 | self.panel = InvariantDetailsPanel(parent=self, |
---|
357 | qstar_container=self.container) |
---|
358 | self.Centre() |
---|
359 | self.ShowModal() |
---|
360 | self.Destroy() |
---|
361 | |
---|
362 | class InvariantDetailsTest(wx.Frame): |
---|
363 | def __init__(self, parent, qstar_container=None, *args, **kwds): |
---|
364 | kwds["size"]= (PANEL_WIDTH , 450) |
---|
365 | wx.Frame.__init__(self, parent, *args, **kwds) |
---|
366 | self.container = qstar_container |
---|
367 | if self.container is None: |
---|
368 | from invariant_panel import InvariantContainer |
---|
369 | self.container = InvariantContainer() |
---|
370 | self.container.qstar_total = 1 |
---|
371 | self.container.qstar = 0.75 |
---|
372 | self.container.qstar_low = 0.60 |
---|
373 | self.container.qstar_high = 0.0049 |
---|
374 | |
---|
375 | self.panel = InvariantDetailsPanel(parent=self, |
---|
376 | qstar_container=self.container) |
---|
377 | self.panel.ShowModal() |
---|
378 | self.panel.Destroy() |
---|
379 | self.Show() |
---|
380 | |
---|
381 | if __name__ =="__main__": |
---|
382 | app = wx.App() |
---|
383 | from invariant_panel import InvariantContainer |
---|
384 | container = InvariantContainer() |
---|
385 | container.qstar_total = 1 |
---|
386 | container.qstar = 0.75 |
---|
387 | container.qstar_low = 0.60 |
---|
388 | container.qstar_high = 0.0049 |
---|
389 | window = InvariantDetailsTest(parent=None, id=-1,qstar_container=container, |
---|
390 | title="Source Editor") |
---|
391 | window.Show() |
---|
392 | app.MainLoop() |
---|