Browse Source

Move key generate logic from Format to FontData/FillData/...

master
Debao Zhang 12 years ago
parent
commit
74839a73b9
  1. 63
      src/xlsx/xlsxformat.cpp
  2. 105
      src/xlsx/xlsxformat_p.h
  3. 20
      src/xlsx/xlsxstyles.cpp

63
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<<d->fontData.bold<<d->fontData.charset<<d->fontData.color<<d->fontData.condense
<<d->fontData.extend<<d->fontData.family<<d->fontData.italic<<d->fontData.name
<<d->fontData.outline<<d->fontData.scheme<<d->fontData.scirpt<<d->fontData.shadow
<<d->fontData.size<<d->fontData.strikeOut<<d->fontData.underline;
const_cast<FormatPrivate*>(d)->fontData._key = key;
const_cast<FormatPrivate*>(d)->fontData._dirty = false;
const_cast<FormatPrivate*>(d)->fontData._indexValid = false; //must re-assign a new index
if (d->fontData._dirty)
const_cast<FormatPrivate*>(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<<d->borderData.bottom<<d->borderData.bottomColor
<<d->borderData.diagonal<<d->borderData.diagonalColor<<d->borderData.diagonalType
<<d->borderData.left<<d->borderData.leftColor
<<d->borderData.right<<d->borderData.rightColor
<<d->borderData.top<<d->borderData.topColor;
const_cast<FormatPrivate*>(d)->borderData._key = key;
const_cast<FormatPrivate*>(d)->borderData._dirty = false;
const_cast<FormatPrivate*>(d)->borderData._indexValid = false;
if (d->borderData._dirty)
const_cast<FormatPrivate*>(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<<d->fillData.bgColor<<d->fillData.fgColor<<d->fillData.pattern;
const_cast<FormatPrivate*>(d)->fillData._key = key;
const_cast<FormatPrivate*>(d)->fillData._dirty = false;
const_cast<FormatPrivate*>(d)->fillData._indexValid = false;
if (d->fillData._dirty)
const_cast<FormatPrivate*>(d)->dirty = true; //Make sure formatKey() will be re-generated.
}
return d->fillData._key;
return d->fillData.key();
}
bool Format::hidden() const

105
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<<bold<<charset<<color<<condense
<<extend<<family<<italic<<name
<<outline<<scheme<<scirpt<<shadow
<<size<<strikeOut<<underline;
const_cast<FontData*>(this)->_key = key;
const_cast<FontData*>(this)->_dirty = false;
const_cast<FontData*>(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<BorderData*>(this)->_key = key;
const_cast<BorderData*>(this)->_dirty = false;
const_cast<BorderData*>(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<<bgColor<<fgColor<<pattern;
const_cast<FillData*>(this)->_key = key;
const_cast<FillData*>(this)->_dirty = false;
const_cast<FillData*>(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

20
src/xlsx/xlsxstyles.cpp

@ -42,7 +42,7 @@ Styles::Styles()
QSharedPointer<FillData> fill = QSharedPointer<FillData>(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<FontData> font = QSharedPointer<FontData>(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<FillData> fill = QSharedPointer<FillData>(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<BorderData> border = QSharedPointer<BorderData>(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

Loading…
Cancel
Save