comparison applications/lab/diagrameditor.py @ 47:c4ec95a588ea

Fixed save to xml
author windel
date Tue, 27 Mar 2012 07:29:59 +0200
parents 7964065400b7
children a309982ab4d5
comparison
equal deleted inserted replaced
46:7964065400b7 47:c4ec95a588ea
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 from PyQt4.QtGui import * 3 from PyQt4.QtGui import *
4 from PyQt4.QtCore import * 4 from PyQt4.QtCore import *
5 import sys 5 import sys
6 from elementtree.SimpleXMLWriter import XMLWriter 6 #from elementtree.SimpleXMLWriter import XMLWriter
7 import xml.dom.minidom as md
7 8
8 """ 9 """
9 This script implements a basic diagram editor. 10 This script implements a basic diagram editor.
10 """ 11 """
11 12
339 self.startedConnection = None 340 self.startedConnection = None
340 fullScreenShortcut = QShortcut(QKeySequence("F11"), self) 341 fullScreenShortcut = QShortcut(QKeySequence("F11"), self)
341 fullScreenShortcut.activated.connect(self.toggleFullScreen) 342 fullScreenShortcut.activated.connect(self.toggleFullScreen)
342 saveShortcut = QShortcut(QKeySequence(QKeySequence.Save), self) 343 saveShortcut = QShortcut(QKeySequence(QKeySequence.Save), self)
343 saveShortcut.activated.connect(self.save) 344 saveShortcut.activated.connect(self.save)
344 self.loadDiagram('diagram.txt') 345 self.loadDiagram('diagram2.usd')
345 346
346 def save(self): 347 def save(self):
347 self.saveDiagram2('diagram2.txt') 348 self.saveDiagram('diagram2.usd')
348 def saveDiagram2(self, filename): 349
349 items = self.diagramScene.items()
350 blocks = [item for item in items if type(item) is BlockItem]
351 with open(filename, 'w') as f:
352 w = XMLWriter(f)
353 model = w.start('model')
354 for block in blocks:
355 w.element("block")
356
357 w.close(model)
358 def saveDiagram(self, filename): 350 def saveDiagram(self, filename):
359 items = self.diagramScene.items() 351 items = self.diagramScene.items()
360 blocks = [item for item in items if type(item) is BlockItem] 352 blocks = [item for item in items if type(item) is BlockItem]
353
354 doc = md.Document()
355 modelElement = doc.createElement('system')
356 doc.appendChild(modelElement)
357 for block in blocks:
358 blockElement = doc.createElement("block")
359 x, y = block.scenePos().x(), block.scenePos().y()
360 rect = block.rect()
361 w, h = rect.width(), rect.height()
362 blockElement.setAttribute("name", block.label.toPlainText())
363 blockElement.setAttribute("x", str(x))
364 blockElement.setAttribute("y", str(y))
365 blockElement.setAttribute("width", str(w))
366 blockElement.setAttribute("height", str(h))
367 modelElement.appendChild(blockElement)
361 with open(filename, 'w') as f: 368 with open(filename, 'w') as f:
362 for block in blocks: 369 f.write(doc.toprettyxml())
363 rect = block.rect()
364 x, y, w, h = block.scenePos().x(), block.scenePos().y(), rect.width(), rect.height()
365 f.write('B({0};{1};{2};{3}) '.format(x,y,w,h)) # (x;y;w;h)
366 370
367 def loadDiagram(self, filename): 371 def loadDiagram(self, filename):
368 try: 372 try:
369 with open(filename, 'r') as f: 373 doc = md.parse(filename)
370 txt = f.read()
371 except IOError as e: 374 except IOError as e:
372 print('{0} not found'.format(filename)) 375 print('{0} not found'.format(filename))
373 return 376 return
374 parts = txt.split(' ') 377 sysElements = doc.getElementsByTagName('system')
375 for part in parts: 378 print('systems:',sysElements)
376 if len(part) < 1: 379 blockElements = doc.getElementsByTagName('block')
377 continue 380 print('blocks:', blockElements)
378 if part[0] == 'B': 381 for sysElement in sysElements:
379 part = part[2:-1] 382 print('system:', sysElement)
380 x,y,w,h = [float(val) for val in part.split(';')] 383 blockElements = sysElement.getElementsByTagName('block')
381 block = BlockItem('Loaded') # TODO: store name. 384 print('blocks:', blockElements)
385 for blockElement in blockElements:
386 x = float(blockElement.getAttribute('x'))
387 y = float(blockElement.getAttribute('y'))
388 w = float(blockElement.getAttribute('width'))
389 h = float(blockElement.getAttribute('height'))
390 name = blockElement.getAttribute('name')
391 block = BlockItem(name)
382 self.diagramScene.addItem(block) 392 self.diagramScene.addItem(block)
383 block.setPos(x, y) 393 block.setPos(x, y)
384 block.sizer.setPos(w, h) 394 block.sizer.setPos(w, h)
395
385 def toggleFullScreen(self): 396 def toggleFullScreen(self):
386 self.setWindowState(self.windowState() ^ Qt.WindowFullScreen) 397 self.setWindowState(self.windowState() ^ Qt.WindowFullScreen)
387 def startConnection(self, port): 398 def startConnection(self, port):
388 self.startedConnection = Connection(port, None) 399 self.startedConnection = Connection(port, None)
389 def sceneMouseMoveEvent(self, event): 400 def sceneMouseMoveEvent(self, event):