/**************************************************************************** ** Copyright (c) 2013-2014 Debao Zhang ** All right reserved. ** ** Permission is hereby granted, free of charge, to any person obtaining ** a copy of this software and associated documentation files (the ** "Software"), to deal in the Software without restriction, including ** without limitation the rights to use, copy, modify, merge, publish, ** distribute, sublicense, and/or sell copies of the Software, and to ** permit persons to whom the Software is furnished to do so, subject to ** the following conditions: ** ** The above copyright notice and this permission notice shall be ** included in all copies or substantial portions of the Software. ** ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ** ****************************************************************************/ #include "xlsxrichstring.h" #include "xlsxsharedstrings_p.h" #include "xlsxutility_p.h" #include "xlsxformat_p.h" #include "xlsxcolor_p.h" #include #include #include #include #include #include #include namespace QXlsx { 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; if (m_stringTable.contains(string)) { XlsxSharedStringInfo &item = m_stringTable[string]; item.count += 1; return item.index; } int index = m_stringTable.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]); } void SharedStrings::removeSharedString(const QString &string) { removeSharedString(RichString(string)); } void SharedStrings::removeSharedString(const RichString &string) { if (!m_stringTable.contains(string)) return; m_stringCount -= 1; XlsxSharedStringInfo &item = m_stringTable[string]; item.count -= 1; if (item.count <= 0) { for (int i=item.index+1; i= 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); 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_stringTable.size())); foreach (RichString string, m_stringList) { writer.writeStartElement(QStringLiteral("si")); if (string.isRichString()) { //Rich text string for (int i=0; i