diff --git a/src/xlsx/xlsxformat.cpp b/src/xlsx/xlsxformat.cpp index 37c9449..274abba 100755 --- a/src/xlsx/xlsxformat.cpp +++ b/src/xlsx/xlsxformat.cpp @@ -232,20 +232,19 @@ void Format::setFontName(const QString &name) bool Format::fontIndexValid() const { Q_D(const Format); - return !d->fontData._dirty && d->fontData._indexValid; + return d->fontData.indexValid(); } int Format::fontIndex() const { Q_D(const Format); - return d->fontData._index; + return d->fontData.index(); } void Format::setFontIndex(int index) { Q_D(Format); - d->fontData._index = index; - d->fontData._indexValid = true; + d->fontData.setIndex(index); } int Format::fontFamily() const @@ -271,21 +270,9 @@ QString Format::fontScheme() const QByteArray Format::fontKey() const { Q_D(const Format); - if (d->fontData._dirty) { - QByteArray key; - QDataStream stream(&key, QIODevice::WriteOnly); - stream<fontData.bold<fontData.charset<fontData.color<fontData.condense - <fontData.extend<fontData.family<fontData.italic<fontData.name - <fontData.outline<fontData.scheme<fontData.scirpt<fontData.shadow - <fontData.size<fontData.strikeOut<fontData.underline; - - const_cast(d)->fontData._key = key; - const_cast(d)->fontData._dirty = false; - const_cast(d)->fontData._indexValid = false; //must re-assign a new index + if (d->fontData._dirty) const_cast(d)->dirty = true; //Make sure formatKey() will be re-generated. - } - - return d->fontData._key; + return d->fontData.key(); } Format::HorizontalAlignment Format::horizontalAlignment() const @@ -621,20 +608,19 @@ void Format::setDiagonalBorderColor(const QColor &color) bool Format::borderIndexValid() const { Q_D(const Format); - return !d->borderData._dirty && d->borderData._indexValid; + return d->borderData.indexValid(); } int Format::borderIndex() const { Q_D(const Format); - return d->borderData._index; + return d->borderData.index(); } void Format::setBorderIndex(int index) { Q_D(Format); - d->borderData._index = index; - d->borderData._indexValid = true; + d->borderData.setIndex(index); } /* Internal @@ -642,21 +628,10 @@ void Format::setBorderIndex(int index) QByteArray Format::borderKey() const { Q_D(const Format); - if (d->borderData._dirty) { - QByteArray key; - QDataStream stream(&key, QIODevice::WriteOnly); - stream<borderData.bottom<borderData.bottomColor - <borderData.diagonal<borderData.diagonalColor<borderData.diagonalType - <borderData.left<borderData.leftColor - <borderData.right<borderData.rightColor - <borderData.top<borderData.topColor; - const_cast(d)->borderData._key = key; - const_cast(d)->borderData._dirty = false; - const_cast(d)->borderData._indexValid = false; + if (d->borderData._dirty) const_cast(d)->dirty = true; //Make sure formatKey() will be re-generated. - } - return d->borderData._key; + return d->borderData.key(); } Format::FillPattern Format::fillPattern() const @@ -705,20 +680,19 @@ void Format::setPatternBackgroundColor(const QColor &color) bool Format::fillIndexValid() const { Q_D(const Format); - return !d->fillData._dirty && d->fillData._indexValid; + return d->fillData.indexValid(); } int Format::fillIndex() const { Q_D(const Format); - return d->fillData._index; + return d->fillData.index(); } void Format::setFillIndex(int index) { Q_D(Format); - d->fillData._index = index; - d->fillData._indexValid = true; + d->fillData.setIndex(index); } /* Internal @@ -726,17 +700,10 @@ void Format::setFillIndex(int index) QByteArray Format::fillKey() const { Q_D(const Format); - if (d->fillData._dirty) { - QByteArray key; - QDataStream stream(&key, QIODevice::WriteOnly); - stream<fillData.bgColor<fillData.fgColor<fillData.pattern; - const_cast(d)->fillData._key = key; - const_cast(d)->fillData._dirty = false; - const_cast(d)->fillData._indexValid = false; + if (d->fillData._dirty) const_cast(d)->dirty = true; //Make sure formatKey() will be re-generated. - } - return d->fillData._key; + return d->fillData.key(); } bool Format::hidden() const diff --git a/src/xlsx/xlsxformat_p.h b/src/xlsx/xlsxformat_p.h index 9418a98..5047068 100644 --- a/src/xlsx/xlsxformat_p.h +++ b/src/xlsx/xlsxformat_p.h @@ -65,8 +65,43 @@ struct FontData int condense; int extend; + QByteArray key() const + { + if (_dirty) { + QByteArray key; + QDataStream stream(&key, QIODevice::WriteOnly); + stream<(this)->_key = key; + const_cast(this)->_dirty = false; + const_cast(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 is need. + 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 @@ -108,8 +143,41 @@ struct BorderData QColor diagonalColor; Format::DiagonalBorderType diagonalType; + QByteArray key() const + { + if (_dirty) { + QByteArray key; + QDataStream stream(&key, QIODevice::WriteOnly); + stream << bottom << bottomColor << top << topColor + << diagonal << diagonalColor << diagonalType + << left << leftColor << right << rightColor; + const_cast(this)->_key = key; + const_cast(this)->_dirty = false; + const_cast(this)->_indexValid = false; + } + 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 is need. + 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 border list @@ -125,8 +193,39 @@ struct FillData { QColor bgColor; QColor fgColor; + QByteArray key() const + { + if (_dirty) { + QByteArray key; + QDataStream stream(&key, QIODevice::WriteOnly); + stream<(this)->_key = key; + const_cast(this)->_dirty = false; + const_cast(this)->_indexValid = false; + } + 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 is need. + 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 border list diff --git a/src/xlsx/xlsxstyles.cpp b/src/xlsx/xlsxstyles.cpp index 6097a11..b89b516 100755 --- a/src/xlsx/xlsxstyles.cpp +++ b/src/xlsx/xlsxstyles.cpp @@ -42,7 +42,7 @@ Styles::Styles() QSharedPointer fill = QSharedPointer(new FillData); fill->pattern = Format::PatternGray125; m_fillsList.append(fill); - m_fillsHash[fill->_key] = fill; + m_fillsHash[fill->key()] = fill; } Styles::~Styles() @@ -124,33 +124,33 @@ void Styles::addFormat(Format *format) if (!format->fontIndexValid()) { if (!m_fontsHash.contains(format->fontKey())) { QSharedPointer font = QSharedPointer(new FontData(format->d_func()->fontData)); - font->_index = m_fontsList.size(); //Assign proper index + font->setIndex(m_fontsList.size()); //Assign proper index m_fontsList.append(font); - m_fontsHash[font->_key] = font; + m_fontsHash[font->key()] = font; } - format->setFontIndex(m_fontsHash[format->fontKey()]->_index); + format->setFontIndex(m_fontsHash[format->fontKey()]->index()); } //Fill if (!format->fillIndexValid()) { if (!m_fillsHash.contains(format->fillKey())) { QSharedPointer fill = QSharedPointer(new FillData(format->d_func()->fillData)); - fill->_index = m_fillsList.size(); //Assign proper index + fill->setIndex(m_fillsList.size()); //Assign proper index m_fillsList.append(fill); - m_fillsHash[fill->_key] = fill; + m_fillsHash[fill->key()] = fill; } - format->setFillIndex(m_fillsHash[format->fillKey()]->_index); + format->setFillIndex(m_fillsHash[format->fillKey()]->index()); } //Border if (!format->borderIndexValid()) { if (!m_bordersHash.contains(format->borderKey())) { QSharedPointer border = QSharedPointer(new BorderData(format->d_func()->borderData)); - border->_index = m_bordersList.size(); //Assign proper index + border->setIndex(m_bordersList.size()); //Assign proper index m_bordersList.append(border); - m_bordersHash[border->_key] = border; + m_bordersHash[border->key()] = border; } - format->setBorderIndex(m_bordersHash[format->borderKey()]->_index); + format->setBorderIndex(m_bordersHash[format->borderKey()]->index()); } //Format