Mercurial > MadButterfly
comparison pyink/domview.py @ 1323:ffbbd3aa218d
Make a component from a group.
User can translate a group into a component from context menu.
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Sun, 30 Jan 2011 19:15:17 +0800 |
parents | 7f25abbc1b61 |
children | 0a2034f490e5 |
comparison
equal
deleted
inserted
replaced
1322:7f25abbc1b61 | 1323:ffbbd3aa218d |
---|---|
270 def _create_component_group(self): | 270 def _create_component_group(self): |
271 doc = self._doc | 271 doc = self._doc |
272 group = doc.createElement('svg:g') | 272 group = doc.createElement('svg:g') |
273 gid = self.new_id() | 273 gid = self.new_id() |
274 group.setAttribute('id', gid) | 274 group.setAttribute('id', gid) |
275 group.setAttribute('component_group', 'true') | |
276 | 275 |
277 self._components_group.appendChild(group) | 276 self._components_group.appendChild(group) |
278 return group | 277 return group |
279 | 278 |
280 ## \brief Create a ns0:component node for a given name. | 279 ## \brief Create a ns0:component node for a given name. |
310 gid = self.new_id() | 309 gid = self.new_id() |
311 | 310 |
312 layer_group = doc.createElement('svg:g') | 311 layer_group = doc.createElement('svg:g') |
313 layer_group.setAttribute('id', gid) | 312 layer_group.setAttribute('id', gid) |
314 layer_group.setAttribute('inkscape:label', layer_name) | 313 layer_group.setAttribute('inkscape:label', layer_name) |
314 layer_group.setAttribute('inkscape:groupmode', 'layer') | |
315 comp_group.appendChild(layer_group) | 315 comp_group.appendChild(layer_group) |
316 pass | 316 return layer_group |
317 | |
318 ## \brief Return group of specified layer in a component. | |
319 # | |
320 # This is here for getting layer group without switch current | |
321 # component. | |
322 # | |
323 def _get_group_of_component_layer(self, comp_name, layer_idx): | |
324 comp = self._get_component(comp_name) | |
325 layer = comp.layers[layer_idx] | |
326 return layer.group | |
317 | 327 |
318 def all_comp_names(self): | 328 def all_comp_names(self): |
319 return [comp.name() for comp in self._components] | 329 return [comp.name() for comp in self._components] |
320 | 330 |
321 def has_component(self, name): | 331 def has_component(self, name): |
322 return name in self._comp_names | 332 return name in self._comp_names |
323 | 333 |
324 def switch_component(self, comp_name): | 334 def switch_component(self, comp_name): |
335 old_comp = self._cur_comp | |
336 | |
325 comp = self._get_component(comp_name) | 337 comp = self._get_component(comp_name) |
326 self._cur_comp = comp | 338 self._cur_comp = comp |
327 self._layers = comp.layers | 339 self._layers = comp.layers |
328 comp_name = self._cur_comp.name() | 340 comp_name = self._cur_comp.name() |
329 # for domview | 341 # for domview |
330 self._layers_parent = self.get_component_group(comp_name) | 342 self._layers_parent = self.get_component_group(comp_name) |
331 | 343 |
332 first_name = comp.all_timeline_names()[0] | 344 first_name = comp.all_timeline_names()[0] |
333 self.switch_timeline(first_name) | 345 self.switch_timeline(first_name) |
346 | |
347 # Hide layers in old component | |
348 for layer in old_comp.layers: | |
349 group = layer.group | |
350 group.setAttribute('style', 'display: none') | |
351 pass | |
352 | |
353 # Show layers in new component | |
354 for layer in comp.layers: | |
355 group = layer.group | |
356 group.setAttribute('style', '') | |
357 pass | |
334 pass | 358 pass |
335 | 359 |
336 def add_component(self, comp_name): | 360 def add_component(self, comp_name): |
337 if self.has_component(comp_name): | 361 if self.has_component(comp_name): |
338 raise ValueError, \ | 362 raise ValueError, \ |
340 | 364 |
341 comp_group = self._create_component_group() | 365 comp_group = self._create_component_group() |
342 comp_group_id = comp_group.getAttribute('id') | 366 comp_group_id = comp_group.getAttribute('id') |
343 comp_node = self._create_component_node(comp_name, comp_group_id) | 367 comp_node = self._create_component_node(comp_name, comp_group_id) |
344 | 368 |
345 self._create_comp_layer_group(comp_group, comp_name + ' Layer1') | 369 layer_group = self._create_comp_layer_group(comp_group, 'Layer1') |
370 layer_group.setAttribute('style', 'display: none') | |
346 | 371 |
347 comp = Component(self, comp_node) | 372 comp = Component(self, comp_node) |
348 comp.parse_timelines() | 373 comp.parse_timelines() |
374 | |
375 layer = Layer(layer_group) | |
376 comp.layers.append(layer) | |
349 | 377 |
350 self._components.append(comp) | 378 self._components.append(comp) |
351 self._comp_names.add(comp_name) | 379 self._comp_names.add(comp_name) |
352 pass | 380 pass |
353 | 381 |
414 r = self._cur_comp.has_timeline(name) | 442 r = self._cur_comp.has_timeline(name) |
415 return r | 443 return r |
416 | 444 |
417 def get_current_timeline(self): | 445 def get_current_timeline(self): |
418 return self._cur_timeline.name() | 446 return self._cur_timeline.name() |
447 | |
448 ## \brief Add a new component from a group node. | |
449 # | |
450 # The group node is reparented to the group of first layer of | |
451 # specified component. | |
452 # | |
453 def mv_group_to_component(self, group, comp_name): | |
454 group_parent = group.parent() | |
455 if group_parent: | |
456 group_parent.removeChild(group) | |
457 pass | |
458 | |
459 layer_group = self._get_group_of_component_layer(comp_name, 0) | |
460 layer_group.appendChild(group) | |
461 pass | |
419 pass | 462 pass |
420 | 463 |
421 | 464 |
422 ## \brief Monitor changes of DOM-tree. | 465 ## \brief Monitor changes of DOM-tree. |
423 # | 466 # |
736 # Following two variables would be changed by class | 779 # Following two variables would be changed by class |
737 # component_manager to switch components and timelines. | 780 # component_manager to switch components and timelines. |
738 # | 781 # |
739 self._scenes_node = None | 782 self._scenes_node = None |
740 self._layers_parent = None | 783 self._layers_parent = None |
784 self._layers = [] | |
741 pass | 785 pass |
742 | 786 |
743 ## \brief Create a scenes node if not existed. | 787 ## \brief Create a scenes node if not existed. |
744 # | 788 # |
745 def _init_metadata(self): | 789 def _init_metadata(self): |
798 pass | 842 pass |
799 | 843 |
800 def handle_doc_root(self, doc, root): | 844 def handle_doc_root(self, doc, root): |
801 self._doc = doc | 845 self._doc = doc |
802 self._root = root | 846 self._root = root |
803 self._layers = [] | 847 self._layers[:] = [] |
804 | 848 |
805 self._init_metadata() | 849 self._init_metadata() |
806 self._start_monitor() # from domview_monitor | 850 self._start_monitor() # from domview_monitor |
807 self._start_component_manager() # from component_manager | 851 self._start_component_manager() # from component_manager |
808 self._parse_all_layers() | 852 self._parse_all_layers() |
809 pass | 853 pass |
810 | 854 |
811 def reset(self): | 855 def reset(self): |
812 self._monitor_reparse() # from domview_monitor | 856 self._monitor_reparse() # from domview_monitor |
813 self._layers = [] | 857 self._layers[:] = [] |
814 self._parse_all_layers() | 858 self._parse_all_layers() |
815 pass | 859 pass |
816 | 860 |
817 def dumpattr(self, n): | 861 def dumpattr(self, n): |
818 s = "" | 862 s = "" |
1221 dst_child.setAttribute('xlink:href', '#' + src_child_id) | 1265 dst_child.setAttribute('xlink:href', '#' + src_child_id) |
1222 dst_child.setAttribute('ns0:duplicate-src', src_child_id) | 1266 dst_child.setAttribute('ns0:duplicate-src', src_child_id) |
1223 dst_group.appendChild(dst_child) | 1267 dst_group.appendChild(dst_child) |
1224 pass | 1268 pass |
1225 pass | 1269 pass |
1270 | |
1271 ## \brief To test a graphic node. | |
1272 # | |
1273 # A graphic node is a SVG node that is not layer group, scene | |
1274 # group, ... etc. It is only a normal node in a layer group or a | |
1275 # scene group. | |
1276 def is_graph_node(self, node): | |
1277 try: | |
1278 mode = node.getAttribute('inkscape:groupmode') | |
1279 except: | |
1280 pass | |
1281 else: | |
1282 if mode == 'layer': | |
1283 return False | |
1284 pass | |
1285 | |
1286 try: | |
1287 label = node.geteAttribute('inkscape:label') | |
1288 except: | |
1289 pass | |
1290 else: | |
1291 return False | |
1292 | |
1293 try: | |
1294 scene_group = node.geteAttribute('scene_group') | |
1295 except: | |
1296 pass | |
1297 else: | |
1298 if scene_group == 'true': | |
1299 return False | |
1300 pass | |
1301 | |
1302 return True | |
1226 pass | 1303 pass |
1227 | 1304 |