Debian Codegraph @

envisage/ui/workbench/workbench_application.py

raw
1 # (C) Copyright 2007-2025 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10 """ The entry point for an Envisage Workbench application. """
11
12
13 # Standard library imports.
14 import logging
15
16 from pyface.api import AboutDialog, Dialog, GUI, ImageResource, SplashScreen
17 from pyface.workbench.api import IWorkbench
18 from traits.api import Callable, Instance, Str, Tuple
19
20 # Enthought library imports.
21 from envisage.api import Application
22
23 # Local imports.
24 from .workbench import Workbench
25
26 # Logging.
27 logger = logging.getLogger(__name__)
28
29
30 class WorkbenchApplication(Application):
31 """The entry point for an Envisage Workbench application.
32
33 i.e. a GUI application whose user interface is provided by the workbench
34 plugin.
35
36 This class handles the common case for Workbench applications, and it is
37 intended to be subclassed to change start/stop behaviour etc. In fact, I
38 generally create a subclass for every Workbench application I write since
39 it is a good place to put branding information etc.
40
41 """
42
43 #### 'WorkbenchApplication' interface #####################################
44
45 # The Pyface GUI for the application (this is here to make it easy for
46 # parts of the application to get a reference to the GUI so they can get
47 # system metrics, etc.
48 gui = Instance(GUI)
49
50 # The workbench.
51 workbench = Instance(IWorkbench)
52
53 # The factory for creating the workbench (used *instead* of providing a
54 # workbench explicitly).
55 workbench_factory = Callable(Workbench)
56
57 # Branding information.
58 #
59 # The 'About' dialog.
60 about_dialog = Instance(Dialog)
61
62 # The icon used on window title bars etc.
63 icon = Instance(ImageResource, ImageResource("application.ico"))
64
65 # The name of the application (also used on window title bars etc).
66 name = Str("Workbench")
67
68 # The splash screen (None, the default, if no splash screen is required).
69 splash_screen = Instance(SplashScreen)
70
71 # The default position of the main window.
72 window_position = Tuple((200, 200))
73
74 # The default size of the main window.
75 window_size = Tuple((800, 600))
76
77 ###########################################################################
78 # 'IApplication' interface.
79 ###########################################################################
80
81 def run(self):
82 """Run the application.
83
84 This does the following (so you don't have to ;^):-
85
86 1) Starts the application
87 2) Creates and opens a workbench window
88 3) Starts the GUI event loop
89 4) When the event loop terminates, stops the application
90
91 """
92
93 logger.debug("---------- workbench application ----------")
94
95 # Make sure the GUI has been created (so that, if required, the splash
96 # screen is shown).
97 gui = self.gui
98
99 # Start the application.
100 if self.start():
101 # Create and open the first workbench window.
102 window = self.workbench.create_window(
103 position=self.window_position, size=self.window_size
104 )
105 window.open()
106
107 # We stop the application when the workbench has exited.
108 self.workbench.on_trait_change(self._on_workbench_exited, "exited")
109
110 # Start the GUI event loop.
111 #
112 # THIS CALL DOES NOT RETURN UNTIL THE GUI IS CLOSED.
113 gui.start_event_loop()
114
115 ###########################################################################
116 # 'WorkbenchApplication' interface.
117 ###########################################################################
118
119 #### Initializers #########################################################
120
121 def _about_dialog_default(self):
122 """Trait initializer."""
123
124 return AboutDialog(image=ImageResource("about"))
125
126 def _gui_default(self):
127 """Trait initializer."""
128
129 return GUI(splash_screen=self.splash_screen)
130
131 def _workbench_default(self):
132 """Trait initializer."""
133
134 return self.create_workbench()
135
136 #### Methods ##############################################################
137
138 def about(self):
139 """Display the about dialog."""
140
141 # fixme: We really need to create a new 'about dialog' every time so
142 # that it can have the active window as its parent.
143 self.about_dialog.open()
144
145 # fixme: Is this needed on the public API? Why can't we just do this in
146 # the default initializer (_workbench_default)?
147 def create_workbench(self):
148 """Create the workbench."""
149
150 logger.debug("workbench factory %s", self.workbench_factory)
151
152 return self.workbench_factory(application=self)
153
154 def exit(self):
155 """Exit the application.
156
157 This closes all open windows and hence exits the GUI event loop.
158
159 """
160
161 self.workbench.exit()
162
163 ###########################################################################
164 # Private interface.
165 ###########################################################################
166
167 def _on_workbench_exited(self):
168 """Dynamic trait change handler."""
169
170 # We don't invoke 'stop' directly because:-
171 #
172 # The workbench is often exited via a user action (either by closing
173 # the last open window, or by choosing 'File/Exit'). If this happens
174 # then the workbench 'exit' method is called from within an event
175 # handler which would cause the 'stop' method to get called *before*
176 # the handling of the window 'closed' event is complete. Hance, this
177 # might mean that somebody listening for the window being closed would
178 # get the event *after* the application had already stopped!
179 self.gui.invoke_later(self.stop)