File: inplace_editor.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 (171 lines) | stat: -rw-r--r-- 7,162 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'''
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/>.
'''

'''
In place editors for viewers.
''' # pylint: disable=W0105

import wx
from taskcoachlib.thirdparty import hypertreelist
from taskcoachlib import widgets
from taskcoachlib.domain import date


class KillFocusAcceptsEditsMixin(object):
    ''' Mixin class to let in place editors accept changes whenever the user
        clicks outside the edit control instead of cancelling the changes. '''
    def StopEditing(self):
        try:
            if self.__has_focus():
                # User hit Escape
                super(KillFocusAcceptsEditsMixin, self).StopEditing()
            else:
                # User clicked outside edit window
                self.AcceptChanges()
                self.Finish()
        except wx.PyDeadObjectError:
            pass

    def __has_focus(self):
        ''' Return whether this control has the focus. '''

        def window_and_all_children(window):
            window_and_children = [window]
            for child in window.GetChildren():
                window_and_children.extend(window_and_all_children(child))
            return window_and_children

        return wx.Window.FindFocus() in window_and_all_children(self)


class SubjectCtrl(KillFocusAcceptsEditsMixin, hypertreelist.EditTextCtrl):
    ''' Single line inline control for editing item subjects. '''
    pass


class DescriptionCtrl(KillFocusAcceptsEditsMixin, hypertreelist.EditTextCtrl):
    ''' Multiline inline text control for editing item descriptions. '''
    def __init__(self, *args, **kwargs):
        kwargs['style'] = kwargs.get('style', 0) | wx.TE_MULTILINE
        super(DescriptionCtrl, self).__init__(*args, **kwargs)


class EscapeKeyMixin(object):
    ''' Mixin class for text(like) controls to properly handle the Escape key. 
        The inheriting class needs to bind to the event handler. For example:
        self._spinCtrl.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) '''
    def OnKeyDown(self, event):
        keyCode = event.GetKeyCode()
        if keyCode == wx.WXK_ESCAPE:
            self.StopEditing()
        elif keyCode in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER) and not event.ShiftDown():
            # Notify the owner about the changes
            self.AcceptChanges()
            # Even if vetoed, close the control (consistent with MSW)
            wx.CallAfter(self.Finish)
        else:
            event.Skip()
      

class _SpinCtrl(EscapeKeyMixin, KillFocusAcceptsEditsMixin, 
                hypertreelist.EditCtrl, widgets.SpinCtrl):
    ''' Base spin control class. '''
    def __init__(self, parent, wxId, item, column, owner, value, 
                 *args, **kwargs):
        super(_SpinCtrl, self).__init__(parent, wxId, item, column, owner, 
                                        str(value), *args, **kwargs)
        self._textCtrl.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

        
class PriorityCtrl(_SpinCtrl):
    ''' Spin control for priority. Since priorities can be any negative or
        positive integer we don't need to set an allowed range. '''
    pass

            
class PercentageCtrl(_SpinCtrl):
    ''' Spin control for percentages. '''
    def __init__(self, *args, **kwargs):
        super(PercentageCtrl, self).__init__(min=0, max=100, *args, **kwargs)
        

class Panel(wx.Panel):
    ''' Panel class for inline controls that need to be put into a panel. '''
    def __init__(self, parent, wxId, value, *args, **kwargs): # pylint: disable=W0613
        # Don't pass the value argument to the wx.Panel since it doesn't take 
        # a value argument
        super(Panel, self).__init__(parent, wxId, *args, **kwargs)      
        
    def makeSizer(self, control):  
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(control, flag=wx.EXPAND)
        self.SetSizerAndFit(sizer)


class BudgetCtrl(EscapeKeyMixin, KillFocusAcceptsEditsMixin, 
                 hypertreelist.EditCtrl, Panel):
    ''' Masked inline text control for editing budgets: 
        <hours>:<minutes>:<seconds>. '''
    def __init__(self, parent, wxId, item, column, owner, value):
        super(BudgetCtrl, self).__init__(parent, wxId, item, column, owner)
        hours, minutes, seconds = value.hoursMinutesSeconds()
        # Can't inherit from TimeDeltaCtrl because we need to override GetValue,
        # so we use composition instead
        self.__timeDeltaCtrl = widgets.masked.TimeDeltaCtrl(self, hours, 
                                                            minutes, seconds)
        self.__timeDeltaCtrl.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.makeSizer(self.__timeDeltaCtrl)
        
    def GetValue(self):
        return date.parseTimeDelta(self.__timeDeltaCtrl.GetValue())


class AmountCtrl(EscapeKeyMixin, KillFocusAcceptsEditsMixin, 
                 hypertreelist.EditCtrl, Panel):
    ''' Masked inline text control for editing amounts (floats >= 0). '''
    def __init__(self, parent, wxId, item, column, owner, value):
        super(AmountCtrl, self).__init__(parent, wxId, item, column, owner)
        self.__floatCtrl = widgets.masked.AmountCtrl(self, value)
        self.__floatCtrl.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.makeSizer(self.__floatCtrl)
        
    def GetValue(self):
        return self.__floatCtrl.GetValue()
 
    
class DateTimeCtrl(KillFocusAcceptsEditsMixin, hypertreelist.EditCtrl, Panel):
    ''' Inline date and time picker control. '''
    def __init__(self, parent, wxId, item, column, owner, value, **kwargs):
        relative = kwargs.pop('relative', False)
        if relative:
            start = kwargs.pop('startDateTime', date.Now())
        super(DateTimeCtrl, self).__init__(parent, wxId, item, column, owner)
        settings = kwargs['settings']
        starthour = settings.getint('view', 'efforthourstart')
        endhour = settings.getint('view', 'efforthourend')
        interval = settings.getint('view', 'effortminuteinterval')
        self._dateTimeCtrl = widgets.DateTimeCtrl(self, starthour=starthour,
                                                   endhour=endhour, 
                                                   interval=interval, showRelative=relative)
        self._dateTimeCtrl.SetValue(value)
        if relative:
            self._dateTimeCtrl.SetRelativeChoicesStart(start=None if start == date.DateTime() else start)
        self.makeSizer(self._dateTimeCtrl)
                
    def GetValue(self):
        return self._dateTimeCtrl.GetValue()