manuskript/manuskript/loadSave.py

149 lines
4.9 KiB
Python
Raw Normal View History

2015-05-31 16:03:07 +12:00
#!/usr/bin/env python
#--!-- coding: utf8 --!--
2015-06-17 23:25:46 +12:00
import zipfile
2016-02-07 00:34:22 +13:00
from PyQt5.QtCore import QModelIndex, Qt
from PyQt5.QtGui import QColor, QStandardItem
from PyQt5.QtWidgets import qApp
from lxml import etree as ET
from manuskript.functions import iconColor, iconFromColorString
2015-06-17 23:25:46 +12:00
try:
import zlib # Used with zipfile for compression
compression = zipfile.ZIP_DEFLATED
except:
compression = zipfile.ZIP_STORED
2015-05-31 16:03:07 +12:00
2015-06-17 23:25:46 +12:00
def saveFilesToZip(files, zipname):
"""Saves given files to zipname.
files is actually a list of (content, filename)."""
zf = zipfile.ZipFile(zipname, mode="w")
for content, filename in files:
zf.writestr(filename, content, compress_type=compression)
zf.close()
def loadFilesFromZip(zipname):
"""Returns the content of zipfile as a dict of filename:content."""
2015-06-23 10:19:40 +12:00
print(zipname)
2015-06-17 23:25:46 +12:00
zf = zipfile.ZipFile(zipname)
files = {}
for f in zf.namelist():
files[f] = zf.read(f)
return files
def saveStandardItemModelXML(mdl, xml=None):
"""Saves the given QStandardItemModel to XML.
If xml (filename) is given, saves to xml. Otherwise returns as string."""
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")
saveItem(data, mdl)
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-17 23:25:46 +12:00
if xml:
ET.ElementTree(root).write(xml, encoding="UTF-8", xml_declaration=True, pretty_print=True)
else:
return ET.tostring(root, encoding="UTF-8", xml_declaration=True, pretty_print=True)
2015-05-31 16:03:07 +12:00
def saveItem(root, mdl, parent=QModelIndex()):
for x in range(mdl.rowCount(parent)):
row = ET.SubElement(root, "row")
row.attrib["row"] = str(x)
for y in range(mdl.columnCount(parent)):
col = ET.SubElement(row, "col")
col.attrib["col"] = str(y)
if mdl.data(mdl.index(x, y, parent), Qt.DecorationRole) != None:
color = iconColor(mdl.data(mdl.index(x, y, parent), Qt.DecorationRole)).name(QColor.HexArgb)
col.attrib["color"] = color if color != "#ff000000" else "#00000000"
if mdl.data(mdl.index(x, y, parent)) != "":
col.text = mdl.data(mdl.index(x, y, parent))
if mdl.hasChildren(mdl.index(x, y, parent)):
saveItem(col, mdl, mdl.index(x, y, parent))
2015-05-31 16:03:07 +12:00
2015-06-17 23:25:46 +12:00
def loadStandardItemModelXML(mdl, xml, fromString=False):
"""Load data to a QStandardItemModel mdl from xml.
By default xml is a filename. If fromString=True, xml is a string containg the data."""
2015-05-31 16:03:07 +12:00
2015-06-17 22:00:03 +12:00
#print(qApp.tr("Loading {}... ").format(xml), end="")
2015-05-31 16:03:07 +12:00
2015-06-17 23:25:46 +12:00
if not fromString:
try:
tree = ET.parse(xml)
except:
print("Failed.")
return
else:
root = ET.fromstring(xml)
2015-05-31 16:03:07 +12:00
2015-06-17 23:25:46 +12:00
#root = tree.getroot()
2015-05-31 16:03:07 +12:00
#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-07-07 01:00:22 +12:00
#mdl.setVerticalHeaderLabels(vLabels)
#mdl.setHorizontalHeaderLabels(hLabels)
# Populates with empty items
for i in enumerate(vLabels):
2015-07-07 01:00:22 +12:00
row = []
for r in enumerate(hLabels):
2015-07-07 01:00:22 +12:00
row.append(QStandardItem())
mdl.appendRow(row)
2015-05-31 16:03:07 +12:00
#Data
data = root.find("data")
loadItem(data, mdl)
2015-07-07 01:00:22 +12:00
return True
def loadItem(root, mdl, parent=QModelIndex()):
for row in root:
2015-05-31 16:03:07 +12:00
r = int(row.attrib["row"])
for col in row:
2015-05-31 16:03:07 +12:00
c = int(col.attrib["col"])
item = mdl.itemFromIndex(mdl.index(r, c, parent))
if not item:
item = QStandardItem()
mdl.itemFromIndex(parent).setChild(r, c, item)
2015-05-31 16:03:07 +12:00
if col.text:
#mdl.setData(mdl.index(r, c, parent), col.text)
item.setText(col.text)
if "color" in col.attrib:
#mdl.itemFromIndex(mdl.index(r, c, parent)).setIcon(iconFromColorString(col.attrib["color"]))
item.setIcon(iconFromColorString(col.attrib["color"]))
if len(col) != 0:
#loadItem(col, mdl, mdl.index(r, c, parent))
loadItem(col, mdl, mdl.indexFromItem(item))