File: dialog_buttons.enaml

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 (93 lines) | stat: -rw-r--r-- 3,186 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
#------------------------------------------------------------------------------
# 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 Atom, Bool, Enum, Str
from enaml.core.looper import Looper
from enaml.layout.layout_helpers import hbox
from enaml.widgets.container import Container
from enaml.widgets.push_button import PushButton


class DialogButton(Atom):
    """ A class for specifying a button in a button box.

    Instances of this class are created by users to specify the buttons
    which will be shown in a DialogButtonBox.

    """
    #: The text for the button.
    text = Str()

    #: The dialog action to perform when the button is clicked.
    action = Enum('accept', 'reject')

    #: Whether or not the button is the default button for the dialog.
    default = Bool(False)

    #: Whether or not the button is enabled. The button will subscribe
    #: to this attribute, and reflect the changes at runtime.
    enabled = Bool(True)

    #: Whether or not the button was clicked by the user.
    was_clicked = Bool(False)

    def __init__(self, text, action, **kwargs):
        """ Initialize a DialogButton.

        Parameters
        ----------
        text : unicode
            The unicode label for the button.

        action : 'accept' or 'reject'
            The dialog action to perform when the button is clicked.

        **kwargs
            Additional optional state to apply to the button.

        """
        super(DialogButton, self).__init__(text=text, action=action, **kwargs)


enamldef DialogButtonBox(Container): box:
    """ A component for defining a button box for a dialog.

    The dialog button box must be used as a decendant of a Dialog, and
    relies on dynamic scoping to invoke the dialog action when a button
    is clicked. The button widgets created by the dialog can be styled
    using the style class 'dialog-box-button'.

    Attributes
    ----------
    buttons : list
        A list of DialogButton objects which represent the buttons to
        create for the dialog box. This value should be set before the
        widget is shown. Dynamic changes will not update the UI.

    Events
    ------
    clicked
        This event will be emitted when a button is clicked, but before
        the dialog action is taken. The payload will be the DialogButton
        instance for the button which was clicked.

    """
    attr buttons: list = []
    event clicked: DialogButton
    padding = 1  # Fix a small clipping issue observed on MacOSX
    constraints = [hbox(*sum(looper.items, []), spacing=6)]
    Looper: looper:
        iterable = buttons
        PushButton:
            style_class = 'dialog-box-button'
            text = loop.item.text
            default = loop.item.default
            enabled << loop.item.enabled
            clicked ::
                loop.item.was_clicked = True
                box.clicked(loop.item)
                nonlocals[loop.item.action]()