Changeset 1059


Ignore:
Timestamp:
Sep 19, 2013 1:49:19 PM (10 years ago)
Author:
toby
Message:

new license file; improve ValidatedTxtCtrl? & ScrolledMultiEditor?

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIgrid.py

    r1057 r1059  
    228228        self.OnLeave = OnLeave
    229229        self.CIFinput = CIFinput
     230        self.type = str
    230231        # initialization
    231232        self.invalid = False   # indicates if the control has invalid contents
     
    233234        val = loc[key]
    234235        if isinstance(val,int) or typeHint is int:
     236            self.type = int
    235237            wx.TextCtrl.__init__(
    236238                self,parent,wx.ID_ANY,
     
    241243                **kw)
    242244            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)
    252246            else: # no default is invalid for a number
    253                 self.ShowStringValidity()
     247                self.invalid = True
     248                self._IndicateValidity()
    254249
    255250        elif isinstance(val,float) or typeHint is float:
     251            self.type = float
    256252            wx.TextCtrl.__init__(
    257253                self,parent,wx.ID_ANY,
     
    262258                **kw)
    263259            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)
    273261            else:
    274                 self.ShowStringValidity()
     262                self.invalid = True
     263                self._IndicateValidity()
     264
    275265        elif isinstance(val,str) or isinstance(val,unicode):
    276266            if self.CIFinput:
     
    292282            raise Exception,("ValidatedTxtCtrl error: Unknown element ("+str(key)+
    293283                             ") type: "+str(type(val)))
    294 #        if size: self.SetSize(size)
    295284        # When the mouse is moved away or the widget loses focus
    296285        # display the last saved value, if an expression
     
    298287        self.Bind(wx.EVT_KILL_FOCUS, self._onLoseFocus)
    299288
     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       
    300321    def _onStringKey(self,event):
    301322        event.Skip()
     
    337358            self.OKcontrol(True)
    338359        # always store the result
    339         if self.CIFinput:
     360        if self.CIFinput: # for CIF make results ASCII
    340361            self.result[self.key] = val.encode('ascii','replace')
    341362        else:
     
    430451                               OKcontrol=self.OKcontrol,
    431452                               CIFinput=self.CIFinput)
    432         tc = self.GetWindow()
    433         tc.invalid = False # make sure the validity flag is defined in parent
    434453    def TransferToWindow(self):
    435454        'Needed by validator, strange, but required component'
     
    469488                tc.invalid = True
    470489                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:
    472497            if val > self.max:
    473498                tc.invalid = True
    474         if self.max != None and self.typ == int:
     499        if self.min != None:
    475500            if val < self.min:
    476501                tc.invalid = True  # invalid
     
    513538        self.TestValid(tc)
    514539        self.ShowValidity(tc)
    515         # if invalid and
     540        # if invalid
    516541        if tc.invalid and self.OKcontrol:
    517542            self.OKcontrol(False)
     
    524549        '''
    525550        key = event.GetKeyCode()
     551        tc = self.GetWindow()
    526552        if key == wx.WXK_RETURN:
    527             tc = self.GetWindow()
    528553            if tc.invalid:
    529554                self.CheckInput(True)
     
    533558        if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: # control characters get processed
    534559            event.Skip()
    535             tc = self.GetWindow()
    536560            if tc.invalid:
    537561                wx.CallAfter(self.CheckInput,True)
     
    541565        elif chr(key) in self.validchars: # valid char pressed?
    542566            event.Skip()
    543             tc = self.GetWindow()
    544567            if tc.invalid:
    545568                wx.CallAfter(self.CheckInput,True)
     
    547570                wx.CallAfter(self.CheckInput,False)
    548571            return
    549         if not wx.Validator_IsSilent():
    550             wx.Bell()
     572        if not wx.Validator_IsSilent(): wx.Bell()
    551573        return  # Returning without calling event.Skip, which eats the keystroke
    552574
     
    677699################################################################################   
    678700def 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):
    680703    '''Shell routine to call a ScrolledMultiEditor dialog. See
    681704    :class:`ScrolledMultiEditor` for parameter definitions.
     
    686709    '''
    687710    dlg = ScrolledMultiEditor(parent,dictlst,elemlst,prelbl,postlbl,
    688                               title,header,size)
     711                              title,header,size,
     712                              CopyButton)
    689713    if dlg.ShowModal() == wx.ID_OK:
    690714        dlg.Destroy()
     
    731755      (300,250).
    732756
     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     
    733760    :returns: the wx.Dialog created here. Use method .ShowModal() to display it.
    734761   
     
    758785   
    759786    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):
    761789        if len(dictlst) != len(elemlst):
    762790            raise Exception,"ScrolledMultiEditor error: len(dictlst) != len(elemlst) "+str(len(dictlst))+" != "+str(len(elemlst))
     
    768796        self.dictlst = dictlst
    769797        self.elemlst = elemlst
     798        self.ButtonIndex = {}
    770799        for d,i in zip(dictlst,elemlst):
    771800            self.orig.append(d[i])
     
    785814            size=size,
    786815            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)
    788819        self.ValidatedControlsList = [] # make list of TextCtrls
    789820        for i,(d,k) in enumerate(zip(dictlst,elemlst)):
     
    792823            else:
    793824                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)
    794835            # create the validated TextCrtl, store it and add it to the sizer
    795836            ctrl = ValidatedTxtCtrl(panel,d,k,OKcontrol=self.ControlOKButton)
     
    818859        mainSizer.Fit(self)
    819860        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)
    820874
    821875    def _onClose(self,event):
     
    34173471
    34183472    Data2 = list(range(100))
    3419     elemlst += range(2,60)
    3420     postlbl += range(2,60)
    3421     dictlst += len(range(2,60))*[Data2,]
     3473    elemlst += range(2,6)
     3474    postlbl += range(2,6)
     3475    dictlst += len(range(2,6))*[Data2,]
    34223476
    34233477    prelbl = range(len(elemlst))
     
    34253479    header="""This is a longer\nmultiline and perhaps silly header"""
    34263480    dlg = ScrolledMultiEditor(frm,dictlst,elemlst,prelbl,postlbl,
    3427                               header=header)
     3481                              header=header,CopyButton=True)
    34283482    print Data1
    34293483    if dlg.ShowModal() == wx.ID_OK:
Note: See TracChangeset for help on using the changeset viewer.