1# ------------------------------------------------------------------------------
2#
3#  Copyright (c) 2008, Enthought, Inc.
4#  All rights reserved.
5#
6#  This software is provided without warranty under the terms of the BSD
7#  license included in LICENSE.txt and may be redistributed only
8#  under the conditions described in the aforementioned license.  The license
9#  is also available online at http://www.enthought.com/licenses/BSD.txt
10#
11#  Thanks for using Enthought open source!
12#
13#  Author: Judah De Paula
14#  Date:   10/7/2008
15#
16# ------------------------------------------------------------------------------
17"""
18A Traits UI editor that wraps a WX timer control.
19
20Future Work
21-----------
22The only editor provided is an editable and constrained "XX:XX:XX XM" field.
23At the minimum, a spinner should be provided so the time can be changed
24without the need for a keyboard.  In addition we need to extend to provide
25all four of the basic editor types, Simple, Custom, Text, and Readonly.
26"""
27import datetime
28
29import wx.adv
30
31from traitsui.wx.editor import Editor
32from traitsui.wx.text_editor import ReadonlyEditor as TextReadonlyEditor
33
34
35class SimpleEditor(Editor):
36    """
37    Traits UI time editor.
38    """
39
40    def init(self, parent):
41        """
42        Finishes initializing the editor by creating the underlying toolkit
43        widget.
44        """
45        tctl = wx.adv.TimePickerCtrl(parent, -1, name="12 hour control")
46        self.control = tctl
47        self.control.Bind(wx.adv.EVT_TIME_CHANGED, self.time_updated)
48        return
49
50    def time_updated(self, event):
51        """
52        Event for when the wx time control is updated.
53        """
54        time = self.control.GetValue()
55        hour = time.GetHour()
56        minute = time.GetMinute()
57        second = time.GetSecond()
58        self.value = datetime.time(hour, minute, second)
59        return
60
61    def update_editor(self):
62        """
63        Updates the editor when the object trait changes externally to the
64        editor.
65        """
66        if self.value:
67            time = self.control.GetValue()
68            time.SetHour(self.value.hour)
69            time.SetMinute(self.value.minute)
70            time.SetSecond(self.value.second)
71            self.control.SetValue(time)
72        return
73
74
75# -- end SimpleEditor definition ------------------------------------------
76
77
78# ------------------------------------------------------------------------------
79# --  Text Editor
80# ------------------------------------------------------------------------------
81# TODO: Write me.  Possibly use TextEditor as a model to show a string
82# representation of the time, and have enter-set do a time evaluation.
83class TextEditor(SimpleEditor):
84    pass
85
86
87# -- end TextEditor definition -------------------------------------------------
88
89
90# ------------------------------------------------------------------------------
91# --  Custom Editor
92# ------------------------------------------------------------------------------
93# TODO: Write me.
94class CustomEditor(SimpleEditor):
95    pass
96
97
98# -- end TextEditor definition -------------------------------------------------
99
100
101# ------------------------------------------------------------------------------
102# --  Readonly Editor
103# ------------------------------------------------------------------------------
104
105
106class ReadonlyEditor(TextReadonlyEditor):
107    """ Use a TextEditor for the view. """
108
109    def _get_str_value(self):
110        """ Replace the default string value with our own time version. """
111        if self.value is None:
112            return self.factory.message
113        else:
114            return self.value.strftime(self.factory.strftime)
115
116
117# -- end ReadonlyEditor definition ---------------------------------------------
118
119
120# -- eof -----------------------------------------------------------------------
121