manuskript/src/loadSave.py

82 lines
2.6 KiB
Python
Raw Normal View History

2015-05-31 16:03:07 +12:00
#!/usr/bin/env python
#--!-- coding: utf8 --!--
2015-06-04 04:40:19 +12:00
from qt import *
from functions import *
2015-05-31 16:03:07 +12:00
from lxml import etree as ET
def saveStandardItemModelXML(mdl, xml):
2015-06-15 22:18:42 +12:00
2015-05-31 16:03:07 +12:00
root = ET.Element("model")
2015-06-15 22:18:42 +12:00
root.attrib["version"] = qApp.applicationVersion()
2015-05-31 16:03:07 +12:00
# Header
header = ET.SubElement(root, "header")
vHeader = ET.SubElement(header, "vertical")
for x in range(mdl.rowCount()):
vH = ET.SubElement(vHeader, "label")
2015-06-08 08:06:57 +12:00
vH.attrib["row"] = str(x)
vH.attrib["text"] = str(mdl.headerData(x, Qt.Vertical))
2015-05-31 16:03:07 +12:00
hHeader = ET.SubElement(header, "horizontal")
for y in range(mdl.columnCount()):
hH = ET.SubElement(hHeader, "label")
2015-06-08 08:06:57 +12:00
hH.attrib["row"] = str(y)
hH.attrib["text"] = str(mdl.headerData(y, Qt.Horizontal))
2015-05-31 16:03:07 +12:00
# Data
data = ET.SubElement(root, "data")
for x in range(mdl.rowCount()):
row = ET.SubElement(data, "row")
2015-06-08 08:06:57 +12:00
row.attrib["row"] = str(x)
2015-05-31 16:03:07 +12:00
for y in range(mdl.columnCount()):
col = ET.SubElement(row, "col")
2015-06-08 08:06:57 +12:00
col.attrib["col"] = str(y)
if mdl.data(mdl.index(x, y), Qt.DecorationRole) != None:
2015-06-11 01:57:44 +12:00
color = iconColor(mdl.data(mdl.index(x, y), Qt.DecorationRole)).name(QColor.HexArgb)
col.attrib["color"] = color if color != "#ff000000" else "#00000000"
2015-06-08 08:06:57 +12:00
if mdl.data(mdl.index(x, y)) != "":
2015-06-04 04:40:19 +12:00
col.text = mdl.data(mdl.index(x, y))
2015-05-31 16:03:07 +12:00
2015-06-17 22:00:03 +12:00
#print(qApp.tr("Saving to {}.").format(xml))
2015-06-04 04:40:19 +12:00
ET.ElementTree(root).write(xml, encoding="UTF-8", xml_declaration=True, pretty_print=True)
2015-05-31 16:03:07 +12:00
def loadStandardItemModelXML(mdl, xml):
2015-06-17 22:00:03 +12:00
#print(qApp.tr("Loading {}... ").format(xml), end="")
2015-05-31 16:03:07 +12:00
try:
tree = ET.parse(xml)
except:
print("Failed.")
return
root = tree.getroot()
#Header
hLabels = []
vLabels = []
for l in root.find("header").find("horizontal").findall("label"):
hLabels.append(l.attrib["text"])
for l in root.find("header").find("vertical").findall("label"):
vLabels.append(l.attrib["text"])
2015-06-03 00:40:48 +12:00
#print(root.find("header").find("vertical").text)
2015-05-31 16:03:07 +12:00
mdl.setVerticalHeaderLabels(vLabels)
mdl.setHorizontalHeaderLabels(hLabels)
#Data
for row in root.find("data").iter("row"):
r = int(row.attrib["row"])
for col in row.iter("col"):
c = int(col.attrib["col"])
if col.text:
mdl.setData(mdl.index(r, c), col.text)
if "color" in col.attrib:
mdl.item(r, c).setIcon(iconFromColorString(col.attrib["color"]))
2015-06-17 22:00:03 +12:00
#print("OK")