Changeset 1059
- Timestamp:
- Sep 19, 2013 1:49:19 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/GSASIIgrid.py
r1057 r1059 228 228 self.OnLeave = OnLeave 229 229 self.CIFinput = CIFinput 230 self.type = str 230 231 # initialization 231 232 self.invalid = False # indicates if the control has invalid contents … … 233 234 val = loc[key] 234 235 if isinstance(val,int) or typeHint is int: 236 self.type = int 235 237 wx.TextCtrl.__init__( 236 238 self,parent,wx.ID_ANY, … … 241 243 **kw) 242 244 if val is not None: 243 self.SetValue(str(val)) 244 try: 245 int(val) 246 except: 247 if CIFinput and (val == '?' or val == '.'): 248 pass 249 else: 250 self.invalid = True 251 self._IndicateValidity() 245 self.SetValue(val) 252 246 else: # no default is invalid for a number 253 self.ShowStringValidity() 247 self.invalid = True 248 self._IndicateValidity() 254 249 255 250 elif isinstance(val,float) or typeHint is float: 251 self.type = float 256 252 wx.TextCtrl.__init__( 257 253 self,parent,wx.ID_ANY, … … 262 258 **kw) 263 259 if val is not None: 264 self.SetValue(str(val)) 265 try: 266 float(val) 267 except: 268 if CIFinput and (val == '?' or val == '.'): 269 pass 270 else: 271 self.invalid = True 272 self._IndicateValidity() 260 self.SetValue(val) 273 261 else: 274 self.ShowStringValidity() 262 self.invalid = True 263 self._IndicateValidity() 264 275 265 elif isinstance(val,str) or isinstance(val,unicode): 276 266 if self.CIFinput: … … 292 282 raise Exception,("ValidatedTxtCtrl error: Unknown element ("+str(key)+ 293 283 ") type: "+str(type(val))) 294 # if size: self.SetSize(size)295 284 # When the mouse is moved away or the widget loses focus 296 285 # display the last saved value, if an expression … … 298 287 self.Bind(wx.EVT_KILL_FOCUS, self._onLoseFocus) 299 288 289 def SetValue(self,val): 290 self.invalid = False 291 if self.type is int: 292 try: 293 if int(val) != val: 294 self.invalid = True 295 else: 296 val = int(val) 297 except: 298 if self.CIFinput and (val == '?' or val == '.'): 299 pass 300 else: 301 self.invalid = True 302 wx.TextCtrl.SetValue(self,str(val)) 303 elif self.type is float: 304 try: 305 float(val) 306 except: 307 if self.CIFinput and (val == '?' or val == '.'): 308 pass 309 else: 310 self.invalid = True 311 wx.TextCtrl.SetValue(self,str(val)) 312 else: 313 wx.TextCtrl.SetValue(self,str(val)) 314 self.ShowStringValidity() # test if valid input 315 return 316 317 self._IndicateValidity() 318 if self.OKcontrol: 319 self.OKcontrol(not self.invalid) 320 300 321 def _onStringKey(self,event): 301 322 event.Skip() … … 337 358 self.OKcontrol(True) 338 359 # always store the result 339 if self.CIFinput: 360 if self.CIFinput: # for CIF make results ASCII 340 361 self.result[self.key] = val.encode('ascii','replace') 341 362 else: … … 430 451 OKcontrol=self.OKcontrol, 431 452 CIFinput=self.CIFinput) 432 tc = self.GetWindow()433 tc.invalid = False # make sure the validity flag is defined in parent434 453 def TransferToWindow(self): 435 454 'Needed by validator, strange, but required component' … … 469 488 tc.invalid = True 470 489 return 471 if self.max != None and self.typ == int: 490 # if self.max != None and self.typ == int: 491 # if val > self.max: 492 # tc.invalid = True 493 # if self.min != None and self.typ == int: 494 # if val < self.min: 495 # tc.invalid = True # invalid 496 if self.max != None: 472 497 if val > self.max: 473 498 tc.invalid = True 474 if self.m ax != None and self.typ == int:499 if self.min != None: 475 500 if val < self.min: 476 501 tc.invalid = True # invalid … … 513 538 self.TestValid(tc) 514 539 self.ShowValidity(tc) 515 # if invalid and540 # if invalid 516 541 if tc.invalid and self.OKcontrol: 517 542 self.OKcontrol(False) … … 524 549 ''' 525 550 key = event.GetKeyCode() 551 tc = self.GetWindow() 526 552 if key == wx.WXK_RETURN: 527 tc = self.GetWindow()528 553 if tc.invalid: 529 554 self.CheckInput(True) … … 533 558 if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: # control characters get processed 534 559 event.Skip() 535 tc = self.GetWindow()536 560 if tc.invalid: 537 561 wx.CallAfter(self.CheckInput,True) … … 541 565 elif chr(key) in self.validchars: # valid char pressed? 542 566 event.Skip() 543 tc = self.GetWindow()544 567 if tc.invalid: 545 568 wx.CallAfter(self.CheckInput,True) … … 547 570 wx.CallAfter(self.CheckInput,False) 548 571 return 549 if not wx.Validator_IsSilent(): 550 wx.Bell() 572 if not wx.Validator_IsSilent(): wx.Bell() 551 573 return # Returning without calling event.Skip, which eats the keystroke 552 574 … … 677 699 ################################################################################ 678 700 def CallScrolledMultiEditor(parent,dictlst,elemlst,prelbl=[],postlbl=[], 679 title='Edit items',header='',size=(300,250)): 701 title='Edit items',header='',size=(300,250), 702 CopyButton=False): 680 703 '''Shell routine to call a ScrolledMultiEditor dialog. See 681 704 :class:`ScrolledMultiEditor` for parameter definitions. … … 686 709 ''' 687 710 dlg = ScrolledMultiEditor(parent,dictlst,elemlst,prelbl,postlbl, 688 title,header,size) 711 title,header,size, 712 CopyButton) 689 713 if dlg.ShowModal() == wx.ID_OK: 690 714 dlg.Destroy() … … 731 755 (300,250). 732 756 757 :param bool CopyButton: if True adds a small button that copies the 758 value for the current row to all fields below (default is False) 759 733 760 :returns: the wx.Dialog created here. Use method .ShowModal() to display it. 734 761 … … 758 785 759 786 def __init__(self,parent,dictlst,elemlst,prelbl=[],postlbl=[], 760 title='Edit items',header='',size=(300,250)): 787 title='Edit items',header='',size=(300,250), 788 CopyButton=False): 761 789 if len(dictlst) != len(elemlst): 762 790 raise Exception,"ScrolledMultiEditor error: len(dictlst) != len(elemlst) "+str(len(dictlst))+" != "+str(len(elemlst)) … … 768 796 self.dictlst = dictlst 769 797 self.elemlst = elemlst 798 self.ButtonIndex = {} 770 799 for d,i in zip(dictlst,elemlst): 771 800 self.orig.append(d[i]) … … 785 814 size=size, 786 815 style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER) 787 subSizer = wx.FlexGridSizer(rows=len(dictlst),cols=3,hgap=2,vgap=2) 816 cols = 3 817 if CopyButton: cols += 1 818 subSizer = wx.FlexGridSizer(rows=len(dictlst),cols=cols,hgap=2,vgap=2) 788 819 self.ValidatedControlsList = [] # make list of TextCtrls 789 820 for i,(d,k) in enumerate(zip(dictlst,elemlst)): … … 792 823 else: 793 824 subSizer.Add(wx.StaticText(panel,wx.ID_ANY,str(prelbl[i]))) 825 if CopyButton: 826 import wx.lib.colourselect as wscs 827 but = wscs.ColourSelect(label=u'\u2193', parent=panel, 828 colour=(255,255,200), 829 size=wx.Size(30,23), 830 style=wx.RAISED_BORDER) 831 but.Bind(wx.EVT_BUTTON, self._OnCopyButton) 832 but.SetToolTipString('Press to copy adjacent value to all rows below') 833 self.ButtonIndex[but] = i 834 subSizer.Add(but) 794 835 # create the validated TextCrtl, store it and add it to the sizer 795 836 ctrl = ValidatedTxtCtrl(panel,d,k,OKcontrol=self.ControlOKButton) … … 818 859 mainSizer.Fit(self) 819 860 self.SetMinSize(self.GetSize()) 861 862 def _OnCopyButton(self,event): 863 'Implements the copy down functionality' 864 but = event.GetEventObject() 865 n = self.ButtonIndex.get(but) 866 if n is None: return 867 for i,(d,k,ctrl) in enumerate(zip(self.dictlst,self.elemlst,self.ValidatedControlsList)): 868 if i < n: continue 869 if i == n: 870 val = d[k] 871 continue 872 d[k] = val 873 ctrl.SetValue(val) 820 874 821 875 def _onClose(self,event): … … 3417 3471 3418 3472 Data2 = list(range(100)) 3419 elemlst += range(2,6 0)3420 postlbl += range(2,6 0)3421 dictlst += len(range(2,6 0))*[Data2,]3473 elemlst += range(2,6) 3474 postlbl += range(2,6) 3475 dictlst += len(range(2,6))*[Data2,] 3422 3476 3423 3477 prelbl = range(len(elemlst)) … … 3425 3479 header="""This is a longer\nmultiline and perhaps silly header""" 3426 3480 dlg = ScrolledMultiEditor(frm,dictlst,elemlst,prelbl,postlbl, 3427 header=header )3481 header=header,CopyButton=True) 3428 3482 print Data1 3429 3483 if dlg.ShowModal() == wx.ID_OK:
Note: See TracChangeset
for help on using the changeset viewer.