wx.lib.masked.TimeCtrl bug

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

wx.lib.masked.TimeCtrl bug

Post by Pako » Tue Dec 07, 2010 2:11 pm

One user reported me (through the Radio?Sure! forum) problems with the plugin Radio?Sure! :
Phred wrote:The program seems to work well, potentially very well - but I've noticed that the 'start time' control doesn't like setting a start time of 10.. o'clock. Keyboard arrows and mouse clicking roll over 10, then jam if you try to come down from 11. :shock:
I'm looking for the cause, and I found this. I tried to find a workaround and I think that can be solved this way:
1) Define a new class:

Code: Select all

class FixedTimeCtrl(wx.lib.masked.TimeCtrl):

    '''Workaround of wx.lib.masked.TimeCtrl bug.
       See http://trac.wxwidgets.org/ticket/11171'''

    def _TimeCtrl__IncrementValue(self, key, pos):
        text = self.GetValue()
        field = self._FindField(pos)
        start, end = field._extent
        slice = text[start:end]
        if slice == 'A':
            newslice = 'P'
        elif slice == 'P':
            newslice = 'A'
        else:
            top = 24 if field._index == 0 else 60
            increment = 1 if key == wx.WXK_UP else -1
            newslice = "%02d" % ((int(slice) + increment) % top)
        newvalue = text[:start] + newslice + text[end:]
        try:
            self._SetValue(newvalue)
        except ValueError:  # must not be in bounds:
            if not wx.Validator_IsSilent():
                wx.Bell()
2) Instead wx.lib.masked.TimeCtrl use in plugin the newly defined class.

I am looking for where (inside EG) is used the wx.lib.masked.TimeCtrl class.
In addition to Radio?Sure! plugin they are still following plugins:
MediaMonkey, SchedulGhost and Timer. I of course take care to fix of plugins, which I am the author.
For the next EventGhost versions (I believe they will), I have prepared two new classes: eg.TimeCtrl and eg.TimeCtrl_Duration.
The second is like eg.TimeCtrl, but also ignores the keys "C", "c" and "!" (set the time on the "Now").

Pako

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: wx.lib.masked.TimeCtrl bug

Post by Pako » Thu Dec 09, 2010 4:26 pm

I tested it wrong :oops: :
Phred wrote:Hmmm, good news, and bad news, Pako.
With 0.1.8 the spinner certainly allows the setting of 10:55 as a Start Time - but after pressing OK for the scheduler the time reverts to 11. :-(
But now I'm a bit further:

Code: Select all

class FixedTimeCtrl(wx.lib.masked.TimeCtrl):

    '''Workaround of wx.lib.masked.TimeCtrl bug.
       See http://trac.wxwidgets.org/ticket/11171'''

    def _TimeCtrl__validateValue(self, value):
        if not value:
            raise ValueError('%s not a valid time value' % repr(value))
        try:
            #value = self.GetWxDateTime(value) - THIS CAUSES THE PROBLEM !!!!!!!
            args = [int(slice) for slice in value.split(":")]
            value = wx.DateTimeFromHMS(*args)
        except:
            raise
        if self.IsLimited() and not self.IsInBounds(value):
            raise ValueError (
                'value %s is not within the bounds of the control' % str(value) )
        return value


    def _TimeCtrl__IncrementValue(self, key, pos):
        text = self.GetValue()
        field = self._FindField(pos)
        start, end = field._extent
        slice = text[start:end]
        if slice == 'A':
            newslice = 'P'
        elif slice == 'P':
            newslice = 'A'
        else:
            top = 24 if field._index == 0 else 60
            increment = 1 if key == wx.WXK_UP else -1
            newslice = "%02d" % ((int(slice) + increment) % top)
        newvalue = text[:start] + newslice + text[end:]
        try:
            self._SetValue(newvalue)
        except ValueError:  # must not be in bounds:
            if not wx.Validator_IsSilent():
                wx.Bell()
I still had to redefine one pseudo-private method.
Now I'm cautious - I'm not sure it's totally fine.
We might maybe find some other problem - for example during the Daylight Saving Time. We'll see.

Pako

Post Reply