Browse Source

RichString: Using Format instead of Format *

master
Debao Zhang 11 years ago
parent
commit
390440d0fa
  1. 19
      src/xlsx/xlsxformat.cpp
  2. 4
      src/xlsx/xlsxformat.h
  3. 23
      src/xlsx/xlsxrichstring.cpp
  4. 10
      src/xlsx/xlsxrichstring_p.h
  5. 98
      src/xlsx/xlsxsharedstrings.cpp
  6. 5
      src/xlsx/xlsxsharedstrings_p.h
  7. 2
      src/xlsx/xlsxworksheet.cpp
  8. 12
      tests/auto/richstring/tst_richstringtest.cpp
  9. 27
      tests/auto/sharedstrings/tst_sharedstringstest.cpp

19
src/xlsx/xlsxformat.cpp

@ -50,7 +50,7 @@ FormatPrivate::FormatPrivate(const FormatPrivate &other)
, 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)
, property(property)
, property(other.property)
{
}
@ -375,6 +375,15 @@ QByteArray Format::fontKey() const
return d->font_key;
}
bool Format::hasFontData() const
{
for (int i=FormatPrivate::P_Font_STARTID; i<FormatPrivate::P_Font_ENDID; ++i) {
if (hasProperty(i))
return true;
}
return false;
}
/*!
* Return the horizontal alignment.
*/
@ -776,6 +785,14 @@ void Format::setLocked(bool locked)
setProperty(FormatPrivate::P_Protection_Locked, locked);
}
/*!
Returns true if the format is valid; otherwise returns false.
*/
bool Format::isValid() const
{
return !d->property.isEmpty();
}
QByteArray Format::formatKey() const
{
if (d->dirty) {

4
src/xlsx/xlsxformat.h

@ -213,6 +213,8 @@ public:
bool hidden() const;
void setHidden(bool hidden);
bool isValid() const;
bool operator == (const Format &format) const;
bool operator != (const Format &format) const;
@ -232,6 +234,7 @@ private:
friend class Worksheet;
friend class WorksheetPrivate;
friend class RichString;
friend class SharedStrings;
friend class ::FormatTest;
bool fontIndexValid() const;
@ -239,6 +242,7 @@ private:
void setFontIndex(int index);
QByteArray fontKey() const;
bool hasFontData() const;
bool hasAlignmentData() const;
bool borderIndexValid() const;

23
src/xlsx/xlsxrichstring.cpp

@ -35,7 +35,7 @@ RichString::RichString()
RichString::RichString(const QString text)
:m_dirty(true)
{
addFragment(text, 0);
addFragment(text, Format());
}
bool RichString::isRichString() const
@ -65,7 +65,7 @@ int RichString::fragmentCount() const
return m_fragmentTexts.size();
}
void RichString::addFragment(const QString &text, Format *format)
void RichString::addFragment(const QString &text, const Format &format)
{
m_fragmentTexts.append(text);
m_fragmentFormats.append(format);
@ -80,25 +80,14 @@ QString RichString::fragmentText(int index) const
return m_fragmentTexts[index];
}
Format *RichString::fragmentFormat(int index) const
Format RichString::fragmentFormat(int index) const
{
if (index < 0 || index >= fragmentCount())
return 0;
return Format();
return m_fragmentFormats[index];
}
/*!
* \internal
*/
Format *RichString::createFormat()
{
Format *format = new Format();
m_createdFormats.append(QSharedPointer<Format>(format));
return format;
}
/*!
* \internal
*/
@ -116,8 +105,8 @@ QByteArray RichString::idKey() const
bytes.append("@Text");
bytes.append(m_fragmentTexts[i].toUtf8());
bytes.append("@Format");
if (m_fragmentFormats[i])
bytes.append(m_fragmentFormats[i]->fontKey());
if (m_fragmentFormats[i].hasFontData())
bytes.append(m_fragmentFormats[i].fontKey());
}
}
rs->m_idKey = bytes;

10
src/xlsx/xlsxrichstring_p.h

@ -46,11 +46,10 @@ public:
QString toPlainString() const;
int fragmentCount() const;
void addFragment(const QString &text, Format *format);
void addFragment(const QString &text, const Format &format);
QString fragmentText(int index) const;
Format *fragmentFormat(int index) const;
Format fragmentFormat(int index) const;
Format *createFormat();
private:
friend XLSX_AUTOTEST_EXPORT uint qHash(const RichString &rs, uint seed) Q_DECL_NOTHROW;
friend XLSX_AUTOTEST_EXPORT bool operator==(const RichString &rs1, const RichString &rs2);
@ -60,10 +59,9 @@ private:
QByteArray idKey() const;
QStringList m_fragmentTexts;
QList<Format *> m_fragmentFormats;
QList<Format> m_fragmentFormats;
QByteArray m_idKey;
bool m_dirty;
QList<QSharedPointer<Format> > m_createdFormats;
};
@ -77,6 +75,6 @@ XLSX_AUTOTEST_EXPORT bool operator!=(const QString &rs1, const RichString &rs2);
QT_END_NAMESPACE_XLSX
Q_DECLARE_METATYPE(QXlsx::RichString);
Q_DECLARE_METATYPE(QXlsx::RichString)
#endif // XLSXRICHSTRING_P_H

98
src/xlsx/xlsxsharedstrings.cpp

@ -125,23 +125,23 @@ QList<RichString> SharedStrings::getSharedStrings() const
return m_stringList;
}
void SharedStrings::writeRichStringPart_rPr(XmlStreamWriter &writer, Format *format) const
void SharedStrings::writeRichStringPart_rPr(XmlStreamWriter &writer, const Format &format) const
{
if (!format)
if (!format.hasFontData())
return;
if (format->fontBold())
if (format.fontBold())
writer.writeEmptyElement(QStringLiteral("b"));
if (format->fontItalic())
if (format.fontItalic())
writer.writeEmptyElement(QStringLiteral("i"));
if (format->fontStrikeOut())
if (format.fontStrikeOut())
writer.writeEmptyElement(QStringLiteral("strike"));
if (format->fontOutline())
if (format.fontOutline())
writer.writeEmptyElement(QStringLiteral("outline"));
if (format->boolProperty(FormatPrivate::P_Font_Shadow))
if (format.boolProperty(FormatPrivate::P_Font_Shadow))
writer.writeEmptyElement(QStringLiteral("shadow"));
if (format->hasProperty(FormatPrivate::P_Font_Underline)) {
Format::FontUnderline u = format->fontUnderline();
if (format.hasProperty(FormatPrivate::P_Font_Underline)) {
Format::FontUnderline u = format.fontUnderline();
if (u != Format::FontUnderlineNone) {
writer.writeEmptyElement(QStringLiteral("u"));
if (u== Format::FontUnderlineDouble)
@ -152,8 +152,8 @@ void SharedStrings::writeRichStringPart_rPr(XmlStreamWriter &writer, Format *for
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("doubleAccounting"));
}
}
if (format->hasProperty(FormatPrivate::P_Font_Script)) {
Format::FontScript s = format->fontScript();
if (format.hasProperty(FormatPrivate::P_Font_Script)) {
Format::FontScript s = format.fontScript();
if (s != Format::FontScriptNormal) {
writer.writeEmptyElement(QStringLiteral("vertAlign"));
if (s == Format::FontScriptSuper)
@ -163,35 +163,35 @@ void SharedStrings::writeRichStringPart_rPr(XmlStreamWriter &writer, Format *for
}
}
if (format->hasProperty(FormatPrivate::P_Font_Size)) {
if (format.hasProperty(FormatPrivate::P_Font_Size)) {
writer.writeEmptyElement(QStringLiteral("sz"));
writer.writeAttribute(QStringLiteral("val"), QString::number(format->fontSize()));
writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontSize()));
}
if (format->fontColor().isValid()) {
if (format.fontColor().isValid()) {
writer.writeEmptyElement(QStringLiteral("color"));
QString color = format->fontColor().name();
QString color = format.fontColor().name();
writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.mid(1));//remove #
} else if (format->hasProperty(FormatPrivate::P_Font_ThemeColor)) {
} else if (format.hasProperty(FormatPrivate::P_Font_ThemeColor)) {
writer.writeEmptyElement(QStringLiteral("color"));
QStringList themes = format->stringProperty(FormatPrivate::P_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]);
}
if (!format->fontName().isEmpty()) {
if (!format.fontName().isEmpty()) {
writer.writeEmptyElement(QStringLiteral("rFont"));
writer.writeAttribute(QStringLiteral("val"), format->fontName());
writer.writeAttribute(QStringLiteral("val"), format.fontName());
}
if (format->hasProperty(FormatPrivate::P_Font_Family)) {
if (format.hasProperty(FormatPrivate::P_Font_Family)) {
writer.writeEmptyElement(QStringLiteral("family"));
writer.writeAttribute(QStringLiteral("val"), QString::number(format->intProperty(FormatPrivate::P_Font_Family)));
writer.writeAttribute(QStringLiteral("val"), QString::number(format.intProperty(FormatPrivate::P_Font_Family)));
}
if (format->hasProperty(FormatPrivate::P_Font_Scheme)) {
if (format.hasProperty(FormatPrivate::P_Font_Scheme)) {
writer.writeEmptyElement(QStringLiteral("scheme"));
writer.writeAttribute(QStringLiteral("val"), format->stringProperty(FormatPrivate::P_Font_Scheme));
writer.writeAttribute(QStringLiteral("val"), format.stringProperty(FormatPrivate::P_Font_Scheme));
}
}
@ -211,7 +211,7 @@ void SharedStrings::saveToXmlFile(QIODevice *device) const
//Rich text string
for (int i=0; i<string.fragmentCount(); ++i) {
writer.writeStartElement(QStringLiteral("r"));
if (string.fragmentFormat(i)) {
if (string.fragmentFormat(i).hasFontData()) {
writer.writeStartElement(QStringLiteral("rPr"));
writeRichStringPart_rPr(writer, string.fragmentFormat(i));
writer.writeEndElement();// rPr
@ -279,12 +279,12 @@ void SharedStrings::readRichStringPart(XmlStreamReader &reader, RichString &rich
Q_ASSERT(reader.name() == QLatin1String("r"));
QString text;
Format *format=0;
Format format;
while (!(reader.name() == QLatin1String("r") && reader.tokenType() == QXmlStreamReader::EndElement)) {
reader.readNextStartElement();
if (reader.tokenType() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("rPr")) {
format = readRichStringPart_rPr(reader, richString);
format = readRichStringPart_rPr(reader);
} else if (reader.name() == QLatin1String("t")) {
text = reader.readElementText();
}
@ -300,68 +300,68 @@ void SharedStrings::readPlainStringPart(XmlStreamReader &reader, RichString &ric
//QXmlStreamAttributes attributes = reader.attributes();
QString text = reader.readElementText();
richString.addFragment(text, 0);
richString.addFragment(text, Format());
}
Format *SharedStrings::readRichStringPart_rPr(XmlStreamReader &reader, RichString &richString)
Format SharedStrings::readRichStringPart_rPr(XmlStreamReader &reader)
{
Q_ASSERT(reader.name() == QLatin1String("rPr"));
Format *format = richString.createFormat();
Format format;
while (!(reader.name() == QLatin1String("rPr") && reader.tokenType() == QXmlStreamReader::EndElement)) {
reader.readNextStartElement();
if (reader.tokenType() == QXmlStreamReader::StartElement) {
QXmlStreamAttributes attributes = reader.attributes();
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")) {
format->setProperty(FormatPrivate::P_Font_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")) {
format->setProperty(FormatPrivate::P_Font_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")) {
format->setFontBold(true);
format.setFontBold(true);
} else if (reader.name() == QLatin1String("i")) {
format->setFontItalic(true);
format.setFontItalic(true);
} else if (reader.name() == QLatin1String("strike")) {
format->setFontStrikeOut(true);
format.setFontStrikeOut(true);
} else if (reader.name() == QLatin1String("outline")) {
format->setFontOutline(true);
format.setFontOutline(true);
} else if (reader.name() == QLatin1String("shadow")) {
format->setProperty(FormatPrivate::P_Font_Shadow, true);
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());
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());
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));
format.setFontColor(fromARGBString(colorString));
} else if (attributes.hasAttribute(QLatin1String("indexed"))) {
// color = getColorByIndex(attributes.value(QLatin1String("indexed")).toString().toInt());
} else if (attributes.hasAttribute(QLatin1String("theme"))) {
QString theme = attributes.value(QLatin1String("theme")).toString();
QString tint = attributes.value(QLatin1String("tint")).toString();
format->setProperty(FormatPrivate::P_Font_ThemeColor, QString(theme + QLatin1Char(':') + tint));
format.setProperty(FormatPrivate::P_Font_ThemeColor, QString(theme + QLatin1Char(':') + tint));
}
} 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")) {
QString value = attributes.value(QLatin1String("val")).toString();
if (value == QLatin1String("double"))
format->setFontUnderline(Format::FontUnderlineDouble);
format.setFontUnderline(Format::FontUnderlineDouble);
else if (value == QLatin1String("doubleAccounting"))
format->setFontUnderline(Format::FontUnderlineDoubleAccounting);
format.setFontUnderline(Format::FontUnderlineDoubleAccounting);
else if (value == QLatin1String("singleAccounting"))
format->setFontUnderline(Format::FontUnderlineSingleAccounting);
format.setFontUnderline(Format::FontUnderlineSingleAccounting);
else
format->setFontUnderline(Format::FontUnderlineSingle);
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);
format.setFontScript(Format::FontScriptSuper);
else if (value == QLatin1String("subscript"))
format->setFontScript(Format::FontScriptSub);
format.setFontScript(Format::FontScriptSub);
} else if (reader.name() == QLatin1String("scheme")) {
format->setProperty(FormatPrivate::P_Font_Scheme, attributes.value(QLatin1String("val")).toString());
format.setProperty(FormatPrivate::P_Font_Scheme, attributes.value(QLatin1String("val")).toString());
}
}
}

5
src/xlsx/xlsxsharedstrings_p.h

@ -37,7 +37,6 @@ namespace QXlsx {
class XmlStreamReader;
class XmlStreamWriter;
class RichString;
class XlsxSharedStringInfo
{
@ -77,8 +76,8 @@ private:
void readString(XmlStreamReader &reader); // <si>
void readRichStringPart(XmlStreamReader &reader, RichString &rich); // <r>
void readPlainStringPart(XmlStreamReader &reader, RichString &rich); // <v>
Format *readRichStringPart_rPr(XmlStreamReader &reader, RichString &richString);
void writeRichStringPart_rPr(XmlStreamWriter &writer, Format *format) const;
Format readRichStringPart_rPr(XmlStreamReader &reader);
void writeRichStringPart_rPr(XmlStreamWriter &writer, const Format &format) const;
QHash<RichString, XlsxSharedStringInfo> m_stringTable; //for fast lookup
QList<RichString> m_stringList;

2
src/xlsx/xlsxworksheet.cpp

@ -1175,7 +1175,7 @@ void WorksheetPrivate::writeCellData(XmlStreamWriter &writer, int row, int col,
RichString string = cell->d_ptr->richString;
for (int i=0; i<string.fragmentCount(); ++i) {
writer.writeStartElement(QStringLiteral("r"));
if (string.fragmentFormat(i)) {
if (string.fragmentFormat(i).hasFontData()) {
writer.writeStartElement(QStringLiteral("rPr"));
//:Todo
writer.writeEndElement();// rPr

12
tests/auto/richstring/tst_richstringtest.cpp

@ -20,16 +20,16 @@ RichstringTest::RichstringTest()
void RichstringTest::testEqual()
{
QXlsx::RichString rs;
rs.addFragment("Hello", 0);
rs.addFragment(" RichText", 0);
rs.addFragment("Hello", QXlsx::Format());
rs.addFragment(" RichText", QXlsx::Format());
QXlsx::RichString rs2;
rs2.addFragment("Hello", 0);
rs2.addFragment(" Qt!", 0);
rs2.addFragment("Hello", QXlsx::Format());
rs2.addFragment(" Qt!", QXlsx::Format());
QXlsx::RichString rs3;
rs3.addFragment("Hello", 0);
rs3.addFragment(" Qt!", 0);
rs3.addFragment("Hello", QXlsx::Format());
rs3.addFragment(" Qt!", QXlsx::Format());
QVERIFY2(rs2 != rs, "Failure");
QVERIFY2(rs2 == rs3, "Failure");

27
tests/auto/sharedstrings/tst_sharedstringstest.cpp

@ -32,14 +32,14 @@ void SharedStringsTest::testAddSharedString()
sst.addSharedString("Xlsx Writer");
QXlsx::RichString rs;
rs.addFragment("Hello", 0);
rs.addFragment(" RichText", 0);
rs.addFragment("Hello", QXlsx::Format());
rs.addFragment(" RichText", QXlsx::Format());
sst.addSharedString(rs);
for (int i=0; i<3; ++i) {
QXlsx::RichString rs2;
rs2.addFragment("Hello", 0);
rs2.addFragment(" Qt!", 0);
rs2.addFragment("Hello", QXlsx::Format());
rs2.addFragment(" Qt!", QXlsx::Format());
sst.addSharedString(rs2);
}
@ -106,13 +106,13 @@ void SharedStringsTest::testLoadXmlData()
sst.addSharedString("Xlsx Writer");
QXlsx::RichString rs;
rs.addFragment("Hello", 0);
rs.addFragment(" RichText", 0);
rs.addFragment("Hello", QXlsx::Format());
rs.addFragment(" RichText", QXlsx::Format());
sst.addSharedString(rs);
for (int i=0; i<3; ++i) {
QXlsx::RichString rs2;
rs2.addFragment("Hello", 0);
rs2.addFragment(" Qt!", 0);
rs2.addFragment("Hello", QXlsx::Format());
rs2.addFragment(" Qt!", QXlsx::Format());
sst.addSharedString(rs2);
}
sst.addSharedString("Hello World");
@ -152,12 +152,11 @@ void SharedStringsTest::testLoadRichStringXmlData()
QXlsx::RichString rs = sst->getSharedString(0);
QVERIFY(rs.fragmentText(0) == "e=mc");
QVERIFY(rs.fragmentText(1) == "2");
QVERIFY(rs.fragmentFormat(0) == 0);
QXlsx::Format *format = rs.fragmentFormat(1);
QCOMPARE(format->fontName(), QString("MyFontName"));
// QCOMPARE(format->fontFamily(), 3);
QCOMPARE(format->fontScript(), QXlsx::Format::FontScriptSuper);
QCOMPARE(format->fontSize(), 11);
QVERIFY(rs.fragmentFormat(0) == QXlsx::Format());
QXlsx::Format format = rs.fragmentFormat(1);
QCOMPARE(format.fontName(), QString("MyFontName"));
QCOMPARE(format.fontScript(), QXlsx::Format::FontScriptSuper);
QCOMPARE(format.fontSize(), 11);
}
QTEST_APPLESS_MAIN(SharedStringsTest)

Loading…
Cancel
Save