comparison inkscape/firefox/MBServer.py @ 274:96aae15527c8

Port all basic scene editor from pyGtk to the firefox
author wycc
date Thu, 29 Jan 2009 22:30:46 +0800
parents c39b24036a75
children
comparison
equal deleted inserted replaced
273:0cadeb9fdfc3 274:96aae15527c8
4 import gtk 4 import gtk
5 from copy import deepcopy 5 from copy import deepcopy
6 from lxml import etree 6 from lxml import etree
7 from twisted.web import server, resource,soap 7 from twisted.web import server, resource,soap
8 from twisted.internet import reactor 8 from twisted.internet import reactor
9 import traceback
9 10
10 import random 11 import random
11 12
12 # Please refer to http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention for the designed document. 13 # Please refer to http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention for the designed document.
13 14
157 if l.node.attrib.get("id") == layer: 158 if l.node.attrib.get("id") == layer:
158 return l 159 return l
159 return None 160 return None
160 161
161 162
162 def insertKeyScene(self): 163 def insertKeyScene(self,layer,nth):
163 """ 164 """
164 Insert a new key scene into the stage. If the nth is always a key scene, we will return without changing anything. 165 Insert a new key scene into the stage. If the nth is always a key scene, we will return without changing anything.
165 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 166 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
166 append a new scene. 167 append a new scene.
167 168
168 """ 169 """
169 nth = self.last_cell.nScene
170 layer = self.getLayer(self.last_cell.layer)
171 x = self.last_cell.nScene
172 y = self.last_cell.nLayer
173 if layer == None: return 170 if layer == None: return
171
172 # Check if the nth is in the middle of any scene
174 for i in range(0,len(layer.scene)): 173 for i in range(0,len(layer.scene)):
175 s = layer.scene[i] 174 s = layer.scene[i]
176 if nth >= s.start and nth <= s.end: 175 if nth >= s.start and nth <= s.end:
177 if nth == s.start: return 176 if nth == s.start: return
178 newscene = Scene(deepcopy(s.node),nth,s.end) 177 newscene = Scene(deepcopy(s.node),nth,s.end)
179 newscene.node.set("id", self.newID()) 178 newscene.node.set("id", self.newID())
180 layer.scene.insert(i+1,newscene) 179 layer.scene.insert(i+1,newscene)
181 layer.scene[i].end = nth-1 180 layer.scene[i].end = nth-1
182 btn = self.newCell('start.png')
183 btn.nScene = nth
184 btn.layer = layer
185 btn.nLayer = y
186 self.grid.remove(self.last_cell)
187 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0)
188 return 181 return
189 if len(layer.scene) > 0: 182 if len(layer.scene) > 0:
183 # extend the last scene befor eit automatically
190 last = nth 184 last = nth
191 lastscene = None 185 lastscene = None
186 # Find the last scene before it
192 for s in layer.scene: 187 for s in layer.scene:
193 if s.end < nth and last < s.end: 188 if s.end < nth and last < s.end:
194 last = s.end 189 last = s.end
195 lastscene = s 190 lastscene = s
196 for x in range(last+1, nth):
197 btn = self.newCell('fill.png')
198 btn.nScene = x
199 btn.layer = layer.node.get('id')
200 btn.nLayer = y
201 self.grid.attach(btn, x, x+1, y , y+1,0,0,0,0)
202 if lastscene == None: 191 if lastscene == None:
203 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') 192 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene')
204 node.set("id", self.newID()) 193 node.set("id", self.newID())
205 newscene = Scene(node,nth,nth) 194 newscene = Scene(node,nth,nth)
206 else: 195 else:
207 lastscene.end = nth-1 196 lastscene.end = nth-1
208 newscene = Scene(deepcopy(lastscene.node),nth,nth) 197 newscene = Scene(deepcopy(lastscene.node),nth,nth)
209 newscene.node.set("id",self.newID()) 198 newscene.node.set("id",self.newID())
210 layer.scene.append(newscene) 199 layer.scene.append(newscene)
211 btn = self.newCell('start.png')
212 x = self.last_cell.nScene
213 y = self.last_cell.nLayer
214 btn.nScene = nth
215 btn.layer = layer.node.get('id')
216 btn.nLayer = y
217 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0)
218 else: 200 else:
219 # This is the first scene in the layer 201 # This is the first scene in the layer
220 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') 202 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene')
221 node.set("id", self.newID()) 203 node.set("id", self.newID())
222 newscene = Scene(node,nth,nth) 204 newscene = Scene(node,nth,nth)
223 layer.scene.append(newscene) 205 layer.scene.append(newscene)
224 btn = self.newCell('start.png') 206
225 btn.nScene = nth 207
226 btn.layer = layer.node.get('id') 208 def deleteScene(self,layer,nth):
227 btn.nLayer = y
228 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0)
229
230
231
232
233 def removeKeyScene(self):
234 nth = self.last_cell.nScene
235 try:
236 layer = self.getLayer(self.last_cell.layer)
237 except:
238 return
239 x = self.last_cell.nScene
240 y = self.last_cell.nLayer
241 for i in range(0,len(layer.scene)): 209 for i in range(0,len(layer.scene)):
242 s = layer.scene[i] 210 s = layer.scene[i]
243 if nth == s.start: 211 if nth == s.start:
244 if i == 0: 212 if i == 0:
245 for j in range(s.start,s.end+1):
246 btn = self.newCell('empty.png')
247 btn.nScene = nth
248 btn.layer = layer
249 btn.nLayer = y
250 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
251 layer.scene.remove(s) 213 layer.scene.remove(s)
252 else: 214 else:
253 if s.start == layer.scene[i-1].end+1: 215 if s.start == layer.scene[i-1].end+1:
254 # If the start of the delete scene segment is the end of the last scene segmenet, convert all scenes in the deleted 216 # If the start of the delete scene segment is the end of the last scene segmenet, convert all scenes in the deleted
255 # scene segmenet to the last one 217 # scene segmenet to the last one
256 layer.scene[i-1].end = s.end 218 layer.scene[i-1].end = s.end
257 layer.scene.remove(s) 219 layer.scene.remove(s)
258 btn = self.newCell('fill.png')
259
260 btn.nScene = nth
261 btn.layer = layer
262 btn.nLayer = y
263 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0)
264 else: 220 else:
265 # Convert all scenes into empty cell 221 # Convert all scenes into empty cell
266 layer.scene.remove(s) 222 layer.scene.remove(s)
267 for j in range(s.start,s.end+1):
268 btn = self.newCell('empty.png')
269 btn.nScene = nth
270 btn.layer = layer
271 btn.nLayer = y
272 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
273
274 223
275 return 224 return
276 225 pass
277 def extendScene(self): 226 pass
278 nth = self.last_cell.nScene 227
279 try: 228
280 layer = self.getLayer(self.last_cell.layer) 229 def extendScene(self,layer,nth):
281 except:
282 return
283 x = self.last_cell.nScene
284 y = self.last_cell.nLayer
285 if layer == None: return 230 if layer == None: return
286 231
287 for i in range(0,len(layer.scene)-1): 232 for i in range(0,len(layer.scene)-1):
288 s = layer.scene[i] 233 s = layer.scene[i]
289 if nth >= layer.scene[i].start and nth <= layer.scene[i].end: 234 if nth >= layer.scene[i].start and nth <= layer.scene[i].end:
290 return 235 return
291 236
292 for i in range(0,len(layer.scene)-1): 237 for i in range(0,len(layer.scene)-1):
293 s = layer.scene[i] 238 s = layer.scene[i]
294 if nth >= layer.scene[i].start and nth < layer.scene[i+1].start: 239 if nth >= layer.scene[i].start and nth < layer.scene[i+1].start:
295 for j in range(layer.scene[i].end+1, nth+1):
296 btn = self.newCell('fill.png')
297 btn.nScene = nth
298 btn.nLayer = y
299 btn.layer = self.last_cell.layer
300 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
301 layer.scene[i].end = nth 240 layer.scene[i].end = nth
302 return 241 return
303 if len(layer.scene) > 0 and nth > layer.scene[len(layer.scene)-1].end: 242 if len(layer.scene) > 0 and nth > layer.scene[len(layer.scene)-1].end:
304 for j in range(layer.scene[len(layer.scene)-1].end+1, nth+1):
305 btn = self.newCell('fill.png')
306 btn.nScene = nth
307 btn.nLayer = y
308 btn.layer = self.last_cell.layer
309 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0)
310 layer.scene[len(layer.scene)-1].end = nth 243 layer.scene[len(layer.scene)-1].end = nth
244
311 def setCurrentScene(self,nth): 245 def setCurrentScene(self,nth):
312 self.current = nth 246 self.current = nth
313 for layer in self.layer: 247 for layer in self.layer:
314 for s in layer.scene: 248 for s in layer.scene:
315 if nth >= s.start and nth <= s.end: 249 if nth >= s.start and nth <= s.end:
443 377
444 def doEditScene(self,w): 378 def doEditScene(self,w):
445 self.setCurrentScene(self.last_cell.nScene) 379 self.setCurrentScene(self.last_cell.nScene)
446 self.generate() 380 self.generate()
447 gtk.main_quit() 381 gtk.main_quit()
448 def doInsertKeyScene(self,w):
449 self.insertKeyScene()
450 self.grid.show_all()
451 self.generate()
452 382
453 def doRemoveScene(self,w): 383 def doRemoveScene(self,w):
454 self.removeKeyScene() 384 self.removeKeyScene()
455 self.grid.show_all()
456 self.generate()
457 def doExtendScene(self,w):
458 self.extendScene()
459 self.grid.show_all() 385 self.grid.show_all()
460 self.generate() 386 self.generate()
461 def addButtons(self,hbox): 387 def addButtons(self,hbox):
462 btn = gtk.Button('Edit') 388 btn = gtk.Button('Edit')
463 btn.connect('clicked', self.doEditScene) 389 btn.connect('clicked', self.doEditScene)
520 def quit(self): 446 def quit(self):
521 reactor.stop() 447 reactor.stop()
522 def soap_SCENE(self,n): 448 def soap_SCENE(self,n):
523 self.target.setCurrentScene(int(n)) 449 self.target.setCurrentScene(int(n))
524 return "OK" 450 return "OK"
525 451 def soap_INSERTKEY(self,layer,n):
526 452 try:
527 453 layer = self.target.getLayer(layer)
528 454 self.target.insertKeyScene(layer,int(n))
529 455 return "OK"
456 except:
457 return traceback.format_exc()
458 def soap_EXTENDSCENE(self,layer,n):
459 try:
460 layer = self.target.getLayer(layer)
461 self.target.extendScene(layer,int(n))
462 return "OK"
463 except:
464 return traceback.format_exc()
465 def soap_DELETESCENE(self,layer,n):
466 try:
467 layer = self.target.getLayer(layer)
468 self.target.deleteScene(layer,int(n))
469 return "OK"
470 except:
471 return traceback.format_exc()
472 def soap_GETDOC(self):
473 try:
474 self.target.generate()
475 return etree.tostring(self.target.document)
476 except:
477 return traceback.format_exc()
530 import os 478 import os
531
532 os.chdir('/usr/share/inkscape/extensions') 479 os.chdir('/usr/share/inkscape/extensions')
533 480
534 A = MBScene() 481 A = MBScene()
535 A.affect() 482 A.affect()
536 483