changeset 1206:1d476b35dc79

Merge and move code of tracking max frame number to MBScene_dom_monitor
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 04 Jan 2011 10:08:02 +0800
parents 1af64bcdfd7d (current diff) 9d715956823a (diff)
children 489e6e474fdf
files pyink/MBScene.py pyink/frameline.py
diffstat 2 files changed, 133 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/pyink/MBScene.py	Mon Jan 03 22:12:30 2011 +0800
+++ b/pyink/MBScene.py	Tue Jan 04 10:08:02 2011 +0800
@@ -158,12 +158,14 @@
     def __init__(self, *args, **kws):
 	super(MBScene_dom_monitor, self).__init__()
 
+	self._maxframe = 0
 	self._id2node = {}	# map ID to the node in the DOM tree.
 	self._group2scene = {}	# map ID of a group to associated scene node.
 	pass
     
     def _start_monitor(self):
 	self._collect_node_ids()
+	self._collect_all_scenes()
 	
 	doc = self._doc
 	addEventListener(doc,'DOMNodeInserted', self._on_insert_node, None)
@@ -192,9 +194,39 @@
 		    self._group2scene[ref] = child
 		    pass
 		pass
+
+	    try:
+		start = child.getAttribute('start')
+		self._maxframe = max(int(start), self._maxframe)
+	    except:
+		pass
+	    try:
+		start = child.getAttribute('end')
+		self._maxframe = max(int(start), self._maxframe)
+	    except:
+		pass
 	    pass
 	pass
 
+    def _find_maxframe(self, scenes_node):
+	maxframe = 0
+	for child in scenes_node.childList():
+	    if child.name() != 'ns0:scene':
+		continue
+	    
+	    try:
+		start = child.getAttribute('start')
+		maxframe = max(int(start), maxframe)
+	    except:
+		pass
+	    try:
+		end = child.getAttribute('end')
+		maxframe = max(int(end), maxframe)
+	    except:
+		pass
+	    pass
+	return maxframe
+
     def _on_remove_node(self, node, child):
 	try:
 	    child_id = child.getAttribute('id')
@@ -215,6 +247,15 @@
 	    else:
 		del self._group2scene[ref]
 		pass
+
+	    try:
+		if node.name() == 'ns0:scenes' and \
+			(int(child.getAttribute('start')) == self._maxframe or
+			 int(child.getAttribute('end')) == self._maxframe):
+		    self._maxframe = self._find_maxframe(node)
+		    pass
+	    except:
+		pass
 	    pass
 	pass
 
@@ -246,7 +287,10 @@
 	    if new_value:
 		self._group2scene[new_value] = node
 		pass
-	    return
+	    pass
+	elif (name in ('start', 'end')) and node.name() == 'ns0:scene':
+	    self._maxframe = max(int(new_value), self._maxframe)
+	    pass
 	pass
     
     ## \brief Collect ID of nodes in the document.
@@ -273,6 +317,67 @@
 	    pass
 	pass
     
+    def _parse_one_scene(self, scene_node):
+	assert scene_node.name() == 'ns0:scene'
+	
+	start = int(scene_node.getAttribute("start"))
+	try:
+	    end = int(scene_node.getAttribute("end"))
+	except:
+	    end = start
+	    pass
+	
+	try:
+	    scene_type = scene_node.getAttribute('type')
+	    if scene_type == None:
+		scene_type = 'normal'
+		pass
+	except:
+	    scene_type = 'normal'
+	    pass
+
+	return start, end, scene_type
+
+    def _parse_one_scenes(self, scenes_node):
+	try:
+	    cur = int(n.getAttribute("current"))
+	except:
+	    cur = 0
+	    pass
+	self.current = cur
+	
+	for scene_node in scenes_node.childList():
+	    if scene_node.name() != 'ns0:scene':
+		continue
+
+	    try:
+		start, end, scene_type = self._parse_one_scene(scene_node)
+	    except:
+		continue
+	    
+	    group_id = scene_node.getAttribute("ref")
+	    self._group2scene[group_id] = (start, end, scene_type)
+	    pass
+	pass
+
+    ## \brief Parse all scenes node in svg:metadata subtree.
+    #
+    def _collect_all_scenes(self):
+	root = self._root
+	for child in root.childList():
+	    if child.name() != 'svg:metadata':
+		continue
+
+	    metadata_node = child
+	    for metachild in metadata_node.childList():
+		if metachild.name() == 'ns0:scenes':
+		    self._parse_one_scenes(metachild)
+		    self._maxframe = self._find_maxframe(metachild)
+		    pass
+		pass
+	    pass
+	pass
+    
     ## \brief Return the node with given ID.
     #
     def get_node(self, node_id):
@@ -311,7 +416,6 @@
 	
 	self._start_monitor()	# start MBScene_dom_monitor
 	self._init_metadata()
-	self._parse_all_scenes()
 	self._parse_all_layers()
 	pass
    
@@ -330,74 +434,6 @@
 	print " " * l * 2,"/>"
 	pass
 
-    def _parse_one_scene(self, scene_node):
-	assert scene_node.name() == 'ns0:scene'
-	
-	start = int(scene_node.getAttribute("start"))
-	try:
-	    end = int(scene_node.getAttribute("end"))
-	except:
-	    end = start
-	    pass
-	
-	try:
-	    scene_type = scene_node.getAttribute('type')
-	    if scene_type == None:
-		scene_type = 'normal'
-		pass
-	except:
-	    scene_type = 'normal'
-	    pass
-
-	return start, end, scene_type
-
-    def _parse_one_scenes(self, scenes_node):
-	self.scenemap = {}
-	try:
-	    cur = int(n.getAttribute("current"))
-	except:
-	    cur = 0
-	    pass
-	self.current = cur
-	
-	for scene_node in scenes_node.childList():
-	    if scene_node.name() != 'ns0:scene':
-		continue
-
-	    try:
-		start, end, scene_type = self._parse_one_scene(scene_node)
-	    except:
-		continue
-	    
-	    if end > self.maxframe:
-		self.maxframe = end
-		pass
-	    
-	    link = scene_node.getAttribute("ref")
-	    self.scenemap[link] = (start, end, scene_type)
-	    if cur >= start and cur <= end:
-		self.currentscene = link
-		pass
-	    pass
-	pass
-
-    ## \brief Parse all scenes node in svg:metadata subtree.
-    #
-    def _parse_all_scenes(self):
-	root = self._root
-	for child in root.childList():
-	    if child.name() != 'svg:metadata':
-		continue
-
-	    metadata_node = child
-	    for metachild in metadata_node.childList():
-		if metachild.name() == 'ns0:scenes':
-		    self._parse_one_scenes(metachild)
-		    pass
-		pass
-	    pass
-	pass
-    
     ## \brief Create a scenes node if not existed.
     #
     def _init_metadata(self):
@@ -433,20 +469,23 @@
 	doc = self._doc
 	
 	scene_node = doc.createElement('ns0:scene')
-	scene_node.setAttribute('start', str(start))
+	self._chg_scene_node(scene_node, start=start)
 	if start != end:
 	    self._chg_scene_node(scene_node, end=end)
 	    pass
 	type_name = type_names[frame_type]
-	scene_node.setAttribute('type', type_name)
+	self._chg_scene_node(scene_node, tween_type=type_name)
 	if ref:
-	    scene_node.setAttribute('ref', ref)
+	    self._chg_scene_node(scene_node, ref=ref)
 	    pass
 	
 	scenes_node.appendChild(scene_node)
 	
 	return scene_node
 
+    ## \brief Change attributes of a scene node.
+    #
+    # This is here to monitor changes of scene node.
     def _chg_scene_node(self, scene_node, start=None, end=None,
 			tween_type=None, ref=None):
 	if start:
@@ -454,9 +493,6 @@
 	    pass
 	if end:
 	    scene_node.setAttribute('end', str(end))
-	    if int(end) > self.maxframe:
-		self.maxframe = int(end)
-		pass
 	    pass
 	if tween_type:
 	    scene_node.setAttribute('type', tween_type)
@@ -593,12 +629,11 @@
 	self.last_update = None
 	pybInkscape.inkscape.connect('change_selection', self.show_selection)
 	self.last_select = None
-	self._lockui=False
-	self.tween=None
+	self._lockui = False
+	self.tween = None
 	self.document = None
 	self.root = root
-	self.framerate=12
-	self.maxframe=0
+	self.framerate = 12
 	self._disable_tween_type_selector = False
 	pass
 
@@ -1079,23 +1114,28 @@
 	if self.btnRun.get_label() == "Run":
 	    self.btnRun.set_label("Stop")
 	    self._lockui = True
-            self.last_update = glib.timeout_add(1000/self.framerate,self.doRunNext)
+	    tmout = 1000 / self.framerate
+            self.last_update = glib.timeout_add(tmout, self.doRunNext)
 	else:
 	    self.btnRun.set_label("Run")
 	    glib.source_remove(self.last_update)
 	    self._lockui = False
 	    pass
+	pass
 
     def doRunNext(self):
-	if self.current > self.maxframe:
+	if self.current > self._maxframe:
 	    self.current = 0
+	    pass
 	try:
 	    self.setCurrentScene(self.current)
 	except:
 	    traceback.print_exc()
 	    raise
 	self.current = self.current + 1
-        self.last_update = glib.timeout_add(1000/self.framerate,self.doRunNext)
+	tmout = 1000 / self.framerate
+        self.last_update = glib.timeout_add(tmout, self.doRunNext)
+	pass
 
     def addButtons(self,hbox):
 	btn = gtk.Button('Insert Key')
@@ -1193,11 +1233,11 @@
 	self.tween = TweenObject(self.document, self.root)
 	self._init_framelines()
 	self._add_frameline_for_layers()
-	# self._updateUI()
 	
 	if self.top == None:
 	    self.top = gtk.VBox(False,0)
-	    self.desktop.getToplevel().child.child.pack_end(self.top,expand=False)
+	    toplevel = self.desktop.getToplevel()
+	    toplevel.child.child.pack_end(self.top,expand=False)
 	else:
 	    self.top.remove(self.startWindow)
 	    pass
--- a/pyink/frameline.py	Mon Jan 03 22:12:30 2011 +0800
+++ b/pyink/frameline.py	Tue Jan 04 10:08:02 2011 +0800
@@ -761,6 +761,14 @@
 		return key.right_tween_type
 	    pass
 	pass
+    
+    ## Get the maximum frame number in a layer(frameline)
+    # Return the frame number
+    def max_frame(self):
+	keys = self._keys
+	if not keys:
+	    return 0
+        return keys[-1].idx
 
     ## \bref Return range of blocks of conesequence frames (tweens).
     #