Browse Source

Add new private class XlsxColor

master
Debao Zhang 11 years ago
parent
commit
88aaad9e02
  1. 6
      src/xlsx/qtxlsx.pri
  2. 172
      src/xlsx/xlsxcolor.cpp
  3. 85
      src/xlsx/xlsxcolor_p.h
  4. 28
      src/xlsx/xlsxformat.cpp
  5. 8
      src/xlsx/xlsxformat_p.h
  6. 27
      src/xlsx/xlsxsharedstrings.cpp
  7. 172
      src/xlsx/xlsxstyles.cpp
  8. 9
      src/xlsx/xlsxstyles_p.h

6
src/xlsx/qtxlsx.pri

@ -33,7 +33,8 @@ HEADERS += $$PWD/xlsxdocpropscore_p.h \
$$PWD/xlsxrichstring_p.h \ $$PWD/xlsxrichstring_p.h \
$$PWD/xlsxrichstring.h \ $$PWD/xlsxrichstring.h \
$$PWD/xlsxconditionalformatting.h \ $$PWD/xlsxconditionalformatting.h \
$$PWD/xlsxconditionalformatting_p.h $$PWD/xlsxconditionalformatting_p.h \
$$PWD/xlsxcolor_p.h
SOURCES += $$PWD/xlsxdocpropscore.cpp \ SOURCES += $$PWD/xlsxdocpropscore.cpp \
$$PWD/xlsxdocpropsapp.cpp \ $$PWD/xlsxdocpropsapp.cpp \
@ -55,4 +56,5 @@ SOURCES += $$PWD/xlsxdocpropscore.cpp \
$$PWD/xlsxdatavalidation.cpp \ $$PWD/xlsxdatavalidation.cpp \
$$PWD/xlsxcellrange.cpp \ $$PWD/xlsxcellrange.cpp \
$$PWD/xlsxrichstring.cpp \ $$PWD/xlsxrichstring.cpp \
$$PWD/xlsxconditionalformatting.cpp $$PWD/xlsxconditionalformatting.cpp \
$$PWD/xlsxcolor.cpp

172
src/xlsx/xlsxcolor.cpp

@ -0,0 +1,172 @@
#include "xlsxcolor_p.h"
#include "xlsxstyles_p.h"
#include "xlsxutility_p.h"
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
namespace QXlsx {
XlsxColor::XlsxColor(const QColor &color)
:val(color)
{
}
XlsxColor::XlsxColor(const QString &theme, const QString &tint)
:val(QStringList()<<theme<<tint)
{
}
XlsxColor::XlsxColor(int index)
:val(index)
{
}
bool XlsxColor::isRgbColor() const
{
if (val.userType() == qMetaTypeId<QColor>() && val.value<QColor>().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<QColor>() && !val.value<QColor>().isValid())
return true;
return false;
}
QColor XlsxColor::rgbColor() const
{
if (isRgbColor())
return val.value<QColor>();
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>()) {
QColor color = val.value<QColor>();
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()<<theme<<tint);
}
return true;
}
XlsxColor::operator QVariant() const
{
return QVariant(qMetaTypeId<XlsxColor>(), this);
}
#if !defined(QT_NO_DATASTREAM)
QDataStream &operator<<(QDataStream &s, const XlsxColor &color)
{
if (color.isInvalid())
s<<0;
else if (color.isRgbColor())
s<<1<<color.rgbColor();
else if (color.isIndexedColor())
s<<2<<color.indexedColor();
else if (color.isThemeColor())
s<<3<<color.themeColor();
else
s<<4;
return s;
}
QDataStream &operator>>(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

85
src/xlsx/xlsxcolor_p.h

@ -0,0 +1,85 @@
/****************************************************************************
** Copyright (c) 2013 Debao Zhang <hello@debao.me>
** 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 <QVariant>
#include <QColor>
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

28
src/xlsx/xlsxformat.cpp

@ -24,6 +24,7 @@
****************************************************************************/ ****************************************************************************/
#include "xlsxformat.h" #include "xlsxformat.h"
#include "xlsxformat_p.h" #include "xlsxformat_p.h"
#include "xlsxcolor_p.h"
#include <QDataStream> #include <QDataStream>
#include <QRegularExpression> #include <QRegularExpression>
#include <QDebug> #include <QDebug>
@ -179,6 +180,7 @@ FormatPrivate::~FormatPrivate()
*/ */
Format::Format() Format::Format()
{ {
qRegisterMetaTypeStreamOperators<XlsxColor>("XlsxColor");
//The d pointer is initialized with a null pointer //The d pointer is initialized with a null pointer
} }
@ -364,12 +366,6 @@ QColor Format::fontColor() const
{ {
if (hasProperty(FormatPrivate::P_Font_Color)) if (hasProperty(FormatPrivate::P_Font_Color))
return colorProperty(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(); return QColor();
} }
@ -378,7 +374,7 @@ QColor Format::fontColor() const
*/ */
void Format::setFontColor(const QColor &color) 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) 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) 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) 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) 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) 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)) if (color.isValid() && !hasProperty(FormatPrivate::P_Fill_Pattern))
setFillPattern(PatternSolid); 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)) if (color.isValid() && !hasProperty(FormatPrivate::P_Fill_Pattern))
setFillPattern(PatternSolid); 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(); return QColor();
const QVariant prop = d->property[propertyId]; const QVariant prop = d->property[propertyId];
if (prop.userType() != qMetaTypeId<QColor>()) if (prop.userType() != qMetaTypeId<XlsxColor>())
return QColor(); return QColor();
return qvariant_cast<QColor>(prop); return qvariant_cast<XlsxColor>(prop).rgbColor();
} }
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM

8
src/xlsx/xlsxformat_p.h

@ -69,7 +69,6 @@ public:
P_Font_Italic, P_Font_Italic,
P_Font_StrikeOut, P_Font_StrikeOut,
P_Font_Color, P_Font_Color,
P_Font_ThemeColor,
P_Font_Bold, P_Font_Bold,
P_Font_Script, P_Font_Script,
P_Font_Underline, P_Font_Underline,
@ -96,11 +95,6 @@ public:
P_Border_BottomColor, P_Border_BottomColor,
P_Border_DiagonalColor, P_Border_DiagonalColor,
P_Border_DiagonalType, P_Border_DiagonalType,
P_Border_ThemeLeftColor,
P_Border_ThemeRightColor,
P_Border_ThemeTopColor,
P_Border_ThemeBottomColor,
P_Border_ThemeDiagonalColor,
P_Border_ENDID, P_Border_ENDID,
//fill //fill
@ -108,8 +102,6 @@ public:
P_Fill_Pattern = P_Fill_STARTID, P_Fill_Pattern = P_Fill_STARTID,
P_Fill_BgColor, P_Fill_BgColor,
P_Fill_FgColor, P_Fill_FgColor,
P_Fill_BgThemeColor,
P_Fill_FgThemeColor,
P_Fill_ENDID, P_Fill_ENDID,
//alignment //alignment

27
src/xlsx/xlsxsharedstrings.cpp

@ -26,6 +26,7 @@
#include "xlsxsharedstrings_p.h" #include "xlsxsharedstrings_p.h"
#include "xlsxutility_p.h" #include "xlsxutility_p.h"
#include "xlsxformat_p.h" #include "xlsxformat_p.h"
#include "xlsxcolor_p.h"
#include <QXmlStreamWriter> #include <QXmlStreamWriter>
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QDir> #include <QDir>
@ -168,16 +169,9 @@ void SharedStrings::writeRichStringPart_rPr(QXmlStreamWriter &writer, const Form
writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontSize())); writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontSize()));
} }
if (format.fontColor().isValid()) { if (format.hasProperty(FormatPrivate::P_Font_Color)) {
writer.writeEmptyElement(QStringLiteral("color")); XlsxColor color = format.property(FormatPrivate::P_Font_Color).value<XlsxColor>();
QString color = format.fontColor().name(); color.saveToXml(writer);
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.fontName().isEmpty()) { if (!format.fontName().isEmpty()) {
@ -332,16 +326,9 @@ Format SharedStrings::readRichStringPart_rPr(QXmlStreamReader &reader)
} else if (reader.name() == QLatin1String("extend")) { } else if (reader.name() == QLatin1String("extend")) {
format.setProperty(FormatPrivate::P_Font_Extend, attributes.value(QLatin1String("val")).toString().toInt()); format.setProperty(FormatPrivate::P_Font_Extend, attributes.value(QLatin1String("val")).toString().toInt());
} else if (reader.name() == QLatin1String("color")) { } else if (reader.name() == QLatin1String("color")) {
if (attributes.hasAttribute(QLatin1String("rgb"))) { XlsxColor color;
QString colorString = attributes.value(QLatin1String("rgb")).toString(); color.loadFromXml(reader);
format.setFontColor(fromARGBString(colorString)); format.setProperty(FormatPrivate::P_Font_Color, color);
} 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));
}
} else if (reader.name() == QLatin1String("sz")) { } else if (reader.name() == QLatin1String("sz")) {
format.setFontSize(attributes.value(QLatin1String("val")).toString().toInt()); format.setFontSize(attributes.value(QLatin1String("val")).toString().toInt());
} else if (reader.name() == QLatin1String("u")) { } else if (reader.name() == QLatin1String("u")) {

172
src/xlsx/xlsxstyles.cpp

@ -25,6 +25,7 @@
#include "xlsxstyles_p.h" #include "xlsxstyles_p.h"
#include "xlsxformat_p.h" #include "xlsxformat_p.h"
#include "xlsxutility_p.h" #include "xlsxutility_p.h"
#include "xlsxcolor_p.h"
#include <QXmlStreamWriter> #include <QXmlStreamWriter>
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QFile> #include <QFile>
@ -403,16 +404,9 @@ void Styles::writeFont(QXmlStreamWriter &writer, const Format &format, bool isDx
writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontSize())); writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontSize()));
} }
if (format.fontColor().isValid()) { if (format.hasProperty(FormatPrivate::P_Font_Color)) {
writer.writeEmptyElement(QStringLiteral("color")); XlsxColor color = format.property(FormatPrivate::P_Font_Color).value<XlsxColor>();
QString color = format.fontColor().name(); color.saveToXml(writer);
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 (!isDxf) { if (!isDxf) {
@ -473,25 +467,20 @@ void Styles::writeFill(QXmlStreamWriter &writer, const Format &fill, bool /*isDx
writer.writeStartElement(QStringLiteral("patternFill")); writer.writeStartElement(QStringLiteral("patternFill"));
writer.writeAttribute(QStringLiteral("patternType"), patternStrings[fill.fillPattern()]); writer.writeAttribute(QStringLiteral("patternType"), patternStrings[fill.fillPattern()]);
// For a solid fill, Excel reverses the role of foreground and background colours // For a solid fill, Excel reverses the role of foreground and background colours
if (fill.patternForegroundColor().isValid()) { if (fill.hasProperty(FormatPrivate::P_Fill_FgColor)) {
writer.writeEmptyElement(fill.fillPattern() == Format::PatternSolid ? QStringLiteral("bgColor") : QStringLiteral("fgColor")); XlsxColor color = fill.property(FormatPrivate::P_Fill_FgColor).value<XlsxColor>();
writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill.patternForegroundColor().name().mid(1)); if (fill.fillPattern() == Format::PatternSolid)
} else if (!fill.stringProperty(FormatPrivate::P_Fill_FgThemeColor).isEmpty()) { color.saveToXml(writer, QStringLiteral("bgColor"));
writer.writeEmptyElement(QStringLiteral("fgColor")); else
QStringList themes = fill.stringProperty(FormatPrivate::P_Fill_FgThemeColor).split(QLatin1Char(':')); color.saveToXml(writer, QStringLiteral("fgColor"));
writer.writeAttribute(QStringLiteral("theme"), themes[0]);
if (!themes[1].isEmpty())
writer.writeAttribute(QStringLiteral("tint"), themes[1]);
} }
if (fill.patternBackgroundColor().isValid()) {
writer.writeEmptyElement(fill.fillPattern() == Format::PatternSolid ? QStringLiteral("fgColor") : QStringLiteral("bgColor")); if (fill.hasProperty(FormatPrivate::P_Fill_BgColor)) {
writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill.patternBackgroundColor().name().mid(1)); XlsxColor color = fill.property(FormatPrivate::P_Fill_BgColor).value<XlsxColor>();
} else if (!fill.stringProperty(FormatPrivate::P_Fill_BgThemeColor).isEmpty()) { if (fill.fillPattern() == Format::PatternSolid)
writer.writeEmptyElement(QStringLiteral("bgColor")); color.saveToXml(writer, QStringLiteral("fgColor"));
QStringList themes = fill.stringProperty(FormatPrivate::P_Fill_BgThemeColor).split(QLatin1Char(':')); else
writer.writeAttribute(QStringLiteral("theme"), themes[0]); color.saveToXml(writer, QStringLiteral("bgColor"));
if (!themes[1].isEmpty())
writer.writeAttribute(QStringLiteral("tint"), themes[1]);
} }
writer.writeEndElement();//patternFill writer.writeEndElement();//patternFill
@ -522,17 +511,17 @@ void Styles::writeBorder(QXmlStreamWriter &writer, const Format &border, bool is
} }
} }
if (border.hasProperty(FormatPrivate::P_Border_LeftStyle)) 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<XlsxColor>());
if (border.hasProperty(FormatPrivate::P_Border_RightStyle)) 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<XlsxColor>());
if (border.hasProperty(FormatPrivate::P_Border_TopStyle)) 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<XlsxColor>());
if (border.hasProperty(FormatPrivate::P_Border_BottomStyle)) 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<XlsxColor>());
//Condition DXF formats don't allow diagonal style //Condition DXF formats don't allow diagonal style
if (!isDxf && border.hasProperty(FormatPrivate::P_Border_DiagonalStyle)) 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<XlsxColor>());
if (isDxf) { if (isDxf) {
// writeSubBorder(wirter, QStringLiteral("vertical"), ); // writeSubBorder(wirter, QStringLiteral("vertical"), );
@ -542,7 +531,7 @@ void Styles::writeBorder(QXmlStreamWriter &writer, const Format &border, bool is
writer.writeEndElement();//border 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) { if (style == Format::BorderNone) {
writer.writeEmptyElement(type); writer.writeEmptyElement(type);
@ -569,17 +558,8 @@ void Styles::writeSubBorder(QXmlStreamWriter &writer, const QString &type, int s
writer.writeStartElement(type); writer.writeStartElement(type);
writer.writeAttribute(QStringLiteral("style"), stylesString[style]); writer.writeAttribute(QStringLiteral("style"), stylesString[style]);
writer.writeEmptyElement(QStringLiteral("color")); color.saveToXml(writer); //write color element
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"));
}
writer.writeEndElement();//type writer.writeEndElement();//type
} }
@ -793,17 +773,9 @@ bool Styles::readFont(QXmlStreamReader &reader, Format &format)
} else if (reader.name() == QLatin1String("extend")) { } else if (reader.name() == QLatin1String("extend")) {
format.setProperty(FormatPrivate::P_Font_Extend, attributes.value(QLatin1String("val")).toString().toInt()); format.setProperty(FormatPrivate::P_Font_Extend, attributes.value(QLatin1String("val")).toString().toInt());
} else if (reader.name() == QLatin1String("color")) { } else if (reader.name() == QLatin1String("color")) {
if (attributes.hasAttribute(QLatin1String("rgb"))) { XlsxColor color;
QString colorString = attributes.value(QLatin1String("rgb")).toString(); color.loadFromXml(reader, this);
format.setFontColor(fromARGBString(colorString)); format.setProperty(FormatPrivate::P_Font_Color, color);
} 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));
}
} else if (reader.name() == QLatin1String("sz")) { } else if (reader.name() == QLatin1String("sz")) {
int sz = attributes.value(QLatin1String("val")).toString().toInt(); int sz = attributes.value(QLatin1String("val")).toString().toInt();
format.setFontSize(sz); format.setFontSize(sz);
@ -899,37 +871,19 @@ bool Styles::readFill(QXmlStreamReader &reader, Format &fill)
fill.setFillPattern(patternValues.contains(pattern) ? patternValues[pattern] : Format::PatternNone); fill.setFillPattern(patternValues.contains(pattern) ? patternValues[pattern] : Format::PatternNone);
} }
} else if (reader.name() == QLatin1String("fgColor")) { } else if (reader.name() == QLatin1String("fgColor")) {
QXmlStreamAttributes attributes = reader.attributes(); XlsxColor c;
QColor c; c.loadFromXml(reader);
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));
}
if (fill.fillPattern() == Format::PatternSolid) if (fill.fillPattern() == Format::PatternSolid)
fill.setPatternBackgroundColor(c); fill.setProperty(FormatPrivate::P_Fill_BgColor, c);
else else
fill.setPatternForegroundColor(c); fill.setProperty(FormatPrivate::P_Fill_FgColor, c);
} else if (reader.name() == QLatin1String("bgColor")) { } else if (reader.name() == QLatin1String("bgColor")) {
QXmlStreamAttributes attributes = reader.attributes(); XlsxColor c;
QColor c; c.loadFromXml(reader);
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));
}
if (fill.fillPattern() == Format::PatternSolid) if (fill.fillPattern() == Format::PatternSolid)
fill.setPatternForegroundColor(c); fill.setProperty(FormatPrivate::P_Fill_FgColor, c);
else 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("top") || reader.name() == QLatin1String("bottom")
|| reader.name() == QLatin1String("diagonal") ) { || reader.name() == QLatin1String("diagonal") ) {
Format::BorderStyle style(Format::BorderNone); Format::BorderStyle style(Format::BorderNone);
QColor color; XlsxColor color;
QString themeColor; readSubBorder(reader, reader.name().toString(), style, color);
readSubBorder(reader, reader.name().toString(), style, color, themeColor);
if (reader.name() == QLatin1String("left")) { if (reader.name() == QLatin1String("left")) {
border.setLeftBorderStyle(style); border.setLeftBorderStyle(style);
if (color.isValid()) if (!color.isInvalid())
border.setLeftBorderColor(color); border.setProperty(FormatPrivate::P_Border_LeftColor, color);
else if (!themeColor.isEmpty())
border.setProperty(FormatPrivate::P_Border_ThemeLeftColor, themeColor);
} else if (reader.name() == QLatin1String("right")) { } else if (reader.name() == QLatin1String("right")) {
border.setRightBorderStyle(style); border.setRightBorderStyle(style);
if (color.isValid()) if (!color.isInvalid())
border.setRightBorderColor(color); border.setProperty(FormatPrivate::P_Border_RightColor, color);
else if (!themeColor.isEmpty())
border.setProperty(FormatPrivate::P_Border_ThemeRightColor, themeColor);
} else if (reader.name() == QLatin1String("top")) { } else if (reader.name() == QLatin1String("top")) {
border.setTopBorderStyle(style); border.setTopBorderStyle(style);
if (color.isValid()) if (!color.isInvalid())
border.setTopBorderColor(color); border.setProperty(FormatPrivate::P_Border_TopColor, color);
else if (!themeColor.isEmpty())
border.setProperty(FormatPrivate::P_Border_ThemeTopColor, themeColor);
} else if (reader.name() == QLatin1String("bottom")) { } else if (reader.name() == QLatin1String("bottom")) {
border.setBottomBorderStyle(style); border.setBottomBorderStyle(style);
if (color.isValid()) if (!color.isInvalid())
border.setBottomBorderColor(color); border.setProperty(FormatPrivate::P_Border_BottomColor, color);
else if (!themeColor.isEmpty())
border.setProperty(FormatPrivate::P_Border_ThemeBottomColor, themeColor);
} else if (reader.name() == QLatin1String("diagonal")) { } else if (reader.name() == QLatin1String("diagonal")) {
border.setDiagonalBorderStyle(style); border.setDiagonalBorderStyle(style);
if (color.isValid()) if (!color.isInvalid())
border.setDiagonalBorderColor(color); border.setProperty(FormatPrivate::P_Border_DiagonalColor, color);
else if (!themeColor.isEmpty())
border.setProperty(FormatPrivate::P_Border_ThemeDiagonalColor, themeColor);
} }
} }
} }
@ -1036,7 +979,7 @@ bool Styles::readBorder(QXmlStreamReader &reader, Format &border)
return true; 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); Q_ASSERT(reader.name() == name);
@ -1066,21 +1009,8 @@ bool Styles::readSubBorder(QXmlStreamReader &reader, const QString &name, Format
style = stylesStringsMap[styleString]; style = stylesStringsMap[styleString];
while((reader.readNextStartElement(),true)) { while((reader.readNextStartElement(),true)) {
if (reader.tokenType() == QXmlStreamReader::StartElement) { if (reader.tokenType() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("color")) { if (reader.name() == QLatin1String("color"))
QXmlStreamAttributes colorAttrs = reader.attributes(); color.loadFromXml(reader, this);
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;
}
}
} else if (reader.tokenType() == QXmlStreamReader::EndElement) { } else if (reader.tokenType() == QXmlStreamReader::EndElement) {
if (reader.name() == name) if (reader.name() == name)
break; break;

9
src/xlsx/xlsxstyles_p.h

@ -53,6 +53,7 @@ class StylesTest;
namespace QXlsx { namespace QXlsx {
class Format; class Format;
class XlsxColor;
struct XlsxFormatNumberData struct XlsxFormatNumberData
{ {
@ -77,6 +78,8 @@ public:
bool loadFromXmlFile(QIODevice *device); bool loadFromXmlFile(QIODevice *device);
bool loadFromXmlData(const QByteArray &data); bool loadFromXmlData(const QByteArray &data);
QColor getColorByIndex(int idx);
private: private:
friend class Format; friend class Format;
friend class ::StylesTest; friend class ::StylesTest;
@ -90,7 +93,7 @@ private:
void writeFill(QXmlStreamWriter &writer, const Format &fill, bool isDxf = false); void writeFill(QXmlStreamWriter &writer, const Format &fill, bool isDxf = false);
void writeBorders(QXmlStreamWriter &writer); void writeBorders(QXmlStreamWriter &writer);
void writeBorder(QXmlStreamWriter &writer, const Format &border, bool isDxf = false); 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 writeCellXfs(QXmlStreamWriter &writer);
void writeDxfs(QXmlStreamWriter &writer); void writeDxfs(QXmlStreamWriter &writer);
void writeDxf(QXmlStreamWriter &writer, const Format &format); void writeDxf(QXmlStreamWriter &writer, const Format &format);
@ -102,15 +105,13 @@ private:
bool readFill(QXmlStreamReader &reader, Format &format); bool readFill(QXmlStreamReader &reader, Format &format);
bool readBorders(QXmlStreamReader &reader); bool readBorders(QXmlStreamReader &reader);
bool readBorder(QXmlStreamReader &reader, Format &format); 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 readCellXfs(QXmlStreamReader &reader);
bool readDxfs(QXmlStreamReader &reader); bool readDxfs(QXmlStreamReader &reader);
bool readDxf(QXmlStreamReader &reader); bool readDxf(QXmlStreamReader &reader);
bool readColors(QXmlStreamReader &reader); bool readColors(QXmlStreamReader &reader);
bool readIndexedColors(QXmlStreamReader &reader); bool readIndexedColors(QXmlStreamReader &reader);
QColor getColorByIndex(int idx);
QHash<QString, int> m_builtinNumFmtsHash; QHash<QString, int> m_builtinNumFmtsHash;
QMap<int, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtIdMap; QMap<int, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtIdMap;
QHash<QString, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtsHash; QHash<QString, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtsHash;

Loading…
Cancel
Save