comparison pyink/MBScene.py @ 1210:3683e02b7482

Refactory frameline management code to MBScene_framelines
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 04 Jan 2011 11:39:28 +0800
parents 41d13feeecc4
children 4bbad66f23b8
comparison
equal deleted inserted replaced
1209:41d13feeecc4 1210:3683e02b7482
528 layers[idx].idx = idx 528 layers[idx].idx = idx
529 pass 529 pass
530 pass 530 pass
531 pass 531 pass
532 532
533 ## \brief Maintain frameline list for MBScene.
534 #
535 class MBScene_framelines(object):
536 _frameline_tween_types = (frameline.TWEEN_TYPE_NONE,
537 frameline.TWEEN_TYPE_SHAPE)
538
539 _framelines = None
540
541 def __init__(self, *args, **kws):
542 super(MBScene_framelines, self).__init__(*args, **kws)
543 pass
544
545 def _remove_active_frame(self,widget,event):
546 """
547 Hide all hover frames. This is a hack. We should use the lost focus
548 event instead in the future to reduce the overhead.
549 """
550 for f in self._framelines:
551 if f != widget:
552 f.hide_hover()
553 pass
554 pass
555 pass
556
557 ## \brief Add a frameline into the frameline box for the given layer.
558 #
559 def _add_frameline(self, layer_idx):
560 if layer_idx > len(self._framelines):
561 raise ValueError, 'layer number should be a consequence'
562
563 vbox = self._frameline_vbox
564
565 line = frameline(self._num_frames_of_line)
566 line.set_size_request(self._num_frames_of_line * 10, 20)
567
568 hbox = gtk.HBox()
569 label = gtk.Label('')
570 label.set_size_request(100,0)
571 hbox.pack_start(label,expand=False, fill=True)
572 hbox.pack_start(line)
573 vbox.pack_start(hbox, False)
574
575 if layer_idx != len(self._framelines):
576 vbox.reorder_child(hbox, layer_idx + 1) # there is ruler at pos 0
577 pass
578
579 self._framelines[layer_idx: layer_idx] = [line]
580
581 line.label = label
582 line.layer_idx = layer_idx
583 line.connect(line.FRAME_BUT_PRESS, self.onCellClick)
584 line.connect('motion-notify-event', self._remove_active_frame)
585 pass
586
587 ## \brief Remove the given frameline from the frameline box.
588 #
589 def _remove_frameline(self, layer_idx):
590 vbox = self._frameline_vbox
591 line = self._framelines[layer_idx]
592
593 hbox = line.parent
594 vbox.remove(hbox)
595 del self._framelines[layer_idx]
596 pass
597
598 def _init_framelines(self):
599 self._framelines = []
600
601 box = gtk.ScrolledWindow()
602 self._frameline_box = box
603 box.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
604 box.set_size_request(-1, 150)
605 vbox = gtk.VBox()
606 self._frameline_vbox = vbox
607 box.add_with_viewport(vbox)
608
609 nframes = self._num_frames_of_line
610
611 #
612 # Set up a ruler
613 #
614 ruler = frameruler(nframes)
615 ruler.set_size_request(nframes * 10, 20)
616 ruler.show()
617 hbox = gtk.HBox()
618 label=gtk.Label('')
619 label.set_size_request(100,0)
620 hbox.pack_start(label,expand=False,fill=True)
621 hbox.pack_start(ruler)
622 vbox.pack_start(hbox, False)
623 pass
624
625 ## \brief Show framelines on the screen.
626 #
627 # When a frameline was inserted or removed, it would not be showed
628 # immediately. This function is used to notify toolkit to update the
629 # screen and drawing framelines.
630 def _show_framelines(self):
631 self._frameline_vbox.show_all()
632 pass
633 pass
634
533 ## \brief MBScene connect GUI and DOM-tree 635 ## \brief MBScene connect GUI and DOM-tree
534 # 636 #
535 # This class connect behavior of GUI to the DOM-tree. All about GUI is 637 # This class connect behavior of GUI to the DOM-tree. All about GUI is
536 # implemented by this class. It use API provided by MBScene_dom to reflect 638 # implemented by this class. It use API provided by MBScene_dom to reflect
537 # actions to the DOM-tree. 639 # actions to the DOM-tree.
538 # 640 #
539 class MBScene(MBScene_dom): 641 class MBScene(MBScene_dom, MBScene_framelines):
540 _frameline_tween_types = (frameline.TWEEN_TYPE_NONE,
541 frameline.TWEEN_TYPE_SHAPE)
542 _tween_obj_tween_types = (TweenObject.TWEEN_TYPE_NORMAL, 642 _tween_obj_tween_types = (TweenObject.TWEEN_TYPE_NORMAL,
543 TweenObject.TWEEN_TYPE_SCALE) 643 TweenObject.TWEEN_TYPE_SCALE)
544 _tween_type_names = ('normal', 'scale') 644 _tween_type_names = ('normal', 'scale')
545 645
546 _num_frames_of_line = 100 646 _num_frames_of_line = 100
776 self.enterGroup(scene_group) 876 self.enterGroup(scene_group)
777 self.setTweenType(tween_type) 877 self.setTweenType(tween_type)
778 pass 878 pass
779 879
780 def setTweenType(self, tween_type): 880 def setTweenType(self, tween_type):
781 sel_type = MBScene._frameline_tween_types.index(tween_type) 881 sel_type = self._frameline_tween_types.index(tween_type)
782 self._disable_tween_type_selector = True 882 self._disable_tween_type_selector = True
783 self.tweenTypeSelector.set_active(sel_type) 883 self.tweenTypeSelector.set_active(sel_type)
784 self._disable_tween_type_selector = False 884 self._disable_tween_type_selector = False
785 pass 885 pass
786 886
787 def _remove_active_frame(self,widget,event):
788 """
789 Hide all hover frames. This is a hack. We should use the lost focus
790 event instead in the future to reduce the overhead.
791 """
792 for f in self._framelines:
793 if f != widget:
794 f.hide_hover()
795 pass
796 pass
797 pass
798
799 ## \brief Add a frameline into the frameline box for the given layer.
800 #
801 def _add_frameline(self, layer_idx):
802 if layer_idx > len(self._framelines):
803 raise ValueError, 'layer number should be a consequence'
804
805 vbox = self._frameline_vbox
806
807 line = frameline(self._num_frames_of_line)
808 line.set_size_request(self._num_frames_of_line * 10, 20)
809
810 hbox = gtk.HBox()
811 label = gtk.Label('')
812 label.set_size_request(100,0)
813 hbox.pack_start(label,expand=False, fill=True)
814 hbox.pack_start(line)
815 vbox.pack_start(hbox, False)
816
817 if layer_idx != len(self._framelines):
818 vbox.reorder_child(hbox, layer_idx + 1) # there is ruler at pos 0
819 pass
820
821 self._framelines[layer_idx: layer_idx] = [line]
822
823 line.label = label
824 line.layer_idx = layer_idx
825 line.connect(line.FRAME_BUT_PRESS, self.onCellClick)
826 line.connect('motion-notify-event', self._remove_active_frame)
827 pass
828
829 ## \brief Remove the given frameline from the frameline box.
830 #
831 def _remove_frameline(self, layer_idx):
832 vbox = self._frameline_vbox
833 line = self._framelines[layer_idx]
834
835 hbox = line.parent
836 vbox.remove(hbox)
837 del self._framelines[layer_idx]
838 pass
839
840 ## \brief Remove the layer that lost the layer group. 887 ## \brief Remove the layer that lost the layer group.
841 # 888 #
842 # This function is called when a layer group being removed from the 889 # This function is called when a layer group being removed from the
843 # DOM-tree. 890 # DOM-tree.
844 def _remove_lost_group_layer(self, layer_idx): 891 def _remove_lost_group_layer(self, layer_idx):
848 scene_node = frameline.get_frame_data(start) 895 scene_node = frameline.get_frame_data(start)
849 self.rm_scene_node(scene_node) 896 self.rm_scene_node(scene_node)
850 pass 897 pass
851 898
852 self._remove_frameline(layer_idx) # TODO 899 self._remove_frameline(layer_idx) # TODO
900 self._show_framelines()
853 del self._layers[layer_idx] 901 del self._layers[layer_idx]
854 pass 902 pass
855 903
856 ## \brief Make status of layers is updated when DOM is changed. 904 ## \brief Make status of layers is updated when DOM is changed.
857 # 905 #
869 if layer.group.name() != 'svg:g': 917 if layer.group.name() != 'svg:g':
870 self._remove_lost_group_layer(layer_idx) # TODO 918 self._remove_lost_group_layer(layer_idx) # TODO
871 continue 919 continue
872 920
873 parent = layer.group.parent() 921 parent = layer.group.parent()
922
923 if not parent: # parent is None when a node being removed from
924 # DOM-tree.
925 self._remove_lost_group_layer(layer_idx)
926 continue
927
874 if parent.name() != 'svg:svg': 928 if parent.name() != 'svg:svg':
875 self._remove_lost_group_layer(layer_idx) 929 self._remove_lost_group_layer(layer_idx)
876 continue 930 continue
877 931
878 if parent.getAttribute('id') != root_id: 932 if parent.getAttribute('id') != root_id:
886 layer_idx = 0 940 layer_idx = 0
887 for child in root.childList(): 941 for child in root.childList():
888 if child.name() != 'svg:g': 942 if child.name() != 'svg:g':
889 continue 943 continue
890 944
891 layer = self._layers[layer_idx] 945 try:
892 layer_group_id = layer.group.getAttribute('id') 946 layer = self._layers[layer_idx]
947 except IndexError:
948 layer = None
949 else:
950 layer_group_id = layer.group.getAttribute('id')
951 pass
952
893 child_id = child.getAttribute('id') 953 child_id = child.getAttribute('id')
894 if layer_group_id != child_id: 954 if (not layer) or layer_group_id != child_id:
895 self.add_layer(layer_idx, child) 955 self.add_layer(layer_idx, child)
896 self.parse_layer(layer_idx) 956 self.parse_layer(layer_idx)
897 self._add_frameline(layer_idx) 957 self._add_frameline(layer_idx)
958 self._show_framelines()
898 pass 959 pass
899 960
900 layer_idx = layer_idx + 1 961 layer_idx = layer_idx + 1
901 pass 962 pass
902 pass
903
904 def _init_framelines(self):
905 self._framelines = []
906
907 box = gtk.ScrolledWindow()
908 self._frameline_box = box
909 box.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
910 box.set_size_request(-1, 150)
911 vbox = gtk.VBox()
912 self._frameline_vbox = vbox
913 box.add_with_viewport(vbox)
914
915 nframes = self._num_frames_of_line
916
917 #
918 # Set up a ruler
919 #
920 ruler = frameruler(nframes)
921 ruler.set_size_request(nframes * 10, 20)
922 ruler.show()
923 hbox = gtk.HBox()
924 label=gtk.Label('')
925 label.set_size_request(100,0)
926 hbox.pack_start(label,expand=False,fill=True)
927 hbox.pack_start(ruler)
928 vbox.pack_start(hbox, False)
929
930 vbox.show_all()
931 pass 963 pass
932 964
933 def _add_frameline_for_layers(self): 965 def _add_frameline_for_layers(self):
934 for layer_idx in range(len(self._layers)): 966 for layer_idx in range(len(self._layers)):
935 self._add_frameline(layer_idx) 967 self._add_frameline(layer_idx)
995 self.editDone = gtk.Button('Set') 1027 self.editDone = gtk.Button('Set')
996 hbox.pack_start(self.editDone,expand=False,fill=False) 1028 hbox.pack_start(self.editDone,expand=False,fill=False)
997 self.editDone.connect('clicked', self.changeObjectLabel) 1029 self.editDone.connect('clicked', self.changeObjectLabel)
998 pass 1030 pass
999 1031
1000 def updateUI(self, node=None, child=None, arg=None): 1032 def updateUI(self, *args):
1001 if self._lockui: return 1033 if self._lockui: return
1002 1034
1003 if self.last_update!= None: 1035 if self.last_update!= None:
1004 glib.source_remove(self.last_update) 1036 glib.source_remove(self.last_update)
1005 self.last_update = glib.timeout_add(300,self._updateUI) 1037 self.last_update = glib.timeout_add(300,self._updateUI)
1116 return 1148 return
1117 frameline = self.last_line 1149 frameline = self.last_line
1118 start, end, old_tween_type = frameline.get_frame_block(self.last_frame) 1150 start, end, old_tween_type = frameline.get_frame_block(self.last_frame)
1119 1151
1120 type_idx = self.tweenTypeSelector.get_active() 1152 type_idx = self.tweenTypeSelector.get_active()
1121 tween_type = MBScene._frameline_tween_types[type_idx] 1153 tween_type = self._frameline_tween_types[type_idx]
1122 type_name = self._tween_type_names[type_idx] 1154 type_name = self._tween_type_names[type_idx]
1123 1155
1124 frameline.tween(start, tween_type) 1156 frameline.tween(start, tween_type)
1125 1157
1126 scene_node = frameline.get_frame_data(start) 1158 scene_node = frameline.get_frame_data(start)
1161 self.document = self.desktop.doc().rdoc 1193 self.document = self.desktop.doc().rdoc
1162 self.handle_doc_root(self.document, self.root) 1194 self.handle_doc_root(self.document, self.root)
1163 self.tween = TweenObject(self.document, self.root) 1195 self.tween = TweenObject(self.document, self.root)
1164 self._init_framelines() 1196 self._init_framelines()
1165 self._add_frameline_for_layers() 1197 self._add_frameline_for_layers()
1198 self._show_framelines()
1166 1199
1167 if self.top == None: 1200 if self.top == None:
1168 self.top = gtk.VBox(False,0) 1201 self.top = gtk.VBox(False,0)
1169 toplevel = self.desktop.getToplevel() 1202 toplevel = self.desktop.getToplevel()
1170 toplevel.child.child.pack_end(self.top,expand=False) 1203 toplevel.child.child.pack_end(self.top,expand=False)
1178 vbox.pack_start(self._frameline_box,expand=False) 1211 vbox.pack_start(self._frameline_box,expand=False)
1179 hbox=gtk.HBox(False,0) 1212 hbox=gtk.HBox(False,0)
1180 self.addButtons(hbox) 1213 self.addButtons(hbox)
1181 vbox.pack_start(hbox,expand=False) 1214 vbox.pack_start(hbox,expand=False)
1182 1215
1216 doc = self.document
1217 addEventListener(doc,'DOMNodeInserted', self.updateUI, None)
1218 addEventListener(doc,'DOMNodeRemoved', self.updateUI, None)
1219 addEventListener(doc, 'DOMAttrModified', self.updateUI, None)
1220
1183 self.top.show_all() 1221 self.top.show_all()
1184 self.last_update = None 1222 self.last_update = None
1185 return False 1223 return False
1186 pass 1224 pass