File: editor_manager.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 (103 lines) | stat: -rwxr-xr-x 3,106 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
# (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!
""" The default editor manager. """


import weakref


from traits.api import HasTraits, Instance, provides


from .i_editor_manager import IEditorManager
from .traits_ui_editor import TraitsUIEditor


@provides(IEditorManager)
class EditorManager(HasTraits):
    """ The default editor manager. """

    # 'IEditorManager' interface -------------------------------------------

    # The workbench window that the editor manager manages editors for ;^)
    window = Instance("pyface.workbench.api.WorkbenchWindow")

    # ------------------------------------------------------------------------
    # 'object' interface.
    # ------------------------------------------------------------------------

    def __init__(self, **traits):
        """ Constructor. """

        super().__init__(**traits)

        # A mapping from editor to editor kind (the factory that created them).
        self._editor_to_kind_map = weakref.WeakKeyDictionary()

        return

    # ------------------------------------------------------------------------
    # 'IEditorManager' interface.
    # ------------------------------------------------------------------------

    def add_editor(self, editor, kind):
        """ Registers an existing editor. """

        self._editor_to_kind_map[editor] = kind

    def create_editor(self, window, obj, kind):
        """ Create an editor for an object. """

        editor = TraitsUIEditor(window=window, obj=obj)

        self.add_editor(editor, kind)

        return editor

    def get_editor(self, window, obj, kind):
        """ Get the editor that is currently editing an object. """

        for editor in window.editors:
            if self._is_editing(editor, obj, kind):
                break
        else:
            editor = None

        return editor

    def get_editor_kind(self, editor):
        """ Return the 'kind' associated with 'editor'. """

        return self._editor_to_kind_map[editor]

    def get_editor_memento(self, editor):
        """ Return the state of an editor suitable for pickling etc.

        By default we don't save the state of editors.
        """

        return None

    def set_editor_memento(self, memento):
        """ Restore the state of an editor from a memento.

        By default we don't try to restore the state of editors.
        """

        return None

    # ------------------------------------------------------------------------
    # 'Protected' 'EditorManager'  interface.
    # ------------------------------------------------------------------------

    def _is_editing(self, editor, obj, kind):
        """ Return True if the editor is editing the object. """

        return editor.obj == obj