1# ------------------------------------------------------------------------------
2#
3#  Copyright (c) 2009, 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: Evan Patterson
14#  Date:   08/04/2009
15#
16# ------------------------------------------------------------------------------
17
18""" A Traits UI editor for datetime.time objects.
19"""
20
21
22
23import datetime
24
25from pyface.qt import QtCore, QtGui
26
27from .editor import Editor
28from .editor_factory import ReadonlyEditor as BaseReadonlyEditor
29
30
31class SimpleEditor(Editor):
32    """ Simple Traits UI time editor that wraps QTimeEdit.
33    """
34
35    def init(self, parent):
36        """ Finishes initializing the editor by creating the underlying toolkit
37            widget.
38        """
39        self.control = QtGui.QTimeEdit()
40
41        self.control.timeChanged.connect(self.update_object)
42
43    def update_editor(self):
44        """ Updates the editor when the object trait changes externally to the
45            editor.
46        """
47        value = self.value
48        if value:
49            q_date = QtCore.QTime(value.hour, value.minute, value.second)
50            self.control.setTime(q_date)
51
52    def update_object(self, q_time):
53        """ Handles the user entering input data in the edit control.
54        """
55        hour = q_time.hour()
56        minute = q_time.minute()
57        second = q_time.second()
58        try:
59            self.value = datetime.time(hour, minute, second)
60        except ValueError:
61            print("Invalid time:", hour, minute, second)
62            raise
63
64
65# ------------------------------------------------------------------------------
66# 'ReadonlyEditor' class:
67# ------------------------------------------------------------------------------
68
69
70class ReadonlyEditor(BaseReadonlyEditor):
71    """ Readonly Traits UI time editor that uses a QLabel for the view.
72    """
73
74    def _get_str_value(self):
75        """ Replace the default string value with our own time verision.
76        """
77        if not self.value:
78            return self.factory.message
79        else:
80            return self.value.strftime(self.factory.strftime)
81