Browse Source

Bugfix: properly parse fillpattern colors and implement optional boolean fields.

Conflicts:
	src/xlsx/xlsxutility.cpp
	src/xlsx/xlsxutility_p.h
master
eho 11 years ago
committed by Debao Zhang
parent
commit
b74e49d097
  1. 94
      src/xlsx/xlsxstyles.cpp
  2. 9
      src/xlsx/xlsxutility.cpp
  3. 2
      src/xlsx/xlsxutility_p.h

94
src/xlsx/xlsxstyles.cpp

@ -886,21 +886,29 @@ bool Styles::readFill(QXmlStreamReader &reader, Format &fill)
if (attributes.hasAttribute(QLatin1String("patternType"))) {
QString pattern = attributes.value(QLatin1String("patternType")).toString();
fill.setFillPattern(patternValues.contains(pattern) ? patternValues[pattern] : Format::PatternNone);
//parse foreground and background colors if they exist
while (!reader.atEnd() && !(reader.tokenType() == QXmlStreamReader::EndElement && reader.name() == QLatin1String("patternFill"))) {
reader.readNextStartElement();
if (reader.tokenType() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("fgColor")) {
XlsxColor c;
c.loadFromXml(reader);
if (fill.fillPattern() == Format::PatternSolid)
fill.setProperty(FormatPrivate::P_Fill_BgColor, c);
else
fill.setProperty(FormatPrivate::P_Fill_FgColor, c);
} else if (reader.name() == QLatin1String("bgColor")) {
XlsxColor c;
c.loadFromXml(reader);
if (fill.fillPattern() == Format::PatternSolid)
fill.setProperty(FormatPrivate::P_Fill_FgColor, c);
else
fill.setProperty(FormatPrivate::P_Fill_BgColor, c);
}
}
}
}
} else if (reader.name() == QLatin1String("fgColor")) {
XlsxColor c;
c.loadFromXml(reader);
if (fill.fillPattern() == Format::PatternSolid)
fill.setProperty(FormatPrivate::P_Fill_BgColor, c);
else
fill.setProperty(FormatPrivate::P_Fill_FgColor, c);
} else if (reader.name() == QLatin1String("bgColor")) {
XlsxColor c;
c.loadFromXml(reader);
if (fill.fillPattern() == Format::PatternSolid)
fill.setProperty(FormatPrivate::P_Fill_FgColor, c);
else
fill.setProperty(FormatPrivate::P_Fill_BgColor, c);
}
}
}
@ -1042,7 +1050,7 @@ bool Styles::readCellXfs(QXmlStreamReader &reader)
bool hasCount = attributes.hasAttribute(QLatin1String("count"));
int count = hasCount ? attributes.value(QLatin1String("count")).toString().toInt() : -1;
while (!reader.atEnd() && !(reader.tokenType() == QXmlStreamReader::EndElement
&& reader.name() == QLatin1String("cellXfs"))) {
&& reader.name() == QLatin1String("cellXfs"))) {
reader.readNextStartElement();
if (reader.tokenType() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("xf")) {
@ -1054,54 +1062,67 @@ bool Styles::readCellXfs(QXmlStreamReader &reader)
// for (int i=0; i<xfAttrs.size(); ++i)
// qDebug()<<"... "<<i<<" "<<xfAttrs[i].name()<<xfAttrs[i].value();
if (xfAttrs.hasAttribute(QLatin1String("applyNumberFormat"))) {
if (xfAttrs.hasAttribute(QLatin1String("numFmtId"))) {
int numFmtIndex = xfAttrs.value(QLatin1String("numFmtId")).toString().toInt();
if (!m_customNumFmtIdMap.contains(numFmtIndex))
format.setNumberFormatIndex(numFmtIndex);
else
format.setNumberFormat(numFmtIndex, m_customNumFmtIdMap[numFmtIndex]->formatString);
bool apply = parseXsdBoolean(xfAttrs.value(QLatin1String("applyNumberFormat")).toString());
if(apply) {
if (!m_customNumFmtIdMap.contains(numFmtIndex))
format.setNumberFormatIndex(numFmtIndex);
else
format.setNumberFormat(numFmtIndex, m_customNumFmtIdMap[numFmtIndex]->formatString);
}
}
if (xfAttrs.hasAttribute(QLatin1String("applyFont"))) {
if (xfAttrs.hasAttribute(QLatin1String("fontId"))) {
int fontIndex = xfAttrs.value(QLatin1String("fontId")).toString().toInt();
if (fontIndex >= m_fontsList.size()) {
qDebug("Error read styles.xml, cellXfs fontId");
} else {
Format fontFormat = m_fontsList[fontIndex];
for (int i=FormatPrivate::P_Font_STARTID; i<FormatPrivate::P_Font_ENDID; ++i) {
if (fontFormat.hasProperty(i))
format.setProperty(i, fontFormat.property(i));
bool apply = parseXsdBoolean(xfAttrs.value(QLatin1String("applyFont")).toString());
if(apply) {
Format fontFormat = m_fontsList[fontIndex];
for (int i=FormatPrivate::P_Font_STARTID; i<FormatPrivate::P_Font_ENDID; ++i) {
if (fontFormat.hasProperty(i))
format.setProperty(i, fontFormat.property(i));
}
}
}
}
if (xfAttrs.hasAttribute(QLatin1String("applyFill"))) {
if (xfAttrs.hasAttribute(QLatin1String("fillId"))) {
int id = xfAttrs.value(QLatin1String("fillId")).toString().toInt();
if (id >= m_fillsList.size()) {
qDebug("Error read styles.xml, cellXfs fillId");
} else {
Format fillFormat = m_fillsList[id];
for (int i=FormatPrivate::P_Fill_STARTID; i<FormatPrivate::P_Fill_ENDID; ++i) {
if (fillFormat.hasProperty(i))
format.setProperty(i, fillFormat.property(i));
bool apply = parseXsdBoolean(xfAttrs.value(QLatin1String("applyFill")).toString());
if(apply) {
Format fillFormat = m_fillsList[id];
for (int i=FormatPrivate::P_Fill_STARTID; i<FormatPrivate::P_Fill_ENDID; ++i) {
if (fillFormat.hasProperty(i))
format.setProperty(i, fillFormat.property(i));
}
}
}
}
if (xfAttrs.hasAttribute(QLatin1String("applyBorder"))) {
if (xfAttrs.hasAttribute(QLatin1String("borderId"))) {
int id = xfAttrs.value(QLatin1String("borderId")).toString().toInt();
if (id >= m_bordersList.size()) {
qDebug("Error read styles.xml, cellXfs borderId");
} else {
Format borderFormat = m_bordersList[id];
for (int i=FormatPrivate::P_Border_STARTID; i<FormatPrivate::P_Border_ENDID; ++i) {
if (borderFormat.hasProperty(i))
format.setProperty(i, borderFormat.property(i));
bool apply = parseXsdBoolean(xfAttrs.value(QLatin1String("applyBorder")).toString());
if(apply) {
Format borderFormat = m_bordersList[id];
for (int i=FormatPrivate::P_Border_STARTID; i<FormatPrivate::P_Border_ENDID; ++i) {
if (borderFormat.hasProperty(i))
format.setProperty(i, borderFormat.property(i));
}
}
}
}
if (xfAttrs.hasAttribute(QLatin1String("applyAlignment"))) {
bool apply = parseXsdBoolean(xfAttrs.value(QLatin1String("applyAlignment")).toString());
if(apply) {
reader.readNextStartElement();
if (reader.name() == QLatin1String("alignment")) {
QXmlStreamAttributes alignAttrs = reader.attributes();
@ -1149,6 +1170,7 @@ bool Styles::readCellXfs(QXmlStreamReader &reader)
if (alignAttrs.hasAttribute(QLatin1String("shrinkToFit")))
format.setShrinkToFit(true);
}
}

9
src/xlsx/xlsxutility.cpp

@ -35,6 +35,15 @@
namespace QXlsx {
bool parseXsdBoolean(const QString &value, bool defaultValue)
{
if (value == QLatin1String("1") || value == QLatin1String("true"))
return true;
if (value == QLatin1String("0") || value == QLatin1String("false"))
return false;
return defaultValue;
}
QStringList splitPath(const QString &path)
{
int idx = path.lastIndexOf(QLatin1Char('/'));

2
src/xlsx/xlsxutility_p.h

@ -46,6 +46,8 @@ class QTime;
namespace QXlsx {
XLSX_AUTOTEST_EXPORT bool parseXsdBoolean(const QString &value, bool defaultValue=false);
XLSX_AUTOTEST_EXPORT QStringList splitPath(const QString &path);
XLSX_AUTOTEST_EXPORT QString getRelFilePath(const QString &filePath);

Loading…
Cancel
Save