comparison pyink/domview.py @ 1315:fcff6978f9bf

User can switch components. - component_manager will change domview._scenes_node and domview._layers_parent for switching component and timeline. - domview relys on domview._scenes_node and domview._layers_parent to find scene nodes and layers.
author Thinker K.F. Li <thinker@codemud.net>
date Sat, 22 Jan 2011 14:23:42 +0800
parents 85d04ba11146
children 9ab8d50dcf7e
comparison
equal deleted inserted replaced
1314:2aa3770f02bf 1315:fcff6978f9bf
130 raise ValueError, 'try to remove a non-existed timeline - %s' % (name) 130 raise ValueError, 'try to remove a non-existed timeline - %s' % (name)
131 pass 131 pass
132 132
133 133
134 ## \brief A mix-in for class domview for management of components. 134 ## \brief A mix-in for class domview for management of components.
135 #
136 # This class is responsible for manage components and timelines. It
137 # is also responsible for switching component and timeline. Switching
138 # to a component is actually switching to a timeline in another
139 # component. When switch to a timeline, it actuall change
140 # domview._scense_node, parent of all scene nodes of a component, and
141 # domview._layers_parent, parent of all layer group of a component.
142 # domview relys on these two variables to operate on right component
143 # and timeline. (It should be changed to get more hint with
144 # meaningful names.)
145 #
146 # FIXME: The root node is always the 'main' component. It is a
147 # special case with slightly different in structure. It should be
148 # removed and normalized to normal components.
135 # 149 #
136 class component_manager(object): 150 class component_manager(object):
137 def __init__(self): 151 def __init__(self):
138 self._components_node = None 152 self._components_node = None
139 self._components = [] 153 self._components = []
140 self._comp_names = set() 154 self._comp_names = set()
141 self._main_comp = None 155 self._main_comp = None
142 self._cur_comp = None 156 self._cur_comp = None
143 self._cur_timeline = None 157 self._cur_timeline = None
158 self._components_group = None
144 pass 159 pass
145 160
146 def _set_main_component(self): 161 def _set_main_component(self):
147 comp = Component(self, None) 162 comp = Component(self, None)
148 comp.layers = self._layers 163 comp.layers = self._layers
150 timeline = Timeline(scenes_node) 165 timeline = Timeline(scenes_node)
151 comp.timelines = [timeline] 166 comp.timelines = [timeline]
152 167
153 self._components.append(comp) 168 self._components.append(comp)
154 self._comp_names.add('main') 169 self._comp_names.add('main')
155 170
156 self._main_comp = comp 171 self._main_comp = comp
157 self._cur_comp = comp 172 self._cur_comp = comp
158 pass 173 pass
159 174
160 def _parse_components(self): 175 def _parse_components(self):
188 else: 203 else:
189 components_node = self._doc.createElement("ns0:components") 204 components_node = self._doc.createElement("ns0:components")
190 node.appendChild(components_node) 205 node.appendChild(components_node)
191 self._components_node = components_node 206 self._components_node = components_node
192 pass 207 pass
208
209 for n in self._root.childList():
210 if n.name() != 'svg:g':
211 continue
212 try:
213 nlabel = n.getAttribute('inkscape:label')
214 except:
215 continue
216 if nlabel == 'components':
217 self._components_group
218 break
219 pass
220 else: # no components group
221 components_group = self._doc.createElement('svg:g')
222 components_group.setAttribute('inkscape:label', 'components')
223 gid = self.new_id()
224 components_group.setAttribute('id', gid)
225
226 self._root.appendChild(components_group)
227 self._components_group = components_group
228 pass
193 pass 229 pass
194 230
195 def _start_component_manager(self): 231 def _start_component_manager(self):
196 self._component_manager_init_metadata() 232 self._component_manager_init_metadata()
197 self._set_main_component() 233 self._set_main_component()
198 self._parse_components() 234 self._parse_components()
199 pass 235 pass
236
237 def _create_component_group(self):
238 doc = self._doc
239 group = doc.createElement('svg:g')
240 gid = self.new_id()
241 group.setAttribute('id', gid)
242 group.setAttribute('component_group', 'true')
243
244 self._components_group.appendChild(group)
245 return group
246
247 ## \brief Create a ns0:component node for a given name.
248 #
249 # \param comp_name is the name of the created component.
250 # \param comp_group_id is the component group.
251 # \return a ns0:component node.
252 #
253 def _create_component_node(self, comp_name, comp_group_id):
254 comp_node = self._doc.createElement('ns0:component')
255 comp_id = self.new_id()
256 comp_node.setAttribute('id', comp_id)
257 comp_node.setAttribute('name', comp_name)
258 comp_node.setAttribute('ref', comp_group_id)
259 self._components_node.appendChild(comp_node)
260 return comp_node
261
262 ## \brief Get Component object associated with the given name.
263 #
264 def _get_component(self, comp_name):
265 if comp_name in self._comp_names:
266 for comp in self._components:
267 if comp.name() == comp_name:
268 return comp
269 pass
270 pass
271 raise ValueError, 'can not find component node - %s' % (comp_name)
272
273 ## \brief Create a layer group for give layer of a component.
274 #
275 def _create_comp_layer_group(self, comp_group, layer_name):
276 doc = self._doc
277 gid = self.new_id()
278
279 layer_group = doc.createElement('svg:g')
280 layer_group.setAttribute('id', gid)
281 layer_group.setAttribute('inkscape:label', layer_name)
282 comp_group.appendChild(layer_group)
283 pass
200 284
201 def all_comp_names(self): 285 def all_comp_names(self):
202 return list(self._comp_names) 286 return list(self._comp_names)
203 287
204 def has_component(self, name): 288 def has_component(self, name):
205 return name in self._comp_names 289 return name in self._comp_names
206 290
207 def switch_component(self, comp_name): 291 def switch_component(self, comp_name):
208 for comp in self._components: 292 comp = self._get_component(comp_name)
209 if comp.name() == comp_name: 293 self._cur_comp = comp
210 self._cu_comp = comp 294 self._layers = comp.layers
211 self._layers = comp.layers 295 comp_name = self._cur_comp.name()
212 296 # for domview
213 first_name = comp.all_timeline_names()[0] 297 self._layers_parent = self.get_component_group(comp_name)
214 self.switch_timeline(self, first_name) 298
215 return 299 first_name = comp.all_timeline_names()[0]
216 pass 300 self.switch_timeline(first_name)
217 raise ValueError, 'not such component %s' % (comp_name) 301 pass
218 302
219 def add_component(self, comp_name): 303 def add_component(self, comp_name):
220 if self.has_component(comp_name): 304 if self.has_component(comp_name):
221 raise ValueError, \ 305 raise ValueError, \
222 'try add a component with existed name %s' % (comp_name) 306 'try add a component with existed name %s' % (comp_name)
223 307
224 comp_node = self._doc.createElement('ns0:component') 308 comp_group = self._create_component_group()
225 comp_id = self.new_id() 309 comp_group_id = comp_group.getAttribute('id')
226 comp_node.setAttribute('id', comp_id) 310 comp_node = self._create_component_node(comp_name, comp_group_id)
227 comp_node.setAttribute('name', comp_name) 311
228 self._components_node.appendChild(comp_node) 312 self._create_comp_layer_group(comp_group, comp_name + ' Layer1')
229 313
230 comp = Component(self, comp_node) 314 comp = Component(self, comp_node)
231 comp.parse_timelines() 315 comp.parse_timelines()
232 316
233 self._components.append(comp) 317 self._components.append(comp)
244 self._components.append(comp) 328 self._components.append(comp)
245 self._comp_names.add(comp_name) 329 self._comp_names.add(comp_name)
246 pass 330 pass
247 331
248 def rm_component(self, comp_name): 332 def rm_component(self, comp_name):
249 if not self.has_component(comp_name): 333 comp = self._get_component(comp_name)
250 raise ValueError, 'try to remove a non-existed component' 334 comp_name = comp.name()
251 335 comp_node = comp.node
336 comp_group = self.get_component_group(comp_name)
337
338 self._components.remove(comp)
252 self._comp_names.remove(comp_name) 339 self._comp_names.remove(comp_name)
253 for i, comp in enumerate(self._components): 340 self._components_node.removeChild(comp_node)
254 if comp.name() == comp_name: 341 self._components_group.removeChild(comp_group)
255 del self._components[i] 342 pass
256 self._comp_names.remove(comp_name) 343
257 break 344 def get_component_group(self, comp_name):
258 pass 345 comp = self._get_component(comp_name)
259 pass 346
260 347 comp_name = comp.name()
348 if comp_name == 'main':
349 return self._root
350
351 comp_node = comp.node
352 gid = comp_node.getAttribute('ref')
353 comp_group = self.get_node(gid)
354 return comp_group
355
261 def switch_timeline(self, timeline_name): 356 def switch_timeline(self, timeline_name):
262 tl = self._cur_comp.get_timeline(timeline_name) 357 tl = self._cur_comp.get_timeline(timeline_name)
263 self._cur_timeline = tl 358 self._cur_timeline = tl
264 self._scenes_node = tl.scenes_node # of class domview 359 self._scenes_node = tl.scenes_node # of class domview
265 360
361 # Make domview to rescan layers and scenes.
266 self.reset() # from domview 362 self.reset() # from domview
267 pass 363 pass
268 364
269 def add_timeline(self, timeline_name): 365 def add_timeline(self, timeline_name):
270 self._cur_comp.add_timeline(timeline_name) 366 self._cur_comp.add_timeline(timeline_name)
596 _root = None 692 _root = None
597 693
598 def __init__(self, *args, **kws): 694 def __init__(self, *args, **kws):
599 super(domview, self).__init__() 695 super(domview, self).__init__()
600 self._metadata_node = None 696 self._metadata_node = None
697 #
698 # Following two variables would be changed by class
699 # component_manager to switch components and timelines.
700 #
601 self._scenes_node = None 701 self._scenes_node = None
702 self._layers_parent = None
602 pass 703 pass
603 704
604 ## \brief Create a scenes node if not existed. 705 ## \brief Create a scenes node if not existed.
605 # 706 #
606 def _init_metadata(self): 707 def _init_metadata(self):
708 self._layers_parent = self._root
709
607 for node in self._root.childList(): 710 for node in self._root.childList():
608 if node.name() == 'svg:metadata': 711 if node.name() == 'svg:metadata':
609 break 712 break
610 pass 713 pass
611 else: 714 else:
627 self._scenes_node = scenes_node 730 self._scenes_node = scenes_node
628 pass 731 pass
629 pass 732 pass
630 733
631 def _parse_all_layers(self): 734 def _parse_all_layers(self):
632 root = self._root 735 root = self._scenes_node
633 layers = self._layers 736 layers = self._layers
634 737 layers_parent = self._layers_parent
635 for child in root.childList(): 738
739 for child in layers_parent.childList():
636 if child.name() != 'svg:g': 740 if child.name() != 'svg:g':
637 continue 741 continue
742
743 try:
744 label = child.getAttribute('inkscape:label')
745 except:
746 pass
747 else: # has no label
748 if label == 'components':
749 continue
750 pass
638 751
639 layer_group = child 752 layer_group = child
640 layer = Layer(layer_group) 753 layer = Layer(layer_group)
641 layer.idx = len(layers) 754 layer.idx = len(layers)
642 layers.append(layer) 755 layers.append(layer)
647 def handle_doc_root(self, doc, root): 760 def handle_doc_root(self, doc, root):
648 self._doc = doc 761 self._doc = doc
649 self._root = root 762 self._root = root
650 self._layers = [] 763 self._layers = []
651 764
765 self._init_metadata()
652 self._start_monitor() # from domview_monitor 766 self._start_monitor() # from domview_monitor
653 self._init_metadata()
654 self._start_component_manager() # from component_manager 767 self._start_component_manager() # from component_manager
655 self._parse_all_layers() 768 self._parse_all_layers()
656 pass 769 pass
657 770
658 def reset(self): 771 def reset(self):