From 88aaad9e024e3be383ec0eb2c3fb5c28d71c5b7f Mon Sep 17 00:00:00 2001 From: Debao Zhang Date: Thu, 28 Nov 2013 22:54:05 +0800 Subject: [PATCH] Add new private class XlsxColor --- src/xlsx/qtxlsx.pri | 6 +- src/xlsx/xlsxcolor.cpp | 172 +++++++++++++++++++++++++++++++++ src/xlsx/xlsxcolor_p.h | 85 ++++++++++++++++ src/xlsx/xlsxformat.cpp | 28 +++--- src/xlsx/xlsxformat_p.h | 8 -- src/xlsx/xlsxsharedstrings.cpp | 27 ++---- src/xlsx/xlsxstyles.cpp | 172 ++++++++++----------------------- src/xlsx/xlsxstyles_p.h | 9 +- 8 files changed, 336 insertions(+), 171 deletions(-) create mode 100644 src/xlsx/xlsxcolor.cpp create mode 100644 src/xlsx/xlsxcolor_p.h diff --git a/src/xlsx/qtxlsx.pri b/src/xlsx/qtxlsx.pri index 51aa53f..43437ae 100755 --- a/src/xlsx/qtxlsx.pri +++ b/src/xlsx/qtxlsx.pri @@ -33,7 +33,8 @@ HEADERS += $$PWD/xlsxdocpropscore_p.h \ $$PWD/xlsxrichstring_p.h \ $$PWD/xlsxrichstring.h \ $$PWD/xlsxconditionalformatting.h \ - $$PWD/xlsxconditionalformatting_p.h + $$PWD/xlsxconditionalformatting_p.h \ + $$PWD/xlsxcolor_p.h SOURCES += $$PWD/xlsxdocpropscore.cpp \ $$PWD/xlsxdocpropsapp.cpp \ @@ -55,4 +56,5 @@ SOURCES += $$PWD/xlsxdocpropscore.cpp \ $$PWD/xlsxdatavalidation.cpp \ $$PWD/xlsxcellrange.cpp \ $$PWD/xlsxrichstring.cpp \ - $$PWD/xlsxconditionalformatting.cpp + $$PWD/xlsxconditionalformatting.cpp \ + $$PWD/xlsxcolor.cpp diff --git a/src/xlsx/xlsxcolor.cpp b/src/xlsx/xlsxcolor.cpp new file mode 100644 index 0000000..96d1a99 --- /dev/null +++ b/src/xlsx/xlsxcolor.cpp @@ -0,0 +1,172 @@ +#include "xlsxcolor_p.h" +#include "xlsxstyles_p.h" +#include "xlsxutility_p.h" + +#include +#include + +namespace QXlsx { + + +XlsxColor::XlsxColor(const QColor &color) + :val(color) +{ +} + +XlsxColor::XlsxColor(const QString &theme, const QString &tint) + :val(QStringList()<() && val.value().isValid()) + return true; + return false; +} + +bool XlsxColor::isIndexedColor() const +{ + return val.userType() == QMetaType::Int; +} + +bool XlsxColor::isThemeColor() const +{ + return val.userType() == QMetaType::QStringList; +} + +bool XlsxColor::isInvalid() const +{ + if (val.userType() == qMetaTypeId() && !val.value().isValid()) + return true; + return false; +} + +QColor XlsxColor::rgbColor() const +{ + if (isRgbColor()) + return val.value(); + return QColor(); +} + +int XlsxColor::indexedColor() const +{ + if (isIndexedColor()) + return val.toInt(); + return -1; +} + +QStringList XlsxColor::themeColor() const +{ + if (isThemeColor()) + return val.toStringList(); + return QStringList(); +} + +bool XlsxColor::saveToXml(QXmlStreamWriter &writer, const QString &node) const +{ + if (!node.isEmpty()) + writer.writeEmptyElement(node); //color, bgColor, fgColor + else + writer.writeEmptyElement(QStringLiteral("color")); + + if (val.userType() == qMetaTypeId()) { + QColor color = val.value(); + if (color.isValid()) + writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.name().mid(1));//remove # + else + writer.writeAttribute(QStringLiteral("auto"), QStringLiteral("1")); + } else if (val.userType() == QMetaType::QStringList) { + QStringList themes = val.toStringList(); + writer.writeAttribute(QStringLiteral("theme"), themes[0]); + if (!themes[1].isEmpty()) + writer.writeAttribute(QStringLiteral("tint"), themes[1]); + } else if (val.userType() == QMetaType::Int) { + writer.writeAttribute(QStringLiteral("indexed"), val.toString()); + } else { + return false; + } + + return true; +} + +bool XlsxColor::loadFromXml(QXmlStreamReader &reader, Styles *styles) +{ + QXmlStreamAttributes attributes = reader.attributes(); + + if (attributes.hasAttribute(QLatin1String("rgb"))) { + QString colorString = attributes.value(QLatin1String("rgb")).toString(); + val.setValue(fromARGBString(colorString)); + } else if (attributes.hasAttribute(QLatin1String("indexed"))) { + int index = attributes.value(QLatin1String("indexed")).toString().toInt(); + if (styles) { + //Convert to rgb color is possible + QColor c = styles->getColorByIndex(index); + if (c.isValid()) + val.setValue(c); + } else { + val.setValue(index); + } + } else if (attributes.hasAttribute(QLatin1String("theme"))) { + QString theme = attributes.value(QLatin1String("theme")).toString(); + QString tint = attributes.value(QLatin1String("tint")).toString(); + val.setValue(QStringList()<(), this); +} + +#if !defined(QT_NO_DATASTREAM) +QDataStream &operator<<(QDataStream &s, const XlsxColor &color) +{ + if (color.isInvalid()) + s<<0; + else if (color.isRgbColor()) + s<<1<>(QDataStream &s, XlsxColor &color) +{ + int marker(4); + s>>marker; + if (marker == 0) { + color = XlsxColor(); + } else if (marker == 1) { + QColor c; + s>>c; + color = XlsxColor(c); + } else if (marker == 2) { + int indexed; + s>>indexed; + color = XlsxColor(indexed); + } else if (marker == 3) { + QStringList list; + s>>list; + color = XlsxColor(list[0], list[1]); + } + + return s; +} + +#endif + +} // namespace QXlsx diff --git a/src/xlsx/xlsxcolor_p.h b/src/xlsx/xlsxcolor_p.h new file mode 100644 index 0000000..9c43f8a --- /dev/null +++ b/src/xlsx/xlsxcolor_p.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** Copyright (c) 2013 Debao Zhang +** All right reserved. +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +****************************************************************************/ + +#ifndef QXLSX_XLSXCOLOR_P_H +#define QXLSX_XLSXCOLOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt Xlsx API. It exists for the convenience +// of the Qt Xlsx. This header file may change from +// version to version without notice, or even be removed. +// +// We mean it. +// + +#include "xlsxglobal.h" +#include +#include + +class QXmlStreamWriter; +class QXmlStreamReader; + +namespace QXlsx { + +class Styles; + +class XlsxColor +{ +public: + explicit XlsxColor(const QColor &color = QColor()); + explicit XlsxColor(const QString &theme, const QString &tint=QString()); + explicit XlsxColor (int index); + + bool isThemeColor() const; + bool isIndexedColor() const; + bool isRgbColor() const; + bool isInvalid() const; + + QColor rgbColor() const; + int indexedColor() const; + QStringList themeColor() const; + + operator QVariant() const; + + bool saveToXml(QXmlStreamWriter &writer, const QString &node=QString()) const; + bool loadFromXml(QXmlStreamReader &reader, Styles *styles=0); + +private: + QVariant val; +}; + +#if !defined(QT_NO_DATASTREAM) +QDataStream &operator<<(QDataStream &, const XlsxColor &); +QDataStream &operator>>(QDataStream &, XlsxColor &); +#endif + +} // namespace QXlsx + +Q_DECLARE_METATYPE(QXlsx::XlsxColor) + +#endif // QXLSX_XLSXCOLOR_P_H diff --git a/src/xlsx/xlsxformat.cpp b/src/xlsx/xlsxformat.cpp index e235fa2..48711b9 100755 --- a/src/xlsx/xlsxformat.cpp +++ b/src/xlsx/xlsxformat.cpp @@ -24,6 +24,7 @@ ****************************************************************************/ #include "xlsxformat.h" #include "xlsxformat_p.h" +#include "xlsxcolor_p.h" #include #include #include @@ -179,6 +180,7 @@ FormatPrivate::~FormatPrivate() */ Format::Format() { + qRegisterMetaTypeStreamOperators("XlsxColor"); //The d pointer is initialized with a null pointer } @@ -364,12 +366,6 @@ QColor Format::fontColor() const { if (hasProperty(FormatPrivate::P_Font_Color)) return colorProperty(FormatPrivate::P_Font_Color); - - if (hasProperty(FormatPrivate::P_Font_ThemeColor)) { - //!Todo, get the real color from the theme{1}.xml file - //The same is ture for border and fill colord - return QColor(); - } return QColor(); } @@ -378,7 +374,7 @@ QColor Format::fontColor() const */ void Format::setFontColor(const QColor &color) { - setProperty(FormatPrivate::P_Font_Color, color); + setProperty(FormatPrivate::P_Font_Color, XlsxColor(color)); } /*! @@ -724,7 +720,7 @@ QColor Format::leftBorderColor() const */ void Format::setLeftBorderColor(const QColor &color) { - setProperty(FormatPrivate::P_Border_LeftColor, color); + setProperty(FormatPrivate::P_Border_LeftColor, XlsxColor(color)); } /*! @@ -756,7 +752,7 @@ QColor Format::rightBorderColor() const */ void Format::setRightBorderColor(const QColor &color) { - setProperty(FormatPrivate::P_Border_RightColor, color); + setProperty(FormatPrivate::P_Border_RightColor, XlsxColor(color)); } /*! @@ -788,7 +784,7 @@ QColor Format::topBorderColor() const */ void Format::setTopBorderColor(const QColor &color) { - setProperty(FormatPrivate::P_Border_TopColor, color); + setProperty(FormatPrivate::P_Border_TopColor, XlsxColor(color)); } /*! @@ -820,7 +816,7 @@ QColor Format::bottomBorderColor() const */ void Format::setBottomBorderColor(const QColor &color) { - setProperty(FormatPrivate::P_Border_BottomColor, color); + setProperty(FormatPrivate::P_Border_BottomColor, XlsxColor(color)); } /*! @@ -868,7 +864,7 @@ QColor Format::diagonalBorderColor() const */ void Format::setDiagonalBorderColor(const QColor &color) { - setProperty(FormatPrivate::P_Border_DiagonalColor, color); + setProperty(FormatPrivate::P_Border_DiagonalColor, XlsxColor(color)); } /*! @@ -968,7 +964,7 @@ void Format::setPatternForegroundColor(const QColor &color) { if (color.isValid() && !hasProperty(FormatPrivate::P_Fill_Pattern)) setFillPattern(PatternSolid); - setProperty(FormatPrivate::P_Fill_FgColor, color); + setProperty(FormatPrivate::P_Fill_FgColor, XlsxColor(color)); } /*! @@ -986,7 +982,7 @@ void Format::setPatternBackgroundColor(const QColor &color) { if (color.isValid() && !hasProperty(FormatPrivate::P_Fill_Pattern)) setFillPattern(PatternSolid); - setProperty(FormatPrivate::P_Fill_BgColor, color); + setProperty(FormatPrivate::P_Fill_BgColor, XlsxColor(color)); } /*! @@ -1363,9 +1359,9 @@ QColor Format::colorProperty(int propertyId) const return QColor(); const QVariant prop = d->property[propertyId]; - if (prop.userType() != qMetaTypeId()) + if (prop.userType() != qMetaTypeId()) return QColor(); - return qvariant_cast(prop); + return qvariant_cast(prop).rgbColor(); } #ifndef QT_NO_DEBUG_STREAM diff --git a/src/xlsx/xlsxformat_p.h b/src/xlsx/xlsxformat_p.h index ccb929a..c4b9468 100644 --- a/src/xlsx/xlsxformat_p.h +++ b/src/xlsx/xlsxformat_p.h @@ -69,7 +69,6 @@ public: P_Font_Italic, P_Font_StrikeOut, P_Font_Color, - P_Font_ThemeColor, P_Font_Bold, P_Font_Script, P_Font_Underline, @@ -96,11 +95,6 @@ public: P_Border_BottomColor, P_Border_DiagonalColor, P_Border_DiagonalType, - P_Border_ThemeLeftColor, - P_Border_ThemeRightColor, - P_Border_ThemeTopColor, - P_Border_ThemeBottomColor, - P_Border_ThemeDiagonalColor, P_Border_ENDID, //fill @@ -108,8 +102,6 @@ public: P_Fill_Pattern = P_Fill_STARTID, P_Fill_BgColor, P_Fill_FgColor, - P_Fill_BgThemeColor, - P_Fill_FgThemeColor, P_Fill_ENDID, //alignment diff --git a/src/xlsx/xlsxsharedstrings.cpp b/src/xlsx/xlsxsharedstrings.cpp index e59987c..1999641 100755 --- a/src/xlsx/xlsxsharedstrings.cpp +++ b/src/xlsx/xlsxsharedstrings.cpp @@ -26,6 +26,7 @@ #include "xlsxsharedstrings_p.h" #include "xlsxutility_p.h" #include "xlsxformat_p.h" +#include "xlsxcolor_p.h" #include #include #include @@ -168,16 +169,9 @@ void SharedStrings::writeRichStringPart_rPr(QXmlStreamWriter &writer, const Form writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontSize())); } - if (format.fontColor().isValid()) { - writer.writeEmptyElement(QStringLiteral("color")); - QString color = format.fontColor().name(); - writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.mid(1));//remove # - } else if (format.hasProperty(FormatPrivate::P_Font_ThemeColor)) { - writer.writeEmptyElement(QStringLiteral("color")); - QStringList themes = format.stringProperty(FormatPrivate::P_Font_ThemeColor).split(QLatin1Char(':')); - writer.writeAttribute(QStringLiteral("theme"), themes[0]); - if (!themes[1].isEmpty()) - writer.writeAttribute(QStringLiteral("tint"), themes[1]); + if (format.hasProperty(FormatPrivate::P_Font_Color)) { + XlsxColor color = format.property(FormatPrivate::P_Font_Color).value(); + color.saveToXml(writer); } if (!format.fontName().isEmpty()) { @@ -332,16 +326,9 @@ Format SharedStrings::readRichStringPart_rPr(QXmlStreamReader &reader) } else if (reader.name() == QLatin1String("extend")) { format.setProperty(FormatPrivate::P_Font_Extend, attributes.value(QLatin1String("val")).toString().toInt()); } else if (reader.name() == QLatin1String("color")) { - if (attributes.hasAttribute(QLatin1String("rgb"))) { - QString colorString = attributes.value(QLatin1String("rgb")).toString(); - format.setFontColor(fromARGBString(colorString)); - } else if (attributes.hasAttribute(QLatin1String("indexed"))) { -// color = getColorByIndex(attributes.value(QLatin1String("indexed")).toString().toInt()); - } else if (attributes.hasAttribute(QLatin1String("theme"))) { - QString theme = attributes.value(QLatin1String("theme")).toString(); - QString tint = attributes.value(QLatin1String("tint")).toString(); - format.setProperty(FormatPrivate::P_Font_ThemeColor, QString(theme + QLatin1Char(':') + tint)); - } + XlsxColor color; + color.loadFromXml(reader); + format.setProperty(FormatPrivate::P_Font_Color, color); } else if (reader.name() == QLatin1String("sz")) { format.setFontSize(attributes.value(QLatin1String("val")).toString().toInt()); } else if (reader.name() == QLatin1String("u")) { diff --git a/src/xlsx/xlsxstyles.cpp b/src/xlsx/xlsxstyles.cpp index 196bbac..2024ae0 100755 --- a/src/xlsx/xlsxstyles.cpp +++ b/src/xlsx/xlsxstyles.cpp @@ -25,6 +25,7 @@ #include "xlsxstyles_p.h" #include "xlsxformat_p.h" #include "xlsxutility_p.h" +#include "xlsxcolor_p.h" #include #include #include @@ -403,16 +404,9 @@ void Styles::writeFont(QXmlStreamWriter &writer, const Format &format, bool isDx writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontSize())); } - if (format.fontColor().isValid()) { - writer.writeEmptyElement(QStringLiteral("color")); - QString color = format.fontColor().name(); - writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.mid(1));//remove # - } else if (format.hasProperty(FormatPrivate::P_Font_ThemeColor)) { - writer.writeEmptyElement(QStringLiteral("color")); - QStringList themes = format.stringProperty(FormatPrivate::P_Font_ThemeColor).split(QLatin1Char(':')); - writer.writeAttribute(QStringLiteral("theme"), themes[0]); - if (!themes[1].isEmpty()) - writer.writeAttribute(QStringLiteral("tint"), themes[1]); + if (format.hasProperty(FormatPrivate::P_Font_Color)) { + XlsxColor color = format.property(FormatPrivate::P_Font_Color).value(); + color.saveToXml(writer); } if (!isDxf) { @@ -473,25 +467,20 @@ void Styles::writeFill(QXmlStreamWriter &writer, const Format &fill, bool /*isDx writer.writeStartElement(QStringLiteral("patternFill")); writer.writeAttribute(QStringLiteral("patternType"), patternStrings[fill.fillPattern()]); // For a solid fill, Excel reverses the role of foreground and background colours - if (fill.patternForegroundColor().isValid()) { - writer.writeEmptyElement(fill.fillPattern() == Format::PatternSolid ? QStringLiteral("bgColor") : QStringLiteral("fgColor")); - writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill.patternForegroundColor().name().mid(1)); - } else if (!fill.stringProperty(FormatPrivate::P_Fill_FgThemeColor).isEmpty()) { - writer.writeEmptyElement(QStringLiteral("fgColor")); - QStringList themes = fill.stringProperty(FormatPrivate::P_Fill_FgThemeColor).split(QLatin1Char(':')); - writer.writeAttribute(QStringLiteral("theme"), themes[0]); - if (!themes[1].isEmpty()) - writer.writeAttribute(QStringLiteral("tint"), themes[1]); + if (fill.hasProperty(FormatPrivate::P_Fill_FgColor)) { + XlsxColor color = fill.property(FormatPrivate::P_Fill_FgColor).value(); + if (fill.fillPattern() == Format::PatternSolid) + color.saveToXml(writer, QStringLiteral("bgColor")); + else + color.saveToXml(writer, QStringLiteral("fgColor")); } - if (fill.patternBackgroundColor().isValid()) { - writer.writeEmptyElement(fill.fillPattern() == Format::PatternSolid ? QStringLiteral("fgColor") : QStringLiteral("bgColor")); - writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill.patternBackgroundColor().name().mid(1)); - } else if (!fill.stringProperty(FormatPrivate::P_Fill_BgThemeColor).isEmpty()) { - writer.writeEmptyElement(QStringLiteral("bgColor")); - QStringList themes = fill.stringProperty(FormatPrivate::P_Fill_BgThemeColor).split(QLatin1Char(':')); - writer.writeAttribute(QStringLiteral("theme"), themes[0]); - if (!themes[1].isEmpty()) - writer.writeAttribute(QStringLiteral("tint"), themes[1]); + + if (fill.hasProperty(FormatPrivate::P_Fill_BgColor)) { + XlsxColor color = fill.property(FormatPrivate::P_Fill_BgColor).value(); + if (fill.fillPattern() == Format::PatternSolid) + color.saveToXml(writer, QStringLiteral("fgColor")); + else + color.saveToXml(writer, QStringLiteral("bgColor")); } writer.writeEndElement();//patternFill @@ -522,17 +511,17 @@ void Styles::writeBorder(QXmlStreamWriter &writer, const Format &border, bool is } } if (border.hasProperty(FormatPrivate::P_Border_LeftStyle)) - writeSubBorder(writer, QStringLiteral("left"), border.leftBorderStyle(), border.leftBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeLeftColor)); + writeSubBorder(writer, QStringLiteral("left"), border.leftBorderStyle(), border.property(FormatPrivate::P_Border_LeftColor).value()); if (border.hasProperty(FormatPrivate::P_Border_RightStyle)) - writeSubBorder(writer, QStringLiteral("right"), border.rightBorderStyle(), border.rightBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeRightColor)); + writeSubBorder(writer, QStringLiteral("right"), border.rightBorderStyle(), border.property(FormatPrivate::P_Border_RightColor).value()); if (border.hasProperty(FormatPrivate::P_Border_TopStyle)) - writeSubBorder(writer, QStringLiteral("top"), border.topBorderStyle(), border.topBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeTopColor)); + writeSubBorder(writer, QStringLiteral("top"), border.topBorderStyle(), border.property(FormatPrivate::P_Border_TopColor).value()); if (border.hasProperty(FormatPrivate::P_Border_BottomStyle)) - writeSubBorder(writer, QStringLiteral("bottom"), border.bottomBorderStyle(), border.bottomBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeBottomColor)); + writeSubBorder(writer, QStringLiteral("bottom"), border.bottomBorderStyle(), border.property(FormatPrivate::P_Border_BottomColor).value()); //Condition DXF formats don't allow diagonal style if (!isDxf && border.hasProperty(FormatPrivate::P_Border_DiagonalStyle)) - writeSubBorder(writer, QStringLiteral("diagonal"), border.diagonalBorderStyle(), border.diagonalBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeDiagonalColor)); + writeSubBorder(writer, QStringLiteral("diagonal"), border.diagonalBorderStyle(), border.property(FormatPrivate::P_Border_DiagonalColor).value()); if (isDxf) { // writeSubBorder(wirter, QStringLiteral("vertical"), ); @@ -542,7 +531,7 @@ void Styles::writeBorder(QXmlStreamWriter &writer, const Format &border, bool is writer.writeEndElement();//border } -void Styles::writeSubBorder(QXmlStreamWriter &writer, const QString &type, int style, const QColor &color, const QString &themeColor) +void Styles::writeSubBorder(QXmlStreamWriter &writer, const QString &type, int style, const XlsxColor &color) { if (style == Format::BorderNone) { writer.writeEmptyElement(type); @@ -569,17 +558,8 @@ void Styles::writeSubBorder(QXmlStreamWriter &writer, const QString &type, int s writer.writeStartElement(type); writer.writeAttribute(QStringLiteral("style"), stylesString[style]); - writer.writeEmptyElement(QStringLiteral("color")); - if (color.isValid()) { - writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.name().mid(1)); //remove # - } else if (!themeColor.isEmpty()) { - QStringList themes = themeColor.split(QLatin1Char(':')); - writer.writeAttribute(QStringLiteral("theme"), themes[0]); - if (!themes[1].isEmpty()) - writer.writeAttribute(QStringLiteral("tint"), themes[1]); - } else { - writer.writeAttribute(QStringLiteral("auto"), QStringLiteral("1")); - } + color.saveToXml(writer); //write color element + writer.writeEndElement();//type } @@ -793,17 +773,9 @@ bool Styles::readFont(QXmlStreamReader &reader, Format &format) } else if (reader.name() == QLatin1String("extend")) { format.setProperty(FormatPrivate::P_Font_Extend, attributes.value(QLatin1String("val")).toString().toInt()); } else if (reader.name() == QLatin1String("color")) { - if (attributes.hasAttribute(QLatin1String("rgb"))) { - QString colorString = attributes.value(QLatin1String("rgb")).toString(); - format.setFontColor(fromARGBString(colorString)); - } else if (attributes.hasAttribute(QLatin1String("indexed"))) { - QColor color = getColorByIndex(attributes.value(QLatin1String("indexed")).toString().toInt()); - format.setFontColor(color); - } else if (attributes.hasAttribute(QLatin1String("theme"))) { - QString theme = attributes.value(QLatin1String("theme")).toString(); - QString tint = attributes.value(QLatin1String("tint")).toString(); - format.setProperty(FormatPrivate::P_Font_ThemeColor, QString(theme + QLatin1Char(':') + tint)); - } + XlsxColor color; + color.loadFromXml(reader, this); + format.setProperty(FormatPrivate::P_Font_Color, color); } else if (reader.name() == QLatin1String("sz")) { int sz = attributes.value(QLatin1String("val")).toString().toInt(); format.setFontSize(sz); @@ -899,37 +871,19 @@ bool Styles::readFill(QXmlStreamReader &reader, Format &fill) fill.setFillPattern(patternValues.contains(pattern) ? patternValues[pattern] : Format::PatternNone); } } else if (reader.name() == QLatin1String("fgColor")) { - QXmlStreamAttributes attributes = reader.attributes(); - QColor c; - if (attributes.hasAttribute(QLatin1String("rgb"))) { - c = fromARGBString(attributes.value(QLatin1String("rgb")).toString()); - } else if (attributes.hasAttribute(QLatin1String("indexed"))) { - c = getColorByIndex(attributes.value(QLatin1String("indexed")).toString().toInt()); - } else if (attributes.hasAttribute(QLatin1String("theme"))) { - QString theme = attributes.value(QLatin1String("theme")).toString(); - QString tint = attributes.value(QLatin1String("tint")).toString(); - fill.setProperty(FormatPrivate::P_Fill_FgThemeColor, QString(theme + QLatin1Char(':') + tint)); - } + XlsxColor c; + c.loadFromXml(reader); if (fill.fillPattern() == Format::PatternSolid) - fill.setPatternBackgroundColor(c); + fill.setProperty(FormatPrivate::P_Fill_BgColor, c); else - fill.setPatternForegroundColor(c); + fill.setProperty(FormatPrivate::P_Fill_FgColor, c); } else if (reader.name() == QLatin1String("bgColor")) { - QXmlStreamAttributes attributes = reader.attributes(); - QColor c; - if (attributes.hasAttribute(QLatin1String("rgb"))) { - c = fromARGBString(attributes.value(QLatin1String("rgb")).toString()); - } else if (attributes.hasAttribute(QLatin1String("indexed"))) { - c = getColorByIndex(attributes.value(QLatin1String("indexed")).toString().toInt()); - } else if (attributes.hasAttribute(QLatin1String("theme"))) { - QString theme = attributes.value(QLatin1String("theme")).toString(); - QString tint = attributes.value(QLatin1String("tint")).toString(); - fill.setProperty(FormatPrivate::P_Fill_BgThemeColor, QString(theme + QLatin1Char(':') + tint)); - } + XlsxColor c; + c.loadFromXml(reader); if (fill.fillPattern() == Format::PatternSolid) - fill.setPatternForegroundColor(c); + fill.setProperty(FormatPrivate::P_Fill_FgColor, c); else - fill.setPatternBackgroundColor(c); + fill.setProperty(FormatPrivate::P_Fill_BgColor, c); } } @@ -991,40 +945,29 @@ bool Styles::readBorder(QXmlStreamReader &reader, Format &border) || reader.name() == QLatin1String("top") || reader.name() == QLatin1String("bottom") || reader.name() == QLatin1String("diagonal") ) { Format::BorderStyle style(Format::BorderNone); - QColor color; - QString themeColor; - readSubBorder(reader, reader.name().toString(), style, color, themeColor); + XlsxColor color; + readSubBorder(reader, reader.name().toString(), style, color); if (reader.name() == QLatin1String("left")) { border.setLeftBorderStyle(style); - if (color.isValid()) - border.setLeftBorderColor(color); - else if (!themeColor.isEmpty()) - border.setProperty(FormatPrivate::P_Border_ThemeLeftColor, themeColor); + if (!color.isInvalid()) + border.setProperty(FormatPrivate::P_Border_LeftColor, color); } else if (reader.name() == QLatin1String("right")) { border.setRightBorderStyle(style); - if (color.isValid()) - border.setRightBorderColor(color); - else if (!themeColor.isEmpty()) - border.setProperty(FormatPrivate::P_Border_ThemeRightColor, themeColor); + if (!color.isInvalid()) + border.setProperty(FormatPrivate::P_Border_RightColor, color); } else if (reader.name() == QLatin1String("top")) { border.setTopBorderStyle(style); - if (color.isValid()) - border.setTopBorderColor(color); - else if (!themeColor.isEmpty()) - border.setProperty(FormatPrivate::P_Border_ThemeTopColor, themeColor); + if (!color.isInvalid()) + border.setProperty(FormatPrivate::P_Border_TopColor, color); } else if (reader.name() == QLatin1String("bottom")) { border.setBottomBorderStyle(style); - if (color.isValid()) - border.setBottomBorderColor(color); - else if (!themeColor.isEmpty()) - border.setProperty(FormatPrivate::P_Border_ThemeBottomColor, themeColor); + if (!color.isInvalid()) + border.setProperty(FormatPrivate::P_Border_BottomColor, color); } else if (reader.name() == QLatin1String("diagonal")) { border.setDiagonalBorderStyle(style); - if (color.isValid()) - border.setDiagonalBorderColor(color); - else if (!themeColor.isEmpty()) - border.setProperty(FormatPrivate::P_Border_ThemeDiagonalColor, themeColor); + if (!color.isInvalid()) + border.setProperty(FormatPrivate::P_Border_DiagonalColor, color); } } } @@ -1036,7 +979,7 @@ bool Styles::readBorder(QXmlStreamReader &reader, Format &border) return true; } -bool Styles::readSubBorder(QXmlStreamReader &reader, const QString &name, Format::BorderStyle &style, QColor &color, QString &themeColor) +bool Styles::readSubBorder(QXmlStreamReader &reader, const QString &name, Format::BorderStyle &style, XlsxColor &color) { Q_ASSERT(reader.name() == name); @@ -1066,21 +1009,8 @@ bool Styles::readSubBorder(QXmlStreamReader &reader, const QString &name, Format style = stylesStringsMap[styleString]; while((reader.readNextStartElement(),true)) { if (reader.tokenType() == QXmlStreamReader::StartElement) { - if (reader.name() == QLatin1String("color")) { - QXmlStreamAttributes colorAttrs = reader.attributes(); - if (colorAttrs.hasAttribute(QLatin1String("rgb"))) { - QString colorString = colorAttrs.value(QLatin1String("rgb")).toString(); - //get color - color = fromARGBString(colorString); - } else if (colorAttrs.hasAttribute(QLatin1String("indexed"))) { - color = getColorByIndex(colorAttrs.value(QLatin1String("indexed")).toString().toInt()); - } else if (colorAttrs.hasAttribute(QLatin1String("theme"))) { - QString theme = attributes.value(QLatin1String("theme")).toString(); - QString tint = attributes.value(QLatin1String("tint")).toString(); - themeColor = theme + QLatin1Char(':') + tint; - } - } - + if (reader.name() == QLatin1String("color")) + color.loadFromXml(reader, this); } else if (reader.tokenType() == QXmlStreamReader::EndElement) { if (reader.name() == name) break; diff --git a/src/xlsx/xlsxstyles_p.h b/src/xlsx/xlsxstyles_p.h index 2074ccb..c58a8d2 100755 --- a/src/xlsx/xlsxstyles_p.h +++ b/src/xlsx/xlsxstyles_p.h @@ -53,6 +53,7 @@ class StylesTest; namespace QXlsx { class Format; +class XlsxColor; struct XlsxFormatNumberData { @@ -77,6 +78,8 @@ public: bool loadFromXmlFile(QIODevice *device); bool loadFromXmlData(const QByteArray &data); + QColor getColorByIndex(int idx); + private: friend class Format; friend class ::StylesTest; @@ -90,7 +93,7 @@ private: void writeFill(QXmlStreamWriter &writer, const Format &fill, bool isDxf = false); void writeBorders(QXmlStreamWriter &writer); void writeBorder(QXmlStreamWriter &writer, const Format &border, bool isDxf = false); - void writeSubBorder(QXmlStreamWriter &writer, const QString &type, int style, const QColor &color, const QString &themeColor); + void writeSubBorder(QXmlStreamWriter &writer, const QString &type, int style, const XlsxColor &color); void writeCellXfs(QXmlStreamWriter &writer); void writeDxfs(QXmlStreamWriter &writer); void writeDxf(QXmlStreamWriter &writer, const Format &format); @@ -102,15 +105,13 @@ private: bool readFill(QXmlStreamReader &reader, Format &format); bool readBorders(QXmlStreamReader &reader); bool readBorder(QXmlStreamReader &reader, Format &format); - bool readSubBorder(QXmlStreamReader &reader, const QString &name, Format::BorderStyle &style, QColor &color, QString &themeColor); + bool readSubBorder(QXmlStreamReader &reader, const QString &name, Format::BorderStyle &style, XlsxColor &color); bool readCellXfs(QXmlStreamReader &reader); bool readDxfs(QXmlStreamReader &reader); bool readDxf(QXmlStreamReader &reader); bool readColors(QXmlStreamReader &reader); bool readIndexedColors(QXmlStreamReader &reader); - QColor getColorByIndex(int idx); - QHash m_builtinNumFmtsHash; QMap > m_customNumFmtIdMap; QHash > m_customNumFmtsHash;