changeset 1262:2f861eea1214

Make domview_ui manages scenes and layers found by checker
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 12 Jan 2011 01:03:33 +0800
parents 6177f5d0ef01
children 1a54486c2987
files pyink/consistency.py pyink/domview.py pyink/domview_ui.py
diffstat 3 files changed, 147 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/pyink/consistency.py	Tue Jan 11 17:59:17 2011 +0800
+++ b/pyink/consistency.py	Wed Jan 12 01:03:33 2011 +0800
@@ -51,6 +51,29 @@
         self._start_check()
         pass
 
+    def _insert_node_recursive(self, node, child):
+        child_name = child.name()
+        
+        if child_name == 'ns0:scene':
+            scene_group_id = child.getAttribute('ref')
+            try:
+                scene_group = self._domview.get_node(scene_group_id)
+            except KeyError:    # can not find associated scene group.
+                pass
+            else:
+                self._domview.manage_scene_node(child, scene_group)
+                pass
+        elif child_name == 'svg:g':
+            if node.name() == 'svg:svg':
+                self._domview.manage_layer_group(child)
+                pass
+            pass
+        
+        for cchild in child.childList():
+            self._remove_node_recursive(child, cchild)
+            pass
+        pass
+
     def _remove_node_recursive(self, node, child):
         for cchild in child.childList():
             self._remove_node_recursive(child, cchild)
@@ -100,6 +123,7 @@
         pass
 
     def do_insert_node(self, node, child):
+        self._insert_node_recursive(node, child)
         pass
 
     def do_remove_node(self, node, child):
--- a/pyink/domview.py	Tue Jan 11 17:59:17 2011 +0800
+++ b/pyink/domview.py	Wed Jan 12 01:03:33 2011 +0800
@@ -391,6 +391,12 @@
 	
 	return scene_node
 
+    ## \brief Manage a existed scene node at given layer.
+    #
+    def manage_scene_node(self, layer_idx, scene_node):
+        self._layers[layer_idx].scenes.append(scene_node)
+        pass
+
     ## \brief Change attributes of a scene node.
     #
     # This is here to monitor changes of scene node.
@@ -499,6 +505,62 @@
 	    pass
 	pass
 
+    ## \brief Manage a existed layer group
+    #
+    # This method scan layer groups of all managed layers, and find a
+    # proper place to insert it.
+    #
+    # \return -1 for error, or layer index.
+    #
+    def manage_layer_group(self, layer_group_id):
+        layer_group = self.get_node(layer_group_id)
+        new_layer = Layer(layer_group)
+
+        if not self._layers:
+            new_layer.idx = 0
+            self._layers.append(new_layer)
+            return 0
+        
+        #
+        # Scan who is after the given group
+        #
+        next_group = layer_group.next()
+        while next_group:
+            next_group_id = next_group.getAttribute('id')
+            
+            for vlayer in self._layers:
+                vlayer_group_id = vlayer.group.getAttribute('id')
+                if vlayer_group_id == next_group_id:
+                    # This layer group is after given one.
+                    self._layers.insert(vlayer.idx, new_layer)
+                    
+                    for idx in range(vlayer.idx, len(self._layers)):
+                        self._layers[idx].idx = idx
+                        pass
+                    return new_layer.idx
+                pass
+            
+            next_group = next_group.next()
+            pass
+        
+        #
+        # Is the given group after last layer group?
+        #
+        tail_group = self._layers[-1].group.next()
+        while tail_group:
+            tail_group_id = tail_group.getAttribute('id')
+            
+            if tail_group_id == layer_group_id:
+                # it is after last layer group.
+                new_layer.idx = len(self._layers)
+                self._layers.append(new_layer)
+                return new_layer.idx
+            
+            tail_group = tail_group.next()
+            pass
+
+        return -1             # error, can not determinze the position
+    
     ## \brief Remove layer and associated scene nodes and scene groups.
     #
     def rm_layer(self, layer_idx):
--- a/pyink/domview_ui.py	Tue Jan 11 17:59:17 2011 +0800
+++ b/pyink/domview_ui.py	Wed Jan 12 01:03:33 2011 +0800
@@ -665,9 +665,69 @@
     pass
 
 
+## \brief Expose some internal interface.
+#
+# This is a mix-in to provide API for internal using, for example,
+# consistency_checker.
+#
+class domview_internal(object):
+    ## \brief Search a node by a ID.
+    #
+    def get_node(self, node_id):
+        node = self._dom.get_node(node_id)
+        return node
+
+    ## \brief Search scene node by scene group ID.
+    #
+    def get_scene_by_group(self, scene_group_id):
+        scene_node = self._dom.get_scene(scene_group_id)
+        return scene_node
+
+    ## \brief Manage a scene node that is unknown by domview_ui before.
+    #
+    def manage_scene_node(self, scene_node, scene_group):
+        layer_group = scene_group.parent()
+        layer_group_id = layer_group.getAttribute('id')
+        layer_idx = self.find_layer_from_group(layer_group_id)
+        self._dom.manage_scene_node(layer_idx, scene_node)
+
+        start, end, tween_name = \
+            self._dom._parse_one_scene(scene_node)
+        tween_type = self._tween_type_names.index(tween_name)
+
+        tween_len = end - start + 1
+        self._fl_stack.mark_keyframe(layer_idx, start)
+        self._fl_stack.set_keyframe_data(layer_idx, start, scene_node)
+        self._fl_stack.tween(layer_idx, start, tween_len, tween_type)
+        pass
+
+    ## \brief Manage a layer group that is unknown by domview_ui before.
+    #
+    def manage_layer_group(self, layer_group):
+        try:
+            layer_group_id = layer_group.getAttribute('id')
+        except:
+            return
+        
+        layer_idx = self._dom.manage_layer_group(layer_group_id)
+        if layer_idx == -1:
+            return
+        
+        self._fl_stack._add_frameline(layer_idx)
+        self._fl_stack._show_framelines()
+        try:
+            label = layer_group.getAttribute('inkscape:label')
+        except:
+            label = layer_group.getAttribute('id')
+            pass
+        self._fl_stack.set_layer_label(layer_idx, label)
+        pass
+    pass
+
+
 ## \brief A mix-in to enable workers for a domview_ui.
 #
-class domview_ui_with_workers(domview_ui):
+class domview_ui_with_workers(domview_ui, domview_internal):
     def __init__(self):
         super(domview_ui_with_workers, self).__init__()