changeset 1247:45e9566ea5c0

Fix bugs of inserting/removing frames. - tracking layer.scenes when add/remove scene nodes. - fix buggy add_frame()/rm_frame() of frameline. - change ID of nodes before inserting when copy nodes from another group.
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 10 Jan 2011 22:15:16 +0800
parents 42c4874c8d1e
children 2f9fa5d59e67
files pyink/domview.py pyink/domview_ui.py pyink/frameline.py
diffstat 3 files changed, 53 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/pyink/domview.py	Mon Jan 10 19:44:15 2011 +0800
+++ b/pyink/domview.py	Mon Jan 10 22:15:16 2011 +0800
@@ -392,7 +392,7 @@
 
     ## \brief Create and add a ns0:scene node under ns0:scenes subtree.
     #
-    def add_scene_node(self, start, end,
+    def add_scene_node(self, layer_idx, start, end,
 		       frame_type=TweenObject.TWEEN_TYPE_NORMAL,
 		       ref=None):
 	type_names = ('normal', 'scale')
@@ -411,6 +411,8 @@
 	    pass
 	
 	scenes_node.appendChild(scene_node)
+        
+        self._layers[layer_idx].scenes.append(scene_node)
 	
 	return scene_node
 
@@ -435,6 +437,14 @@
 
     def rm_scene_node(self, scene_node):
 	self._scenes_node.removeChild(scene_node)
+        for layer in self._layers:
+            try:
+                layer.scenes.remove(scene_node)
+            except ValueError:  # not in the list
+                pass
+            else:
+                break
+            pass
 	pass
 
     def rm_scene_node_n_group(self, scene_node):
@@ -442,7 +452,7 @@
 	scene_group_node = self.get_node(scene_group_id)
 	scene_group_node.parent().removeChild(scene_group_node)
 	
-	self._scenes_node.removeChild(scene_node)
+        self.rm_scene_node(scene_node)
 	pass
 
     ## \brief Create and add a svg:g for a scene under a group for a layer.
@@ -589,7 +599,7 @@
 	last_rm = frame_idx + num - 1 # last removed frame
 	for scene_node in layer.scenes:
 	    start, end, tween_type = \
-		self._dom._parse_one_scene(scene_node)
+		self._parse_one_scene(scene_node)
 	    
 	    if end < frame_idx:
 		continue
@@ -614,20 +624,36 @@
     def copy_group_children(self, src_group, dst_group):
 	# Search for the duplicated group
 	doc = self._doc
+
+	dup_group = src_group.duplicate(doc)
+        
+	old_nodes = _DOM_iterator(src_group)
+	new_nodes = _DOM_iterator(dup_group)
+        new_gids = set()
+	for old_node in old_nodes:
+	    old_node_id = old_node.getAttribute('id')
+	    new_node = new_nodes.next()
+	    new_node.setAttribute('ns0:duplicate-src', old_node_id)
+            
+            #
+            # Change ID here, or inkscape would insert the node with
+            # the same ID, and change it later to avoid duplication.
+            # But, our event handler would be called before changing
+            # ID.  It would confuse our code.  We change ID of nodes
+            # before inserting them into the DOM-tree.
+            #
+            gid = self.new_id()
+            while gid in new_gids:
+                gid = self.new_id()
+                pass
+            new_gids.add(gid)
+            new_node.setAttribute('id', gid)
+	    pass
 	
-	dup_group = src_group.duplicate(doc)
 	for child in dup_group.childList():
 	    dup_group.removeChild(child) # prvent from crash
 	    dst_group.appendChild(child)
 	    pass
-
-	old_nodes = _DOM_iterator(src_group)
-	new_nodes = _DOM_iterator(dst_group)
-	for old_node in old_nodes:
-	    old_node_id = old_node.getAttribute('id')
-	    new_node = new_nodes.next()
-	    new_node.setAttribute('ns0:duplicate-src', old_node_id)
-	    pass
 	pass
     pass
 
--- a/pyink/domview_ui.py	Mon Jan 10 19:44:15 2011 +0800
+++ b/pyink/domview_ui.py	Mon Jan 10 22:15:16 2011 +0800
@@ -416,7 +416,7 @@
 	scene_group = self._dom.add_scene_group(layer_idx)
 	scene_group_id = scene_group.getAttribute('id')
 	
-	scene_node = self._dom.add_scene_node(key_idx, key_idx)
+	scene_node = self._dom.add_scene_node(layer_idx, key_idx, key_idx)
 	self._dom.chg_scene_node(scene_node, ref=scene_group_id)
 	
 	self._fl_stack.mark_keyframe(layer_idx, key_idx)
@@ -488,7 +488,7 @@
     # \ref num frames are removed.
     #
     def rm_frames(self, layer_idx, frame_idx, num):
-	self._fl_stack.insert_frames(layer_idx, frame_idx, num)
+	self._fl_stack.rm_frames(layer_idx, frame_idx, num)
 	self._dom.rm_frames(layer_idx, frame_idx, num)
 	pass
 
--- a/pyink/frameline.py	Mon Jan 10 19:44:15 2011 +0800
+++ b/pyink/frameline.py	Mon Jan 10 22:15:16 2011 +0800
@@ -691,13 +691,19 @@
 	    key = self._keys[pos]
 	    if key.idx == idx:
 		self.unmark_keyframe(idx)
+	    else:
+		pos = pos + 1
 		pass
 
 	    while pos < len(self._keys):
-		self._keys[pos].idx = self._keys[pos].idx - 1
-		pos = pos+1
+		key = self._keys[pos]
+		key.idx = key.idx - 1
+		pos = pos + 1
 		pass
 	    pass
+
+	self._draw_all_frames()
+	self._draw_active_frame()
 	pass
 
     ## \brief Inser a frame before given frame.
@@ -709,15 +715,19 @@
 	pos = self._find_keyframe_floor(idx)
 	if pos != -1:
 	    key = self._keys[pos]
-	    if key.idx == idx:
+	    if key.idx != idx:
 		pos = pos + 1
 		pass
+	    
 	    while pos < len(self._keys):
 		key = self._keys[pos]
 		key.idx = key.idx + 1
 		pos = pos + 1
 		pass
 	    pass
+
+	self._draw_all_frames()
+	self._draw_active_frame()
 	pass
     
     def unmark_keyframe(self, idx):