manuskript/manuskript/loadSave.py

157 lines
4.8 KiB
Python
Raw Normal View History

2015-05-31 16:03:07 +12:00
#!/usr/bin/env python
2016-03-04 06:48:53 +13:00
# --!-- coding: utf8 --!--
2015-05-31 16:03:07 +12:00
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:
2016-03-04 06:48:53 +13:00
import zlib # Used with zipfile for compression
2015-06-17 23:25:46 +12:00
compression = zipfile.ZIP_DEFLATED
except:
compression = zipfile.ZIP_STORED
2015-05-31 16:03:07 +12:00
2016-03-04 06:48:53 +13: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)."""
2016-03-04 06:48:53 +13:00
2015-06-17 23:25:46 +12:00
zf = zipfile.ZipFile(zipname, mode="w")
2016-03-04 06:48:53 +13:00
2015-06-17 23:25:46 +12:00
for content, filename in files:
zf.writestr(filename, content, compress_type=compression)
2016-03-04 06:48:53 +13:00
2015-06-17 23:25:46 +12:00
zf.close()
2016-03-04 06:48:53 +13:00
2015-06-17 23:25:46 +12:00
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
2016-03-04 06:48:53 +13:00
2015-06-17 23:25:46 +12:00
def saveStandardItemModelXML(mdl, xml=None):
"""Saves the given QStandardItemModel to XML.
If xml (filename) is given, saves to xml. Otherwise returns as string."""
2016-03-04 06:48:53 +13: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()
2016-03-04 06:48:53 +13:00
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))
2016-03-04 06:48:53 +13:00
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))
2016-03-04 06:48:53 +13:00
2015-05-31 16:03:07 +12:00
# Data
data = ET.SubElement(root, "data")
saveItem(data, mdl)
2016-03-04 06:48:53 +13: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)
2016-03-04 06:48:53 +13:00
def saveItem(root, mdl, parent=QModelIndex()):
for x in range(mdl.rowCount(parent)):
row = ET.SubElement(root, "row")
row.attrib["row"] = str(x)
2016-03-04 06:48:53 +13:00
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))
2016-03-04 06:48:53 +13: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."""
2016-03-04 06:48:53 +13:00
# print(qApp.tr("Loading {}... ").format(xml), end="")
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)
2016-03-04 06:48:53 +13:00
# root = tree.getroot()
# Header
2015-05-31 16:03:07 +12:00
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"])
2016-03-04 06:48:53 +13:00
# print(root.find("header").find("vertical").text)
# mdl.setVerticalHeaderLabels(vLabels)
# mdl.setHorizontalHeaderLabels(hLabels)
2015-07-07 01:00:22 +12:00
# 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)
2016-03-04 06:48:53 +13:00
# Data
data = root.find("data")
loadItem(data, mdl)
2016-03-04 06:48:53 +13:00
2015-07-07 01:00:22 +12:00
return True
2016-03-04 06:48:53 +13:00
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)
2016-03-04 06:48:53 +13:00
if col.text:
# mdl.setData(mdl.index(r, c, parent), col.text)
item.setText(col.text)
2016-03-04 06:48:53 +13:00
if "color" in col.attrib:
2016-03-04 06:48:53 +13:00
# mdl.itemFromIndex(mdl.index(r, c, parent)).setIcon(iconFromColorString(col.attrib["color"]))
item.setIcon(iconFromColorString(col.attrib["color"]))
2016-03-04 06:48:53 +13:00
if len(col) != 0:
2016-03-04 06:48:53 +13:00
# loadItem(col, mdl, mdl.index(r, c, parent))
loadItem(col, mdl, mdl.indexFromItem(item))