Mercurial > MadButterfly
comparison pyink/domview.py @ 1302:c53331c55a23
Refactory domview to more clear responsibilities
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Wed, 19 Jan 2011 10:01:09 +0800 |
parents | 18af917cf855 |
children | 8c0a4684c1f7 |
comparison
equal
deleted
inserted
replaced
1301:18af917cf855 | 1302:c53331c55a23 |
---|---|
130 | 130 |
131 ## \brief A mix-in for class domview for management of components. | 131 ## \brief A mix-in for class domview for management of components. |
132 # | 132 # |
133 class component_manager(object): | 133 class component_manager(object): |
134 def __init__(self): | 134 def __init__(self): |
135 self._components_node = None | |
135 self._components = [] | 136 self._components = [] |
136 self._comp_names = set() | 137 self._comp_names = set() |
137 self._main_comp = None | 138 self._main_comp = None |
138 self._cur_comp = None | 139 self._cur_comp = None |
139 self._cur_timeline = None | 140 self._cur_timeline = None |
167 comp.parse_timelines() | 168 comp.parse_timelines() |
168 | 169 |
169 self._components.append(comp) | 170 self._components.append(comp) |
170 comp_names.add(child_name) | 171 comp_names.add(child_name) |
171 pass | 172 pass |
173 pass | |
174 | |
175 ## \brief To initialize subtree of metadata. | |
176 # | |
177 # This method is called by domview._init_metadata(). | |
178 # | |
179 def _component_manager_init_metadata(self): | |
180 for n in self._metadata_node.childList(): | |
181 if n.name() == 'ns0:components': | |
182 self._components_node = n | |
183 break | |
184 pass | |
185 else: | |
186 components_node = self._doc.createElement("ns0:components") | |
187 node.appendChild(components_node) | |
188 self._components_node = components_node | |
189 pass | |
190 pass | |
191 | |
192 def _start_component_manager(self): | |
193 self._component_manager_init_metadata() | |
194 self._set_main_component() | |
195 self._parse_components() | |
172 pass | 196 pass |
173 | 197 |
174 def all_comp_names(self): | 198 def all_comp_names(self): |
175 return list(self._comp_names) | 199 return list(self._comp_names) |
176 | 200 |
234 def switch_timeline(self, timeline_name): | 258 def switch_timeline(self, timeline_name): |
235 tl = self._cur_comp.get_timeline(timeline_name) | 259 tl = self._cur_comp.get_timeline(timeline_name) |
236 self._cur_timeline = tl | 260 self._cur_timeline = tl |
237 self._scenes_node = tl.scenes_node # of class domview | 261 self._scenes_node = tl.scenes_node # of class domview |
238 | 262 |
239 self._monitor_rescan() # from domview_monitor | 263 self.reset() # from domview |
240 pass | 264 pass |
241 | 265 |
242 def add_timeline(self, timeline_name): | 266 def add_timeline(self, timeline_name): |
243 self._cur_comp.add_timeline(timeline_name) | 267 self._cur_comp.add_timeline(timeline_name) |
244 pass | 268 pass |
275 self._on_attr_modified, None) | 299 self._on_attr_modified, None) |
276 pass | 300 pass |
277 | 301 |
278 ## \brief Rescan the tree. | 302 ## \brief Rescan the tree. |
279 # | 303 # |
280 def _monitor_rescan(self): | 304 def _monitor_reparse(self): |
281 self._maxframe = 0 | 305 self._maxframe = 0 |
282 self._id2node = {} | 306 self._id2node = {} |
283 self._group2scene = {} | 307 self._group2scene = {} |
284 | 308 |
285 self._collect_node_ids() | 309 self._collect_node_ids() |
553 # | 577 # |
554 # This class maintains layers information, and provides functions to create, | 578 # This class maintains layers information, and provides functions to create, |
555 # change and destroy scene node and scene group. A scene node is a 'ns0:scene' | 579 # change and destroy scene node and scene group. A scene node is a 'ns0:scene' |
556 # in 'ns0:scenes' tag. A scene group is respective 'svg:g' for a scene. | 580 # in 'ns0:scenes' tag. A scene group is respective 'svg:g' for a scene. |
557 # | 581 # |
558 class domview(domview_monitor): | 582 class domview(domview_monitor, component_manager): |
559 # Declare variables, here, for keeping tracking | 583 # Declare variables, here, for keeping tracking |
560 _doc = None | 584 _doc = None |
561 _root = None | 585 _root = None |
562 | 586 |
563 def __init__(self, *args, **kws): | 587 def __init__(self, *args, **kws): |
564 super(domview, self).__init__() | 588 super(domview, self).__init__() |
589 self._metadata_node = None | |
590 self._scenes_node = None | |
565 pass | 591 pass |
566 | 592 |
567 ## \brief Create a scenes node if not existed. | 593 ## \brief Create a scenes node if not existed. |
568 # | 594 # |
569 def _init_metadata(self): | 595 def _init_metadata(self): |
572 break | 598 break |
573 pass | 599 pass |
574 else: | 600 else: |
575 raise RuntimeError, \ | 601 raise RuntimeError, \ |
576 'can not find <svg:metadata> node in the document' | 602 'can not find <svg:metadata> node in the document' |
603 | |
604 self._metadata_node = node | |
577 | 605 |
578 for n in node.childList(): | 606 for n in node.childList(): |
579 if n.name() == 'ns0:scenes': | 607 if n.name() == 'ns0:scenes': |
580 self._scenes_node = n | 608 self._scenes_node = n |
581 break | 609 break |
585 self._root.setAttribute("xmlns:ns0", ns) | 613 self._root.setAttribute("xmlns:ns0", ns) |
586 scenes_node = self._doc.createElement("ns0:scenes") | 614 scenes_node = self._doc.createElement("ns0:scenes") |
587 node.appendChild(scenes_node) | 615 node.appendChild(scenes_node) |
588 self._scenes_node = scenes_node | 616 self._scenes_node = scenes_node |
589 pass | 617 pass |
590 | |
591 for n in node.childList(): | |
592 if n.name() == 'ns0:components': | |
593 self._components_node = n | |
594 break | |
595 pass | |
596 else: | |
597 components_node = self._doc.createElement("ns0:components") | |
598 node.appendChild(components_node) | |
599 self._components_node = components_node | |
600 pass | |
601 pass | 618 pass |
602 | 619 |
603 def _parse_all_layers(self): | 620 def _parse_all_layers(self): |
604 root = self._root | 621 root = self._root |
605 layers = self._layers | 622 layers = self._layers |
619 def handle_doc_root(self, doc, root): | 636 def handle_doc_root(self, doc, root): |
620 self._doc = doc | 637 self._doc = doc |
621 self._root = root | 638 self._root = root |
622 self._layers = [] | 639 self._layers = [] |
623 | 640 |
624 self._start_monitor() # start domview_monitor | 641 self._start_monitor() # from domview_monitor |
625 self._init_metadata() | 642 self._init_metadata() |
643 self._start_component_manager() # from component_manager | |
626 self._parse_all_layers() | 644 self._parse_all_layers() |
627 pass | 645 pass |
628 | 646 |
629 def reset(self): | 647 def reset(self): |
648 self._monitor_reparse() # from domview_monitor | |
630 self._layers = [] | 649 self._layers = [] |
631 self._parse_all_layers() | 650 self._parse_all_layers() |
632 pass | 651 pass |
633 | 652 |
634 def dumpattr(self, n): | 653 def dumpattr(self, n): |