File: layout_widget.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (119 lines) | stat: -rw-r--r-- 3,628 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
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

import wx

from traits.api import provides

from pyface.i_layout_item import DEFAULT_SIZE
from pyface.i_layout_widget import ILayoutWidget, MLayoutWidget
from pyface.ui.wx.widget import Widget


#: The special "default" size for Wx Size objects.
WX_DEFAULT_SIZE = -1


@provides(ILayoutWidget)
class LayoutWidget(MLayoutWidget, Widget):
    """ A widget which can participate as part of a layout.

    This is an abstract class, as Widget._create_control needs to be
    implemented at a minimum.
    """

    def _set_control_minimum_size(self, size):
        """ Set the minimum size of the control. """
        wx_size = _size_to_wx_size(size)
        self.control.SetMinSize(wx_size)

    def _get_control_minimum_size(self):
        """ Get the minimum size of the control. """
        wx_size = self.control.GetMinSize()
        return _wx_size_to_size(wx_size)

    def _set_control_maximum_size(self, size):
        """ Set the maximum size of the control. """
        wx_size = _size_to_wx_size(size)
        self.control.SetMaxSize(wx_size)

    def _get_control_maximum_size(self):
        """ Get the maximum size of the control. """
        wx_size = self.control.GetMaxSize()
        return _wx_size_to_size(wx_size)

    def _set_control_stretch(self, size_policy):
        """ Set the stretch factor of the control.

        In Wx the stretch factor is set at layout time and can't be changed
        without removing and adding the widget back into a layout.
        """
        pass

    def _get_control_stretch(self):
        """ Get the stretch factor of the control.

        In Wx the stretch factor is set at layout time and can't be obtained
        from the widget itself.  As a result, this method simply returns the
        current trait value.  This method is only used for testing.
        """
        return self.stretch

    def _set_control_size_policy(self, size_policy):
        """ Set the size policy of the control

        In Wx the size policy is set at layout time and can't be changed
        without removing and adding the widget back into a layout.
        """
        pass

    def _get_control_size_policy(self):
        """ Get the size policy of the control

        In Wx the size policy is set at layout time and can't be obtained from
        the widget itself.  As a result, this method simply returns the
        current trait value.  This method is only used for testing.
        """
        return self.size_policy


def _size_to_wx_size(size):
    """ Convert a size tuple to a wx.Size instance.

    Parameters
    ----------
    size : tuple of (width, height)
        The width and height as a tuple of ints.

    Returns
    -------
    wx_size : wx.Size
        A corresponding wx Size instance.
    """
    return wx.Size(*(
        x if x != DEFAULT_SIZE else WX_DEFAULT_SIZE
        for x in size
    ))


def _wx_size_to_size(wx_size):
    """ Convert a wx.Size instance to a size tuple.

    Parameters
    ----------
    wx_size : wx.Size
        A wx Size instance.

    Returns
    -------
    size : tuple of (width, height)
        The corresponding width and height as a tuple of ints.
    """
    return (wx_size.GetWidth(), wx_size.GetHeight())