File: masked.py

package info (click to toggle)
taskcoach 1.4.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,496 kB
  • ctags: 17,810
  • sloc: python: 72,170; makefile: 254; ansic: 120; xml: 29; sh: 16
file content (100 lines) | stat: -rwxr-xr-x 4,805 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'''
Task Coach - Your friendly task manager
Copyright (C) 2004-2014 Task Coach developers <developers@taskcoach.org>

Task Coach is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Task Coach is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

from taskcoachlib import operating_system
from wx.lib import masked
import wx
import locale


class FixOverwriteSelectionMixin(object):
    def _SetSelection(self, start, end):
        if operating_system.isGTK():  # pragma: no cover
            # By exchanging the start and end parameters we make sure that the 
            # cursor is at the start of the field so that typing overwrites the 
            # current field instead of moving to the next field:
            start, end = end, start
        super(FixOverwriteSelectionMixin, self)._SetSelection(start, end)

    def _OnKeyDown(self, event):
        # Allow keyboard navigation in notebook. Just skipping the event does not work;
        # propagate it all the way up...
        if event.GetKeyCode() == wx.WXK_TAB and event.GetModifiers() and hasattr(self.GetParent(), 'NavigateBook'):
            if self.GetParent().NavigateBook(event):
                return
        super(FixOverwriteSelectionMixin, self)._OnKeyDown(event)


class TextCtrl(FixOverwriteSelectionMixin, masked.TextCtrl):
    pass


class AmountCtrl(FixOverwriteSelectionMixin, masked.NumCtrl):
    def __init__(self, parent, value=0, locale_conventions=None):
        locale_conventions = locale_conventions or locale.localeconv()
        decimalChar = locale_conventions['decimal_point'] or '.'
        groupChar = locale_conventions['thousands_sep'] or ','
        groupDigits = len(locale_conventions['grouping']) > 1
        # The thousands separator may come up as ISO-8859-1 character
        # 0xa0, which looks like a space but isn't ASCII, which
        # confuses NumCtrl... Play it safe and avoid any non-ASCII
        # character here, or groupChars that consist of multiple characters.
        if len(groupChar) > 1 or ord(groupChar) >= 128:
            groupChar = ','
        # Prevent decimalChar and groupChar from being the same:
        if groupChar == decimalChar:
            groupChar = '.' if decimalChar == ',' else ','
        super(AmountCtrl, self).__init__(parent, value=value, 
            allowNegative=False, fractionWidth=2, selectOnEntry=True, 
            decimalChar=decimalChar, groupChar=groupChar, 
            groupDigits=groupDigits)
        

class TimeDeltaCtrl(TextCtrl):
    ''' Masked edit control for entering or displaying time deltas of the
        form <hour>:<minute>:<second>. Entering negative time deltas is not
        allowed, displaying negative time deltas is allowed if the control
        is read only. '''
    def __init__(self, parent, hours, minutes, seconds, readonly=False, 
                 negative_value=False, *args, **kwargs):
        # If the control is read only (meaning it could potentially have to 
        # show negative values) or if the value is actually negative, allow
        # the minus sign in the mask. Otherwise only allow for numbers.
        mask = 'X{9}:##:##' if negative_value or readonly else '#{9}:##:##'
        hours = self.__hour_string(hours, negative_value)
        super(TimeDeltaCtrl, self).__init__(parent, mask=mask, formatcodes='FS',
            fields=[masked.Field(formatcodes='Rr', defaultValue=hours),
                    masked.Field(defaultValue='%02d' % minutes),
                    masked.Field(defaultValue='%02d' % seconds)], 
            *args, **kwargs)
               
    def set_value(self, hours, minutes, seconds, negative_value=False):
        hours = self.__hour_string(hours, negative_value)
        self.SetCtrlParameters(formatcodes='FS',
            fields=[masked.Field(formatcodes='Rr', defaultValue=hours),
                    masked.Field(defaultValue='%02d' % minutes),
                    masked.Field(defaultValue='%02d' % seconds)])
        self.Refresh()
        
    @staticmethod
    def __hour_string(hours, negative_value):
        ''' If the value is negative (e.g. over budget), place a minus sign 
            before the hours number and make sure the field has the appropriate
            width. '''
        return '%9s' % ('-' + '%d' % hours) if negative_value else \
               '%9d' % hours