File: node_tree_model.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 (324 lines) | stat: -rw-r--r-- 9,881 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# (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 model for a tree control with extensible node types. """


from traits.api import Dict, Instance


from .node_manager import NodeManager
from .tree_model import TreeModel


class NodeTreeModel(TreeModel):
    """ The model for a tree control with extensible node types. """

    # 'NodeTreeModel' interface --------------------------------------------

    # The node manager looks after all node types.
    node_manager = Instance(NodeManager, ())

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

    # Node monitors.
    _monitors = Dict()

    # ------------------------------------------------------------------------
    # 'TreeModel' interface.
    # ------------------------------------------------------------------------

    def has_children(self, node):
        """ Returns True if a node has children, otherwise False.

        This method is provided in case the model has an efficient way to
        determine whether or not a node has any children without having to
        actually get the children themselves.

        """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        if node_type.allows_children(node):
            has_children = node_type.has_children(node)

        else:
            has_children = False

        return has_children

    def get_children(self, node):
        """ Returns the children of a node. """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        # Does the node allow children (ie. a folder allows children, a file
        # does not)?
        if node_type.allows_children(node):
            # Get the node's children.
            children = node_type.get_children(node)

        else:
            children = []

        return children

    def get_default_action(self, node):
        """ Returns the default action for a node. """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.get_default_action(node)

    def get_drag_value(self, node):
        """ Get the value that is dragged for a node.

        By default the drag value is the node itself.

        """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.get_drag_value(node)

    def can_drop(self, node, data):
        """ Returns True if a node allows an object to be dropped onto it. """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.can_drop(node, data)

    def drop(self, node, data):
        """ Drops an object onto a node. """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        node_type.drop(node, data)

    def get_image(self, node, selected, expanded):
        """ Returns the label image for a node.

        Return None (the default) if no image is required.

        """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.get_image(node, selected, expanded)

    def get_key(self, node):
        """ Generate a unique key for a node. """

        return self.node_manager.get_key(node)

    def get_selection_value(self, node):
        """ Get the value that is used when a node is selected.

        By default the selection value is the node itself.

        """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.get_selection_value(node)

    def get_text(self, node):
        """ Returns the label text for a node.

        Return None if no text is required.  By default we return 'str(node)'.

        """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.get_text(node)

    def can_set_text(self, node, text):
        """ Returns True if the node's label can be set. """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.can_set_text(node, text)

    def set_text(self, node, text):
        """ Sets the label text for a node. """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.set_text(node, text)

    def is_collapsible(self, node):
        """ Returns True if the node is collapsible, otherwise False. """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.is_collapsible(node)

    def is_draggable(self, node):
        """ Returns True if the node is draggable, otherwise False. """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.is_draggable(node)

    def is_editable(self, node):
        """ Returns True if the node is editable, otherwise False.

        If the node is editable, its text can be set via the UI.

        """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.is_editable(node)

    def is_expandable(self, node):
        """ Returns True if the node is expandanble, otherwise False. """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.is_expandable(node)

    def add_listener(self, node):
        """ Adds a listener for changes to a node. """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        # Create a monitor to listen for changes to the node.
        monitor = node_type.get_monitor(node)
        if monitor is not None:
            self._start_monitor(monitor)
            self._monitors[self.node_manager.get_key(node)] = monitor

    def remove_listener(self, node):
        """ Removes a listener for changes to a node. """

        key = self.node_manager.get_key(node)

        monitor = self._monitors.get(key)
        if monitor is not None:
            self._stop_monitor(monitor)
            del self._monitors[key]

        return

    # ------------------------------------------------------------------------
    # 'NodeTreeModel' interface.
    # ------------------------------------------------------------------------

    def get_context_menu(self, node):
        """ Returns the context menu for a node. """

        # Determine the node type for this node.
        node_type = self.node_manager.get_node_type(node)

        return node_type.get_context_menu(node)

    # ------------------------------------------------------------------------
    # 'Private' interface
    # ------------------------------------------------------------------------

    def _start_monitor(self, monitor):
        """ Starts a monitor. """

        monitor.observe(self._on_nodes_changed, "nodes_changed")

        monitor.observe(self._on_nodes_inserted, "nodes_inserted")

        monitor.observe(self._on_nodes_removed, "nodes_removed")

        monitor.observe(self._on_nodes_replaced, "nodes_replaced")

        monitor.observe(self._on_structure_changed, "structure_changed")

        monitor.start()

    def _stop_monitor(self, monitor):
        """ Stops a monitor. """

        monitor.observe(self._on_nodes_changed, "nodes_changed", remove=True)

        monitor.observe(self._on_nodes_inserted, "nodes_inserted", remove=True)

        monitor.observe(self._on_nodes_removed, "nodes_removed", remove=True)

        monitor.observe(self._on_nodes_replaced, "nodes_replaced", remove=True)

        monitor.observe(
            self._on_structure_changed, "structure_changed", remove=True
        )

        monitor.stop()

        return

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

    # Static ----

    # fixme: Commented this out as listeners are added and removed by the tree.
    # This caused duplicate monitors to be created for the root node.
    ##     def _root_changed(self, old, new):
    ##         """ Called when the root of the model has been changed. """

    ##         if old is not None:
    ##             # Remove a listener for structure/appearance changes
    ##             self.remove_listener(old)

    ##         if new is not None:
    ##             # Wire up a listener for structure/appearance changes
    ##             self.add_listener(new)

    ##         return

    # Dynamic ----

    def _on_nodes_changed(self, event):
        """ Called when nodes have changed. """

        self.nodes_changed = event.new

    def _on_nodes_inserted(self, event):
        """ Called when nodes have been inserted. """

        self.nodes_inserted = event.new

    def _on_nodes_removed(self, event):
        """ Called when nodes have been removed. """

        self.nodes_removed = event.new

    def _on_nodes_replaced(self, event):
        """ Called when nodes have been replaced. """

        self.nodes_replaced = event.new

    def _on_structure_changed(self, event):
        """ Called when the structure of a node has changed drastically. """

        self.structure_changed = event.new

        return