Esempio n. 1
0
    def validate(self) -> 'Theme':
        """
        Validate the values of the theme. If there's a invalid parameter throws an
        ``AssertionError``.

        This function also converts all lists to tuples. This is done because lists
        are mutable.

        :return: Self reference
        """
        if self._disable_validation:
            return self

        # Boolean asserts
        assert isinstance(self.scrollbar_shadow, bool)
        assert isinstance(self.title_bar_modify_scrollarea, bool)
        assert isinstance(self.title_close_button, bool)
        assert isinstance(self.title_font_antialias, bool)
        assert isinstance(self.title_font_shadow, bool)
        assert isinstance(self.widget_font_antialias, bool)
        assert isinstance(self.widget_font_background_color_from_menu, bool)
        assert isinstance(self.widget_font_shadow, bool)

        # Value type checks
        assert_alignment(self.widget_alignment)
        assert_cursor(self.scrollbar_cursor)
        assert_cursor(self.title_close_button_cursor)
        assert_cursor(self.widget_cursor)
        assert_font(self.title_font)
        assert_font(self.widget_font)
        assert_position(self.scrollbar_shadow_position)
        assert_position(self.title_font_shadow_position)
        assert_position(self.widget_font_shadow_position)
        assert_position_vector(self.widget_border_position)

        assert _check_menubar_style(self.title_bar_style)
        assert get_scrollbars_from_position(
            self.scrollarea_position) is not None

        # Check selection effect if None
        if self.widget_selection_effect is None:
            self.widget_selection_effect = NoneSelection()

        assert isinstance(self.cursor_switch_ms, NumberInstance)
        assert isinstance(self.fps, NumberInstance)
        assert isinstance(self.scrollbar_shadow_offset, int)
        assert isinstance(self.scrollbar_slider_pad, NumberInstance)
        assert isinstance(self.scrollbar_thick, int)
        assert isinstance(self.title, bool)
        assert isinstance(self.title_fixed, bool)
        assert isinstance(self.title_floating, bool)
        assert isinstance(self.title_font_shadow_offset, int)
        assert isinstance(self.title_font_size, int)
        assert isinstance(self.title_updates_pygame_display, bool)
        assert isinstance(self.widget_background_inflate_to_selection, bool)
        assert isinstance(self.widget_border_width, int)
        assert isinstance(self.widget_box_border_width, int)
        assert isinstance(self.widget_font_shadow_offset, int)
        assert isinstance(self.widget_font_size, int)
        assert isinstance(self.widget_padding, PaddingInstance)
        assert isinstance(self.widget_selection_effect, Selection)
        assert isinstance(self.widget_tab_size, int)

        # Format colors, this converts all color lists to tuples automatically,
        # if image, return the same object
        self.background_color = self._format_color_opacity(
            self.background_color)
        self.cursor_color = self._format_color_opacity(self.cursor_color)
        self.cursor_selection_color = self._format_color_opacity(
            self.cursor_selection_color)
        self.focus_background_color = self._format_color_opacity(
            self.focus_background_color)
        self.readonly_color = self._format_color_opacity(self.readonly_color)
        self.readonly_selected_color = self._format_color_opacity(
            self.readonly_selected_color)
        self.scrollbar_color = self._format_color_opacity(self.scrollbar_color)
        self.scrollbar_shadow_color = self._format_color_opacity(
            self.scrollbar_shadow_color)
        self.scrollbar_slider_color = self._format_color_opacity(
            self.scrollbar_slider_color)
        self.scrollbar_slider_hover_color = self._format_color_opacity(
            self.scrollbar_slider_hover_color)
        self.selection_color = self._format_color_opacity(self.selection_color)
        self.surface_clear_color = self._format_color_opacity(
            self.surface_clear_color)
        self.title_background_color = self._format_color_opacity(
            self.title_background_color)
        self.title_close_button_background_color = self._format_color_opacity(
            self.title_close_button_background_color)
        self.title_font_color = self._format_color_opacity(
            self.title_font_color)
        self.title_font_shadow_color = self._format_color_opacity(
            self.title_font_shadow_color)
        self.widget_background_color = self._format_color_opacity(
            self.widget_background_color, none=True)
        self.widget_border_color = self._format_color_opacity(
            self.widget_border_color)
        self.widget_box_arrow_color = self._format_color_opacity(
            self.widget_box_arrow_color)
        self.widget_box_background_color = self._format_color_opacity(
            self.widget_box_background_color)
        self.widget_box_border_color = self._format_color_opacity(
            self.widget_box_border_color)
        self.widget_font_background_color = self._format_color_opacity(
            self.widget_font_background_color, none=True)
        self.widget_font_color = self._format_color_opacity(
            self.widget_font_color)
        self.widget_font_shadow_color = self._format_color_opacity(
            self.widget_font_shadow_color)
        self.widget_url_color = self._format_color_opacity(
            self.widget_url_color)

        # List to tuple
        self.scrollarea_outer_margin = self._vec_to_tuple(
            self.scrollarea_outer_margin, 2, NumberInstance)
        self.title_offset = self._vec_to_tuple(self.title_offset, 2,
                                               NumberInstance)
        self.widget_background_inflate = self._vec_to_tuple(
            self.widget_background_inflate, 2, int)
        self.widget_border_inflate = self._vec_to_tuple(
            self.widget_border_inflate, 2, int)
        self.widget_box_arrow_margin = self._vec_to_tuple(
            self.widget_box_arrow_margin, 3, int)
        self.widget_box_inflate = self._vec_to_tuple(self.widget_box_inflate,
                                                     2, int)
        self.widget_box_margin = self._vec_to_tuple(self.widget_box_margin, 2,
                                                    NumberInstance)
        self.widget_margin = self._vec_to_tuple(self.widget_margin, 2,
                                                NumberInstance)
        if isinstance(self.widget_padding, VectorInstance):
            self.widget_padding = self._vec_to_tuple(self.widget_padding)
            assert 2 <= len(self.widget_padding) <= 4, \
                'widget padding tuple length must be 2, 3 or 4'
            for p in self.widget_padding:
                assert isinstance(p, NumberInstance), \
                    'each padding element must be numeric (integer or float)'
                assert p >= 0, \
                    'all padding elements must be equal or greater than zero'
        else:
            assert self.widget_padding >= 0, 'padding cannot be a negative number'
        self.widget_offset = self._vec_to_tuple(self.widget_offset, 2,
                                                NumberInstance)

        # Check sizes
        assert self.scrollarea_outer_margin[0] >= 0 and self.scrollarea_outer_margin[1] >= 0, \
            'scroll area outer margin must be equal or greater than zero on both axis'
        assert self.widget_offset[0] >= 0 and self.widget_offset[1] >= 0, \
            'widget offset must be equal or greater than zero'
        assert self.widget_background_inflate[0] >= 0 and self.widget_background_inflate[1] >= 0, \
            'widget background inflate must be equal or greater than zero on both axis'
        assert self.widget_border_inflate[0] >= 0 and self.widget_border_inflate[1] >= 0, \
            'widget border inflate must be equal or greater than zero on both axis'
        assert self.widget_box_inflate[0] >= 0 and self.widget_box_inflate[1] >= 0, \
            'widget box inflate inflate must be equal or greater than zero on both axis'

        assert self.cursor_switch_ms > 0, 'cursor switch ms must be greater than zero'
        assert self.fps >= 0, 'fps must be equal or greater than zero'
        assert self.scrollbar_shadow_offset > 0, 'scrollbar shadow offset must be greater than zero'
        assert self.scrollbar_slider_pad >= 0, 'slider pad must be equal or greater than zero'
        assert self.scrollbar_thick > 0, 'scrollbar thickness must be greater than zero'
        assert self.title_font_size > 0, 'title font size must be greater than zero'
        assert self.widget_border_width >= 0, 'widget border width must be equal or greater than zero'
        assert self.widget_box_border_width >= 0, 'widget border box width must be equal or greater than zero'
        assert self.widget_font_shadow_offset > 0, 'widget font shadow offset must be greater than zero'
        assert self.widget_font_size > 0, 'widget font size must be greater than zero'
        assert self.widget_tab_size >= 0, 'widget tab size must be equal or greater than zero'

        # Color asserts
        assert self.focus_background_color[3] != 0, \
            'focus background color cannot be fully transparent, suggested opacity between 1 and 255'

        return self
Esempio n. 2
0
    def test_none(self) -> None:
        """
        Test none widget.
        """
        menu = MenuUtils.generic_menu()
        for wid in [
                NoneWidget(),
                menu.add.vertical_margin(10),
                menu.add.horizontal_margin(10),
                menu.add.vertical_fill(10)
        ]:
            wid.set_margin(9, 9)
            self.assertEqual(wid.get_margin(), (0, 0))

            wid.set_padding(9)
            self.assertEqual(wid.get_padding(), (0, 0, 0, 0))

            wid.set_background_color((1, 1, 1))
            wid._draw_background_color(surface)
            self.assertIsNone(wid._background_color)

            no_sel = NoneSelection()
            wid.set_selection_effect(no_sel)
            self.assertNotEqual(no_sel, wid.get_selection_effect())

            wid.set_title('none')
            self.assertEqual(wid.get_title(), '')

            r = wid.get_rect(inflate=(10, 10))
            self.assertEqual(r.x, 0)
            self.assertEqual(r.y, 0)

            self.assertFalse(wid.is_selectable)
            self.assertTrue(wid.is_visible())

            wid.apply()
            wid.change()

            # noinspection SpellCheckingInspection
            wid.set_font('myfont', 0, (1, 1, 1), (1, 1, 1), (1, 1, 1),
                         (0, 0, 0), (0, 0, 0))
            wid.update_font({'name': ''})
            wid._apply_font()
            self.assertIsNone(wid._font)

            # Test font rendering
            surf = wid._render_string('nice', (1, 1, 1))
            self.assertEqual(surf.get_width(), 0)
            self.assertEqual(surf.get_height(), 0)

            wid._apply_transforms()

            wid.hide()
            self.assertFalse(wid.is_visible())
            wid.show()
            self.assertTrue(wid.is_visible())

            self.assertRaises(ValueError, lambda: wid.get_value())

            surf = wid.get_surface()
            self.assertEqual(surf.get_width(), 0)
            self.assertEqual(surf.get_height(), 0)

            # Apply transforms
            wid.set_position(1, 1)
            self.assertEqual(wid.get_position(), (0, 0))

            self.assertRaises(WidgetTransformationNotImplemented,
                              lambda: wid.translate(1, 1))
            self.assertEqual(wid.get_translate(), (0, 0))

            self.assertRaises(WidgetTransformationNotImplemented,
                              lambda: wid.rotate(10))
            self.assertEqual(wid._angle, 0)

            self.assertRaises(WidgetTransformationNotImplemented,
                              lambda: wid.resize(10, 10))
            self.assertFalse(wid._scale[0])
            self.assertEqual(wid._scale[1], 1)
            self.assertEqual(wid._scale[2], 1)

            self.assertRaises(WidgetTransformationNotImplemented,
                              lambda: wid.scale(100, 100))
            self.assertFalse(wid._scale[0])
            self.assertEqual(wid._scale[1], 1)
            self.assertEqual(wid._scale[2], 1)

            self.assertRaises(WidgetTransformationNotImplemented,
                              lambda: wid.flip(True, True))
            self.assertFalse(wid._flip[0])
            self.assertFalse(wid._flip[1])

            self.assertRaises(WidgetTransformationNotImplemented,
                              lambda: wid.set_max_width(100))
            self.assertIsNone(wid._max_width[0])

            self.assertRaises(WidgetTransformationNotImplemented,
                              lambda: wid.set_max_height(100))
            self.assertIsNone(wid._max_height[0])

            # Selection
            wid.select()
            self.assertFalse(wid.is_selected())
            self.assertFalse(wid.is_selectable)

            # noinspection PyTypeChecker
            wid.set_sound(None)
            self.assertIsNotNone(wid._sound)

            wid.set_border(1, (0, 0, 0), (0, 0))
            self.assertEqual(wid._border_width, 0)
            self.assertEqual(wid.get_selected_time(), 0)

            # Test events
            def my_event() -> None:
                """
                Generic event object.
                """
                return

            wid.set_onchange(my_event)
            self.assertIsNone(wid._onchange)
            wid.set_onmouseover(my_event)
            self.assertIsNone(wid._onmouseover)
            wid.set_onmouseleave(my_event)
            self.assertIsNone(wid._onmouseleave)
            wid.set_onselect(my_event)
            self.assertIsNone(wid._onselect)
            wid.set_onreturn(my_event)
            self.assertIsNone(wid._onreturn)
            wid.mouseleave()
            wid.mouseover()
            wid._mouseover = True
            wid._check_mouseover()
            self.assertFalse(wid._mouseover)