File: wizard_controller.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (181 lines) | stat: -rw-r--r-- 4,904 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

""" A wizard controller that has a static list of pages. """


from traits.api import (
    Bool, HasTraits, Instance, List, Property, provides, observe
)


from .i_wizard_controller import IWizardController
from .i_wizard_page import IWizardPage


@provides(IWizardController)
class WizardController(HasTraits):
    """ A wizard controller that has a static list of pages. """

    # 'IWizardController' interface ----------------------------------------

    # The pages under the control of this controller.
    pages = Property(List(IWizardPage))

    # The current page.
    current_page = Instance(IWizardPage)

    # Set if the wizard is complete.
    complete = Bool(False)

    # Protected 'IWizardController' interface -----------------------------#

    # Shadow trait for the 'pages' property.
    _pages = List(IWizardPage)

    # ------------------------------------------------------------------------
    # 'IWizardController' interface.
    # ------------------------------------------------------------------------

    def get_first_page(self):
        """ Returns the first page. """

        if self._pages:
            return self._pages[0]

        return None

    def get_next_page(self, page):
        """ Returns the next page. """

        if page.last_page:
            pass

        elif page.next_id:
            for p in self._pages:
                if p.id == page.next_id:
                    return p

        else:
            index = self._pages.index(page) + 1

            if index < len(self._pages):
                return self._pages[index]

        return None

    def get_previous_page(self, page):
        """ Returns the previous page. """

        for p in self._pages:
            next = self.get_next_page(p)

            if next is page:
                return p

        return None

    def is_first_page(self, page):
        """ Is the page the first page? """

        return page is self._pages[0]

    def is_last_page(self, page):
        """ Is the page the last page? """

        if page.last_page:
            return True

        if page.next_id:
            return False

        return page is self._pages[-1]

    def dispose_pages(self):
        """ Dispose the wizard pages. """

        for page in self._pages:
            page.dispose_page()

        return

    # ------------------------------------------------------------------------
    # 'WizardController' interface.
    # ------------------------------------------------------------------------

    def _get_pages(self):
        """ Returns the pages in the wizard. """

        return self._pages[:]

    def _set_pages(self, pages):
        """ Sets the pages in the wizard. """

        self._pages = pages

        # Make sure the current page is valid.
        # If the current page is None (i.e., the current page has
        # not been set yet), do not set it here. The current page will
        # get set when the wizard calls _show_page.
        if (
            self.current_page is not None
            and self.current_page not in self._pages
        ):
            self.current_page = self._pages[0]
        else:
            self._update()

        return

    # ------------------------------------------------------------------------
    # Private interface.
    # ------------------------------------------------------------------------

    def _update(self):
        """ Checks the completion status of the controller. """

        # The entire wizard is complete when the last page is complete.
        if self.current_page is None:
            self.complete = False
        elif self.is_last_page(self.current_page):
            self.complete = self.current_page.complete
        else:
            self.complete = False

        return

    # Trait event handlers -------------------------------------------------

    # Static ----

    @observe("current_page")
    def _reset_observers_on_current_page_and_update(self, event):
        """ Called when the current page is changed. """
        old, new = event.old, event.new
        if old is not None:
            old.observe(
                self._on_page_complete, "complete", remove=True
            )

        if new is not None:
            new.observe(self._on_page_complete, "complete")

        self._update()

        return

    # Dynamic ----

    def _on_page_complete(self, event):
        """ Called when the current page is complete. """

        self._update()

        return