Browse Source

Add read support for indexedColors

Some old .xlsx files use indexed colors
master
Debao Zhang 11 years ago
parent
commit
2e2a4ff9bc
  1. 90
      src/xlsx/xlsxstyles.cpp
  2. 7
      src/xlsx/xlsxstyles_p.h

90
src/xlsx/xlsxstyles.cpp

@ -620,7 +620,7 @@ bool Styles::readFonts(XmlStreamReader &reader)
QString colorString = attributes.value(QLatin1String("rgb")).toString(); QString colorString = attributes.value(QLatin1String("rgb")).toString();
font->color = fromARGBString(colorString); font->color = fromARGBString(colorString);
} else if (attributes.hasAttribute(QLatin1String("indexed"))) { } else if (attributes.hasAttribute(QLatin1String("indexed"))) {
font->color = getColorByIndex(attributes.value(QLatin1String("indexed")).toInt());
} else if (attributes.hasAttribute(QLatin1String("theme"))) { } else if (attributes.hasAttribute(QLatin1String("theme"))) {
} }
@ -693,24 +693,28 @@ bool Styles::readFill(XmlStreamReader &reader)
fill->pattern = patternValues.contains(pattern) ? patternValues[pattern] : Format::PatternNone; fill->pattern = patternValues.contains(pattern) ? patternValues[pattern] : Format::PatternNone;
} else if (reader.name() == QLatin1String("fgColor")) { } else if (reader.name() == QLatin1String("fgColor")) {
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
if (attributes.hasAttribute(QLatin1String("rgb"))) { QColor c;
QColor c = fromARGBString(attributes.value(QLatin1String("rgb")).toString()); if (attributes.hasAttribute(QLatin1String("rgb")))
c = fromARGBString(attributes.value(QLatin1String("rgb")).toString());
else if (attributes.hasAttribute(QLatin1String("indexed")))
c = getColorByIndex(attributes.value(QLatin1String("indexed")).toInt());
if (fill->pattern == Format::PatternSolid) if (fill->pattern == Format::PatternSolid)
fill->bgColor = c; fill->bgColor = c;
else else
fill->fgColor = c; fill->fgColor = c;
}
} else if (reader.name() == QLatin1String("bgColor")) { } else if (reader.name() == QLatin1String("bgColor")) {
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
if (attributes.hasAttribute(QLatin1String("rgb"))) { QColor c;
QColor c = fromARGBString(attributes.value(QLatin1String("rgb")).toString()); if (attributes.hasAttribute(QLatin1String("rgb")))
c = fromARGBString(attributes.value(QLatin1String("rgb")).toString());
else if (attributes.hasAttribute(QLatin1String("indexed")))
c = getColorByIndex(attributes.value(QLatin1String("indexed")).toInt());
if (fill->pattern == Format::PatternSolid) if (fill->pattern == Format::PatternSolid)
fill->fgColor = c; fill->fgColor = c;
else else
fill->bgColor = c; fill->bgColor = c;
} }
} }
}
if (reader.tokenType() == QXmlStreamReader::EndElement && reader.name() == QLatin1String("fill")) if (reader.tokenType() == QXmlStreamReader::EndElement && reader.name() == QLatin1String("fill"))
break; break;
@ -814,6 +818,8 @@ bool Styles::readSubBorder(XmlStreamReader &reader, const QString &name, Format:
QString colorString = colorAttrs.value(QLatin1String("rgb")).toString(); QString colorString = colorAttrs.value(QLatin1String("rgb")).toString();
//get color //get color
color = fromARGBString(colorString); color = fromARGBString(colorString);
} else if (colorAttrs.hasAttribute(QLatin1String("indexed"))) {
color = getColorByIndex(colorAttrs.value(QLatin1String("indexed")).toInt());
} }
} }
@ -940,8 +946,53 @@ bool Styles::readCellXfs(XmlStreamReader &reader)
return true; return true;
} }
bool Styles::readColors(XmlStreamReader &reader)
{
Q_ASSERT(reader.name() == QLatin1String("colors"));
while (!(reader.name() == QLatin1String("colors") && reader.tokenType() == QXmlStreamReader::EndElement)) {
reader.readNextStartElement();
if (reader.tokenType() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("indexedColors")) {
readIndexedColors(reader);
} else if (reader.name() == QLatin1String("mruColors")) {
}
}
}
return true;
}
bool Styles::readIndexedColors(XmlStreamReader &reader)
{
Q_ASSERT(reader.name() == QLatin1String("indexedColors"));
while (!(reader.name() == QLatin1String("indexedColors") && reader.tokenType() == QXmlStreamReader::EndElement)) {
reader.readNextStartElement();
if (reader.tokenType() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("rgbColor")) {
QString color = reader.attributes().value(QLatin1String("rgb")).toString();
m_indexedColors.append(fromARGBString(color));
}
}
}
return true;
}
bool Styles::loadFromXmlFile(QIODevice *device) bool Styles::loadFromXmlFile(QIODevice *device)
{ {
{
//Try load colors part first!
XmlStreamReader reader(device);
while(!reader.atEnd()) {
QXmlStreamReader::TokenType token = reader.readNext();
if (token == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("colors")) {
readColors(reader);
}
}
}
device->seek(0);
}
XmlStreamReader reader(device); XmlStreamReader reader(device);
while(!reader.atEnd()) { while(!reader.atEnd()) {
QXmlStreamReader::TokenType token = reader.readNext(); QXmlStreamReader::TokenType token = reader.readNext();
@ -979,4 +1030,29 @@ bool Styles::loadFromXmlData(const QByteArray &data)
return loadFromXmlFile(&buffer); return loadFromXmlFile(&buffer);
} }
QColor Styles::getColorByIndex(int idx)
{
if (m_indexedColors.isEmpty()) {
m_indexedColors<<QColor("#000000") <<QColor("#FFFFFF") <<QColor("#FF0000") <<QColor("#00FF00")
<<QColor("#0000FF") <<QColor("#FFFF00") <<QColor("#FF00FF") <<QColor("#00FFFF")
<<QColor("#000000") <<QColor("#FFFFFF") <<QColor("#FF0000") <<QColor("#00FF00")
<<QColor("#0000FF") <<QColor("#FFFF00") <<QColor("#FF00FF") <<QColor("#00FFFF")
<<QColor("#800000") <<QColor("#008000") <<QColor("#000080") <<QColor("#808000")
<<QColor("#800080") <<QColor("#008080") <<QColor("#C0C0C0") <<QColor("#808080")
<<QColor("#9999FF") <<QColor("#993366") <<QColor("#FFFFCC") <<QColor("#CCFFFF")
<<QColor("#660066") <<QColor("#FF8080") <<QColor("#0066CC") <<QColor("#CCCCFF")
<<QColor("#000080") <<QColor("#FF00FF") <<QColor("#FFFF00") <<QColor("#00FFFF")
<<QColor("#800080") <<QColor("#800000") <<QColor("#008080") <<QColor("#0000FF")
<<QColor("#00CCFF") <<QColor("#CCFFFF") <<QColor("#CCFFCC") <<QColor("#FFFF99")
<<QColor("#99CCFF") <<QColor("#FF99CC") <<QColor("#CC99FF") <<QColor("#FFCC99")
<<QColor("#3366FF") <<QColor("#33CCCC") <<QColor("#99CC00") <<QColor("#FFCC00")
<<QColor("#FF9900") <<QColor("#FF6600") <<QColor("#666699") <<QColor("#969696")
<<QColor("#003366") <<QColor("#339966") <<QColor("#003300") <<QColor("#333300")
<<QColor("#993300") <<QColor("#993366") <<QColor("#333399") <<QColor("#333333");
}
if (idx < 0 || idx >= m_indexedColors.size())
return QColor();
return m_indexedColors[idx];
}
} //namespace QXlsx } //namespace QXlsx

7
src/xlsx/xlsxstyles_p.h

@ -32,6 +32,7 @@
#include <QList> #include <QList>
#include <QMap> #include <QMap>
#include <QStringList> #include <QStringList>
#include <QVector>
class QIODevice; class QIODevice;
class StylesTest; class StylesTest;
@ -81,6 +82,10 @@ private:
bool readBorder(XmlStreamReader &reader); bool readBorder(XmlStreamReader &reader);
bool readSubBorder(XmlStreamReader &reader, const QString &name, Format::BorderStyle &style, QColor &color); bool readSubBorder(XmlStreamReader &reader, const QString &name, Format::BorderStyle &style, QColor &color);
bool readCellXfs(XmlStreamReader &reader); bool readCellXfs(XmlStreamReader &reader);
bool readColors(XmlStreamReader &reader);
bool readIndexedColors(XmlStreamReader &reader);
QColor getColorByIndex(int idx);
QHash<QString, int> m_builtinNumFmtsHash; QHash<QString, int> m_builtinNumFmtsHash;
QMap<int, QSharedPointer<NumberData> > m_customNumFmtIdMap; QMap<int, QSharedPointer<NumberData> > m_customNumFmtIdMap;
@ -93,6 +98,8 @@ private:
QHash<QByteArray, QSharedPointer<FillData> > m_fillsHash; QHash<QByteArray, QSharedPointer<FillData> > m_fillsHash;
QHash<QByteArray, QSharedPointer<BorderData> > m_bordersHash; QHash<QByteArray, QSharedPointer<BorderData> > m_bordersHash;
QVector<QColor> m_indexedColors;
QList<QSharedPointer<Format> > m_createdFormatsList; //All created formats QList<QSharedPointer<Format> > m_createdFormatsList; //All created formats
QList<Format *> m_xf_formatsList; QList<Format *> m_xf_formatsList;

Loading…
Cancel
Save