diff --git a/src/xlsx/xlsxstyles.cpp b/src/xlsx/xlsxstyles.cpp index cd780eb..03f1444 100755 --- a/src/xlsx/xlsxstyles.cpp +++ b/src/xlsx/xlsxstyles.cpp @@ -42,7 +42,8 @@ namespace QXlsx { */ Styles::Styles(CreateFlag flag) - : AbstractOOXmlFile(flag), m_nextCustomNumFmtId(176), m_emptyFormatAdded(false) + : AbstractOOXmlFile(flag), m_nextCustomNumFmtId(176), m_isIndexedColorsDefault(true) + , m_emptyFormatAdded(false) { //!Fix me. Should the custom num fmt Id starts with 164 or 176 or others?? @@ -307,6 +308,8 @@ void Styles::saveToXmlFile(QIODevice *device) const writer.writeAttribute(QStringLiteral("defaultPivotStyle"), QStringLiteral("PivotStyleLight16")); writer.writeEndElement();//tableStyles + writeColors(writer); + writer.writeEndElement();//styleSheet writer.writeEndDocument(); } @@ -671,6 +674,24 @@ void Styles::writeDxf(QXmlStreamWriter &writer, const Format &format) const writer.writeEndElement();//dxf } +void Styles::writeColors(QXmlStreamWriter &writer) const +{ + if (m_isIndexedColorsDefault) //Don't output the default indexdeColors + return; + + writer.writeStartElement(QStringLiteral("colors")); + + writer.writeStartElement(QStringLiteral("indexedColors")); + foreach(QColor color, m_indexedColors) { + writer.writeEmptyElement(QStringLiteral("rgbColor")); + writer.writeAttribute(QStringLiteral("rgb"), XlsxColor::toARGBString(color)); + } + + writer.writeEndElement();//indexedColors + + writer.writeEndElement();//colors +} + bool Styles::readNumFmts(QXmlStreamReader &reader) { Q_ASSERT(reader.name() == QLatin1String("numFmts")); @@ -1203,6 +1224,7 @@ bool Styles::readColors(QXmlStreamReader &reader) bool Styles::readIndexedColors(QXmlStreamReader &reader) { Q_ASSERT(reader.name() == QLatin1String("indexedColors")); + m_indexedColors.clear(); while (!reader.atEnd() && !(reader.name() == QLatin1String("indexedColors") && reader.tokenType() == QXmlStreamReader::EndElement)) { reader.readNextStartElement(); if (reader.tokenType() == QXmlStreamReader::StartElement) { @@ -1212,6 +1234,8 @@ bool Styles::readIndexedColors(QXmlStreamReader &reader) } } } + if (!m_indexedColors.isEmpty()) + m_isIndexedColorsDefault = false; return true; } @@ -1268,6 +1292,7 @@ QColor Styles::getColorByIndex(int idx) <= m_indexedColors.size()) return QColor(); diff --git a/src/xlsx/xlsxstyles_p.h b/src/xlsx/xlsxstyles_p.h index 9da10cc..7567e23 100755 --- a/src/xlsx/xlsxstyles_p.h +++ b/src/xlsx/xlsxstyles_p.h @@ -96,6 +96,7 @@ private: void writeCellXfs(QXmlStreamWriter &writer) const; void writeDxfs(QXmlStreamWriter &writer) const; void writeDxf(QXmlStreamWriter &writer, const Format &format) const; + void writeColors(QXmlStreamWriter &writer) const; bool readNumFmts(QXmlStreamReader &reader); bool readFonts(QXmlStreamReader &reader); @@ -123,6 +124,7 @@ private: QHash m_bordersHash; QVector m_indexedColors; + bool m_isIndexedColorsDefault; QList m_xf_formatsList; QHash m_xf_formatsHash;