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