comparison pyink/MBScene.py @ 957:8e3e46c26137

Break long lines
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 17 Nov 2010 14:31:59 +0800
parents 167e359c9d5b
children 67823f7a0a17
comparison
equal deleted inserted replaced
956:167e359c9d5b 957:8e3e46c26137
6 from copy import deepcopy 6 from copy import deepcopy
7 from lxml import etree 7 from lxml import etree
8 import random 8 import random
9 import traceback 9 import traceback
10 10
11 # Please refer to http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention for the designed document. 11 # Please refer to
12 # http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention
13 # for the designed document.
12 14
13 15
14 # Algorithm: 16 # Algorithm:
15 # 17 #
16 # We will parse the first two level of the SVG DOM. collect a table of 18 # We will parse the first two level of the SVG DOM. collect a table of
48 self.start = int(start) 50 self.start = int(start)
49 self.end = int(end) 51 self.end = int(end)
50 pass 52 pass
51 pass 53 pass
52 54
55 _scenes = '{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scenes'
56 _scene = '{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene'
53 57
54 class MBScene(): 58 class MBScene():
55 def __init__(self,desktop,win): 59 def __init__(self,desktop,win):
56 self.desktop = desktop 60 self.desktop = desktop
57 self.window = win 61 self.window = win
108 except: 112 except:
109 end = start 113 end = start
110 pass 114 pass
111 link = s.repr.attribute("ref") 115 link = s.repr.attribute("ref")
112 self.scenemap[link] = [int(start),int(end)] 116 self.scenemap[link] = [int(start),int(end)]
113 print "scene %d to %d" % (self.scenemap[link][0],self.scenemap[link][1]) 117 print "scene %d to %d" % (self.scenemap[link][0],
118 self.scenemap[link][1])
114 if cur >= start and cur <= end: 119 if cur >= start and cur <= end:
115 self.currentscene = link 120 self.currentscene = link
116 pass 121 pass
117 pass 122 pass
118 pass 123 pass
121 pass 126 pass
122 127
123 128
124 def parseScene(self): 129 def parseScene(self):
125 """ 130 """
126 In this function, we will collect all items for the current scene and then relocate them back to the appropriate scene object. 131 In this function, we will collect all items for the current
132 scene and then relocate them back to the appropriate scene
133 object.
127 """ 134 """
128 self.layer = [] 135 self.layer = []
129 self.scenemap = None 136 self.scenemap = None
130 doc = self.desktop.doc().root() 137 doc = self.desktop.doc().root()
131 138
144 try: 151 try:
145 scmap = self.scenemap[scene.getId()] 152 scmap = self.scenemap[scene.getId()]
146 if scmap == None: 153 if scmap == None:
147 lyobj.current_scene.append(scene) 154 lyobj.current_scene.append(scene)
148 continue 155 continue
149 if self.current <= scmap[1] and self.current >= scmap[0]: 156 if self.current <= scmap[1] and \
157 self.current >= scmap[0]:
150 oldscene = scene 158 oldscene = scene
151 pass 159 pass
152 except: 160 except:
153 lyobj.current_scene.append(scene) 161 lyobj.current_scene.append(scene)
154 continue 162 continue
211 return None 219 return None
212 220
213 221
214 def insertKeyScene(self): 222 def insertKeyScene(self):
215 """ 223 """
216 Insert a new key scene into the stage. If the nth is always a key scene, we will return without changing anything. 224 Insert a new key scene into the stage. If the nth is always a
217 If the nth is a filled scene, we will break the original scene into two parts. If the nth is out of any scene, we will 225 key scene, we will return without changing anything. If the
218 append a new scene. 226 nth is a filled scene, we will break the original scene into
227 two parts. If the nth is out of any scene, we will append a
228 new scene.
219 229
220 """ 230 """
221 nth = self.last_cell.nScene 231 nth = self.last_cell.nScene
222 layer = self.getLayer(self.last_cell.layer) 232 layer = self.getLayer(self.last_cell.layer)
223 x = self.last_cell.nScene 233 x = self.last_cell.nScene
257 btn.nLayer = y 267 btn.nLayer = y
258 self.grid.attach(btn, x, x+1, y , y+1,0,0,0,0) 268 self.grid.attach(btn, x, x+1, y , y+1,0,0,0,0)
259 pass 269 pass
260 270
261 if lastscene == None: 271 if lastscene == None:
262 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') 272 node = etree.Element(_scene)
263 node.setId(self.newID()) 273 node.setId(self.newID())
264 newscene = Scene(node,nth,nth) 274 newscene = Scene(node,nth,nth)
265 else: 275 else:
266 lastscene.end = nth-1 276 lastscene.end = nth-1
267 newscene = Scene(DuplicateNode(lastscene.node),nth,nth) 277 newscene = Scene(DuplicateNode(lastscene.node),nth,nth)
276 btn.layer = layer.node.getId() 286 btn.layer = layer.node.getId()
277 btn.nLayer = y 287 btn.nLayer = y
278 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0) 288 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0)
279 else: 289 else:
280 # This is the first scene in the layer 290 # This is the first scene in the layer
281 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') 291 node = etree.Element(_scene)
282 node.repr.setId(self.newID()) 292 node.repr.setId(self.newID())
283 newscene = Scene(node,nth,nth) 293 newscene = Scene(node,nth,nth)
284 layer.scene.append(newscene) 294 layer.scene.append(newscene)
285 btn = self.newCell('start.png') 295 btn = self.newCell('start.png')
286 btn.nScene = nth 296 btn.nScene = nth
311 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0) 321 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
312 pass 322 pass
313 layer.scene.remove(s) 323 layer.scene.remove(s)
314 else: 324 else:
315 if s.start == layer.scene[i-1].end+1: 325 if s.start == layer.scene[i-1].end+1:
316 # If the start of the delete scene segment is the end of the last scene segmenet, convert all scenes in the deleted 326 # If the start of the delete scene segment is
317 # scene segmenet to the last one 327 # the end of the last scene segmenet, convert
328 # all scenes in the deleted scene segmenet to
329 # the last one
318 layer.scene[i-1].end = s.end 330 layer.scene[i-1].end = s.end
319 layer.scene.remove(s) 331 layer.scene.remove(s)
320 btn = self.newCell('fill.png') 332 btn = self.newCell('fill.png')
321 333
322 btn.nScene = nth 334 btn.nScene = nth
367 btn.layer = self.last_cell.layer 379 btn.layer = self.last_cell.layer
368 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0) 380 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
369 layer.scene[i].end = nth 381 layer.scene[i].end = nth
370 return 382 return
371 pass 383 pass
372 if len(layer.scene) > 0 and nth > layer.scene[len(layer.scene)-1].end: 384 if len(layer.scene) > 0 and \
385 nth > layer.scene[len(layer.scene)-1].end:
373 for j in range(layer.scene[len(layer.scene)-1].end+1, nth+1): 386 for j in range(layer.scene[len(layer.scene)-1].end+1, nth+1):
374 btn = self.newCell('fill.png') 387 btn = self.newCell('fill.png')
375 btn.nScene = nth 388 btn.nScene = nth
376 btn.nLayer = y 389 btn.nLayer = y
377 btn.layer = self.last_cell.layer 390 btn.layer = self.last_cell.layer
409 root = newdoc.getroot() 422 root = newdoc.getroot()
410 has_scene = False 423 has_scene = False
411 for n in root: 424 for n in root:
412 if n.tag == '{http://www.w3.org/2000/svg}metadata': 425 if n.tag == '{http://www.w3.org/2000/svg}metadata':
413 for nn in n: 426 for nn in n:
414 if nn.tag == '{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scenes': 427 if nn.tag == _scenes:
415 nn.clear() 428 nn.clear()
416 nn.set("current", "%d" % self.current) 429 nn.set("current", "%d" % self.current)
417 scenes = [] 430 scenes = []
418 for l in self.layer: 431 for l in self.layer:
419 for s in l.scene: 432 for s in l.scene:
420 id = s.node.get("id") 433 id = s.node.get("id")
421 scene = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') 434 scene = etree.Element(_scene)
422 scene.set("ref", id) 435 scene.set("ref", id)
423 if s.start == s.end: 436 if s.start == s.end:
424 scene.set("start", "%d" % s.start) 437 scene.set("start", "%d" % s.start)
425 else: 438 else:
426 scene.set("start", "%d" % s.start) 439 scene.set("start", "%d" % s.start)
435 pass 448 pass
436 has_scene = True 449 has_scene = True
437 pass 450 pass
438 pass 451 pass
439 if has_scene == False: 452 if has_scene == False:
440 scenes = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scenes') 453 scenes = etree.Element(_scenes)
441 scenes.set("current","%d" % self.current) 454 scenes.set("current","%d" % self.current)
442 for l in self.layer: 455 for l in self.layer:
443 for s in l.scene: 456 for s in l.scene:
444 scene = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') 457 scene = etree.Element(_scene)
445 scene.set("ref", s.node.get("id")) 458 scene.set("ref", s.node.get("id"))
446 if s.start == s.end: 459 if s.start == s.end:
447 scene.set("start", "%d" % s.start) 460 scene.set("start", "%d" % s.start)
448 else: 461 else:
449 scene.set("start", "%d" % s.start) 462 scene.set("start", "%d" % s.start)
513 self.grid.attach(gtk.Label('%d'% i), i,i+1,0,1,0,0,0,0) 526 self.grid.attach(gtk.Label('%d'% i), i,i+1,0,1,0,0,0,0)
514 pass 527 pass
515 for i in range(1,len(self.layer)+1): 528 for i in range(1,len(self.layer)+1):
516 print "Layer", i 529 print "Layer", i
517 l = self.layer[i-1] 530 l = self.layer[i-1]
518 #self.grid.attach(gtk.Label(l.node.get('{http://www.inkscape.org/namespaces/inkscape}label')), 0, 1, i, i+1,0,0,10,0)
519 for s in l.scene: 531 for s in l.scene:
520 btn = self.newCell('start.png') 532 btn = self.newCell('start.png')
521 btn.nScene = s.start 533 btn.nScene = s.start
522 btn.layer = l.node.getId() 534 btn.layer = l.node.getId()
523 btn.nLayer = i 535 btn.nLayer = i
524 536
525 self.grid.attach(btn, s.start, s.start+1, i, i+1,0,0,0,0) 537 self.grid.attach(btn, s.start, s.start+1, i, i+1,0,0,0,0)
526 for j in range(s.start+1,s.end+1): 538 for j in range(s.start+1,s.end+1):
527 btn = self.newCell('fill.png') 539 btn = self.newCell('fill.png')
528 self.grid.attach(btn, j, j+1, i , i+1,0,0,0,0) 540 self.grid.attach(btn, j, j+1, i , i+1,0,0,0,0)
529 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray")) 541 color = btn.get_colormap().alloc_color("gray")
542 btn.modify_bg(gtk.STATE_NORMAL, color)
530 btn.nScene = j 543 btn.nScene = j
531 btn.layer = l.node.getId() 544 btn.layer = l.node.getId()
532 btn.nLayer = i 545 btn.nLayer = i
533 pass 546 pass
534 pass 547 pass
539 pass 552 pass
540 553
541 for j in range(start,max): 554 for j in range(start,max):
542 btn = self.newCell('empty.png') 555 btn = self.newCell('empty.png')
543 self.grid.attach(btn, j+1, j+2,i,i+1,0,0,0,0) 556 self.grid.attach(btn, j+1, j+2,i,i+1,0,0,0,0)
544 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray")) 557 color = btn.get_colormap().alloc_color("gray")
558 btn.modify_bg(gtk.STATE_NORMAL, color)
545 btn.nScene = j+1 559 btn.nScene = j+1
546 btn.layer = l.node.getId() 560 btn.layer = l.node.getId()
547 btn.nLayer = i 561 btn.nLayer = i
548 pass 562 pass
549 pass 563 pass
550 self.last_cell = None 564 self.last_cell = None
551 pass 565 pass
552 566
553 def cellSelect(self, cell, data): 567 def cellSelect(self, cell, data):
554 if self.last_cell: 568 if self.last_cell:
555 self.last_cell.modify_bg(gtk.STATE_NORMAL, self.last_cell.get_colormap().alloc_color("gray")) 569 color = self.last_cell.get_colormap().alloc_color("gray")
570 self.last_cell.modify_bg(gtk.STATE_NORMAL, color)
556 pass 571 pass
557 572
558 self.last_cell = cell 573 self.last_cell = cell
559 cell.modify_bg(gtk.STATE_NORMAL, cell.get_colormap().alloc_color("green")) 574 color = cell.get_colormap().alloc_color("green")
575 cell.modify_bg(gtk.STATE_NORMAL, color)
560 pass 576 pass
561 577
562 def doEditScene(self,w): 578 def doEditScene(self,w):
563 self.setCurrentScene(self.last_cell.nScene) 579 self.setCurrentScene(self.last_cell.nScene)
564 pass 580 pass
606 pass 622 pass
607 623
608 def onConfirmDelete(self): 624 def onConfirmDelete(self):
609 if self.scenemap == None: 625 if self.scenemap == None:
610 vbox = gtk.VBox(False,0) 626 vbox = gtk.VBox(False,0)
611 vbox.pack_start(gtk.Label('Convert the SVG into a MadButterfly SVG file. All current element will be delted')) 627 vbox.pack_start(gtk.Label('Convert the SVG into a MadButterfly'
628 ' SVG file. All current element will'
629 ' be delted'))
612 hbox = gtk.HBox(False,0) 630 hbox = gtk.HBox(False,0)
613 self.button = gtk.Button('OK') 631 self.button = gtk.Button('OK')
614 hbox.pack_start(self.button,expand=False,fill=False) 632 hbox.pack_start(self.button,expand=False,fill=False)
615 self.button.connect('clicked', self.onOK) 633 self.button.connect('clicked', self.onOK)
616 self.button = gtk.Button('Cancel') 634 self.button = gtk.Button('Cancel')