Debian Codegraph @

envisage/ui/tasks/preferences_pane.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 apptools.preferences.api import PreferencesHelper
12 from traits.api import Callable, Instance, Str
13 from traitsui.api import Controller
14
15
16 class PreferencesPane(Controller):
17 """A panel for configuring application preferences."""
18
19 #### 'Controller' interface ###############################################
20
21 #: The preferences helper for which this pane is a view.
22 model = Instance(PreferencesHelper)
23
24 #### 'PreferencesPane' interface ##########################################
25
26 #: An identifier for the pane (unique within a category).
27 id = Str
28
29 #: The ID of the category in which to place the pane.
30 category = Str("General")
31
32 #: The pane appears after the pane with this ID.
33 before = Str
34
35 #: The pane appears after the pane with this ID.
36 after = Str
37
38 #: The preferences dialog to which the pane belongs. Set by the framework.
39 dialog = Instance("envisage.ui.tasks.preferences_dialog.PreferencesDialog")
40
41 #: The factory to use for creating the preferences model object, of form:
42 #:
43 #: ``callable(**traits) -> PreferencesHelper``
44 #:
45 #: If not specified, the preferences helper must be supplied manually.
46 model_factory = Callable
47
48 #### Private interface ####################################################
49
50 _model = Instance(PreferencesHelper)
51
52 ###########################################################################
53 # 'HasTraits' interface.
54 ###########################################################################
55
56 def trait_context(self):
57 """Re-implemented to use a copy of the model that is not connected to
58 the preferences node.
59 """
60 if self.model is None:
61 if self.model_factory is not None:
62 preferences = self.dialog.application.preferences
63 self.model = self.model_factory(preferences=preferences)
64 else:
65 raise ValueError("A preferences pane must have a model!")
66
67 # Make sure that we don't clone the preferences trait, since that
68 # can lead to the preferences node being updated prematurely.
69 # xref: enthought/envisage#582
70 traits_to_clone = [
71 trait_name
72 for trait_name in self.model.copyable_trait_names()
73 if trait_name != "preferences"
74 ]
75 self._model = self.model.clone_traits(traits_to_clone)
76 self._model.preferences = None
77 return {"object": self._model, "controller": self, "handler": self}
78
79 ###########################################################################
80 # 'Handler' interface.
81 ###########################################################################
82
83 def apply(self, info=None):
84 """Handles the Apply button being clicked."""
85 trait_names = list(
86 filter(self._model._is_preference_trait, self._model.trait_names())
87 )
88 self.model.copy_traits(self._model, trait_names)
89
90 def close(self, info, is_ok):
91 """
92 Handles the user attempting to close a dialog-based user interface.
93 """
94 if is_ok:
95 self.apply()
96 return super().close(info, is_ok)