changeset 47:c4ec95a588ea

Fixed save to xml
author windel
date Tue, 27 Mar 2012 07:29:59 +0200
parents 7964065400b7
children a309982ab4d5
files applications/lab/diagrameditor.py
diffstat 1 files changed, 37 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/applications/lab/diagrameditor.py	Thu Mar 15 20:30:13 2012 +0100
+++ b/applications/lab/diagrameditor.py	Tue Mar 27 07:29:59 2012 +0200
@@ -3,7 +3,8 @@
 from PyQt4.QtGui import *
 from PyQt4.QtCore import *
 import sys
-from elementtree.SimpleXMLWriter import XMLWriter
+#from elementtree.SimpleXMLWriter import XMLWriter
+import xml.dom.minidom as md
 
 """
   This script implements a basic diagram editor.
@@ -341,47 +342,57 @@
       fullScreenShortcut.activated.connect(self.toggleFullScreen)
       saveShortcut = QShortcut(QKeySequence(QKeySequence.Save), self)
       saveShortcut.activated.connect(self.save)
-      self.loadDiagram('diagram.txt')
+      self.loadDiagram('diagram2.usd')
 
    def save(self):
-      self.saveDiagram2('diagram2.txt')
-   def saveDiagram2(self, filename):
-      items = self.diagramScene.items()
-      blocks = [item for item in items if type(item) is BlockItem]
-      with open(filename, 'w') as f:
-         w = XMLWriter(f)
-         model = w.start('model')
-         for block in blocks:
-            w.element("block")
+      self.saveDiagram('diagram2.usd')
 
-         w.close(model)
    def saveDiagram(self, filename):
       items = self.diagramScene.items()
       blocks = [item for item in items if type(item) is BlockItem]
+
+      doc = md.Document()
+      modelElement = doc.createElement('system')
+      doc.appendChild(modelElement)
+      for block in blocks:
+         blockElement = doc.createElement("block")
+         x, y = block.scenePos().x(), block.scenePos().y()
+         rect = block.rect()
+         w, h = rect.width(), rect.height()
+         blockElement.setAttribute("name", block.label.toPlainText())
+         blockElement.setAttribute("x", str(x))
+         blockElement.setAttribute("y", str(y))
+         blockElement.setAttribute("width", str(w))
+         blockElement.setAttribute("height", str(h))
+         modelElement.appendChild(blockElement)
       with open(filename, 'w') as f:
-         for block in blocks:
-            rect = block.rect()
-            x, y, w, h = block.scenePos().x(), block.scenePos().y(), rect.width(), rect.height()
-            f.write('B({0};{1};{2};{3}) '.format(x,y,w,h)) # (x;y;w;h)
+         f.write(doc.toprettyxml())
          
    def loadDiagram(self, filename):
       try:
-         with open(filename, 'r') as f:
-            txt = f.read()
+         doc = md.parse(filename)
       except IOError as e:
          print('{0} not found'.format(filename))
          return
-      parts = txt.split(' ')
-      for part in parts:
-         if len(part) < 1:
-            continue
-         if part[0] == 'B':
-            part = part[2:-1]
-            x,y,w,h = [float(val) for val in part.split(';')]
-            block = BlockItem('Loaded') # TODO: store name.
+      sysElements = doc.getElementsByTagName('system')
+      print('systems:',sysElements)
+      blockElements = doc.getElementsByTagName('block')
+      print('blocks:', blockElements)
+      for sysElement in sysElements:
+         print('system:', sysElement)
+         blockElements = sysElement.getElementsByTagName('block')
+         print('blocks:', blockElements)
+         for blockElement in blockElements:
+            x = float(blockElement.getAttribute('x'))
+            y = float(blockElement.getAttribute('y'))
+            w = float(blockElement.getAttribute('width'))
+            h = float(blockElement.getAttribute('height'))
+            name = blockElement.getAttribute('name')
+            block = BlockItem(name)
             self.diagramScene.addItem(block)
             block.setPos(x, y)
             block.sizer.setPos(w, h)
+
    def toggleFullScreen(self):
       self.setWindowState(self.windowState() ^ Qt.WindowFullScreen)
    def startConnection(self, port):