[91f151a] | 1 | |
---|
| 2 | import wx |
---|
| 3 | import sys |
---|
| 4 | from copy import deepcopy |
---|
| 5 | from DataLoader.loader import Loader |
---|
| 6 | from sans.guiframe.utils import check_float |
---|
| 7 | |
---|
| 8 | _BOX_WIDTH = 60 |
---|
| 9 | if sys.platform.count("win32")>0: |
---|
| 10 | _STATICBOX_WIDTH = 450 |
---|
| 11 | PANEL_WIDTH = 500 |
---|
| 12 | PANEL_HEIGHT = 290 |
---|
| 13 | FONT_VARIANT = 0 |
---|
| 14 | else: |
---|
| 15 | _STATICBOX_WIDTH = 480 |
---|
| 16 | PANEL_WIDTH = 530 |
---|
| 17 | PANEL_HEIGHT = 320 |
---|
| 18 | FONT_VARIANT = 1 |
---|
| 19 | |
---|
| 20 | class ApertureDialog(wx.Dialog): |
---|
| 21 | def __init__(self, parent=None, manager=None,aperture=None, *args, **kwds): |
---|
[ad6f597] | 22 | """ |
---|
| 23 | Dialog allows to enter values for aperture |
---|
| 24 | """ |
---|
| 25 | kwds['size'] =(PANEL_WIDTH, PANEL_HEIGHT) |
---|
| 26 | kwds['title'] = "Aperture Editor" |
---|
| 27 | wx.Dialog.__init__(self, parent=parent, *args, **kwds) |
---|
| 28 | self.parent = parent |
---|
| 29 | self.manager = manager |
---|
| 30 | self._aperture = aperture |
---|
| 31 | self._reset_aperture = deepcopy(aperture) |
---|
| 32 | self._notes = "" |
---|
| 33 | self_description = "Edit aperture" |
---|
| 34 | self._do_layout() |
---|
| 35 | self.set_values() |
---|
| 36 | |
---|
[91f151a] | 37 | def _define_structure(self): |
---|
| 38 | """ |
---|
| 39 | define initial sizer |
---|
| 40 | """ |
---|
| 41 | self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
| 42 | self.box_aperture = wx.StaticBox(self, -1,str("Aperture")) |
---|
| 43 | self.boxsizer_aperture = wx.StaticBoxSizer(self.box_aperture, wx.VERTICAL) |
---|
| 44 | |
---|
| 45 | self.name_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 46 | self.type_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 47 | self.distance_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 48 | self.size_name_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 49 | self.aperture_size_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 50 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 51 | |
---|
| 52 | def _layout_name(self): |
---|
| 53 | """ |
---|
| 54 | Do the layout for aperture name related widgets |
---|
| 55 | """ |
---|
| 56 | #Aperture name [string] |
---|
| 57 | aperture_name_txt = wx.StaticText(self, -1, 'Aperture Name : ') |
---|
| 58 | self.aperture_name_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH*5, 20), style=0) |
---|
| 59 | self.name_sizer.AddMany([(aperture_name_txt, 0, wx.LEFT|wx.RIGHT, 10), |
---|
| 60 | (self.aperture_name_tcl, 0, wx.EXPAND)]) |
---|
| 61 | def _layout_type(self): |
---|
| 62 | """ |
---|
| 63 | Do the layout for aperture type related widgets |
---|
| 64 | """ |
---|
| 65 | #Aperture type [string] |
---|
| 66 | type_txt = wx.StaticText(self, -1, 'Type: ') |
---|
| 67 | self.type_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0) |
---|
| 68 | self.type_sizer.AddMany([(type_txt, 0, wx.LEFT|wx.RIGHT, 10), |
---|
| 69 | (self.type_tcl, 0, wx.LEFT, 20)]) |
---|
| 70 | |
---|
| 71 | def _layout_distance(self): |
---|
| 72 | """ |
---|
| 73 | Do the layout for aperture distance related widgets |
---|
| 74 | """ |
---|
| 75 | #Aperture distance [float] |
---|
| 76 | distance_txt = wx.StaticText(self, -1, 'Distance:') |
---|
| 77 | self.distance_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0) |
---|
| 78 | distance_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
| 79 | self.distance_unit_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 20),style=0) |
---|
| 80 | self.distance_sizer.AddMany([(distance_txt, 0, wx.LEFT|wx.RIGHT, 10), |
---|
| 81 | (self.distance_tcl, 0, wx.LEFT, 10), |
---|
| 82 | (distance_unit_txt, 0, wx.LEFT|wx.RIGHT, 10), |
---|
| 83 | (self.distance_unit_tcl, 0, wx.EXPAND)]) |
---|
| 84 | def _layout_size_name(self): |
---|
| 85 | """ |
---|
| 86 | Do the layout for size name related widgets |
---|
| 87 | """ |
---|
| 88 | # Size name [string] |
---|
| 89 | size_name_txt = wx.StaticText(self, -1, 'Size Name : ') |
---|
| 90 | self.size_name_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH*5, 20), style=0) |
---|
| 91 | self.size_name_sizer.AddMany([(size_name_txt, 0, wx.LEFT|wx.RIGHT, 10), |
---|
| 92 | (self.size_name_tcl, 0, wx.EXPAND)]) |
---|
| 93 | |
---|
| 94 | def _layout_size(self): |
---|
| 95 | """ |
---|
| 96 | Do the layout for aperture size related widgets |
---|
| 97 | """ |
---|
| 98 | #Aperture size [Vector] |
---|
| 99 | aperture_size_txt = wx.StaticText(self, -1, 'Size:') |
---|
| 100 | x_aperture_size_txt = wx.StaticText(self, -1, 'x = ') |
---|
| 101 | self.x_aperture_size_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
| 102 | y_aperture_size_txt = wx.StaticText(self, -1, 'y = ') |
---|
| 103 | self.y_aperture_size_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
| 104 | z_aperture_size_txt = wx.StaticText(self, -1, 'z = ') |
---|
| 105 | self.z_aperture_size_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
| 106 | aperture_size_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
| 107 | self.aperture_size_unit_tcl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
| 108 | self.aperture_size_sizer.AddMany([(aperture_size_txt, 0, wx.LEFT|wx.RIGHT, 10), |
---|
| 109 | (x_aperture_size_txt, 0, wx.LEFT, 17), |
---|
| 110 | (self.x_aperture_size_tcl, 0, wx.RIGHT, 10), |
---|
| 111 | (y_aperture_size_txt, 0, wx.EXPAND), |
---|
| 112 | (self.y_aperture_size_tcl, 0, wx.RIGHT, 10), |
---|
| 113 | (z_aperture_size_txt, 0, wx.EXPAND), |
---|
| 114 | (self.z_aperture_size_tcl, 0, wx.RIGHT, 10), |
---|
| 115 | (aperture_size_unit_txt, 0, wx.EXPAND), |
---|
| 116 | (self.aperture_size_unit_tcl, 0, wx.RIGHT, 10)]) |
---|
| 117 | |
---|
| 118 | def _layout_button(self): |
---|
| 119 | """ |
---|
| 120 | Do the layout for the button widgets |
---|
| 121 | """ |
---|
| 122 | self.bt_apply = wx.Button(self, -1,'Apply') |
---|
| 123 | self.bt_apply.Bind(wx.EVT_BUTTON, self.on_click_apply) |
---|
| 124 | self.bt_apply.SetToolTipString("Apply current changes to aperture.") |
---|
| 125 | self.bt_cancel = wx.Button(self, -1,'Cancel') |
---|
| 126 | self.bt_cancel.SetToolTipString("Cancel current changes.") |
---|
| 127 | self.bt_cancel.Bind(wx.EVT_BUTTON, self.on_click_cancel) |
---|
| 128 | self.bt_close = wx.Button(self, wx.ID_CANCEL,'Close') |
---|
| 129 | self.bt_close.SetToolTipString("Close window.") |
---|
| 130 | self.button_sizer.AddMany([(self.bt_apply, 0, wx.LEFT, 200), |
---|
| 131 | (self.bt_cancel, 0, wx.LEFT, 10), |
---|
| 132 | (self.bt_close, 0, wx.LEFT, 10)]) |
---|
| 133 | |
---|
| 134 | def _do_layout(self, data=None): |
---|
| 135 | """ |
---|
| 136 | Draw the current panel |
---|
| 137 | """ |
---|
| 138 | self._define_structure() |
---|
| 139 | self._layout_name() |
---|
| 140 | self._layout_type() |
---|
| 141 | self._layout_distance() |
---|
| 142 | self._layout_size_name() |
---|
| 143 | self._layout_size() |
---|
| 144 | self._layout_button() |
---|
| 145 | self.boxsizer_aperture.AddMany([(self.name_sizer, 0, |
---|
| 146 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5), |
---|
| 147 | (self.type_sizer, 0, |
---|
| 148 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5), |
---|
| 149 | (self.distance_sizer, 0, |
---|
| 150 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5), |
---|
| 151 | (self.size_name_sizer, 0, |
---|
| 152 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5), |
---|
| 153 | (self.aperture_size_sizer, 0, |
---|
| 154 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5)]) |
---|
| 155 | self.main_sizer.AddMany([(self.boxsizer_aperture, 0, wx.ALL, 10), |
---|
| 156 | (self.button_sizer, 0, |
---|
| 157 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5)]) |
---|
| 158 | self.SetSizer(self.main_sizer) |
---|
| 159 | self.SetAutoLayout(True) |
---|
| 160 | |
---|
| 161 | def set_manager(self, manager): |
---|
| 162 | """ |
---|
| 163 | Set manager of this window |
---|
| 164 | """ |
---|
| 165 | self.manager = manager |
---|
| 166 | |
---|
| 167 | def reset_aperture(self): |
---|
| 168 | """ |
---|
| 169 | put the default value of the detector back to the current aperture |
---|
| 170 | """ |
---|
| 171 | self._aperture.name = self._reset_aperture.name |
---|
| 172 | self._aperture.type = self._reset_aperture.type |
---|
| 173 | self._aperture.size_name = self._reset_aperture.size_name |
---|
| 174 | self._aperture.size.x = self._reset_aperture.size.x |
---|
| 175 | self._aperture.size.y = self._reset_aperture.size.y |
---|
| 176 | self._aperture.size.z = self._reset_aperture.size.z |
---|
| 177 | self._aperture.size_unit = self._reset_aperture.size_unit |
---|
| 178 | self._aperture.distance = self._reset_aperture.distance |
---|
| 179 | self._aperture.distance_unit = self._reset_aperture.distance_unit |
---|
| 180 | |
---|
| 181 | def set_values(self): |
---|
| 182 | """ |
---|
| 183 | take the aperture values of the current data and display them |
---|
| 184 | through the panel |
---|
| 185 | """ |
---|
| 186 | aperture = self._aperture |
---|
| 187 | #Name |
---|
| 188 | self.aperture_name_tcl.SetValue(str(aperture.name)) |
---|
| 189 | #Type |
---|
| 190 | self.type_tcl.SetValue(str(aperture.type)) |
---|
| 191 | #distance |
---|
| 192 | self.distance_tcl.SetValue(str(aperture.distance)) |
---|
| 193 | #distance unit |
---|
| 194 | self.distance_unit_tcl.SetValue(str(aperture.distance_unit)) |
---|
| 195 | #Size name |
---|
| 196 | self.size_name_tcl.SetValue(str(aperture.size_name)) |
---|
| 197 | #Aperture size as a vector |
---|
| 198 | x, y, z = aperture.size.x, aperture.size.y, aperture.size.z |
---|
| 199 | self.x_aperture_size_tcl.SetValue(str(x)) |
---|
| 200 | self.y_aperture_size_tcl.SetValue(str(y)) |
---|
| 201 | self.z_aperture_size_tcl.SetValue(str(z)) |
---|
| 202 | self.aperture_size_unit_tcl.SetValue(str(aperture.size_unit)) |
---|
| 203 | |
---|
| 204 | def get_aperture(self): |
---|
| 205 | """ |
---|
| 206 | return the current aperture |
---|
| 207 | """ |
---|
| 208 | return self._aperture |
---|
| 209 | |
---|
| 210 | def get_notes(self): |
---|
| 211 | """ |
---|
| 212 | return notes |
---|
| 213 | """ |
---|
| 214 | return self._notes |
---|
| 215 | |
---|
| 216 | def on_change_name(self): |
---|
| 217 | """ |
---|
| 218 | Change name |
---|
| 219 | """ |
---|
| 220 | #Change the name of the aperture |
---|
| 221 | name = self.aperture_name_tcl.GetValue().lstrip().rstrip() |
---|
| 222 | if name == "": |
---|
| 223 | name = str(None) |
---|
| 224 | if self._aperture.name != name: |
---|
| 225 | self._notes += "Change sample 's " |
---|
| 226 | self._notes += "name from %s to %s \n"%(self._aperture.name, name) |
---|
| 227 | self._aperture.name = name |
---|
| 228 | |
---|
| 229 | def on_change_type(self): |
---|
| 230 | """ |
---|
| 231 | Change aperture type |
---|
| 232 | """ |
---|
| 233 | #Change type |
---|
| 234 | type = self.type_tcl.GetValue().lstrip().rstrip() |
---|
| 235 | self._aperture.type = type |
---|
| 236 | self._notes += " Change type from" |
---|
| 237 | self._notes += " %s to %s \n"%(self._aperture.type, type) |
---|
| 238 | |
---|
| 239 | def on_change_distance(self): |
---|
| 240 | """ |
---|
| 241 | Change distance of the aperture |
---|
| 242 | """ |
---|
| 243 | #Change distance |
---|
| 244 | distance = self.distance_tcl.GetValue().lstrip().rstrip() |
---|
| 245 | if distance == "" or distance == str(None): |
---|
| 246 | distance = None |
---|
| 247 | self._aperture.distance = distance |
---|
| 248 | else: |
---|
| 249 | if check_float(self.distance_tcl): |
---|
| 250 | if self._aperture.distance != float(distance): |
---|
| 251 | self._notes += "Change distance from " |
---|
| 252 | self._notes += "%s to %s \n"%(self._aperture.distance, distance) |
---|
| 253 | self._aperture.distance = float(distance) |
---|
| 254 | else: |
---|
| 255 | self._notes += "Error: Expected a float for distance " |
---|
| 256 | self._notes += "won't changes distance from " |
---|
| 257 | self._notes += "%s to %s"%(self._aperture.distance, distance) |
---|
| 258 | #change the distance unit |
---|
| 259 | unit = self.distance_unit_tcl.GetValue().lstrip().rstrip() |
---|
| 260 | if self._aperture.distance_unit != unit: |
---|
| 261 | self._notes += " Change distance 's unit from " |
---|
| 262 | self._notes += "%s to %s"%(self._aperture.distance_unit, unit) |
---|
| 263 | |
---|
| 264 | def on_change_size_name(self): |
---|
| 265 | """ |
---|
| 266 | Change the size's name |
---|
| 267 | """ |
---|
| 268 | #Change size name |
---|
| 269 | size_name = self.size_name_tcl.GetValue().lstrip().rstrip() |
---|
| 270 | self._aperture.size_name = size_name |
---|
| 271 | self._notes += " Change size name from" |
---|
| 272 | self._notes += " %s to %s \n"%(self._aperture.size_name, size_name) |
---|
| 273 | |
---|
| 274 | def on_change_size(self): |
---|
| 275 | """ |
---|
| 276 | Change aperture size |
---|
| 277 | """ |
---|
| 278 | #Change x coordinate |
---|
| 279 | x_aperture_size = self.x_aperture_size_tcl.GetValue().lstrip().rstrip() |
---|
| 280 | if x_aperture_size == "" or x_aperture_size == str(None): |
---|
| 281 | x_aperture_size = None |
---|
| 282 | else: |
---|
| 283 | if check_float(self.x_aperture_size_tcl): |
---|
| 284 | if self._aperture.size.x != float(x_aperture_size) : |
---|
| 285 | self._notes += "Change x of aperture size from " |
---|
| 286 | self._notes += "%s to %s \n"%(self._aperture.size.x, x_aperture_size) |
---|
| 287 | self._aperture.aperture_size.x = float(x_aperture_size) |
---|
| 288 | else: |
---|
| 289 | self._notes += "Error: Expected a float for the aperture size 's x " |
---|
| 290 | self._notes += "won't changes x aperture size from " |
---|
| 291 | self._notes += "%s to %s"%(self._aperture.size.x, x_aperture_size) |
---|
| 292 | #Change y coordinate |
---|
| 293 | y_aperture_size = self.y_aperture_size_tcl.GetValue().lstrip().rstrip() |
---|
| 294 | if y_aperture_size == "" or y_aperture_size == str(None): |
---|
| 295 | y_aperture_size = None |
---|
| 296 | self._aperture.size.y = y_aperture_size |
---|
| 297 | else: |
---|
| 298 | if check_float(self.y_aperture_size_tcl): |
---|
| 299 | if self._aperture.size.y != float(y_aperture_size): |
---|
| 300 | self._notes += "Change y of aperture size from " |
---|
| 301 | self._notes += "%s to %s \n"%(self._aperture.size.y, y_aperture_size) |
---|
| 302 | self._aperture.size.y = float(y_aperture_size) |
---|
| 303 | else: |
---|
| 304 | self._notes += "Error: Expected a float for the aperture size's y " |
---|
| 305 | self._notes += "won't changes y aperture size from " |
---|
| 306 | self._notes += "%s to %s"%(self._aperture.size.y, y_aperture_size) |
---|
| 307 | #Change z coordinate |
---|
| 308 | z_aperture_size = self.z_aperture_size_tcl.GetValue().lstrip().rstrip() |
---|
| 309 | if z_aperture_size == "" or z_aperture_size == str(None): |
---|
| 310 | z_aperture_size = None |
---|
| 311 | self._aperture.size.z = z_aperture_size |
---|
| 312 | else: |
---|
| 313 | if check_float(self.z_aperture_size_tcl): |
---|
| 314 | if self._aperture.size.z != float(z_aperture_size): |
---|
| 315 | self._notes += "Change z of aperture size from " |
---|
| 316 | self._notes += "%s to %s \n"%(self._aperture.size.z, |
---|
| 317 | z_aperture_size) |
---|
[ad6f597] | 318 | self._aperture.size.z = float(z_aperture_size) |
---|
[91f151a] | 319 | else: |
---|
| 320 | self._notes += "Error: Expected a float for the offset 's x " |
---|
| 321 | self._notes += "won't changes z aperture size from " |
---|
| 322 | self._notes += "%s to %s"%(self._aperture.size.z, z_aperture_size) |
---|
| 323 | #change the aperture center unit |
---|
| 324 | unit = self.aperture_size_unit_tcl.GetValue().lstrip().rstrip() |
---|
| 325 | if self._aperture.size_unit != unit: |
---|
| 326 | self._notes += " Change aperture size's unit from " |
---|
| 327 | self._notes += "%s to %s"%(self._aperture.size_unit, unit) |
---|
| 328 | self._aperture.size_unit = unit |
---|
| 329 | |
---|
| 330 | def on_click_apply(self, event): |
---|
| 331 | """ |
---|
| 332 | Apply user values to the aperture |
---|
| 333 | """ |
---|
| 334 | self.on_change_name() |
---|
| 335 | self.on_change_type() |
---|
| 336 | self.on_change_distance() |
---|
| 337 | self.on_change_size_name() |
---|
| 338 | self.on_change_size() |
---|
| 339 | self.set_values() |
---|
| 340 | if self.manager is not None: |
---|
| 341 | self.manager.set_aperture(self._aperture) |
---|
| 342 | |
---|
| 343 | def on_click_cancel(self, event): |
---|
| 344 | """ |
---|
| 345 | reset the current aperture to its initial values |
---|
| 346 | """ |
---|
| 347 | self.reset_aperture() |
---|
| 348 | self.set_values() |
---|
| 349 | if self.manager is not None: |
---|
| 350 | self.manager.set_aperture(self._aperture) |
---|
| 351 | |
---|
| 352 | if __name__ =="__main__": |
---|
| 353 | |
---|
| 354 | app = wx.App() |
---|
| 355 | # Instantiate a loader |
---|
| 356 | loader = Loader() |
---|
| 357 | # Load data |
---|
[ad6f597] | 358 | from DataLoader.data_info import Aperture |
---|
| 359 | aperture = Aperture() |
---|
| 360 | dlg = ApertureDialog(aperture=aperture) |
---|
| 361 | dlg.ShowModal() |
---|
[91f151a] | 362 | app.MainLoop() |
---|
| 363 | |
---|