comparison pyink/domview.py @ 1318:b1776f441e8a

Fix issue of parsing scene nodes
author Thinker K.F. Li <thinker@codemud.net>
date Sat, 22 Jan 2011 22:08:42 +0800
parents e2ad9eb7f7a4
children 6a014a4ec79e
comparison
equal deleted inserted replaced
1317:e2ad9eb7f7a4 1318:b1776f441e8a
1 import random 1 import random
2 import dom_event 2 import dom_event
3 from tween import TweenObject 3 from tween import TweenObject
4
5
6 ## \brief Compare two nodes with ID.
7 #
8 # \return True if node1 ID equivalent to ndoe2 ID.
9 #
10 def _id_eq(node1, node2):
11 try:
12 node1_id = node1.getAttribute('id')
13 except:
14 return False
15
16 try:
17 node2_id = node2.getAttribute('id')
18 except:
19 return False
20 return node1_id == node2_id
4 21
5 22
6 class Layer: 23 class Layer:
7 def __init__(self, node): 24 def __init__(self, node):
8 self.scenes = [] 25 self.scenes = []
448 if child_id not in self._id2node: 465 if child_id not in self._id2node:
449 self._id2node[child_id] = child 466 self._id2node[child_id] = child
450 pass 467 pass
451 pass 468 pass
452 469
453 if child.name() == 'ns0:scene': 470 if child.name() == 'ns0:scene' and _id_eq(node, self._scenes_node):
454 try: 471 try:
455 ref = child.getAttribute('ref') 472 ref = child.getAttribute('ref')
456 except: 473 except:
457 pass 474 pass
458 else: 475 else:
507 raise ValueError, \ 524 raise ValueError, \
508 'remove a node that is never known (%s)' % (child_id) 525 'remove a node that is never known (%s)' % (child_id)
509 del self._id2node[child_id] 526 del self._id2node[child_id]
510 pass 527 pass
511 528
512 if child.name() == 'ns0:scene': 529 if child.name() == 'ns0:scene' and _id_eq(node, self._scenes_node):
513 try: 530 try:
514 ref = child.getAttribute('ref') 531 ref = child.getAttribute('ref')
515 except: 532 except:
516 pass 533 pass
517 else: 534 else:
518 del self._group2scene[ref] 535 del self._group2scene[ref]
519 pass 536 pass
520 537
521 try: 538 try:
522 if node.name() == 'ns0:scenes' and \ 539 if int(child.getAttribute('start')) == self._maxframe or \
523 (int(child.getAttribute('start')) == self._maxframe or 540 int(child.getAttribute('end')) == self._maxframe:
524 int(child.getAttribute('end')) == self._maxframe):
525 self._maxframe = self._find_maxframe(node) 541 self._maxframe = self._find_maxframe(node)
526 pass 542 pass
527 except: 543 except:
528 pass 544 pass
529 pass 545 pass
530 pass 546 pass
531 547
532 def _on_attr_modified(self, node, name, old_value, new_value): 548 def _on_attr_modified(self, node, name, old_value, new_value):
533 if name == 'id' and old_value != new_value: 549 if old_value == new_value:
550 return
551
552 if name == 'id':
534 if old_value and (old_value not in self._id2node): 553 if old_value and (old_value not in self._id2node):
535 raise ValueError, \ 554 raise ValueError, \
536 'old ID value of passed node is invalid one (%s)' % \ 555 'old ID value of passed node is invalid one (%s)' % \
537 (old_value) 556 (old_value)
538 if (new_value in self._id2node): 557 if (new_value in self._id2node):
544 del self._id2node[old_value] 563 del self._id2node[old_value]
545 pass 564 pass
546 self._id2node[new_value] = node 565 self._id2node[new_value] = node
547 pass 566 pass
548 elif name == 'ref' and node.name() == 'ns0:scene': 567 elif name == 'ref' and node.name() == 'ns0:scene':
549 if old_value == new_value: 568 parent_node = node.parent()
550 return 569 scenes_node = self._scenes_node
570 if not _id_eq(parent_node, scenes_node):
571 return # not in current timeline
572
551 if old_value: 573 if old_value:
552 node = self._group2scene[old_value] # use old node. Binding 574 node = self._group2scene[old_value] # use old node. Binding
553 # may generate a new 575 # may generate a new
554 # wrapper. 576 # wrapper.
555 del self._group2scene[old_value] 577 del self._group2scene[old_value]
557 if new_value: 579 if new_value:
558 self._group2scene[new_value] = node 580 self._group2scene[new_value] = node
559 pass 581 pass
560 pass 582 pass
561 elif (name in ('start', 'end')) and node.name() == 'ns0:scene': 583 elif (name in ('start', 'end')) and node.name() == 'ns0:scene':
584 parent_node = node.parent()
585 scenes_node = self._scenes_node
586 if not _id_eq(parent_node, scenes_node):
587 return # not in current timeline
588
562 try: 589 try:
563 new_value = int(new_value) 590 new_value = int(new_value)
564 old_value = int(old_value) 591 old_value = int(old_value)
565 except TypeError: 592 except TypeError:
566 scenes_node = node.parent()
567 self._maxframe = self._find_maxframe(scenes_node) 593 self._maxframe = self._find_maxframe(scenes_node)
568 else: 594 else:
569 if old_value == self._maxframe and old_value > new_value: 595 if old_value == self._maxframe and old_value > new_value:
570 # _maxframe may be reduced. 596 # _maxframe may be reduced.
571 scenes_node = node.parent()
572 self._maxframe = self._find_maxframe(scenes_node) 597 self._maxframe = self._find_maxframe(scenes_node)
573 else: 598 else:
574 self._maxframe = max(int(new_value), self._maxframe) 599 self._maxframe = max(int(new_value), self._maxframe)
575 pass 600 pass
576 pass 601 pass
645 pass 670 pass
646 671
647 ## \brief Parse all scenes node in svg:metadata subtree. 672 ## \brief Parse all scenes node in svg:metadata subtree.
648 # 673 #
649 def _collect_all_scenes(self): 674 def _collect_all_scenes(self):
650 root = self._root 675 scenes_node = self._scenes_node
651 for child in root.childList(): 676 self._parse_one_scenes(scenes_node)
652 if child.name() != 'svg:metadata': 677 self._maxframe = self._find_maxframe(scenes_node)
653 continue 678 pass
654
655 metadata_node = child
656 for metachild in metadata_node.childList():
657 if metachild.name() == 'ns0:scenes':
658 self._parse_one_scenes(metachild)
659 self._maxframe = self._find_maxframe(metachild)
660 pass
661 pass
662 pass
663 pass 679 pass
664 680
665 ## \brief Return the node with given ID. 681 ## \brief Return the node with given ID.
666 # 682 #
667 def get_node(self, node_id): 683 def get_node(self, node_id):
748 self._scenes_node = scenes_node 764 self._scenes_node = scenes_node
749 pass 765 pass
750 pass 766 pass
751 767
752 def _parse_all_layers(self): 768 def _parse_all_layers(self):
753 root = self._scenes_node
754 layers = self._layers 769 layers = self._layers
755 layers_parent = self._layers_parent 770 layers_parent = self._layers_parent
756 771
757 for child in layers_parent.childList(): 772 for child in layers_parent.childList():
758 if child.name() != 'svg:g': 773 if child.name() != 'svg:g':