Browse Source

Code refactor: Remove private class XlsxFormatFontData

master
Debao Zhang 11 years ago
parent
commit
88ffbe3def
  1. 140
      src/xlsx/xlsxformat.cpp
  2. 8
      src/xlsx/xlsxformat.h
  3. 109
      src/xlsx/xlsxformat_p.h
  4. 69
      src/xlsx/xlsxsharedstrings.cpp
  5. 180
      src/xlsx/xlsxstyles.cpp
  6. 5
      src/xlsx/xlsxstyles_p.h
  7. 3
      tests/auto/styles/tst_stylestest.cpp

140
src/xlsx/xlsxformat.cpp

@ -31,23 +31,20 @@
QT_BEGIN_NAMESPACE_XLSX QT_BEGIN_NAMESPACE_XLSX
FormatPrivate::FormatPrivate() FormatPrivate::FormatPrivate()
: dirty(true)
, font_dirty(true), font_index_valid(false), font_index(-1)
, xf_index(-1), xf_indexValid(false)
, is_dxf_fomat(false), dxf_index(-1), dxf_indexValid(false)
, theme(0)
{ {
dirty = true;
is_dxf_fomat = false;
xf_index = -1;
dxf_index = -1;
xf_indexValid = false;
dxf_indexValid = false;
theme = 0;
} }
FormatPrivate::FormatPrivate(const FormatPrivate &other) FormatPrivate::FormatPrivate(const FormatPrivate &other)
: QSharedData(other) : QSharedData(other)
, fontData(other.fontData), alignmentData(other.alignmentData) , alignmentData(other.alignmentData)
, borderData(other.borderData), fillData(other.fillData), protectionData(other.protectionData) , borderData(other.borderData), fillData(other.fillData), protectionData(other.protectionData)
, dirty(other.dirty), formatKey(other.formatKey) , dirty(other.dirty), formatKey(other.formatKey)
, font_dirty(other.dirty), font_index_valid(other.font_index_valid), font_key(other.font_key), font_index(other.font_index)
, xf_index(other.xf_index), xf_indexValid(other.xf_indexValid) , xf_index(other.xf_index), xf_indexValid(other.xf_indexValid)
, is_dxf_fomat(other.is_dxf_fomat), dxf_index(other.dxf_index), dxf_indexValid(other.dxf_indexValid) , is_dxf_fomat(other.is_dxf_fomat), dxf_index(other.dxf_index), dxf_indexValid(other.dxf_indexValid)
, theme(other.theme) , theme(other.theme)
@ -181,7 +178,7 @@ void Format::setNumberFormat(int id, const QString &format)
*/ */
int Format::fontSize() const int Format::fontSize() const
{ {
return d->fontData.size; return intProperty(FormatPrivate::P_Font_Size);
} }
/*! /*!
@ -189,8 +186,7 @@ int Format::fontSize() const
*/ */
void Format::setFontSize(int size) void Format::setFontSize(int size)
{ {
d->fontData.size = size; setProperty(FormatPrivate::P_Font_Size, size);
d->fontData._dirty = true;
} }
/*! /*!
@ -198,7 +194,7 @@ void Format::setFontSize(int size)
*/ */
bool Format::fontItalic() const bool Format::fontItalic() const
{ {
return d->fontData.italic; return boolProperty(FormatPrivate::P_Font_Italic);
} }
/*! /*!
@ -206,8 +202,7 @@ bool Format::fontItalic() const
*/ */
void Format::setFontItalic(bool italic) void Format::setFontItalic(bool italic)
{ {
d->fontData.italic = italic; setProperty(FormatPrivate::P_Font_Italic, italic);
d->fontData._dirty = true;
} }
/*! /*!
@ -215,7 +210,7 @@ void Format::setFontItalic(bool italic)
*/ */
bool Format::fontStrikeOut() const bool Format::fontStrikeOut() const
{ {
return d->fontData.strikeOut; return boolProperty(FormatPrivate::P_Font_StrikeOut);
} }
/*! /*!
@ -223,8 +218,7 @@ bool Format::fontStrikeOut() const
*/ */
void Format::setFontStrikeOut(bool strikeOut) void Format::setFontStrikeOut(bool strikeOut)
{ {
d->fontData.strikeOut = strikeOut; setProperty(FormatPrivate::P_Font_StrikeOut, strikeOut);
d->fontData._dirty = true;
} }
/*! /*!
@ -232,12 +226,15 @@ void Format::setFontStrikeOut(bool strikeOut)
*/ */
QColor Format::fontColor() const QColor Format::fontColor() const
{ {
if (!d->fontData.color.isValid() && !d->fontData.themeColor.isEmpty()) { 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 //!Todo, get the real color from the theme{1}.xml file
//The same is ture for border and fill colord //The same is ture for border and fill colord
return QColor(); return QColor();
} }
return d->fontData.color; return QColor();
} }
/*! /*!
@ -245,8 +242,7 @@ QColor Format::fontColor() const
*/ */
void Format::setFontColor(const QColor &color) void Format::setFontColor(const QColor &color)
{ {
d->fontData.color = color; setProperty(FormatPrivate::P_Font_Color, color);
d->fontData._dirty = true;
} }
/*! /*!
@ -254,7 +250,7 @@ void Format::setFontColor(const QColor &color)
*/ */
bool Format::fontBold() const bool Format::fontBold() const
{ {
return d->fontData.bold; return boolProperty(FormatPrivate::P_Font_Bold);
} }
/*! /*!
@ -262,8 +258,7 @@ bool Format::fontBold() const
*/ */
void Format::setFontBold(bool bold) void Format::setFontBold(bool bold)
{ {
d->fontData.bold = bold; setProperty(FormatPrivate::P_Font_Bold, bold);
d->fontData._dirty = true;
} }
/*! /*!
@ -271,7 +266,7 @@ void Format::setFontBold(bool bold)
*/ */
Format::FontScript Format::fontScript() const Format::FontScript Format::fontScript() const
{ {
return d->fontData.scirpt; return static_cast<Format::FontScript>(intProperty(FormatPrivate::P_Font_Script));
} }
/*! /*!
@ -279,8 +274,7 @@ Format::FontScript Format::fontScript() const
*/ */
void Format::setFontScript(FontScript script) void Format::setFontScript(FontScript script)
{ {
d->fontData.scirpt = script; setProperty(FormatPrivate::P_Font_Script, script);
d->fontData._dirty = true;
} }
/*! /*!
@ -288,7 +282,7 @@ void Format::setFontScript(FontScript script)
*/ */
Format::FontUnderline Format::fontUnderline() const Format::FontUnderline Format::fontUnderline() const
{ {
return d->fontData.underline; return static_cast<Format::FontUnderline>(intProperty(FormatPrivate::P_Font_Underline));
} }
/*! /*!
@ -296,8 +290,7 @@ Format::FontUnderline Format::fontUnderline() const
*/ */
void Format::setFontUnderline(FontUnderline underline) void Format::setFontUnderline(FontUnderline underline)
{ {
d->fontData.underline = underline; setProperty(FormatPrivate::P_Font_Underline, underline);
d->fontData._dirty = true;
} }
/*! /*!
@ -305,7 +298,7 @@ void Format::setFontUnderline(FontUnderline underline)
*/ */
bool Format::fontOutline() const bool Format::fontOutline() const
{ {
return d->fontData.outline; return boolProperty(FormatPrivate::P_Font_Outline);
} }
/*! /*!
@ -313,8 +306,7 @@ bool Format::fontOutline() const
*/ */
void Format::setFontOutline(bool outline) void Format::setFontOutline(bool outline)
{ {
d->fontData.outline = outline; setProperty(FormatPrivate::P_Font_Outline, outline);
d->fontData._dirty = true;
} }
/*! /*!
@ -322,7 +314,7 @@ void Format::setFontOutline(bool outline)
*/ */
QString Format::fontName() const QString Format::fontName() const
{ {
return d->fontData.name; return stringProperty(FormatPrivate::P_Font_Name);
} }
/*! /*!
@ -330,8 +322,7 @@ QString Format::fontName() const
*/ */
void Format::setFontName(const QString &name) void Format::setFontName(const QString &name)
{ {
d->fontData.name = name; setProperty(FormatPrivate::P_Font_Name, name);
d->fontData._dirty = true;
} }
/*! /*!
@ -339,7 +330,7 @@ void Format::setFontName(const QString &name)
*/ */
bool Format::fontIndexValid() const bool Format::fontIndexValid() const
{ {
return d->fontData.indexValid(); return d->font_index_valid;
} }
/*! /*!
@ -347,48 +338,38 @@ bool Format::fontIndexValid() const
*/ */
int Format::fontIndex() const int Format::fontIndex() const
{ {
return d->fontData.index(); if (fontIndexValid())
} return d->font_index;
/*! return -1;
* \internal
*/
void Format::setFontIndex(int index)
{
d->fontData.setIndex(index);
}
/*!
* \internal
*/
int Format::fontFamily() const
{
return d->fontData.family;
}
/*!
* \internal
*/
bool Format::fontShadow() const
{
return d->fontData.shadow;
} }
/*! /*!
* \internal * \internal
*/ */
QString Format::fontScheme() const void Format::setFontIndex(int index)
{ {
return d->fontData.scheme; d->font_index = index;
d->font_index_valid = true;
} }
/* Internal /* Internal
*/ */
QByteArray Format::fontKey() const QByteArray Format::fontKey() const
{ {
if (d->fontData._dirty) if (d->font_dirty) {
d->dirty = true; //Make sure formatKey() will be re-generated. QByteArray key;
return d->fontData.key(); QDataStream stream(&key, QIODevice::WriteOnly);
for (int i=FormatPrivate::P_Font_STARTID; i<FormatPrivate::P_Font_ENDID; ++i) {
if (d->property.contains(i))
stream << i << d->property[i];
};
const_cast<Format*>(this)->d->font_key = key;
const_cast<Format*>(this)->d->font_dirty = false;
}
return d->font_key;
} }
/*! /*!
@ -849,7 +830,7 @@ void Format::setLocked(bool locked)
QByteArray Format::formatKey() const QByteArray Format::formatKey() const
{ {
if (d->dirty || d->fontData._dirty || d->borderData._dirty || d->fillData._dirty) { if (d->dirty || d->borderData._dirty || d->fillData._dirty) {
QByteArray key; QByteArray key;
QDataStream stream(&key, QIODevice::WriteOnly); QDataStream stream(&key, QIODevice::WriteOnly);
stream<<fontKey()<<borderKey()<<fillKey() stream<<fontKey()<<borderKey()<<fillKey()
@ -933,11 +914,25 @@ QVariant Format::property(int propertyId) const
*/ */
void Format::setProperty(int propertyId, const QVariant &value) void Format::setProperty(int propertyId, const QVariant &value)
{ {
if (value.isValid()) if (value.isValid()) {
if (d->property.contains(propertyId) && d->property[propertyId] == value)
return;
d.detach();
d->property[propertyId] = value; d->property[propertyId] = value;
else } else {
if (!d->property.contains(propertyId))
return;
d.detach();
d->property.remove(propertyId); d->property.remove(propertyId);
}
d->dirty = true; d->dirty = true;
d->xf_indexValid = false;
if (propertyId >= FormatPrivate::P_Font_STARTID && propertyId < FormatPrivate::P_Font_ENDID) {
d->font_dirty = true;
d->font_index_valid = false;
}
} }
/*! /*!
@ -945,8 +940,7 @@ void Format::setProperty(int propertyId, const QVariant &value)
*/ */
void Format::clearProperty(int propertyId) void Format::clearProperty(int propertyId)
{ {
d->property.remove(propertyId); setProperty(propertyId, QVariant());
d->dirty = true;
} }
/*! /*!

8
src/xlsx/xlsxformat.h

@ -30,7 +30,7 @@
#include <QColor> #include <QColor>
#include <QByteArray> #include <QByteArray>
#include <QList> #include <QList>
#include <QSharedDataPointer> #include <QExplicitlySharedDataPointer>
class FormatTest; class FormatTest;
@ -232,16 +232,12 @@ private:
friend class Worksheet; friend class Worksheet;
friend class WorksheetPrivate; friend class WorksheetPrivate;
friend class RichString; friend class RichString;
friend class SharedStrings;
friend class ::FormatTest; friend class ::FormatTest;
bool fontIndexValid() const; bool fontIndexValid() const;
int fontIndex() const; int fontIndex() const;
void setFontIndex(int index); void setFontIndex(int index);
QByteArray fontKey() const; QByteArray fontKey() const;
int fontFamily() const;
bool fontShadow() const;
QString fontScheme() const;
bool alignmentChanged() const; bool alignmentChanged() const;
QString horizontalAlignmentString() const; QString horizontalAlignmentString() const;
@ -268,7 +264,7 @@ private:
int theme() const; int theme() const;
QSharedDataPointer<FormatPrivate> d; QExplicitlySharedDataPointer<FormatPrivate> d;
}; };
QT_END_NAMESPACE_XLSX QT_END_NAMESPACE_XLSX

109
src/xlsx/xlsxformat_p.h

@ -30,76 +30,6 @@
namespace QXlsx { namespace QXlsx {
struct XlsxFormatFontData
{
XlsxFormatFontData() :
size(11), italic(false), strikeOut(false), color(QColor()), bold(false)
, scirpt(Format::FontScriptNormal), underline(Format::FontUnderlineNone)
, outline(false), shadow(false), name(QStringLiteral("Calibri")), family(2), charset(0)
, scheme(QStringLiteral("minor")), condense(0), extend(0)
, _dirty(true), _indexValid(false), _index(-1)
{}
int size;
bool italic;
bool strikeOut;
QColor color;
QString themeColor;
bool bold;
Format::FontScript scirpt;
Format::FontUnderline underline;
bool outline;
bool shadow;
QString name;
int family;
int charset;
QString scheme;
int condense;
int extend;
QByteArray key() const
{
if (_dirty) {
QByteArray key;
QDataStream stream(&key, QIODevice::WriteOnly);
stream<<bold<<charset<<color<<themeColor<<condense
<<extend<<family<<italic<<name
<<outline<<scheme<<scirpt<<shadow
<<size<<strikeOut<<underline;
const_cast<XlsxFormatFontData*>(this)->_key = key;
const_cast<XlsxFormatFontData*>(this)->_dirty = false;
const_cast<XlsxFormatFontData*>(this)->_indexValid = false;//dirty flag can not be simply cleared.
}
return _key;
}
bool indexValid() const
{
return !_dirty && _indexValid;
}
int index() const
{
return _index;
}
void setIndex(int index)
{
_index = index;
_indexValid = true;
}
//helper member
bool _dirty; //key re-generated and proper index assign is need.
private:
QByteArray _key;
bool _indexValid; //has a valid index, so no need to assign a new one
int _index; //index in the Font list
};
struct XlsxFormatAlignmentData struct XlsxFormatAlignmentData
{ {
XlsxFormatAlignmentData() : XlsxFormatAlignmentData() :
@ -262,10 +192,24 @@ public:
P_NumFmt_FormatCode, P_NumFmt_FormatCode,
//font //font
P_Font_, P_Font_STARTID,
P_Font_Size = P_Font_STARTID,
//alignment P_Font_Italic,
P_Alignment_, P_Font_StrikeOut,
P_Font_Color,
P_Font_ThemeColor,
P_Font_Bold,
P_Font_Script,
P_Font_Underline,
P_Font_Outline,
P_Font_Shadow,
P_Font_Name,
P_Font_Family,
P_Font_Charset,
P_Font_Scheme,
P_Font_Condense,
P_Font_Extend,
P_Font_ENDID,
//border //border
P_Border_, P_Border_,
@ -273,6 +217,9 @@ public:
//fill //fill
P_Fill_, P_Fill_,
//alignment
P_Alignment_,
//protection //protection
P_Protection_, P_Protection_,
}; };
@ -281,21 +228,25 @@ public:
FormatPrivate(const FormatPrivate &other); FormatPrivate(const FormatPrivate &other);
~FormatPrivate(); ~FormatPrivate();
XlsxFormatFontData fontData;
XlsxFormatAlignmentData alignmentData; XlsxFormatAlignmentData alignmentData;
XlsxFormatBorderData borderData; XlsxFormatBorderData borderData;
XlsxFormatFillData fillData; XlsxFormatFillData fillData;
XlsxFormatProtectionData protectionData; XlsxFormatProtectionData protectionData;
mutable bool dirty; //The key re-generation is need. bool dirty; //The key re-generation is need.
mutable QByteArray formatKey; QByteArray formatKey;
bool font_dirty;
bool font_index_valid;
QByteArray font_key;
int font_index;
int xf_index; int xf_index;
mutable bool xf_indexValid; bool xf_indexValid;
bool is_dxf_fomat; bool is_dxf_fomat;
int dxf_index; int dxf_index;
mutable bool dxf_indexValid; bool dxf_indexValid;
int theme; int theme;

69
src/xlsx/xlsxsharedstrings.cpp

@ -130,57 +130,68 @@ void SharedStrings::writeRichStringPart_rPr(XmlStreamWriter &writer, Format *for
if (!format) if (!format)
return; return;
XlsxFormatFontData *font = &format->d->fontData; if (format->fontBold())
if (font->bold)
writer.writeEmptyElement(QStringLiteral("b")); writer.writeEmptyElement(QStringLiteral("b"));
if (font->italic) if (format->fontItalic())
writer.writeEmptyElement(QStringLiteral("i")); writer.writeEmptyElement(QStringLiteral("i"));
if (font->strikeOut) if (format->fontStrikeOut())
writer.writeEmptyElement(QStringLiteral("strike")); writer.writeEmptyElement(QStringLiteral("strike"));
if (font->outline) if (format->fontOutline())
writer.writeEmptyElement(QStringLiteral("outline")); writer.writeEmptyElement(QStringLiteral("outline"));
if (font->shadow) if (format->boolProperty(FormatPrivate::P_Font_Shadow))
writer.writeEmptyElement(QStringLiteral("shadow")); writer.writeEmptyElement(QStringLiteral("shadow"));
if (font->underline != Format::FontUnderlineNone) { if (format->hasProperty(FormatPrivate::P_Font_Underline)) {
Format::FontUnderline u = format->fontUnderline();
if (u != Format::FontUnderlineNone) {
writer.writeEmptyElement(QStringLiteral("u")); writer.writeEmptyElement(QStringLiteral("u"));
if (font->underline == Format::FontUnderlineDouble) if (u== Format::FontUnderlineDouble)
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("double")); writer.writeAttribute(QStringLiteral("val"), QStringLiteral("double"));
else if (font->underline == Format::FontUnderlineSingleAccounting) else if (u == Format::FontUnderlineSingleAccounting)
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("singleAccounting")); writer.writeAttribute(QStringLiteral("val"), QStringLiteral("singleAccounting"));
else if (font->underline == Format::FontUnderlineDoubleAccounting) else if (u == Format::FontUnderlineDoubleAccounting)
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("doubleAccounting")); writer.writeAttribute(QStringLiteral("val"), QStringLiteral("doubleAccounting"));
} }
if (font->scirpt != Format::FontScriptNormal) { }
if (format->hasProperty(FormatPrivate::P_Font_Script)) {
Format::FontScript s = format->fontScript();
if (s != Format::FontScriptNormal) {
writer.writeEmptyElement(QStringLiteral("vertAlign")); writer.writeEmptyElement(QStringLiteral("vertAlign"));
if (font->scirpt == Format::FontScriptSuper) if (s == Format::FontScriptSuper)
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("superscript")); writer.writeAttribute(QStringLiteral("val"), QStringLiteral("superscript"));
else else
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("subscript")); writer.writeAttribute(QStringLiteral("val"), QStringLiteral("subscript"));
} }
}
if (format->hasProperty(FormatPrivate::P_Font_Size)) {
writer.writeEmptyElement(QStringLiteral("sz")); writer.writeEmptyElement(QStringLiteral("sz"));
writer.writeAttribute(QStringLiteral("val"), QString::number(font->size)); writer.writeAttribute(QStringLiteral("val"), QString::number(format->fontSize()));
}
if (font->color.isValid()) { if (format->fontColor().isValid()) {
writer.writeEmptyElement(QStringLiteral("color")); writer.writeEmptyElement(QStringLiteral("color"));
QString color = font->color.name(); QString color = format->fontColor().name();
writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.mid(1));//remove # writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.mid(1));//remove #
} else if (!font->themeColor.isEmpty()) { } else if (format->hasProperty(FormatPrivate::P_Font_ThemeColor)) {
writer.writeEmptyElement(QStringLiteral("color")); writer.writeEmptyElement(QStringLiteral("color"));
QStringList themes = font->themeColor.split(QLatin1Char(':')); QStringList themes = format->stringProperty(FormatPrivate::P_Font_ThemeColor).split(QLatin1Char(':'));
writer.writeAttribute(QStringLiteral("theme"), themes[0]); writer.writeAttribute(QStringLiteral("theme"), themes[0]);
if (!themes[1].isEmpty()) if (!themes[1].isEmpty())
writer.writeAttribute(QStringLiteral("tint"), themes[1]); writer.writeAttribute(QStringLiteral("tint"), themes[1]);
} }
if (!format->fontName().isEmpty()) {
writer.writeEmptyElement(QStringLiteral("rFont")); writer.writeEmptyElement(QStringLiteral("rFont"));
writer.writeAttribute(QStringLiteral("val"), font->name); writer.writeAttribute(QStringLiteral("val"), format->fontName());
}
if (format->hasProperty(FormatPrivate::P_Font_Family)) {
writer.writeEmptyElement(QStringLiteral("family")); writer.writeEmptyElement(QStringLiteral("family"));
writer.writeAttribute(QStringLiteral("val"), QString::number(font->family)); writer.writeAttribute(QStringLiteral("val"), QString::number(format->intProperty(FormatPrivate::P_Font_Family)));
if (font->name == QLatin1String("Calibri")) { }
if (format->hasProperty(FormatPrivate::P_Font_Scheme)) {
writer.writeEmptyElement(QStringLiteral("scheme")); writer.writeEmptyElement(QStringLiteral("scheme"));
writer.writeAttribute(QStringLiteral("val"), font->scheme); writer.writeAttribute(QStringLiteral("val"), format->stringProperty(FormatPrivate::P_Font_Scheme));
} }
} }
@ -303,9 +314,9 @@ Format *SharedStrings::readRichStringPart_rPr(XmlStreamReader &reader, RichStrin
if (reader.name() == QLatin1String("rFont")) { if (reader.name() == QLatin1String("rFont")) {
format->setFontName(attributes.value(QLatin1String("val")).toString()); format->setFontName(attributes.value(QLatin1String("val")).toString());
} else if (reader.name() == QLatin1String("charset")) { } else if (reader.name() == QLatin1String("charset")) {
format->d->fontData.charset = attributes.value(QLatin1String("val")).toString().toInt(); format->setProperty(FormatPrivate::P_Font_Charset, attributes.value(QLatin1String("val")).toString().toInt());
} else if (reader.name() == QLatin1String("family")) { } else if (reader.name() == QLatin1String("family")) {
format->d->fontData.family = attributes.value(QLatin1String("val")).toString().toInt(); format->setProperty(FormatPrivate::P_Font_Family, attributes.value(QLatin1String("val")).toString().toInt());
} else if (reader.name() == QLatin1String("b")) { } else if (reader.name() == QLatin1String("b")) {
format->setFontBold(true); format->setFontBold(true);
} else if (reader.name() == QLatin1String("i")) { } else if (reader.name() == QLatin1String("i")) {
@ -315,11 +326,11 @@ Format *SharedStrings::readRichStringPart_rPr(XmlStreamReader &reader, RichStrin
} else if (reader.name() == QLatin1String("outline")) { } else if (reader.name() == QLatin1String("outline")) {
format->setFontOutline(true); format->setFontOutline(true);
} else if (reader.name() == QLatin1String("shadow")) { } else if (reader.name() == QLatin1String("shadow")) {
format->d->fontData.shadow = true; format->setProperty(FormatPrivate::P_Font_Shadow, true);
} else if (reader.name() == QLatin1String("condense")) { } else if (reader.name() == QLatin1String("condense")) {
format->d->fontData.condense = attributes.value(QLatin1String("val")).toString().toInt(); format->setProperty(FormatPrivate::P_Font_Condense, attributes.value(QLatin1String("val")).toString().toInt());
} else if (reader.name() == QLatin1String("extend")) { } else if (reader.name() == QLatin1String("extend")) {
format->d->fontData.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"))) { if (attributes.hasAttribute(QLatin1String("rgb"))) {
QString colorString = attributes.value(QLatin1String("rgb")).toString(); QString colorString = attributes.value(QLatin1String("rgb")).toString();
@ -329,7 +340,7 @@ Format *SharedStrings::readRichStringPart_rPr(XmlStreamReader &reader, RichStrin
} else if (attributes.hasAttribute(QLatin1String("theme"))) { } else if (attributes.hasAttribute(QLatin1String("theme"))) {
QString theme = attributes.value(QLatin1String("theme")).toString(); QString theme = attributes.value(QLatin1String("theme")).toString();
QString tint = attributes.value(QLatin1String("tint")).toString(); QString tint = attributes.value(QLatin1String("tint")).toString();
format->d->fontData.themeColor = theme + QLatin1Char(':') + tint; 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());
@ -350,7 +361,7 @@ Format *SharedStrings::readRichStringPart_rPr(XmlStreamReader &reader, RichStrin
else if (value == QLatin1String("subscript")) else if (value == QLatin1String("subscript"))
format->setFontScript(Format::FontScriptSub); format->setFontScript(Format::FontScriptSub);
} else if (reader.name() == QLatin1String("scheme")) { } else if (reader.name() == QLatin1String("scheme")) {
format->d->fontData.scheme = attributes.value(QLatin1String("val")).toString(); format->setProperty(FormatPrivate::P_Font_Scheme, attributes.value(QLatin1String("val")).toString());
} }
} }
} }

180
src/xlsx/xlsxstyles.cpp

@ -151,12 +151,12 @@ void Styles::addFormat(Format *format, bool force)
//Font //Font
if (!format->fontIndexValid()) { if (!format->fontIndexValid()) {
if (!m_fontsHash.contains(format->fontKey())) { if (!m_fontsHash.contains(format->fontKey())) {
QSharedPointer<XlsxFormatFontData> font = QSharedPointer<XlsxFormatFontData>(new XlsxFormatFontData(format->d->fontData)); format->setFontIndex(m_fontsList.size()); //Assign proper index
font->setIndex(m_fontsList.size()); //Assign proper index m_fontsList.append(format);
m_fontsList.append(font); m_fontsHash[format->fontKey()] = format;
m_fontsHash[font->key()] = font; } else {
format->setFontIndex(m_fontsHash[format->fontKey()]->fontIndex());
} }
format->setFontIndex(m_fontsHash[format->fontKey()]->index());
} }
//Fill //Fill
@ -287,81 +287,77 @@ void Styles::writeFonts(XmlStreamWriter &writer)
writer.writeStartElement(QStringLiteral("fonts")); writer.writeStartElement(QStringLiteral("fonts"));
writer.writeAttribute(QStringLiteral("count"), QString::number(m_fontsList.count())); writer.writeAttribute(QStringLiteral("count"), QString::number(m_fontsList.count()));
for (int i=0; i<m_fontsList.size(); ++i) { for (int i=0; i<m_fontsList.size(); ++i) {
QSharedPointer<XlsxFormatFontData> font = m_fontsList[i]; Format *format = m_fontsList[i];
writer.writeStartElement(QStringLiteral("font")); writer.writeStartElement(QStringLiteral("font"));
if (font->bold)
if (format->fontBold())
writer.writeEmptyElement(QStringLiteral("b")); writer.writeEmptyElement(QStringLiteral("b"));
if (font->italic) if (format->fontItalic())
writer.writeEmptyElement(QStringLiteral("i")); writer.writeEmptyElement(QStringLiteral("i"));
if (font->strikeOut) if (format->fontStrikeOut())
writer.writeEmptyElement(QStringLiteral("strike")); writer.writeEmptyElement(QStringLiteral("strike"));
if (font->outline) if (format->fontOutline())
writer.writeEmptyElement(QStringLiteral("outline")); writer.writeEmptyElement(QStringLiteral("outline"));
if (font->shadow) if (format->boolProperty(FormatPrivate::P_Font_Shadow))
writer.writeEmptyElement(QStringLiteral("shadow")); writer.writeEmptyElement(QStringLiteral("shadow"));
if (font->underline != Format::FontUnderlineNone) { if (format->hasProperty(FormatPrivate::P_Font_Underline)) {
Format::FontUnderline u = format->fontUnderline();
if (u != Format::FontUnderlineNone) {
writer.writeEmptyElement(QStringLiteral("u")); writer.writeEmptyElement(QStringLiteral("u"));
if (font->underline == Format::FontUnderlineDouble) if (u== Format::FontUnderlineDouble)
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("double")); writer.writeAttribute(QStringLiteral("val"), QStringLiteral("double"));
else if (font->underline == Format::FontUnderlineSingleAccounting) else if (u == Format::FontUnderlineSingleAccounting)
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("singleAccounting")); writer.writeAttribute(QStringLiteral("val"), QStringLiteral("singleAccounting"));
else if (font->underline == Format::FontUnderlineDoubleAccounting) else if (u == Format::FontUnderlineDoubleAccounting)
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("doubleAccounting")); writer.writeAttribute(QStringLiteral("val"), QStringLiteral("doubleAccounting"));
} }
if (font->scirpt != Format::FontScriptNormal) { }
writer.writeEmptyElement(QStringLiteral("vertAligh")); if (format->hasProperty(FormatPrivate::P_Font_Script)) {
if (font->scirpt == Format::FontScriptSuper) Format::FontScript s = format->fontScript();
if (s != Format::FontScriptNormal) {
writer.writeEmptyElement(QStringLiteral("vertAlign"));
if (s == Format::FontScriptSuper)
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("superscript")); writer.writeAttribute(QStringLiteral("val"), QStringLiteral("superscript"));
else else
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("subscript")); writer.writeAttribute(QStringLiteral("val"), QStringLiteral("subscript"));
} }
}
if (format->hasProperty(FormatPrivate::P_Font_Size)) {
writer.writeEmptyElement(QStringLiteral("sz")); writer.writeEmptyElement(QStringLiteral("sz"));
writer.writeAttribute(QStringLiteral("val"), QString::number(font->size)); writer.writeAttribute(QStringLiteral("val"), QString::number(format->fontSize()));
}
if (font->color.isValid()) { if (format->fontColor().isValid()) {
writer.writeEmptyElement(QStringLiteral("color")); writer.writeEmptyElement(QStringLiteral("color"));
QString color = font->color.name(); QString color = format->fontColor().name();
writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.mid(1));//remove # writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.mid(1));//remove #
} else if (!font->themeColor.isEmpty()) { } else if (format->hasProperty(FormatPrivate::P_Font_ThemeColor)) {
writer.writeEmptyElement(QStringLiteral("color")); writer.writeEmptyElement(QStringLiteral("color"));
QStringList themes = font->themeColor.split(QLatin1Char(':')); QStringList themes = format->stringProperty(FormatPrivate::P_Font_ThemeColor).split(QLatin1Char(':'));
writer.writeAttribute(QStringLiteral("theme"), themes[0]); writer.writeAttribute(QStringLiteral("theme"), themes[0]);
if (!themes[1].isEmpty()) if (!themes[1].isEmpty())
writer.writeAttribute(QStringLiteral("tint"), themes[1]); writer.writeAttribute(QStringLiteral("tint"), themes[1]);
} }
if (!format->fontName().isEmpty()) {
writer.writeEmptyElement(QStringLiteral("name")); writer.writeEmptyElement(QStringLiteral("name"));
writer.writeAttribute(QStringLiteral("val"), font->name); writer.writeAttribute(QStringLiteral("val"), format->fontName());
}
if (format->hasProperty(FormatPrivate::P_Font_Family)) {
writer.writeEmptyElement(QStringLiteral("family")); writer.writeEmptyElement(QStringLiteral("family"));
writer.writeAttribute(QStringLiteral("val"), QString::number(font->family)); writer.writeAttribute(QStringLiteral("val"), QString::number(format->intProperty(FormatPrivate::P_Font_Family)));
if (font->name == QLatin1String("Calibri")) { }
if (format->hasProperty(FormatPrivate::P_Font_Scheme)) {
writer.writeEmptyElement(QStringLiteral("scheme")); writer.writeEmptyElement(QStringLiteral("scheme"));
writer.writeAttribute(QStringLiteral("val"), font->scheme); writer.writeAttribute(QStringLiteral("val"), format->stringProperty(FormatPrivate::P_Font_Scheme));
} }
// if (!format->isDxfFormat()) { // if (!format->isDxfFormat()) {
// writer.writeEmptyElement(QStringLiteral("sz")); // writer.writeEmptyElement(QStringLiteral("sz"));
// writer.writeAttribute(QStringLiteral("val"), QString::number(format->fontSize())); // writer.writeAttribute(QStringLiteral("val"), QString::number(format->fontSize()));
// } // }
//
// //font color
// if (format->theme()) {
// writer.writeEmptyElement(QStringLiteral("color"));
// writer.writeAttribute(QStringLiteral("theme"), QString::number(format->theme()));
// } else if (format->colorIndexed()) {
// writer.writeEmptyElement(QStringLiteral("color"));
// writer.writeAttribute(QStringLiteral("indexed"), QString::number(format->colorIndexed()));
// } else 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->isDxfFormat()) {
// writer.writeEmptyElement(QStringLiteral("color"));
// writer.writeAttribute(QStringLiteral("theme"), QStringLiteral("1"));
// }
// if (!format->isDxfFormat()) { // if (!format->isDxfFormat()) {
// writer.writeEmptyElement(QStringLiteral("name")); // writer.writeEmptyElement(QStringLiteral("name"));
// writer.writeAttribute(QStringLiteral("val"), format->fontName()); // writer.writeAttribute(QStringLiteral("val"), format->fontName());
@ -602,72 +598,78 @@ bool Styles::readNumFmts(XmlStreamReader &reader)
bool Styles::readFonts(XmlStreamReader &reader) bool Styles::readFonts(XmlStreamReader &reader)
{ {
Q_ASSERT(reader.name() == QLatin1String("fonts")); Q_ASSERT(reader.name() == QLatin1String("fonts"));
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attrs = reader.attributes();
int count = attributes.value(QLatin1String("count")).toString().toInt(); int count = attrs.value(QLatin1String("count")).toString().toInt();
for (int i=0; i<count; ++i) { for (int i=0; i<count; ++i) {
reader.readNextStartElement(); reader.readNextStartElement();
if (reader.name() != QLatin1String("font")) if (reader.name() != QLatin1String("font"))
return false; return false;
QSharedPointer<XlsxFormatFontData> font(new XlsxFormatFontData); Format *format = createFormat();
while((reader.readNextStartElement(),true)) { //read until font endelement. while((reader.readNextStartElement(),true)) { //read until font endelement.
if (reader.tokenType() == QXmlStreamReader::StartElement) { if (reader.tokenType() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("b")) { QXmlStreamAttributes attributes = reader.attributes();
font->bold = true; if (reader.name() == QLatin1String("name")) {
format->setFontName(attributes.value(QLatin1String("val")).toString());
} else if (reader.name() == QLatin1String("charset")) {
format->setProperty(FormatPrivate::P_Font_Charset, attributes.value(QLatin1String("val")).toString().toInt());
} else if (reader.name() == QLatin1String("family")) {
format->setProperty(FormatPrivate::P_Font_Family, attributes.value(QLatin1String("val")).toString().toInt());
} else if (reader.name() == QLatin1String("b")) {
format->setFontBold(true);
} else if (reader.name() == QLatin1String("i")) { } else if (reader.name() == QLatin1String("i")) {
font->italic = true; format->setFontItalic(true);
} else if (reader.name() == QLatin1String("strike")) { } else if (reader.name() == QLatin1String("strike")) {
font->strikeOut = true; format->setFontStrikeOut(true);
} else if (reader.name() == QLatin1String("outline")) { } else if (reader.name() == QLatin1String("outline")) {
font->outline = true; format->setFontOutline(true);
} else if (reader.name() == QLatin1String("shadow")) { } else if (reader.name() == QLatin1String("shadow")) {
font->shadow = true; format->setProperty(FormatPrivate::P_Font_Shadow, true);
} else if (reader.name() == QLatin1String("u")) { } else if (reader.name() == QLatin1String("condense")) {
QXmlStreamAttributes attributes = reader.attributes(); format->setProperty(FormatPrivate::P_Font_Condense, attributes.value(QLatin1String("val")).toString().toInt());
QString value = attributes.value(QLatin1String("val")).toString(); } else if (reader.name() == QLatin1String("extend")) {
if (value == QLatin1String("double")) format->setProperty(FormatPrivate::P_Font_Extend, attributes.value(QLatin1String("val")).toString().toInt());
font->underline = Format::FontUnderlineDouble;
else if (value == QLatin1String("doubleAccounting"))
font->underline = Format::FontUnderlineDoubleAccounting;
else if (value == QLatin1String("singleAccounting"))
font->underline = Format::FontUnderlineSingleAccounting;
else
font->underline = Format::FontUnderlineSingle;
} else if (reader.name() == QLatin1String("vertAligh")) {
QXmlStreamAttributes attributes = reader.attributes();
QString value = attributes.value(QLatin1String("val")).toString();
if (value == QLatin1String("superscript"))
font->scirpt = Format::FontScriptSuper;
else
font->scirpt = Format::FontScriptSub;
} else if (reader.name() == QLatin1String("sz")) {
font->size = reader.attributes().value(QLatin1String("val")).toString().toInt();
} else if (reader.name() == QLatin1String("color")) { } else if (reader.name() == QLatin1String("color")) {
QXmlStreamAttributes attributes = reader.attributes();
if (attributes.hasAttribute(QLatin1String("rgb"))) { if (attributes.hasAttribute(QLatin1String("rgb"))) {
QString colorString = attributes.value(QLatin1String("rgb")).toString(); QString colorString = attributes.value(QLatin1String("rgb")).toString();
font->color = fromARGBString(colorString); format->setFontColor(fromARGBString(colorString));
} else if (attributes.hasAttribute(QLatin1String("indexed"))) { } else if (attributes.hasAttribute(QLatin1String("indexed"))) {
font->color = getColorByIndex(attributes.value(QLatin1String("indexed")).toString().toInt()); QColor color = getColorByIndex(attributes.value(QLatin1String("indexed")).toString().toInt());
format->setFontColor(color);
} else if (attributes.hasAttribute(QLatin1String("theme"))) { } else if (attributes.hasAttribute(QLatin1String("theme"))) {
QString theme = attributes.value(QLatin1String("theme")).toString(); QString theme = attributes.value(QLatin1String("theme")).toString();
QString tint = attributes.value(QLatin1String("tint")).toString(); QString tint = attributes.value(QLatin1String("tint")).toString();
font->themeColor = theme + QLatin1Char(':') + tint; format->setProperty(FormatPrivate::P_Font_ThemeColor, QString(theme + QLatin1Char(':') + tint));
} }
} else if (reader.name() == QLatin1String("name")) { } else if (reader.name() == QLatin1String("sz")) {
font->name = reader.attributes().value(QLatin1String("val")).toString(); int sz = attributes.value(QLatin1String("val")).toString().toInt();
} else if (reader.name() == QLatin1String("family")) { format->setFontSize(sz);
font->family = reader.attributes().value(QLatin1String("val")).toString().toInt(); } else if (reader.name() == QLatin1String("u")) {
QString value = attributes.value(QLatin1String("val")).toString();
if (value == QLatin1String("double"))
format->setFontUnderline(Format::FontUnderlineDouble);
else if (value == QLatin1String("doubleAccounting"))
format->setFontUnderline(Format::FontUnderlineDoubleAccounting);
else if (value == QLatin1String("singleAccounting"))
format->setFontUnderline(Format::FontUnderlineSingleAccounting);
else
format->setFontUnderline(Format::FontUnderlineSingle);
} else if (reader.name() == QLatin1String("vertAlign")) {
QString value = attributes.value(QLatin1String("val")).toString();
if (value == QLatin1String("superscript"))
format->setFontScript(Format::FontScriptSuper);
else if (value == QLatin1String("subscript"))
format->setFontScript(Format::FontScriptSub);
} else if (reader.name() == QLatin1String("scheme")) { } else if (reader.name() == QLatin1String("scheme")) {
font->scheme = reader.attributes().value(QLatin1String("val")).toString(); format->setProperty(FormatPrivate::P_Font_Scheme, attributes.value(QLatin1String("val")).toString());
} }
} }
if (reader.tokenType() == QXmlStreamReader::EndElement && reader.name() == QLatin1String("font")) if (reader.tokenType() == QXmlStreamReader::EndElement && reader.name() == QLatin1String("font"))
break; break;
} }
m_fontsList.append(font); m_fontsList.append(format);
m_fontsHash.insert(font->key(), font); m_fontsHash.insert(format->fontKey(), format);
font->setIndex(m_fontsList.size()-1);//first call key(), then setIndex() format->setFontIndex(m_fontsList.size()-1);
} }
return true; return true;
} }
@ -907,7 +909,11 @@ bool Styles::readCellXfs(XmlStreamReader &reader)
if (fontIndex >= m_fontsList.size()) { if (fontIndex >= m_fontsList.size()) {
qDebug("Error read styles.xml, cellXfs fontId"); qDebug("Error read styles.xml, cellXfs fontId");
} else { } else {
format->d->fontData = *m_fontsList[fontIndex]; 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));
}
} }
} }

5
src/xlsx/xlsxstyles_p.h

@ -40,7 +40,6 @@ class StylesTest;
namespace QXlsx { namespace QXlsx {
class Format; class Format;
struct XlsxFormatFontData;
struct XlsxFormatFillData; struct XlsxFormatFillData;
struct XlsxFormatBorderData; struct XlsxFormatBorderData;
class XmlStreamWriter; class XmlStreamWriter;
@ -98,10 +97,10 @@ private:
QMap<int, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtIdMap; QMap<int, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtIdMap;
QHash<QString, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtsHash; QHash<QString, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtsHash;
int m_nextCustomNumFmtId; int m_nextCustomNumFmtId;
QList<QSharedPointer<XlsxFormatFontData> > m_fontsList; //Keep a copy of unique fonts QList<Format *> m_fontsList;
QList<QSharedPointer<XlsxFormatFillData> > m_fillsList; //Keep a copy of unique fills QList<QSharedPointer<XlsxFormatFillData> > m_fillsList; //Keep a copy of unique fills
QList<QSharedPointer<XlsxFormatBorderData> > m_bordersList; //Keep a copy of unique borders QList<QSharedPointer<XlsxFormatBorderData> > m_bordersList; //Keep a copy of unique borders
QHash<QByteArray, QSharedPointer<XlsxFormatFontData> > m_fontsHash; QHash<QByteArray, Format *> m_fontsHash;
QHash<QByteArray, QSharedPointer<XlsxFormatFillData> > m_fillsHash; QHash<QByteArray, QSharedPointer<XlsxFormatFillData> > m_fillsHash;
QHash<QByteArray, QSharedPointer<XlsxFormatBorderData> > m_bordersHash; QHash<QByteArray, QSharedPointer<XlsxFormatBorderData> > m_bordersHash;

3
tests/auto/styles/tst_stylestest.cpp

@ -95,6 +95,9 @@ void StylesTest::testReadFonts()
styles.readFonts(reader); styles.readFonts(reader);
QCOMPARE(styles.m_fontsList.size(), 3); QCOMPARE(styles.m_fontsList.size(), 3);
QXlsx::Format *font0 = styles.m_fontsList[0];
QCOMPARE(font0->fontSize(), 11);
QCOMPARE(font0->fontName(), QString("Calibri"));
} }
void StylesTest::testReadFills() void StylesTest::testReadFills()

Loading…
Cancel
Save