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. 117
      src/xlsx/xlsxsharedstrings.cpp
  5. 200
      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
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)
: QSharedData(other)
, fontData(other.fontData), alignmentData(other.alignmentData)
, alignmentData(other.alignmentData)
, borderData(other.borderData), fillData(other.fillData), protectionData(other.protectionData)
, 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)
, is_dxf_fomat(other.is_dxf_fomat), dxf_index(other.dxf_index), dxf_indexValid(other.dxf_indexValid)
, theme(other.theme)
@ -181,7 +178,7 @@ void Format::setNumberFormat(int id, const QString &format)
*/
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)
{
d->fontData.size = size;
d->fontData._dirty = true;
setProperty(FormatPrivate::P_Font_Size, size);
}
/*!
@ -198,7 +194,7 @@ void Format::setFontSize(int size)
*/
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)
{
d->fontData.italic = italic;
d->fontData._dirty = true;
setProperty(FormatPrivate::P_Font_Italic, italic);
}
/*!
@ -215,7 +210,7 @@ void Format::setFontItalic(bool italic)
*/
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)
{
d->fontData.strikeOut = strikeOut;
d->fontData._dirty = true;
setProperty(FormatPrivate::P_Font_StrikeOut, strikeOut);
}
/*!
@ -232,12 +226,15 @@ void Format::setFontStrikeOut(bool strikeOut)
*/
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
//The same is ture for border and fill colord
return QColor();
}
return d->fontData.color;
return QColor();
}
/*!
@ -245,8 +242,7 @@ QColor Format::fontColor() const
*/
void Format::setFontColor(const QColor &color)
{
d->fontData.color = color;
d->fontData._dirty = true;
setProperty(FormatPrivate::P_Font_Color, color);
}
/*!
@ -254,7 +250,7 @@ void Format::setFontColor(const QColor &color)
*/
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)
{
d->fontData.bold = bold;
d->fontData._dirty = true;
setProperty(FormatPrivate::P_Font_Bold, bold);
}
/*!
@ -271,7 +266,7 @@ void Format::setFontBold(bool bold)
*/
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)
{
d->fontData.scirpt = script;
d->fontData._dirty = true;
setProperty(FormatPrivate::P_Font_Script, script);
}
/*!
@ -288,7 +282,7 @@ void Format::setFontScript(FontScript script)
*/
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)
{
d->fontData.underline = underline;
d->fontData._dirty = true;
setProperty(FormatPrivate::P_Font_Underline, underline);
}
/*!
@ -305,7 +298,7 @@ void Format::setFontUnderline(FontUnderline underline)
*/
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)
{
d->fontData.outline = outline;
d->fontData._dirty = true;
setProperty(FormatPrivate::P_Font_Outline, outline);
}
/*!
@ -322,7 +314,7 @@ void Format::setFontOutline(bool outline)
*/
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)
{
d->fontData.name = name;
d->fontData._dirty = true;
setProperty(FormatPrivate::P_Font_Name, name);
}
/*!
@ -339,7 +330,7 @@ void Format::setFontName(const QString &name)
*/
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
{
return d->fontData.index();
}
if (fontIndexValid())
return d->font_index;
/*!
* \internal
*/
void Format::setFontIndex(int index)
{
d->fontData.setIndex(index);
return -1;
}
/*!
* \internal
*/
int Format::fontFamily() const
{
return d->fontData.family;
}
/*!
* \internal
*/
bool Format::fontShadow() const
{
return d->fontData.shadow;
}
/*!
* \internal
*/
QString Format::fontScheme() const
void Format::setFontIndex(int index)
{
return d->fontData.scheme;
d->font_index = index;
d->font_index_valid = true;
}
/* Internal
*/
QByteArray Format::fontKey() const
{
if (d->fontData._dirty)
d->dirty = true; //Make sure formatKey() will be re-generated.
return d->fontData.key();
if (d->font_dirty) {
QByteArray 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
{
if (d->dirty || d->fontData._dirty || d->borderData._dirty || d->fillData._dirty) {
if (d->dirty || d->borderData._dirty || d->fillData._dirty) {
QByteArray key;
QDataStream stream(&key, QIODevice::WriteOnly);
stream<<fontKey()<<borderKey()<<fillKey()
@ -933,11 +914,25 @@ QVariant Format::property(int propertyId) const
*/
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;
else
} else {
if (!d->property.contains(propertyId))
return;
d.detach();
d->property.remove(propertyId);
}
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)
{
d->property.remove(propertyId);
d->dirty = true;
setProperty(propertyId, QVariant());
}
/*!

8
src/xlsx/xlsxformat.h

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

109
src/xlsx/xlsxformat_p.h

@ -30,76 +30,6 @@
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
{
XlsxFormatAlignmentData() :
@ -262,10 +192,24 @@ public:
P_NumFmt_FormatCode,
//font
P_Font_,
//alignment
P_Alignment_,
P_Font_STARTID,
P_Font_Size = P_Font_STARTID,
P_Font_Italic,
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
P_Border_,
@ -273,6 +217,9 @@ public:
//fill
P_Fill_,
//alignment
P_Alignment_,
//protection
P_Protection_,
};
@ -281,21 +228,25 @@ public:
FormatPrivate(const FormatPrivate &other);
~FormatPrivate();
XlsxFormatFontData fontData;
XlsxFormatAlignmentData alignmentData;
XlsxFormatBorderData borderData;
XlsxFormatFillData fillData;
XlsxFormatProtectionData protectionData;
mutable bool dirty; //The key re-generation is need.
mutable QByteArray formatKey;
bool dirty; //The key re-generation is need.
QByteArray formatKey;
bool font_dirty;
bool font_index_valid;
QByteArray font_key;
int font_index;
int xf_index;
mutable bool xf_indexValid;
bool xf_indexValid;
bool is_dxf_fomat;
int dxf_index;
mutable bool dxf_indexValid;
bool dxf_indexValid;
int theme;

117
src/xlsx/xlsxsharedstrings.cpp

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

200
src/xlsx/xlsxstyles.cpp

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

3
tests/auto/styles/tst_stylestest.cpp

@ -95,6 +95,9 @@ void StylesTest::testReadFonts()
styles.readFonts(reader);
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()

Loading…
Cancel
Save