// xlsxsharedstrings.cpp #include #include #include #include #include #include #include #include "xlsxrichstring.h" #include "xlsxsharedstrings_p.h" #include "xlsxutility_p.h" #include "xlsxformat_p.h" #include "xlsxcolor_p.h" QT_BEGIN_NAMESPACE_XLSX /* * Note that, when we open an existing .xlsx file (broken file?), * duplicated string items may exist in the shared string table. * * In such case, the size of stringList will larger than stringTable. * Duplicated items can be removed once we loaded all the worksheets. */ SharedStrings::SharedStrings(CreateFlag flag) :AbstractOOXmlFile(flag) { m_stringCount = 0; } int SharedStrings::count() const { return m_stringCount; } bool SharedStrings::isEmpty() const { return m_stringList.isEmpty(); } int SharedStrings::addSharedString(const QString &string) { return addSharedString(RichString(string)); } int SharedStrings::addSharedString(const RichString &string) { m_stringCount += 1; auto it = m_stringTable.find(string); if (it != m_stringTable.end()) { it->count += 1; return it->index; } int index = m_stringList.size(); m_stringTable[string] = XlsxSharedStringInfo(index); m_stringList.append(string); return index; } void SharedStrings::incRefByStringIndex(int idx) { if (idx <0 || idx >= m_stringList.size()) { qDebug("SharedStrings: invlid index"); return; } addSharedString(m_stringList[idx]); } /* * Broken, don't use. */ void SharedStrings::removeSharedString(const QString &string) { removeSharedString(RichString(string)); } /* * Broken, don't use. */ void SharedStrings::removeSharedString(const RichString &string) { auto it = m_stringTable.find(string); if (it == m_stringTable.end()) return; m_stringCount -= 1; it->count -= 1; if (it->count <= 0) { for (int i=it->index+1; iindex); m_stringTable.remove(string); } } int SharedStrings::getSharedStringIndex(const QString &string) const { return getSharedStringIndex(RichString(string)); } int SharedStrings::getSharedStringIndex(const RichString &string) const { auto it = m_stringTable.constFind(string); if (it != m_stringTable.constEnd()) return it->index; return -1; } RichString SharedStrings::getSharedString(int index) const { if (index < m_stringList.count() && index >= 0) return m_stringList[index]; return RichString(); } QList SharedStrings::getSharedStrings() const { return m_stringList; } void SharedStrings::writeRichStringPart_rPr(QXmlStreamWriter &writer, const Format &format) const { if (!format.hasFontData()) return; if (format.fontBold()) writer.writeEmptyElement(QStringLiteral("b")); if (format.fontItalic()) writer.writeEmptyElement(QStringLiteral("i")); if (format.fontStrikeOut()) writer.writeEmptyElement(QStringLiteral("strike")); if (format.fontOutline()) writer.writeEmptyElement(QStringLiteral("outline")); if (format.boolProperty(FormatPrivate::P_Font_Shadow)) writer.writeEmptyElement(QStringLiteral("shadow")); 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 (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")); } } if (format.hasProperty(FormatPrivate::P_Font_Size)) { writer.writeEmptyElement(QStringLiteral("sz")); writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontSize())); } if (format.hasProperty(FormatPrivate::P_Font_Color)) { XlsxColor color = format.property(FormatPrivate::P_Font_Color).value(); color.saveToXml(writer); } 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"), format.stringProperty(FormatPrivate::P_Font_Scheme)); } } void SharedStrings::saveToXmlFile(QIODevice *device) const { QXmlStreamWriter writer(device); if (m_stringList.size() != m_stringTable.size()) { //Duplicated string items exist in m_stringList //Clean up can not be done here, as the indices //have been used when we save the worksheets part. } writer.writeStartDocument(QStringLiteral("1.0"), true); writer.writeStartElement(QStringLiteral("sst")); writer.writeAttribute(QStringLiteral("xmlns"), QStringLiteral("http://schemas.openxmlformats.org/spreadsheetml/2006/main")); writer.writeAttribute(QStringLiteral("count"), QString::number(m_stringCount)); writer.writeAttribute(QStringLiteral("uniqueCount"), QString::number(m_stringList.size())); for (const RichString &string : m_stringList) { writer.writeStartElement(QStringLiteral("si")); if (string.isRichString()) { //Rich text string for (int i=0; i