File: node_monitor.py

package info (click to toggle)
python-pyface 6.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,756 kB
  • sloc: python: 39,728; makefile: 79
file content (144 lines) | stat: -rw-r--r-- 4,368 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
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/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!
#
# Author: Enthought, Inc.
# Description: <Enthought pyface package component>
#------------------------------------------------------------------------------
""" A monitor for appearance and structural changes to a node. """

# Standard library imports.
import logging

# Enthought library imports.
from traits.api import Any, Event, HasTraits

# Local imports.
from .node_event import NodeEvent


# Create a logger for this module.
logger = logging.getLogger(__name__)


class NodeMonitor(HasTraits):
    """ A monitor for appearance and structural changes to a node. """

    #### 'NodeMonitor' interface ##############################################

    # The node that we are monitoring.
    node = Any

    #### Events ####

    # Fired when child nodes in the node that we are monitoring have changed in
    # some way that affects their appearance but NOT their structure.
    nodes_changed = Event(NodeEvent)

    # Fired when child nodes have been inserted into the node that we are
    # monitoring.
    nodes_inserted = Event(NodeEvent)

    # Fired when child nodes have been removed from the node that we are
    # monitoring.
    nodes_removed = Event(NodeEvent)

    # Fired when child nodes have been replaced in the node that we are
    # monitoring.
    nodes_replaced = Event(NodeEvent)

    # Fired when the structure of the node that we are monitoring has changed
    # DRASTICALLY (i.e., we do not have enough information to make individual
    # changes/inserts/removals).
    structure_changed = Event(NodeEvent)


    ###########################################################################
    # 'NodeMonitor' interface.
    ###########################################################################

    #### public methods #######################################################

    def start(self):
        """ Start listening to changes to the node. """

        if self.node.obj is not None:
            self._setup_trait_change_handlers(self.node.obj)

        return


    def stop(self):
        """ Stop listening to changes to the node. """

        if self.node.obj is not None:
            self._setup_trait_change_handlers(self.node.obj, remove=True)

        return

    def fire_nodes_changed(self, children=[]):
        """ Fires the nodes changed event. """

        self.nodes_changed = NodeEvent(node=self.node, children=children)

        return

    def fire_nodes_inserted(self, children, index=-1):
        """ Fires the nodes inserted event.

        If the index is -1 it means the nodes were appended.

        fixme: The tree and model should probably have an 'appended' event.

        """

        self.nodes_inserted = NodeEvent(
            node=self.node, children=children, index=index
        )

        return

    def fire_nodes_removed(self, children):
        """ Fires the nodes removed event. """

        self.nodes_removed = NodeEvent(node=self.node, children=children)

        return

    def fire_nodes_replaced(self, old_children, new_children):
        """ Fires the nodes replaced event. """

        self.nodes_replaced = NodeEvent(
            node=self.node, old_children=old_children, children=new_children
        )

        return

    def fire_structure_changed(self):
        """ Fires the structure changed event. """

        self.structure_changed = NodeEvent(node=self.node)

        return


    #### protected methods ####################################################

    def _setup_trait_change_handlers(self, obj, remove=False):
        """ Add or remove trait change handlers to/from a node. """

        logger.debug('%s trait listeners on (%s) in NodeMonitor (%s)',
            (remove and 'Removing' or 'Adding'), obj, self)

        pass # derived classes should do something here!

        return


#### EOF ######################################################################