comparison pyink/MBScene.py @ 956:167e359c9d5b

Re-indent MBScene.py
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 17 Nov 2010 14:17:59 +0800
parents 53b0f8dc2284
children 8e3e46c26137
comparison
equal deleted inserted replaced
955:53b0f8dc2284 956:167e359c9d5b
41 self.nodes=[] 41 self.nodes=[]
42 pass 42 pass
43 pass 43 pass
44 44
45 class Scene: 45 class Scene:
46 def __init__(self, node, start,end): 46 def __init__(self, node, start,end):
47 self.node = node 47 self.node = node
48 self.start = int(start) 48 self.start = int(start)
49 self.end = int(end) 49 self.end = int(end)
50 pass 50 pass
51 pass 51 pass
52 52
53 53
54 class MBScene(): 54 class MBScene():
55 def __init__(self,desktop,win): 55 def __init__(self,desktop,win):
56 self.desktop = desktop 56 self.desktop = desktop
57 self.window = win 57 self.window = win
58 self.layer = [] 58 self.layer = []
59 self.layer.append(Layer(None)) 59 self.layer.append(Layer(None))
60 self.scenemap = None 60 self.scenemap = None
61 pass 61 pass
62 62
63 def confirm(self,msg): 63 def confirm(self,msg):
64 vbox = gtk.VBox() 64 vbox = gtk.VBox()
65 vbox.pack_start(gtk.Label(msg)) 65 vbox.pack_start(gtk.Label(msg))
66 self.button = gtk.Button('OK') 66 self.button = gtk.Button('OK')
67 vbox.pack_start(self.button) 67 vbox.pack_start(self.button)
68 self.button.connect("clicked", self.onQuit) 68 self.button.connect("clicked", self.onQuit)
69 self.window.add(vbox) 69 self.window.add(vbox)
70 pass 70 pass
71
72 def dumpattr(self,n):
73 s = ""
74 for a,v in n.attrib.items():
75 s = s + ("%s=%s" % (a,v))
76 pass
77 return s
71 78
72 def dumpattr(self,n): 79 def dump(self,node,l=0):
73 s = "" 80 print " " * l*2,"<", node.tag, self.dumpattr(node),">"
74 for a,v in n.attrib.items(): 81 for n in node:
75 s = s + ("%s=%s" % (a,v)) 82 self.dump(n,l+1)
76 return s 83 pass
84 print " " * l * 2,"/>"
85 pass
86
87 def parseMetadata(self,node):
88 self.current = 1
89 for n in node.childList():
90 if n.repr.name() == 'ns0:scenes':
91 self.scenemap={}
92 cur = int(n.repr.attribute("current"))
93 self.current = cur
94
95 for s in n.childList():
96 print '--->',s.repr.name()
97 if s.repr.name() == 'ns0:scene':
98 try:
99 start = int(s.repr.attribute("start"))
100 except:
101 traceback.print_exc()
102 continue
103 try:
104 end = s.repr.attribute("end")
105 if end == None:
106 end = start
107 pass
108 except:
109 end = start
110 pass
111 link = s.repr.attribute("ref")
112 self.scenemap[link] = [int(start),int(end)]
113 print "scene %d to %d" % (self.scenemap[link][0],self.scenemap[link][1])
114 if cur >= start and cur <= end:
115 self.currentscene = link
116 pass
117 pass
118 pass
119 pass
120 pass
121 pass
122
123
124 def parseScene(self):
125 """
126 In this function, we will collect all items for the current scene and then relocate them back to the appropriate scene object.
127 """
128 self.layer = []
129 self.scenemap = None
130 doc = self.desktop.doc().root()
131
132 for node in doc.childList():
133 if node.repr.name() == 'svg:metadata':
134 self.parseMetadata(node)
135 pass
136 elif node.repr.name() == 'svg:g':
137 oldscene = None
138 #print layer.attrib.get("id")
139 lyobj = Layer(node)
140 self.layer.append(lyobj)
141 lyobj.current_scene = []
142 for scene in node.childList():
143 if scene.repr.name() == 'svg:g':
144 try:
145 scmap = self.scenemap[scene.getId()]
146 if scmap == None:
147 lyobj.current_scene.append(scene)
148 continue
149 if self.current <= scmap[1] and self.current >= scmap[0]:
150 oldscene = scene
151 pass
152 except:
153 lyobj.current_scene.append(scene)
154 continue
155
156 lyobj.scene.append(Scene(scene,scmap[0],scmap[1]))
157 pass
158 else:
159 lyobj.current_scene.append(scene)
160 pass
161 pass
162
163 if oldscene != None:
164 # Put the objects back to the current scene
165 # for o in lyobj.current_scene:
166 # print o.tag
167 # oldscene.append(o)
168 pass
169 pass
170 pass
171
172 self.collectID()
173 self.dumpID()
174 pass
175
176 def collectID(self):
177 self.ID = {}
178 root = self.desktop.doc().root()
179 for n in root.childList():
180 self.collectID_recursive(n)
181 pass
182 pass
183
184 def collectID_recursive(self,node):
185 self.ID[node.getId()] = 1
186 for n in node.childList():
187 self.collectID_recursive(n)
188 pass
189 pass
190
191 def newID(self):
192 while True:
193 n = 's%d' % int(random.random()*10000)
194 #print "try %s" % n
195 if self.ID.has_key(n) == False:
196 return n
197 pass
198 pass
199
200 def dumpID(self):
201 for a,v in self.ID.items():
202 print a
203 pass
204 pass
205
206 def getLayer(self, layer):
207 for l in self.layer:
208 if l.node.getId() == layer:
209 return l
210 pass
211 return None
212
213
214 def insertKeyScene(self):
215 """
216 Insert a new key scene into the stage. If the nth is always a key scene, we will return without changing anything.
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
218 append a new scene.
219
220 """
221 nth = self.last_cell.nScene
222 layer = self.getLayer(self.last_cell.layer)
223 x = self.last_cell.nScene
224 y = self.last_cell.nLayer
225 if layer == None: return
226 for i in range(0,len(layer.scene)):
227 s = layer.scene[i]
228 if nth >= s.start and nth <= s.end:
229 if nth == s.start: return
230 newscene = Scene(DuplicateNode(s.node),nth,s.end)
231 newscene.node.setId(self.newID())
232 layer.scene.insert(i+1,newscene)
233 layer.scene[i].end = nth-1
234 btn = self.newCell('start.png')
235 btn.nScene = nth
236 btn.layer = layer
237 btn.nLayer = y
238 self.grid.remove(self.last_cell)
239 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0)
240 return
241 pass
242
243 if len(layer.scene) > 0:
244 last = nth
245 lastscene = None
246 for s in layer.scene:
247 if s.end < nth and last < s.end:
248 last = s.end
249 lastscene = s
250 pass
251 pass
252
253 for x in range(last+1, nth):
254 btn = self.newCell('fill.png')
255 btn.nScene = x
256 btn.layer = layer.node.getId()
257 btn.nLayer = y
258 self.grid.attach(btn, x, x+1, y , y+1,0,0,0,0)
259 pass
260
261 if lastscene == None:
262 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene')
263 node.setId(self.newID())
264 newscene = Scene(node,nth,nth)
265 else:
266 lastscene.end = nth-1
267 newscene = Scene(DuplicateNode(lastscene.node),nth,nth)
268 newscene.node.setId(self.newID())
269 pass
270
271 layer.scene.append(newscene)
272 btn = self.newCell('start.png')
273 x = self.last_cell.nScene
274 y = self.last_cell.nLayer
275 btn.nScene = nth
276 btn.layer = layer.node.getId()
277 btn.nLayer = y
278 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0)
279 else:
280 # This is the first scene in the layer
281 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene')
282 node.repr.setId(self.newID())
283 newscene = Scene(node,nth,nth)
284 layer.scene.append(newscene)
285 btn = self.newCell('start.png')
286 btn.nScene = nth
287 btn.layer = layer.node.getId()
288 btn.nLayer = y
289 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0)
290 pass
291 pass
292
293
294 def removeKeyScene(self):
295 nth = self.last_cell.nScene
296 try:
297 layer = self.getLayer(self.last_cell.layer)
298 except:
299 return
300 x = self.last_cell.nScene
301 y = self.last_cell.nLayer
302 for i in range(0,len(layer.scene)):
303 s = layer.scene[i]
304 if nth == s.start:
305 if i == 0:
306 for j in range(s.start,s.end+1):
307 btn = self.newCell('empty.png')
308 btn.nScene = nth
309 btn.layer = layer
310 btn.nLayer = y
311 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
312 pass
313 layer.scene.remove(s)
314 else:
315 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
317 # scene segmenet to the last one
318 layer.scene[i-1].end = s.end
319 layer.scene.remove(s)
320 btn = self.newCell('fill.png')
321
322 btn.nScene = nth
323 btn.layer = layer
324 btn.nLayer = y
325 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0)
326 else:
327 # Convert all scenes into empty cell
328 layer.scene.remove(s)
329 for j in range(s.start,s.end+1):
330 btn = self.newCell('empty.png')
331 btn.nScene = nth
332 btn.layer = layer
333 btn.nLayer = y
334 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
335 pass
336 pass
337 pass
338 return
339 pass
340 pass
341
342 def extendScene(self):
343 nth = self.last_cell.nScene
344 try:
345 layer = self.getLayer(self.last_cell.layer)
346 except:
347 traceback.print_exc()
348 return
349 x = self.last_cell.nScene
350 y = self.last_cell.nLayer
351 if layer == None:
352 return
353
354 for i in range(0,len(layer.scene)-1):
355 s = layer.scene[i]
356 if nth >= layer.scene[i].start and nth <= layer.scene[i].end:
357 return
358 pass
359
360 for i in range(0,len(layer.scene)-1):
361 s = layer.scene[i]
362 if nth >= layer.scene[i].start and nth < layer.scene[i+1].start:
363 for j in range(layer.scene[i].end+1, nth+1):
364 btn = self.newCell('fill.png')
365 btn.nScene = nth
366 btn.nLayer = y
367 btn.layer = self.last_cell.layer
368 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
369 layer.scene[i].end = nth
370 return
371 pass
372 if len(layer.scene) > 0 and nth > layer.scene[len(layer.scene)-1].end:
373 for j in range(layer.scene[len(layer.scene)-1].end+1, nth+1):
374 btn = self.newCell('fill.png')
375 btn.nScene = nth
376 btn.nLayer = y
377 btn.layer = self.last_cell.layer
378 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
379 pass
380 layer.scene[len(layer.scene)-1].end = nth
381 pass
382 pass
383 pass
384
385 def setCurrentScene(self,nth):
386 self.current = nth
387 for layer in self.layer:
388 for s in layer.scene:
389 if nth >= s.start and nth <= s.end:
390 s.node.repr.setAttribute("style","",True)
391 # print "Put the elemenets out"
392 layer.nodes = []
77 393
78 def dump(self,node,l=0): 394 # for o in s.node:
79 print " " * l*2,"<", node.tag, self.dumpattr(node),">" 395 # print " ",o.tag
80 for n in node: 396 # layer.nodes.append(o)
81 self.dump(n,l+1) 397 # for o in s.node:
82 pass 398 # s.node.remove(o)
83 print " " * l * 2,"/>" 399 else:
84 pass 400 s.node.repr.setAttribute("style","display:none",True)
401 pass
402 pass
403 pass
404 pass
405 pass
85 406
86 def parseMetadata(self,node): 407 def generate(self):
87 self.current = 1 408 newdoc = deepcopy(self.document)
88 for n in node.childList(): 409 root = newdoc.getroot()
89 if n.repr.name() == 'ns0:scenes': 410 has_scene = False
90 self.scenemap={} 411 for n in root:
91 cur = int(n.repr.attribute("current")) 412 if n.tag == '{http://www.w3.org/2000/svg}metadata':
92 self.current = cur 413 for nn in n:
93 414 if nn.tag == '{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scenes':
94 for s in n.childList(): 415 nn.clear()
95 print '--->',s.repr.name() 416 nn.set("current", "%d" % self.current)
96 if s.repr.name() == 'ns0:scene': 417 scenes = []
97 try: 418 for l in self.layer:
98 start = int(s.repr.attribute("start")) 419 for s in l.scene:
99 except: 420 id = s.node.get("id")
100 traceback.print_exc() 421 scene = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene')
101 continue 422 scene.set("ref", id)
102 try: 423 if s.start == s.end:
103 end = s.repr.attribute("end") 424 scene.set("start", "%d" % s.start)
104 if end == None: 425 else:
105 end = start 426 scene.set("start", "%d" % s.start)
106 except: 427 scene.set("end", "%d" % s.end)
107 end = start 428 pass
108 pass 429
109 link = s.repr.attribute("ref") 430 scenes.append(scene)
110 self.scenemap[link] = [int(start),int(end)]
111 print "scene %d to %d" % (self.scenemap[link][0],self.scenemap[link][1])
112 if cur >= start and cur <= end:
113 self.currentscene = link
114 pass
115 pass
116 pass
117 pass 431 pass
118 pass 432 pass
119 pass 433 for s in scenes:
434 nn.append(s)
435 pass
436 has_scene = True
437 pass
438 pass
439 if has_scene == False:
440 scenes = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scenes')
441 scenes.set("current","%d" % self.current)
442 for l in self.layer:
443 for s in l.scene:
444 scene = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene')
445 scene.set("ref", s.node.get("id"))
446 if s.start == s.end:
447 scene.set("start", "%d" % s.start)
448 else:
449 scene.set("start", "%d" % s.start)
450 scene.set("end", "%d" % s.end)
451 pass
452 scenes.append(scene)
453 pass
454 pass
455 n.append(scenes)
456 pass
457 pass
458 if n.tag == '{http://www.w3.org/2000/svg}g':
459 root.remove(n)
460 pass
461 pass
120 462
121 463 for l in self.layer:
122 def parseScene(self): 464 # Duplicate all attribute of the layer
123 """ 465 lnode = etree.Element("{http://www.w3.org/2000/svg}g")
124 In this function, we will collect all items for the current scene and then relocate them back to the appropriate scene object. 466 for a,v in l.node.attrib.items():
125 """ 467 lnode.set(a,v)
126 self.layer = [] 468 pass
127 self.scenemap = None 469 for n in l.nodes:
128 doc = self.desktop.doc().root() 470 lnode.append(n)
129 471 pass
130 for node in doc.childList(): 472 root.append(lnode)
131 if node.repr.name() == 'svg:metadata': 473 for s in l.scene:
132 self.parseMetadata(node) 474 snode = etree.Element("{http://www.w3.org/2000/svg}g")
133 pass 475 for a,v in s.node.attrib.items():
134 elif node.repr.name() == 'svg:g': 476 snode.set(a,v)
135 oldscene = None 477 pass
136 #print layer.attrib.get("id") 478 for n in s.node:
137 lyobj = Layer(node) 479 snode.append(deepcopy(n))
138 self.layer.append(lyobj) 480 pass
139 lyobj.current_scene = [] 481 lnode.append(snode)
140 for scene in node.childList(): 482 pass
141 if scene.repr.name() == 'svg:g': 483 pass
142 try: 484 self.document = newdoc
143 scmap = self.scenemap[scene.getId()] 485 pass
144 if scmap == None:
145 lyobj.current_scene.append(scene)
146 continue
147 if self.current <= scmap[1] and self.current >= scmap[0]:
148 oldscene = scene
149 pass
150 except:
151 lyobj.current_scene.append(scene)
152 continue
153
154 lyobj.scene.append(Scene(scene,scmap[0],scmap[1]))
155 pass
156 else:
157 lyobj.current_scene.append(scene)
158 pass
159 pass
160
161 if oldscene != None:
162 # Put the objects back to the current scene
163 #for o in lyobj.current_scene:
164 #print o.tag
165 #oldscene.append(o)
166 pass
167 pass
168 pass
169
170 self.collectID()
171 self.dumpID()
172 pass
173
174 def collectID(self):
175 self.ID = {}
176 root = self.desktop.doc().root()
177 for n in root.childList():
178 self.collectID_recursive(n)
179 pass
180 pass
181 486
182 def collectID_recursive(self,node): 487 def newCell(self,file):
183 self.ID[node.getId()] = 1 488 img = gtk.Image()
184 for n in node.childList(): 489 img.set_from_file(file)
185 self.collectID_recursive(n) 490 btn = gtk.EventBox()
186 pass 491 btn.add(img)
187 pass 492 btn.connect("button_press_event", self.cellSelect)
493 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray"))
494 return btn
495
496 def showGrid(self):
497 max = 0
498 for layer in self.layer:
499 for s in layer.scene:
500 if s.end > max:
501 max = s.end
502 pass
503 pass
504 pass
505 max = 50
506
507 self.grid = gtk.Table(len(self.layer)+1, 50)
508 self.scrollwin = gtk.ScrolledWindow()
509 self.scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
510 self.scrollwin.add_with_viewport(self.grid)
511 self.scrollwin.set_size_request(-1,150)
512 for i in range(1,max):
513 self.grid.attach(gtk.Label('%d'% i), i,i+1,0,1,0,0,0,0)
514 pass
515 for i in range(1,len(self.layer)+1):
516 print "Layer", i
517 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:
520 btn = self.newCell('start.png')
521 btn.nScene = s.start
522 btn.layer = l.node.getId()
523 btn.nLayer = i
524
525 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):
527 btn = self.newCell('fill.png')
528 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"))
530 btn.nScene = j
531 btn.layer = l.node.getId()
532 btn.nLayer = i
533 pass
534 pass
535 if len(l.scene) == 0:
536 start = 0
537 else:
538 start = l.scene[len(l.scene)-1].end
539 pass
540
541 for j in range(start,max):
542 btn = self.newCell('empty.png')
543 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"))
545 btn.nScene = j+1
546 btn.layer = l.node.getId()
547 btn.nLayer = i
548 pass
549 pass
550 self.last_cell = None
551 pass
552
553 def cellSelect(self, cell, data):
554 if self.last_cell:
555 self.last_cell.modify_bg(gtk.STATE_NORMAL, self.last_cell.get_colormap().alloc_color("gray"))
556 pass
188 557
189 def newID(self): 558 self.last_cell = cell
190 while True: 559 cell.modify_bg(gtk.STATE_NORMAL, cell.get_colormap().alloc_color("green"))
191 n = 's%d' % int(random.random()*10000) 560 pass
192 #print "try %s" % n 561
193 if self.ID.has_key(n) == False: 562 def doEditScene(self,w):
194 return n 563 self.setCurrentScene(self.last_cell.nScene)
195 pass 564 pass
196 pass 565
197 566 def doInsertKeyScene(self,w):
198 def dumpID(self): 567 # self.insertKeyScene()
199 for a,v in self.ID.items(): 568 # self.grid.show_all()
200 print a 569 return
201 pass 570
202 pass 571 def doRemoveScene(self,w):
203 572 # self.removeKeyScene()
204 def getLayer(self, layer): 573 # self.grid.show_all()
205 for l in self.layer: 574 # self.generate()
206 if l.node.getId() == layer: 575 return
207 return l 576
208 pass 577
209 return None 578 def doExtendScene(self,w):
210 579 self.extendScene()
211 580 self.grid.show_all()
212 def insertKeyScene(self): 581 pass
213 """ 582
214 Insert a new key scene into the stage. If the nth is always a key scene, we will return without changing anything. 583 def addButtons(self,hbox):
215 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 584 btn = gtk.Button('Edit')
216 append a new scene. 585 btn.connect('clicked', self.doEditScene)
217 586 hbox.pack_start(btn,expand=False,fill=False)
218 """ 587 btn = gtk.Button('Insert Key')
219 nth = self.last_cell.nScene 588 btn.connect('clicked',self.doInsertKeyScene)
220 layer = self.getLayer(self.last_cell.layer) 589 hbox.pack_start(btn,expand=False,fill=False)
221 x = self.last_cell.nScene 590 btn=gtk.Button('Remove Key')
222 y = self.last_cell.nLayer 591 btn.connect('clicked', self.doRemoveScene)
223 if layer == None: return 592 hbox.pack_start(btn,expand=False,fill=False)
224 for i in range(0,len(layer.scene)): 593 btn=gtk.Button('Extend scene')
225 s = layer.scene[i] 594 btn.connect('clicked', self.doExtendScene)
226 if nth >= s.start and nth <= s.end: 595 hbox.pack_start(btn,expand=False,fill=False)
227 if nth == s.start: return 596 pass
228 newscene = Scene(DuplicateNode(s.node),nth,s.end) 597
229 newscene.node.setId(self.newID()) 598 def onQuit(self, event):
230 layer.scene.insert(i+1,newscene) 599 self.OK = False
231 layer.scene[i].end = nth-1 600 gtk.main_quit()
232 btn = self.newCell('start.png') 601 pass
233 btn.nScene = nth 602
234 btn.layer = layer 603 def onOK(self,event):
235 btn.nLayer = y 604 self.OK = True
236 self.grid.remove(self.last_cell) 605 gtk.main_quit()
237 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0) 606 pass
238 return 607
239 pass 608 def onConfirmDelete(self):
240 609 if self.scenemap == None:
241 if len(layer.scene) > 0: 610 vbox = gtk.VBox(False,0)
242 last = nth 611 vbox.pack_start(gtk.Label('Convert the SVG into a MadButterfly SVG file. All current element will be delted'))
243 lastscene = None 612 hbox = gtk.HBox(False,0)
244 for s in layer.scene: 613 self.button = gtk.Button('OK')
245 if s.end < nth and last < s.end: 614 hbox.pack_start(self.button,expand=False,fill=False)
246 last = s.end 615 self.button.connect('clicked', self.onOK)
247 lastscene = s 616 self.button = gtk.Button('Cancel')
248 pass 617 hbox.pack_start(self.button,expand=False,fill=False)
249 pass 618 self.button.connect("clicked", self.onQuit)
250 619 vbox.pack_start(hbox,expand=False,fill=False)
251 for x in range(last+1, nth): 620 self.window.add(vbox)
252 btn = self.newCell('fill.png') 621 self.window.show_all()
253 btn.nScene = x 622 gtk.main()
254 btn.layer = layer.node.getId() 623 self.window.remove(vbox)
255 btn.nLayer = y 624 pass
256 self.grid.attach(btn, x, x+1, y , y+1,0,0,0,0) 625 pass
257 pass 626
258 627 def show(self):
259 if lastscene == None: 628 self.OK = True
260 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') 629 self.parseScene()
261 node.setId(self.newID()) 630 self.showGrid()
262 newscene = Scene(node,nth,nth) 631 vbox = gtk.VBox(False,0)
263 else: 632 self.desktop.getToplevel().child.child.pack_end(vbox,expand=False)
264 lastscene.end = nth-1 633 self.window = vbox
265 newscene = Scene(DuplicateNode(lastscene.node),nth,nth) 634 # self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
266 newscene.node.setId(self.newID()) 635 # self.window.connect("destroy", gtk.main_quit)
267 pass 636 # self.window.set_position(gtk.WIN_POS_MOUSE)
268 637 if self.scenemap == None:
269 layer.scene.append(newscene) 638 self.onConfirmDelete()
270 btn = self.newCell('start.png') 639 pass
271 x = self.last_cell.nScene 640 if self.OK:
272 y = self.last_cell.nLayer 641 vbox = gtk.VBox(False,0)
273 btn.nScene = nth 642 self.window.pack_start(vbox,expand=False)
274 btn.layer = layer.node.getId() 643 vbox.pack_start(self.scrollwin,expand=False)
275 btn.nLayer = y 644 self.vbox = vbox
276 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0) 645 hbox=gtk.HBox(False,0)
277 else: 646 self.addButtons(hbox)
278 # This is the first scene in the layer 647 vbox.pack_start(hbox,expand=False)
279 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') 648 else:
280 node.repr.setId(self.newID()) 649 return
281 newscene = Scene(node,nth,nth) 650
282 layer.scene.append(newscene) 651 # self.window.set_size_request(600,200)
283 btn = self.newCell('start.png') 652
284 btn.nScene = nth 653 self.window.show_all()
285 btn.layer = layer.node.getId() 654 pass
286 btn.nLayer = y 655 pass
287 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0)
288 pass
289 pass
290
291
292 def removeKeyScene(self):
293 nth = self.last_cell.nScene
294 try:
295 layer = self.getLayer(self.last_cell.layer)
296 except:
297 return
298 x = self.last_cell.nScene
299 y = self.last_cell.nLayer
300 for i in range(0,len(layer.scene)):
301 s = layer.scene[i]
302 if nth == s.start:
303 if i == 0:
304 for j in range(s.start,s.end+1):
305 btn = self.newCell('empty.png')
306 btn.nScene = nth
307 btn.layer = layer
308 btn.nLayer = y
309 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
310 pass
311 layer.scene.remove(s)
312 else:
313 if s.start == layer.scene[i-1].end+1:
314 # If the start of the delete scene segment is the end of the last scene segmenet, convert all scenes in the deleted
315 # scene segmenet to the last one
316 layer.scene[i-1].end = s.end
317 layer.scene.remove(s)
318 btn = self.newCell('fill.png')
319
320 btn.nScene = nth
321 btn.layer = layer
322 btn.nLayer = y
323 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0)
324 else:
325 # Convert all scenes into empty cell
326 layer.scene.remove(s)
327 for j in range(s.start,s.end+1):
328 btn = self.newCell('empty.png')
329 btn.nScene = nth
330 btn.layer = layer
331 btn.nLayer = y
332 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
333 pass
334 pass
335 pass
336 return
337 pass
338 pass
339
340 def extendScene(self):
341 nth = self.last_cell.nScene
342 try:
343 layer = self.getLayer(self.last_cell.layer)
344 except:
345 traceback.print_exc()
346 return
347 x = self.last_cell.nScene
348 y = self.last_cell.nLayer
349 if layer == None:
350 return
351
352 for i in range(0,len(layer.scene)-1):
353 s = layer.scene[i]
354 if nth >= layer.scene[i].start and nth <= layer.scene[i].end:
355 return
356 pass
357
358 for i in range(0,len(layer.scene)-1):
359 s = layer.scene[i]
360 if nth >= layer.scene[i].start and nth < layer.scene[i+1].start:
361 for j in range(layer.scene[i].end+1, nth+1):
362 btn = self.newCell('fill.png')
363 btn.nScene = nth
364 btn.nLayer = y
365 btn.layer = self.last_cell.layer
366 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
367 layer.scene[i].end = nth
368 return
369 pass
370 if len(layer.scene) > 0 and nth > layer.scene[len(layer.scene)-1].end:
371 for j in range(layer.scene[len(layer.scene)-1].end+1, nth+1):
372 btn = self.newCell('fill.png')
373 btn.nScene = nth
374 btn.nLayer = y
375 btn.layer = self.last_cell.layer
376 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
377 pass
378 layer.scene[len(layer.scene)-1].end = nth
379 pass
380 pass
381
382 def setCurrentScene(self,nth):
383 self.current = nth
384 for layer in self.layer:
385 for s in layer.scene:
386 if nth >= s.start and nth <= s.end:
387 s.node.repr.setAttribute("style","",True)
388 #print "Put the elemenets out"
389 layer.nodes = []
390
391 #for o in s.node:
392 #print " ",o.tag
393 # layer.nodes.append(o)
394 #for o in s.node:
395 # s.node.remove(o)
396 else:
397 s.node.repr.setAttribute("style","display:none",True)
398 pass
399 pass
400 pass
401 pass
402
403 def generate(self):
404 newdoc = deepcopy(self.document)
405 root = newdoc.getroot()
406 has_scene = False
407 for n in root:
408 if n.tag == '{http://www.w3.org/2000/svg}metadata':
409 for nn in n:
410 if nn.tag == '{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scenes':
411 nn.clear()
412 nn.set("current", "%d" % self.current)
413 scenes = []
414 for l in self.layer:
415 for s in l.scene:
416 id = s.node.get("id")
417 scene = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene')
418 scene.set("ref", id)
419 if s.start == s.end:
420 scene.set("start", "%d" % s.start)
421 else:
422 scene.set("start", "%d" % s.start)
423 scene.set("end", "%d" % s.end)
424 pass
425
426 scenes.append(scene)
427 pass
428 pass
429 for s in scenes:
430 nn.append(s)
431 pass
432 has_scene = True
433 pass
434 pass
435 if has_scene == False:
436 scenes = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scenes')
437 scenes.set("current","%d" % self.current)
438 for l in self.layer:
439 for s in l.scene:
440 scene = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene')
441 scene.set("ref", s.node.get("id"))
442 if s.start == s.end:
443 scene.set("start", "%d" % s.start)
444 else:
445 scene.set("start", "%d" % s.start)
446 scene.set("end", "%d" % s.end)
447 pass
448 scenes.append(scene)
449 pass
450 pass
451 n.append(scenes)
452 if n.tag == '{http://www.w3.org/2000/svg}g':
453 root.remove(n)
454 pass
455 pass
456
457 for l in self.layer:
458 # Duplicate all attribute of the layer
459 lnode = etree.Element("{http://www.w3.org/2000/svg}g")
460 for a,v in l.node.attrib.items():
461 lnode.set(a,v)
462 pass
463 for n in l.nodes:
464 lnode.append(n)
465 pass
466 root.append(lnode)
467 for s in l.scene:
468 snode = etree.Element("{http://www.w3.org/2000/svg}g")
469 for a,v in s.node.attrib.items():
470 snode.set(a,v)
471 pass
472 for n in s.node:
473 snode.append(deepcopy(n))
474 pass
475 lnode.append(snode)
476 pass
477 pass
478 self.document = newdoc
479 pass
480
481 def newCell(self,file):
482 img = gtk.Image()
483 img.set_from_file(file)
484 btn = gtk.EventBox()
485 btn.add(img)
486 btn.connect("button_press_event", self.cellSelect)
487 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray"))
488 return btn
489
490 def showGrid(self):
491 max = 0
492 for layer in self.layer:
493 for s in layer.scene:
494 if s.end > max:
495 max = s.end
496 pass
497 pass
498 pass
499 max = 50
500
501 self.grid = gtk.Table(len(self.layer)+1, 50)
502 self.scrollwin = gtk.ScrolledWindow()
503 self.scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
504 self.scrollwin.add_with_viewport(self.grid)
505 self.scrollwin.set_size_request(-1,150)
506 for i in range(1,max):
507 self.grid.attach(gtk.Label('%d'% i), i,i+1,0,1,0,0,0,0)
508 pass
509 for i in range(1,len(self.layer)+1):
510 print "Layer", i
511 l = self.layer[i-1]
512 #self.grid.attach(gtk.Label(l.node.get('{http://www.inkscape.org/namespaces/inkscape}label')), 0, 1, i, i+1,0,0,10,0)
513 for s in l.scene:
514 btn = self.newCell('start.png')
515 btn.nScene = s.start
516 btn.layer = l.node.getId()
517 btn.nLayer = i
518
519 self.grid.attach(btn, s.start, s.start+1, i, i+1,0,0,0,0)
520 for j in range(s.start+1,s.end+1):
521 btn = self.newCell('fill.png')
522 self.grid.attach(btn, j, j+1, i , i+1,0,0,0,0)
523 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray"))
524 btn.nScene = j
525 btn.layer = l.node.getId()
526 btn.nLayer = i
527 pass
528 pass
529 if len(l.scene) == 0:
530 start = 0
531 else:
532 start = l.scene[len(l.scene)-1].end
533 pass
534
535 for j in range(start,max):
536 btn = self.newCell('empty.png')
537 self.grid.attach(btn, j+1, j+2,i,i+1,0,0,0,0)
538 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray"))
539 btn.nScene = j+1
540 btn.layer = l.node.getId()
541 btn.nLayer = i
542 pass
543 pass
544 self.last_cell = None
545 pass
546
547 def cellSelect(self, cell, data):
548 if self.last_cell:
549 self.last_cell.modify_bg(gtk.STATE_NORMAL, self.last_cell.get_colormap().alloc_color("gray"))
550 pass
551
552 self.last_cell = cell
553 cell.modify_bg(gtk.STATE_NORMAL, cell.get_colormap().alloc_color("green"))
554 pass
555
556 def doEditScene(self,w):
557 self.setCurrentScene(self.last_cell.nScene)
558 pass
559
560 def doInsertKeyScene(self,w):
561 #self.insertKeyScene()
562 #self.grid.show_all()
563 return
564
565 def doRemoveScene(self,w):
566 return
567 self.removeKeyScene()
568 self.grid.show_all()
569 self.generate()
570 pass
571
572 def doExtendScene(self,w):
573 self.extendScene()
574 self.grid.show_all()
575 pass
576
577 def addButtons(self,hbox):
578 btn = gtk.Button('Edit')
579 btn.connect('clicked', self.doEditScene)
580 hbox.pack_start(btn,expand=False,fill=False)
581 btn = gtk.Button('Insert Key')
582 btn.connect('clicked',self.doInsertKeyScene)
583 hbox.pack_start(btn,expand=False,fill=False)
584 btn=gtk.Button('Remove Key')
585 btn.connect('clicked', self.doRemoveScene)
586 hbox.pack_start(btn,expand=False,fill=False)
587 btn=gtk.Button('Extend scene')
588 btn.connect('clicked', self.doExtendScene)
589 hbox.pack_start(btn,expand=False,fill=False)
590 pass
591
592 def onQuit(self, event):
593 self.OK = False
594 gtk.main_quit()
595 pass
596
597 def onOK(self,event):
598 self.OK = True
599 gtk.main_quit()
600 pass
601
602 def onConfirmDelete(self):
603 if self.scenemap == None:
604 vbox = gtk.VBox(False,0)
605 vbox.pack_start(gtk.Label('Convert the SVG into a MadButterfly SVG file. All current element will be delted'))
606 hbox = gtk.HBox(False,0)
607 self.button = gtk.Button('OK')
608 hbox.pack_start(self.button,expand=False,fill=False)
609 self.button.connect('clicked', self.onOK)
610 self.button = gtk.Button('Cancel')
611 hbox.pack_start(self.button,expand=False,fill=False)
612 self.button.connect("clicked", self.onQuit)
613 vbox.pack_start(hbox,expand=False,fill=False)
614 self.window.add(vbox)
615 self.window.show_all()
616 gtk.main()
617 self.window.remove(vbox)
618 pass
619 pass
620
621 def show(self):
622 self.OK = True
623 self.parseScene()
624 self.showGrid()
625 vbox = gtk.VBox(False,0)
626 self.desktop.getToplevel().child.child.pack_end(vbox,expand=False)
627 self.window = vbox
628 #self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
629 #self.window.connect("destroy", gtk.main_quit)
630 #self.window.set_position(gtk.WIN_POS_MOUSE)
631 if self.scenemap == None:
632 self.onConfirmDelete()
633 pass
634 if self.OK:
635 vbox = gtk.VBox(False,0)
636 self.window.pack_start(vbox,expand=False)
637 vbox.pack_start(self.scrollwin,expand=False)
638 self.vbox = vbox
639 hbox=gtk.HBox(False,0)
640 self.addButtons(hbox)
641 vbox.pack_start(hbox,expand=False)
642 else:
643 return
644
645 #self.window.set_size_request(600,200)
646
647 self.window.show_all()
648 pass
649 pass