tuikit/layouts/fixed.py
author Radek Brich <radek.brich@devl.cz>
Sun, 15 Feb 2015 17:50:24 +0100
changeset 116 165b5d65e1cb
parent 115 b4ff7392003a
child 117 8680c2333546
permissions -rw-r--r--
Drop AnchorLayout, merge its features into FixedLayout.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
from .layout import Layout
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
class FixedLayout(Layout):
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
116
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
     6
    """Widgets are placed on fixed position as specified in `sizereq`.
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
     7
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
     8
    Align hints (kwargs to :meth:`add`):
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
     9
        * halign = 'left' (default) | 'right' | 'fill' | 'center'
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    10
        * valign = 'top' (default) | 'bottom' | 'fill' | 'center'
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    11
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    12
    """
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    13
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    14
    def __init__(self):
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    15
        Layout.__init__(self)
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    16
        self._widget_hints = {}  # Widget : (halign, valign)
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    17
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    18
    def add(self, widget, *args, **kwargs):
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    19
        Layout.add(self, widget)
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    20
        assert len(args) == 0, \
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    21
            "FixedLayout does not support positional hint args: %s" % args
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    22
        assert all(key in ('halign', 'valign') for key in kwargs.keys()), \
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    23
            "Unsupported hints: %s" % tuple(kwargs.keys())
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    24
        halign = kwargs.get('halign', 'left')
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    25
        valign = kwargs.get('valign', 'top')
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    26
        self._widget_hints[widget] = (halign, valign)
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    27
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    28
    def update(self, w, h):
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
        for widget in self._managed_widgets:
116
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    30
            halign, valign = self._widget_hints[widget]
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    31
            px, py = widget.posreq
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    32
            sw, sh = widget.sizereq
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    33
            if halign == 'right':   px = w - px
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    34
            if valign == 'bottom':  py = h - py
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    35
            if halign == 'fill':    px = 0; sw = w
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    36
            if valign == 'fill':    py = 0; sh = h
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    37
            if halign == 'center':  px = (w - sw) // 2
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    38
            if valign == 'center':  py = (h - sh) // 2
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    39
            widget.resize(sw, sh)
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    40
            widget.pos.update(px, py)