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, wx.EXPAND)]) |
---|
176 | |
---|
177 | def _layout_button(self): |
---|
178 | """ |
---|
179 | Draw widgets related to button |
---|
180 | """ |
---|
181 | #Close button |
---|
182 | id = wx.NewId() |
---|
183 | button_ok = wx.Button(self, id, "Ok") |
---|
184 | button_ok.SetToolTipString("Give Details on Computation") |
---|
185 | self.Bind(wx.EVT_BUTTON, self.on_close, id=id) |
---|
186 | self.button_sizer.AddMany([((20,20), 0 , wx.LEFT, 350), |
---|
187 | (button_ok, 0 , wx.RIGHT, 10)]) |
---|
188 | def _do_layout(self): |
---|
189 | """ |
---|
190 | Draw window content |
---|
191 | """ |
---|
192 | self._define_structure() |
---|
193 | self._layout_shart() |
---|
194 | self._layout_invariant() |
---|
195 | self._layout_warning() |
---|
196 | self._layout_button() |
---|
197 | self.main_sizer.AddMany([(self.chart_sizer, 1, wx.ALL, 10), |
---|
198 | (self.invariant_sizer, 0, wx.ALL, 10), |
---|
199 | (self.warning_sizer, 0, wx.ALL, 10), |
---|
200 | (self.button_sizer, 0, wx.ALL, 10)]) |
---|
201 | self.SetSizer(self.main_sizer) |
---|
202 | |
---|
203 | |
---|
204 | def set_values(self): |
---|
205 | """ |
---|
206 | Set value of txtcrtl |
---|
207 | """ |
---|
208 | self.invariant_tcl.SetValue(format_number(self.qstar_container.qstar)) |
---|
209 | self.invariant_err_tcl.SetValue(format_number(self.qstar_container.qstar_err)) |
---|
210 | self.invariant_low_tcl.SetValue(format_number(self.qstar_container.qstar_low)) |
---|
211 | self.invariant_low_err_tcl.SetValue(format_number(self.qstar_container.qstar_low_err)) |
---|
212 | self.invariant_high_tcl.SetValue(format_number(self.qstar_container.qstar_high)) |
---|
213 | self.invariant_high_err_tcl.SetValue(format_number(self.qstar_container.qstar_high_err)) |
---|
214 | |
---|
215 | def compute_scale(self): |
---|
216 | """ |
---|
217 | Compute scale |
---|
218 | """ |
---|
219 | qstar_total = self.qstar_container.qstar_total |
---|
220 | if qstar_total is None: |
---|
221 | qstar_total = DEFAULT_QSTAR |
---|
222 | #compute the percentage of invariant |
---|
223 | inv = self.qstar_container.qstar |
---|
224 | if inv is None: |
---|
225 | inv = 0.0 |
---|
226 | inv_scale = inv/qstar_total |
---|
227 | self.qstar_container.qstar_scale = inv_scale |
---|
228 | #compute the percentage of low q invariant |
---|
229 | inv_low = self.qstar_container.qstar_low |
---|
230 | if inv_low is None: |
---|
231 | inv_low = 0.0 |
---|
232 | inv_scale = inv_low/qstar_total |
---|
233 | self.qstar_container.qstar_low_scale = inv_scale |
---|
234 | #compute the percentage of high q invariant |
---|
235 | inv_high = self.qstar_container.qstar_high |
---|
236 | if inv_high is None: |
---|
237 | inv_high = 0.0 |
---|
238 | inv_scale = inv_high/qstar_total |
---|
239 | self.qstar_container.qstar_high_scale = inv_scale |
---|
240 | |
---|
241 | def check_scale(self, scale, scale_name='scale'): |
---|
242 | """ |
---|
243 | Check scale receive in this panel. i |
---|
244 | """ |
---|
245 | try: |
---|
246 | if scale is None: |
---|
247 | scale = 0.0 |
---|
248 | scale = float(scale) |
---|
249 | if scale == 0.0: |
---|
250 | scale = RECTANGLE_SCALE |
---|
251 | except: |
---|
252 | scale = RECTANGLE_SCALE |
---|
253 | self.warning_msg += "Receive an invalid scale for %s\n" |
---|
254 | self.warning_msg += "check this value : %s\n"%(str(scale_name),str(scale)) |
---|
255 | return scale |
---|
256 | |
---|
257 | def set_color_bar(self): |
---|
258 | """ |
---|
259 | Change the color for low and high bar when necessary |
---|
260 | """ |
---|
261 | #warning to the user when the extrapolated invariant is greater than %5 |
---|
262 | if self.low_scale >= 0.05: |
---|
263 | self.extrapolation_color_low = wx.Colour(255, 0, 0, 128) |
---|
264 | self.warning_msg += "The value of invariant extrapolated at \n" |
---|
265 | self.warning_msg += "low q range is %s percent \n"%(str(self.low_scale*100)) |
---|
266 | if self.high_scale >= 0.05: |
---|
267 | self.extrapolation_color_high = wx.Colour(255, 0, 0, 128) |
---|
268 | self.warning_msg += "The value of invariant extrapolated at \n" |
---|
269 | self.warning_msg += "high q range is %s percent \n"%(str(self.high_scale*100)) |
---|
270 | |
---|
271 | def on_close(self, event): |
---|
272 | """ |
---|
273 | Close the current window |
---|
274 | """ |
---|
275 | self.Close() |
---|
276 | |
---|
277 | def on_paint(self, event): |
---|
278 | """ |
---|
279 | Draw the chart |
---|
280 | """ |
---|
281 | dc = wx.PaintDC(self.panel_chart) |
---|
282 | try: |
---|
283 | gc = wx.GraphicsContext.Create(dc) |
---|
284 | except NotImplementedError: |
---|
285 | dc.DrawText("This build of wxPython does not support the wx.GraphicsContext " |
---|
286 | "family of classes.", 25, 25) |
---|
287 | return |
---|
288 | #Start the drawing |
---|
289 | font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) |
---|
290 | font.SetWeight(wx.BOLD) |
---|
291 | gc.SetFont(font) |
---|
292 | # Draw a rectangle |
---|
293 | path = gc.CreatePath() |
---|
294 | path.AddRectangle(-RECTANGLE_WIDTH/2,-RECTANGLE_HEIGHT/2, |
---|
295 | RECTANGLE_WIDTH/2,RECTANGLE_HEIGHT/2) |
---|
296 | x_origine = 50 |
---|
297 | y_origine = 15 |
---|
298 | #Draw low rectangle |
---|
299 | gc.PushState() |
---|
300 | label = "Low" |
---|
301 | PathFunc = gc.DrawPath |
---|
302 | w, h = gc.GetTextExtent(label) |
---|
303 | gc.DrawText(label, x_origine, y_origine) |
---|
304 | #Translate the rectangle |
---|
305 | x_center = x_origine + RECTANGLE_WIDTH * self.low_scale/2 + w +10 |
---|
306 | y_center = y_origine + h |
---|
307 | gc.Translate(x_center, y_center) |
---|
308 | gc.SetPen(wx.Pen("black", 1)) |
---|
309 | gc.SetBrush(wx.Brush(self.extrapolation_color_low)) |
---|
310 | # Increase width by self.low_scale |
---|
311 | gc.Scale(self.low_scale, 1.0) |
---|
312 | PathFunc(path) |
---|
313 | gc.PopState() |
---|
314 | #Draw rectangle for invariant |
---|
315 | gc.PushState() # save it again |
---|
316 | y_origine += 20 |
---|
317 | gc.DrawText("Inv", x_origine, y_origine) |
---|
318 | # offset to the lower part of the window |
---|
319 | x_center = x_origine + RECTANGLE_WIDTH * self.inv_scale/2 + w + 10 |
---|
320 | y_center = y_origine + h |
---|
321 | gc.Translate(x_center, y_center) |
---|
322 | # 128 == half transparent |
---|
323 | gc.SetBrush(wx.Brush(wx.Colour(67, 208, 128, 128))) |
---|
324 | # Increase width by self.inv_scale |
---|
325 | gc.Scale(self.inv_scale, 1.0) |
---|
326 | gc.DrawPath(path) |
---|
327 | gc.PopState() |
---|
328 | # restore saved state |
---|
329 | #Draw rectangle for high invariant |
---|
330 | gc.PushState() |
---|
331 | y_origine += 20 |
---|
332 | gc.DrawText("High", x_origine, y_origine) |
---|
333 | #define the position of the new rectangle |
---|
334 | x_center = x_origine + RECTANGLE_WIDTH * self.high_scale/2 + w + 10 |
---|
335 | y_center = y_origine + h |
---|
336 | gc.Translate(x_center, y_center) |
---|
337 | gc.SetBrush(wx.Brush(self.extrapolation_color_high)) |
---|
338 | # increase scale by self.high_scale |
---|
339 | gc.Scale(self.high_scale, 1.0) |
---|
340 | gc.DrawPath(path) |
---|
341 | gc.PopState() |
---|
342 | |
---|
343 | class InvariantDetailsWindow(wx.Dialog): |
---|
344 | def __init__(self, parent, qstar_container=None, *args, **kwds): |
---|
345 | kwds["size"]= (PANEL_WIDTH +100, 450) |
---|
346 | wx.Dialog.__init__(self, parent, *args, **kwds) |
---|
347 | self.container = qstar_container |
---|
348 | if self.container is None: |
---|
349 | from invariant_panel import InvariantContainer |
---|
350 | self.container = InvariantContainer() |
---|
351 | self.container.qstar_total = 1 |
---|
352 | self.container.qstar = 0.75 |
---|
353 | self.container.qstar_low = 0.60 |
---|
354 | self.container.qstar_high = 0.0049 |
---|
355 | self.panel = InvariantDetailsPanel(parent=self, |
---|
356 | qstar_container=self.container) |
---|
357 | self.Centre() |
---|
358 | self.ShowModal() |
---|
359 | self.Destroy() |
---|
360 | |
---|
361 | class InvariantDetailsTest(wx.Frame): |
---|
362 | def __init__(self, parent, qstar_container=None, *args, **kwds): |
---|
363 | kwds["size"]= (PANEL_WIDTH , 450) |
---|
364 | wx.Frame.__init__(self, parent, *args, **kwds) |
---|
365 | self.container = qstar_container |
---|
366 | if self.container is None: |
---|
367 | from invariant_panel import InvariantContainer |
---|
368 | self.container = InvariantContainer() |
---|
369 | self.container.qstar_total = 1 |
---|
370 | self.container.qstar = 0.75 |
---|
371 | self.container.qstar_low = 0.60 |
---|
372 | self.container.qstar_high = 0.0049 |
---|
373 | |
---|
374 | self.panel = InvariantDetailsPanel(parent=self, |
---|
375 | qstar_container=self.container) |
---|
376 | self.panel.ShowModal() |
---|
377 | self.panel.Destroy() |
---|
378 | self.Show() |
---|
379 | |
---|
380 | if __name__ =="__main__": |
---|
381 | app = wx.App() |
---|
382 | from invariant_panel import InvariantContainer |
---|
383 | container = InvariantContainer() |
---|
384 | container.qstar_total = 1 |
---|
385 | container.qstar = 0.75 |
---|
386 | container.qstar_low = 0.60 |
---|
387 | container.qstar_high = 0.0049 |
---|
388 | window = InvariantDetailsTest(parent=None, id=-1,qstar_container=container, |
---|
389 | title="Source Editor") |
---|
390 | window.Show() |
---|
391 | app.MainLoop() |
---|