File: dialog.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 (110 lines) | stat: -rw-r--r-- 3,067 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
#------------------------------------------------------------------------------
# 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, Event, Typed, ForwardTyped, set_default

from enaml.core.declarative import d_

from .window import Window, ProxyWindow


class ProxyDialog(ProxyWindow):
    """ The abstract definition of a proxy Dialog object.

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

    def exec_(self):
        raise NotImplementedError

    def open(self):
        raise NotImplementedError

    def done(self, result):
        raise NotImplementedError


class Dialog(Window):
    """ A top-level Window class for creating dialogs.

    """
    #: The result of the dialog. This value is updated before any of
    #: the related dialog events are fired.
    result = d_(Bool(False), writable=False)

    #: An event fired when the dialog is finished. The payload will be
    #: the boolean result of the dialog. This event is fired before
    #: the 'accepted' or rejected event.
    finished = d_(Event(bool), writable=False)

    #: An event fired when the dialog is accepted.
    accepted = d_(Event(), writable=False)

    #: An event fired when the dialog is rejected.
    rejected = d_(Event(), writable=False)

    #: Dialogs are application modal by default.
    modality = set_default('application_modal')

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

    def exec_(self):
        """ Launch the dialog as a modal window.

        This call will block until the dialog is closed.

        Returns
        -------
        result : bool
            The result value of the dialog.

        """
        if not self.is_initialized:
            self.initialize()
        if not self.proxy_is_active:
            self.activate_proxy()
        return self.proxy.exec_()

    def open(self):
        """ Launch the dialog as a modal window.

        Note that this is nonblocking and you will recieve
        the result via the `finished` and `accepted` or `rejected` events.
        """
        if not self.is_initialized:
            self.initialize()
        if not self.proxy_is_active:
            self.activate_proxy()
        return self.proxy.open()

    def done(self, result):
        """ Close the dialog and set the result value.

        This will cause a call to `exec_` to return.

        Parameters
        ----------
        result : bool
            The result value for the dialog.

        """
        if self.proxy_is_active:
            self.proxy.done(result)

    def accept(self):
        """ Close the dialog and set the result to True.

        """
        self.done(True)

    def reject(self):
        """ Close the dialog and set the result to False.

        """
        self.done(False)