mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-05-23 22:29:42 +12:00
Clean up
This commit is contained in:
parent
f57e8d2ab1
commit
cec3d2563c
|
@ -1,5 +1,5 @@
|
|||
#!/usr/bin/env python
|
||||
#--!-- coding: utf8 --!--
|
||||
# --!-- coding: utf8 --!--
|
||||
|
||||
import zipfile
|
||||
|
||||
|
@ -12,10 +12,12 @@ from manuskript.functions import iconColor, iconFromColorString
|
|||
|
||||
try:
|
||||
import zlib # Used with zipfile for compression
|
||||
|
||||
compression = zipfile.ZIP_DEFLATED
|
||||
except:
|
||||
compression = zipfile.ZIP_STORED
|
||||
|
||||
|
||||
def saveFilesToZip(files, zipname):
|
||||
"""Saves given files to zipname.
|
||||
files is actually a list of (content, filename)."""
|
||||
|
@ -27,6 +29,7 @@ def saveFilesToZip(files, zipname):
|
|||
|
||||
zf.close()
|
||||
|
||||
|
||||
def loadFilesFromZip(zipname):
|
||||
"""Returns the content of zipfile as a dict of filename:content."""
|
||||
print(zipname)
|
||||
|
@ -36,6 +39,7 @@ def loadFilesFromZip(zipname):
|
|||
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."""
|
||||
|
@ -61,12 +65,13 @@ def saveStandardItemModelXML(mdl, xml=None):
|
|||
data = ET.SubElement(root, "data")
|
||||
saveItem(data, mdl)
|
||||
|
||||
#print(qApp.tr("Saving to {}.").format(xml))
|
||||
# print(qApp.tr("Saving to {}.").format(xml))
|
||||
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)
|
||||
|
||||
|
||||
def saveItem(root, mdl, parent=QModelIndex()):
|
||||
for x in range(mdl.rowCount(parent)):
|
||||
row = ET.SubElement(root, "row")
|
||||
|
@ -83,11 +88,12 @@ def saveItem(root, mdl, parent=QModelIndex()):
|
|||
if mdl.hasChildren(mdl.index(x, y, parent)):
|
||||
saveItem(col, mdl, mdl.index(x, y, parent))
|
||||
|
||||
|
||||
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."""
|
||||
|
||||
#print(qApp.tr("Loading {}... ").format(xml), end="")
|
||||
# print(qApp.tr("Loading {}... ").format(xml), end="")
|
||||
|
||||
if not fromString:
|
||||
try:
|
||||
|
@ -98,9 +104,9 @@ def loadStandardItemModelXML(mdl, xml, fromString=False):
|
|||
else:
|
||||
root = ET.fromstring(xml)
|
||||
|
||||
#root = tree.getroot()
|
||||
# root = tree.getroot()
|
||||
|
||||
#Header
|
||||
# Header
|
||||
hLabels = []
|
||||
vLabels = []
|
||||
for l in root.find("header").find("horizontal").findall("label"):
|
||||
|
@ -108,10 +114,10 @@ def loadStandardItemModelXML(mdl, xml, fromString=False):
|
|||
for l in root.find("header").find("vertical").findall("label"):
|
||||
vLabels.append(l.attrib["text"])
|
||||
|
||||
#print(root.find("header").find("vertical").text)
|
||||
# print(root.find("header").find("vertical").text)
|
||||
|
||||
#mdl.setVerticalHeaderLabels(vLabels)
|
||||
#mdl.setHorizontalHeaderLabels(hLabels)
|
||||
# mdl.setVerticalHeaderLabels(vLabels)
|
||||
# mdl.setHorizontalHeaderLabels(hLabels)
|
||||
|
||||
# Populates with empty items
|
||||
for i in enumerate(vLabels):
|
||||
|
@ -120,12 +126,13 @@ def loadStandardItemModelXML(mdl, xml, fromString=False):
|
|||
row.append(QStandardItem())
|
||||
mdl.appendRow(row)
|
||||
|
||||
#Data
|
||||
# Data
|
||||
data = root.find("data")
|
||||
loadItem(data, mdl)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def loadItem(root, mdl, parent=QModelIndex()):
|
||||
for row in root:
|
||||
r = int(row.attrib["row"])
|
||||
|
@ -137,13 +144,13 @@ def loadItem(root, mdl, parent=QModelIndex()):
|
|||
mdl.itemFromIndex(parent).setChild(r, c, item)
|
||||
|
||||
if col.text:
|
||||
#mdl.setData(mdl.index(r, c, parent), 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"]))
|
||||
# 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.index(r, c, parent))
|
||||
loadItem(col, mdl, mdl.indexFromItem(item))
|
Loading…
Reference in a new issue