Exemplo n.º 1
0
 def test_datetime_editor_simple(self):
     view = get_date_time_simple_view(DatetimeEditor())
     date_time = datetime.datetime(2000, 1, 2, 1, 2, 3)
     instance = InstanceWithDatetime(date_time=date_time)
     with reraise_exceptions(), \
             self.launch_editor(instance, view):
         pass
Exemplo n.º 2
0
    def test_datetime_editor_mutate_minimum_datetime_after_init(self):
        # Test if the minimum_datetime on the editor is mutated after init,
        # the values on the edited object and the view are synchronized.
        editor_factory = DatetimeEditor(minimum_datetime=datetime.datetime(
            2000, 1, 1), )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with reraise_exceptions(), \
                self.launch_editor(instance, view) as editor:

            # This value is in-range
            instance.date_time = datetime.datetime(2001, 1, 1)

            # Now change the editor's minimum datetime such that the original
            # value is out-of-range
            new_minimum_datetime = datetime.datetime(2010, 1, 1)
            editor.minimum_datetime = new_minimum_datetime

            # does not seem needed to flush the event loop, but just in case.
            process_cascade_events()

            displayed_value = to_datetime(editor.control.dateTime())

        self.assertEqual(instance.date_time, displayed_value)
        self.assertEqual(displayed_value, new_minimum_datetime)
Exemplo n.º 3
0
    def test_datetime_editor_simple_with_minimum_datetime(self):
        # Test the editor uses the minimum datetime as expected
        minimum_datetime = datetime.datetime(2000, 1, 1)
        editor_factory = DatetimeEditor(minimum_datetime=minimum_datetime, )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with self.launch_editor(instance, view) as editor:
            q_minimum_datetime = editor.control.minimumDateTime()
            actual_minimum_datetime = to_datetime(q_minimum_datetime)

        self.assertEqual(actual_minimum_datetime, minimum_datetime)
Exemplo n.º 4
0
    def test_datetime_editor_simple_with_maximum_datetime(self):
        # Test the editor uses the maximum datetime as expected
        maximum_datetime = datetime.datetime(2000, 1, 1)
        editor_factory = DatetimeEditor(maximum_datetime=maximum_datetime, )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with self.launch_editor(instance, view) as editor:
            q_maximum_datetime = editor.control.maximumDateTime()

            # does not seem needed to flush the event loop, but just in case.
            self.gui.process_events()

            actual_maximum_datetime = to_datetime(q_maximum_datetime)

        self.assertEqual(actual_maximum_datetime, maximum_datetime)
Exemplo n.º 5
0
    def test_datetime_editor_simple_with_maximum_datetime_out_of_bound(self):
        maximum_datetime = datetime.datetime(2000, 1, 1)
        editor_factory = DatetimeEditor(maximum_datetime=maximum_datetime, )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with self.launch_editor(instance, view) as editor:
            # out-of-bound
            instance.date_time = datetime.datetime(2020, 1, 1)

            # does not seem needed to flush the event loop, but just in case.
            self.gui.process_events()

            displayed_value = to_datetime(editor.control.dateTime())

        self.assertEqual(displayed_value, maximum_datetime)
        self.assertEqual(instance.date_time, maximum_datetime)
Exemplo n.º 6
0
    def test_datetime_editor_simple_with_minimum_datetime_out_of_bound(self):
        # Test when out-of-bound value is given, it is reset to the bounded
        # value.
        minimum_datetime = datetime.datetime(2000, 1, 1)
        editor_factory = DatetimeEditor(minimum_datetime=minimum_datetime, )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with reraise_exceptions(), \
                self.launch_editor(instance, view) as editor:
            instance.date_time = datetime.datetime(1980, 1, 1)

            # does not seem needed to flush the event loop, but just in case.
            process_cascade_events()

            displayed_value = to_datetime(editor.control.dateTime())

        self.assertEqual(displayed_value, minimum_datetime)
        self.assertEqual(instance.date_time, minimum_datetime)
Exemplo n.º 7
0
    def test_datetime_editor_qt_datetime_out_of_bound(self):
        # The maximum datetime supported by Qt is larger than the maximum
        # datetime supported by Python
        editor_factory = DatetimeEditor()
        view = get_date_time_simple_view(editor_factory)
        init_datetime = datetime.datetime(1900, 1, 1)
        instance = InstanceWithDatetime(date_time=init_datetime)
        with self.launch_editor(instance, view) as editor:
            # the user set the datetime on the Qt widget to a value
            # too large for Python
            from pyface.qt.QtCore import QDateTime, QDate
            q_datetime = QDateTime(QDate(datetime.MAXYEAR + 1, 1, 1))
            editor.control.setDateTime(q_datetime)

            # Get the displayed value back.
            displayed_value = to_datetime(editor.control.dateTime())

        self.assertEqual(instance.date_time, displayed_value)
Exemplo n.º 8
0
def datetime_editor():
    """ Factory function that returns an editor with date & time for
    editing Datetime values.
    """

    try:
        from traitsui.api import DatetimeEditor

        return DatetimeEditor()

    except ImportError:

        logger.warn(msg="Could not import DatetimeEditor from "
                    "traitsui.api, using TextEditor instead")

        from traitsui.api import TextEditor

        return TextEditor(evaluate=_datetime_str_to_datetime,
                          format_func=_datetime_to_datetime_str)
Exemplo n.º 9
0
    def test_datetime_editor_python_datetime_out_of_bound(self):
        # The minimum datetime supported by Qt is larger than the minimum
        # datetime supported by Python.
        editor_factory = DatetimeEditor()
        view = get_date_time_simple_view(editor_factory)
        init_datetime = datetime.datetime(1900, 1, 1)
        instance = InstanceWithDatetime(date_time=init_datetime)
        with self.launch_editor(instance, view) as editor:
            # This value is too early and is not supported by Qt
            # But the editor should not crash
            new_value = datetime.datetime(1, 1, 1)
            instance.date_time = new_value

            # does not seem needed to flush the event loop, but just in case.
            self.gui.process_events()

            displayed_value = to_datetime(editor.control.dateTime())

        # The value has changed to a filled value
        self.assertNotEqual(instance.date_time, new_value)
        self.assertEqual(displayed_value, instance.date_time)
Exemplo n.º 10
0
 def test_datetime_editor_simple(self):
     view = get_date_time_simple_view(DatetimeEditor())
     instance = InstanceWithDatetime(date_time=datetime.datetime.now())
     with self.launch_editor(instance, view):
         pass
Exemplo n.º 11
0
def datetime_editor():
    """ Factory function that returns an editor with date & time for
    editing Datetime values.
    """
    from traitsui.api import DatetimeEditor
    return DatetimeEditor()