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