Changeset 1881


Ignore:
Timestamp:
Jun 5, 2015 5:28:49 PM (7 years ago)
Author:
toby
Message:

fix bugs in single crystal editing as well as problems with ValidatedTextCtrl?

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIctrls.py

    r1866 r1881  
    313313        self.evaluated = False # set to True when the validator recognizes an expression
    314314        val = loc[key]
     315        if 'style' in kw: # add a "Process Enter" to style
     316            kw['style'] += kw['style'] | wx.TE_PROCESS_ENTER
     317        else:
     318            kw['style'] = wx.TE_PROCESS_ENTER
    315319        if isinstance(val,int) or typeHint is int:
    316320            self.type = int
     
    365369        # When the mouse is moved away or the widget loses focus,
    366370        # display the last saved value, if an expression
    367         #self.Bind(wx.EVT_LEAVE_WINDOW, self._onLeaveWindow)
     371        self.Bind(wx.EVT_LEAVE_WINDOW, self._onLeaveWindow)
    368372        self.Bind(wx.EVT_TEXT_ENTER, self._onLoseFocus)
    369373        self.Bind(wx.EVT_KILL_FOCUS, self._onLoseFocus)
     
    379383        self._setValue(val)
    380384
    381     def _setValue(self,val):
     385    def _setValue(self,val,show=True):
     386        '''Check the validity of an int or float value and convert to a str.
     387        Possibly format it. If show is True, display the formatted value in
     388        the Text widget.
     389        '''
    382390        self.invalid = False
    383391        if self.type is int:
     
    392400                else:
    393401                    self.invalid = True
    394             wx.TextCtrl.SetValue(self,str(val))
     402            if show: wx.TextCtrl.SetValue(self,str(val))
    395403        elif self.type is float:
    396404            try:
     
    401409                else:
    402410                    self.invalid = True
    403             if self.nDig:
     411            if self.nDig and show:
    404412                wx.TextCtrl.SetValue(self,str(G2py3.FormatValue(val,self.nDig)))
    405             else:
     413            elif show:
    406414                wx.TextCtrl.SetValue(self,str(G2py3.FormatSigFigs(val)).rstrip('0'))
    407415        else:
    408             wx.TextCtrl.SetValue(self,str(val))
     416            if show: wx.TextCtrl.SetValue(self,str(val))
    409417            self.ShowStringValidity() # test if valid input
    410418            return
     
    419427        if key in [wx.WXK_BACK, wx.WXK_DELETE]:
    420428            if self.Validator: wx.CallAfter(self.Validator.TestValid,self)
    421         if key == wx.WXK_RETURN:
     429        if key == wx.WXK_RETURN or key == wx.WXK_NUMPAD_ENTER:
    422430            self._onLoseFocus(None)
    423431        event.Skip()
     
    483491        log.LogVarChange(self.result,self.key)
    484492
     493    def _onLeaveWindow(self,event):
     494        '''If the mouse leaves the text box, save the result, if valid,
     495        but (unlike _onLoseFocus) don't update the textbox contents.
     496        '''
     497        if self.evaluated and not self.invalid: # deal with computed expressions
     498            self.evaluated = False # expression has been recast as value, reset flag
     499        if self.invalid: # don't update an invalid expression
     500            if event: event.Skip()
     501            return
     502        self._setValue(self.result[self.key],show=False) # save value quietly
     503        if self.OnLeave: self.OnLeave(invalid=self.invalid,
     504                                      value=self.result[self.key],
     505                                      tc=self,
     506                                      **self.OnLeaveArgs)
     507        if event: event.Skip()
     508           
    485509    def _onLoseFocus(self,event):
    486         if self.evaluated:
    487             self.EvaluateExpression()
     510        '''Enter has been pressed or focus transferred to another control,
     511        Evaluate and update the current control contents
     512        '''
     513        if self.evaluated: # deal with computed expressions
     514            if self.invalid: # don't substitute for an invalid expression
     515                if event: event.Skip()
     516                return
     517            self.evaluated = False # expression has been recast as value, reset flag
     518            self._setValue(self.result[self.key])
    488519        elif self.result is not None: # show formatted result, as Bob wants
    489520            self._setValue(self.result[self.key])
     
    494525        if event: event.Skip()
    495526
    496     def EvaluateExpression(self):
    497         '''Show the computed value when an expression is entered to the TextCtrl
    498         Make sure that the number fits by truncating decimal places and switching
    499         to scientific notation, as needed.
    500         Called on loss of focus, enter, etc..
    501         '''
    502         if self.invalid: return # don't substitute for an invalid expression
    503         if not self.evaluated: return # true when an expression is evaluated
    504         if self.result is not None: # retrieve the stored result
    505             self._setValue(self.result[self.key])
    506         self.evaluated = False # expression has been recast as value, reset flag
    507        
    508527class NumberValidator(wx.PyValidator):
    509528    '''A validator to be used with a TextCtrl to prevent
     
    676695        key = event.GetKeyCode()
    677696        tc = self.GetWindow()
    678         if key == wx.WXK_RETURN:
     697        if key == wx.WXK_RETURN or key == wx.WXK_NUMPAD_ENTER:
    679698            if tc.invalid:
    680699                self.CheckInput(True)
    681700            else:
    682701                self.CheckInput(False)
     702            event.Skip()
    683703            return
    684704        if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: # control characters get processed
     
    750770        key = event.GetKeyCode()
    751771        tc = self.GetWindow()
    752         if key == wx.WXK_RETURN:
     772        if key == wx.WXK_RETURN or key == wx.WXK_NUMPAD_ENTER:
    753773            self.TestValid(tc)
     774            event.Skip()
    754775            return
    755776        if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: # control characters get processed
  • trunk/GSASIIpwdGUI.py

    r1880 r1881  
    11061106        return good
    11071107       
    1108     def inst2data(inst,ref,data):
     1108    def updateData(inst,ref):
     1109        data = G2frame.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(G2frame,
     1110            G2frame.PatternId,'Instrument Parameters'))[0]
    11091111        for item in data:
    11101112            try:
    11111113                data[item] = [data[item][0],inst[item],ref[item]]
    11121114            except KeyError:
    1113                 pass        #skip 'Polariz.' for N-data
    1114         return data
    1115        
    1116     def updateData(inst,ref):
    1117         return inst2data(inst,ref,G2frame.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(G2frame,
    1118             G2frame.PatternId,'Instrument Parameters'))[0])       
     1115                try:
     1116                    data[item] = [data[item][0],inst[item]]
     1117                except KeyError:
     1118                    pass        #skip 'Polariz.' for N-data
    11191119   
    11201120    def RefreshInstrumentGrid(event,doAnyway=False):
Note: See TracChangeset for help on using the changeset viewer.