File: layout_helpers.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (157 lines) | stat: -rw-r--r-- 3,873 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from .constraint_helper import ConstraintHelper
from .factory_helper import FactoryHelper
from .grid_helper import GridHelper
from .linear_box_helper import LinearBoxHelper
from .sequence_helper import SequenceHelper
from .spacers import LayoutSpacer


spacer = LayoutSpacer(10)


def horizontal(*items, **config):
    """ Create a left-to-right SequenceHelper object.

    Parameters
    ----------
    items
        The constraint items to pass to the helper.

    config
        Additional keyword arguments to pass to the helper.

    """
    return SequenceHelper('right', 'left', items, **config)


def vertical(*items, **config):
    """ Create a top-to-bottom SequenceHelper object.

    Parameters
    ----------
    items
        The constraint items to pass to the helper.

    config
        Additional keyword arguments to pass to the helper.

    """
    return SequenceHelper('bottom', 'top', items, **config)


def hbox(*items, **config):
    """ Create a horizontal LinearBoxHelper object.

    Parameters
    ----------
    items
        The constraint items to pass to the helper.

    config
        Additional keyword arguments to pass to the helper.

    """
    return LinearBoxHelper('horizontal', items, **config)


def vbox(*items, **config):
    """ Create a vertical LinearBoxHelper object.

    Parameters
    ----------
    items
        The constraint items to pass to the helper.

    config
        Additional keyword arguments to pass to the helper.

    """
    return LinearBoxHelper('vertical', items, **config)


def align(anchor, *items, **config):
    """ Create a SequenceHelper with the given anchor object.

    Parameters
    ----------
    anchor : str
        The name of the target anchor on the constrainable object.

    items
        The constraint items to pass to the helper.

    config
        Additional keyword arguments to pass to the helper.

    """
    config.setdefault('spacing', 0)
    return SequenceHelper(anchor, anchor, items, **config)


def factory(func, *args, **kwargs):
    """ Create a FactoryHelper with the given factory function.

    Parameters
    ----------
    func : callable
        The callable which will generate the list of constraints.
        The owner widget will be passed as the first argument.

    args
        Additional positional arguments to pass to the factory.

    kwargs
        Additional keyword arguments to pass to the factory.

    """
    return FactoryHelper(func, *args, **kwargs)


def grid(*rows, **config):
    """ Create a GridHelper object with the given rows.

    Parameters
    ----------
    rows
        Rows of identifier to use to build a grid.

    config
        Additional keyword arguments to pass to the helper.

    """
    return GridHelper(rows, **config)


def expand_constraints(component, constraints):
    """ A function which expands any ConstraintHelper in the list.

    Parameters
    ----------
    component : Constrainable
        The constrainable component with which the constraints are
        associated. This will be passed to the .create_constraints()
        method of any ConstraintHelper instance.

    constraints : list
        The list of constraints to expand.

    Returns
    -------
    result : list
        The list of expanded constraints.

    """
    cns = []
    for cn in constraints:
        if isinstance(cn, ConstraintHelper):
            cns.extend(cn.create_constraints(component))
        elif cn is not None:
            cns.append(cn)
    return cns