Debian Codegraph @

envisage/ui/tasks/preferences_dialog.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 # Enthought library imports.
11 from pyface.tasks.topological_sort import before_after_sort
12 from traits.api import Bool, HasTraits, Instance, List, on_trait_change, Str
13 from traitsui.api import Handler, Item, ListEditor, View
14
15 # Local imports.
16 from .preferences_category import PreferencesCategory
17 from .preferences_pane import PreferencesPane
18
19
20 class PreferencesTab(HasTraits):
21 """An object used internally by PreferencesDialog."""
22
23 name = Str
24 panes = List(PreferencesPane)
25
26 view = View(
27 Item(
28 "panes",
29 editor=ListEditor(style="custom"),
30 show_label=False,
31 style="readonly",
32 ),
33 resizable=True,
34 )
35
36
37 class PreferencesDialog(Handler):
38 """A dialog for editing preferences."""
39
40 #### 'PreferencesDialog' interface ########################################
41
42 # The application that created and is managing this dialog.
43 application = Instance("envisage.ui.tasks.api.TasksApplication")
44
45 # The list of categories to use when building the dialog.
46 categories = List(PreferencesCategory)
47
48 # The list of panes to use when building the dialog.
49 panes = List(PreferencesPane)
50
51 # Should the Apply button be shown?
52 show_apply = Bool(False)
53
54 #### Private interface ####################################################
55
56 _tabs = List(PreferencesTab)
57 _selected = Instance(PreferencesTab)
58
59 ###########################################################################
60 # Public interface
61 ###########################################################################
62
63 def select_pane(self, pane_id):
64 """
65 Find and activate the notebook tab that contains the given pane id.
66 """
67 for tab in self._tabs:
68 for pane in tab.panes:
69 if pane.id == pane_id:
70 self._selected = tab
71 return
72
73 ###########################################################################
74 # 'HasTraits' interface.
75 ###########################################################################
76
77 def trait_context(self):
78 """Returns the default context to use for editing or configuring
79 traits.
80 """
81 return {"object": self, "handler": self}
82
83 def traits_view(self):
84 """Build the dynamic dialog view."""
85 buttons = ["OK", "Cancel"]
86 if self.show_apply:
87 buttons = ["Apply"] + buttons
88
89 # Only show the tab bar if there is more than one category.
90 tabs_style = "custom" if len(self._tabs) > 1 else "readonly"
91
92 return View(
93 Item(
94 "_tabs",
95 editor=ListEditor(
96 page_name=".name",
97 style="custom",
98 use_notebook=True,
99 selected="_selected",
100 ),
101 show_label=False,
102 style=tabs_style,
103 ),
104 buttons=buttons,
105 kind="livemodal",
106 resizable=True,
107 title="Preferences",
108 )
109
110 ###########################################################################
111 # 'Handler' interface.
112 ###########################################################################
113
114 def apply(self, info=None):
115 """Handles the Apply button being clicked."""
116 for tab in self._tabs:
117 for pane in tab.panes:
118 pane.apply()
119
120 def close(self, info, is_ok):
121 """
122 Handles the user attempting to close a dialog-based user interface.
123 """
124 if is_ok:
125 self.apply()
126 return super().close(info, is_ok)
127
128 ###########################################################################
129 # Protected interface.
130 ###########################################################################
131
132 @on_trait_change("categories, panes")
133 def _update_tabs(self):
134 # Build a { category id -> [ PreferencePane ] } map.
135 categories = self.categories[:]
136 category_map = dict((category.id, []) for category in categories)
137 for pane in self.panes:
138 if pane.category in category_map:
139 category_map[pane.category].append(pane)
140 else:
141 categories.append(PreferencesCategory(id=pane.category))
142 category_map[pane.category] = [pane]
143
144 # Construct the appropriately sorted list of preference tabs.
145 tabs = []
146 for category in before_after_sort(categories):
147 panes = before_after_sort(category_map[category.id])
148 tabs.append(PreferencesTab(name=category.name, panes=panes))
149 self._tabs = tabs