File: abstract_button.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 (116 lines) | stat: -rw-r--r-- 3,858 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
#------------------------------------------------------------------------------
# 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 atom.api import (
    Bool, Str, Coerced, Event, Typed, ForwardTyped, set_default
)

from enaml.core.declarative import d_, observe
from enaml.icon import Icon
from enaml.layout.geometry import Size

from .control import Control, ProxyControl


def ButtonGroup():
    from .button_group import ButtonGroup
    return ButtonGroup


class ProxyAbstractButton(ProxyControl):
    """ The abstract definition of a proxy AbstractButton object.

    """
    #: A reference to the AbstractButton declaration.
    declaration = ForwardTyped(lambda: AbstractButton)

    def set_text(self, text):
        raise NotImplementedError

    def set_icon(self, icon):
        raise NotImplementedError

    def set_icon_size(self, size):
        raise NotImplementedError

    def set_group(self, group):
        raise NotImplementedError

    def set_checkable(self, checkable):
        raise NotImplementedError

    def set_checked(self, checked):
        raise NotImplementedError


class AbstractButton(Control):
    """ A base class for creating button-like controls.

    """
    #: The text to use as the button's label.
    text = d_(Str())

    #: The source url for the icon to use for the button.
    icon = d_(Typed(Icon))

    #: The size to use for the icon. The default is an invalid size
    #: and indicates that an appropriate default should be used.
    icon_size = d_(Coerced(Size, (-1, -1)))

    #: Group to which this button belongs to.
    group = d_(ForwardTyped(ButtonGroup))

    #: Whether or not the button is checkable. The default is False.
    checkable = d_(Bool(False))

    #: Whether a checkable button is currently checked.
    checked = d_(Bool(False))

    #: Fired when the button is pressed then released. The payload will
    #: be the current checked state. This event is triggered by the
    #: proxy object when the button is clicked.
    clicked = d_(Event(bool), writable=False)

    #: Fired when a checkable button is toggled. The payload will be
    #: the current checked state. This event is triggered by the
    #: proxy object when a togglable button is toggled.
    toggled = d_(Event(bool), writable=False)

    #: Buttons hug their contents' width weakly by default.
    hug_width = set_default('weak')

    #: A reference to the ProxyAbstractButton object.
    proxy = Typed(ProxyAbstractButton)

    def __init__(self, parent=None, **kwargs):
        super(AbstractButton, self).__init__(parent, **kwargs)
        # Overridden to synchronize the group_members member of the group
        if self.group:
            self.group.group_members.add(self)

    #--------------------------------------------------------------------------
    # Observers
    #--------------------------------------------------------------------------
    @observe('text', 'icon', 'icon_size', 'group', 'checkable', 'checked')
    def _update_proxy(self, change):
        """ An observer which updates the proxy widget.

        """
        # The superclass implementation is sufficient.
        super(AbstractButton, self)._update_proxy(change)

    #--------------------------------------------------------------------------
    # Post Setattr Handlers
    #--------------------------------------------------------------------------
    def _post_setattr_group(self, old, new):
        """ Update the group_members of the group this button belongs too.

        """
        if old:
            old.group_members.remove(self)
        if new:
            new.group_members.add(self)