Browse Source

Fix Issue #61: sheet hidden/visible state works now.

master
Debao Zhang 10 years ago
parent
commit
b213f56ca0
  1. 65
      src/xlsx/xlsxabstractsheet.cpp
  2. 18
      src/xlsx/xlsxabstractsheet.h
  3. 2
      src/xlsx/xlsxabstractsheet_p.h
  4. 17
      src/xlsx/xlsxworkbook.cpp

65
src/xlsx/xlsxabstractsheet.cpp

@ -31,8 +31,8 @@ QT_BEGIN_NAMESPACE_XLSX
AbstractSheetPrivate::AbstractSheetPrivate(AbstractSheet *p, AbstractSheet::CreateFlag flag) AbstractSheetPrivate::AbstractSheetPrivate(AbstractSheet *p, AbstractSheet::CreateFlag flag)
: AbstractOOXmlFilePrivate(p, flag) : AbstractOOXmlFilePrivate(p, flag)
{ {
hidden = false;
type = AbstractSheet::ST_WorkSheet; type = AbstractSheet::ST_WorkSheet;
sheetState = AbstractSheet::SS_Visible;
} }
AbstractSheetPrivate::~AbstractSheetPrivate() AbstractSheetPrivate::~AbstractSheetPrivate()
@ -48,12 +48,20 @@ AbstractSheetPrivate::~AbstractSheetPrivate()
/*! /*!
\enum AbstractSheet::SheetType \enum AbstractSheet::SheetType
\value ST_WorkSheet, \value ST_WorkSheet
\value ST_ChartSheet, \value ST_ChartSheet
\omitvalue ST_DialogSheet, \omitvalue ST_DialogSheet
\omitvalue ST_MacroSheet \omitvalue ST_MacroSheet
*/ */
/*!
\enum AbstractSheet::SheetState
\value SS_Visible
\value SS_Hidden
\value SS_VeryHidden User cann't make a veryHidden sheet visible in normal way.
*/
/*! /*!
\fn AbstractSheet::copy(const QString &distName, int distId) const \fn AbstractSheet::copy(const QString &distName, int distId) const
@ -110,21 +118,62 @@ void AbstractSheet::setSheetType(SheetType type)
} }
/*! /*!
* \internal * Returns the state of the sheet.
*
* \sa isHidden(), isVisible(), setSheetState()
*/
AbstractSheet::SheetState AbstractSheet::sheetState() const
{
Q_D(const AbstractSheet);
return d->sheetState;
}
/*!
* Set the state of the sheet to \a state.
*/
void AbstractSheet::setSheetState(SheetState state)
{
Q_D(AbstractSheet);
d->sheetState = state;
}
/*!
* Returns true if the sheet is not visible, otherwise false will be returned.
*
* \sa sheetState(), setHidden()
*/ */
bool AbstractSheet::isHidden() const bool AbstractSheet::isHidden() const
{ {
Q_D(const AbstractSheet); Q_D(const AbstractSheet);
return d->hidden; return d->sheetState != SS_Visible;
} }
/*! /*!
* \internal * Returns true if the sheet is visible.
*/
bool AbstractSheet::isVisible() const
{
return !isHidden();
}
/*!
* Make the sheet hiden or visible based on \a hidden.
*/ */
void AbstractSheet::setHidden(bool hidden) void AbstractSheet::setHidden(bool hidden)
{ {
Q_D(AbstractSheet); Q_D(AbstractSheet);
d->hidden = hidden; if (hidden == isHidden())
return;
d->sheetState = hidden ? SS_Hidden : SS_Visible;
}
/*!
* Convenience function, equivalent to setHidden(! \a visible).
*/
void AbstractSheet::setVisible(bool visible)
{
setHidden(!visible);
} }
/*! /*!

18
src/xlsx/xlsxabstractsheet.h

@ -37,17 +37,28 @@ class Q_XLSX_EXPORT AbstractSheet : public AbstractOOXmlFile
{ {
Q_DECLARE_PRIVATE(AbstractSheet) Q_DECLARE_PRIVATE(AbstractSheet)
public: public:
enum SheetType enum SheetType {
{
ST_WorkSheet, ST_WorkSheet,
ST_ChartSheet, ST_ChartSheet,
ST_DialogSheet, ST_DialogSheet,
ST_MacroSheet ST_MacroSheet
}; };
SheetType sheetType() const; enum SheetState {
SS_Visible,
SS_Hidden,
SS_VeryHidden
};
QString sheetName() const; QString sheetName() const;
SheetType sheetType() const;
SheetState sheetState() const;
void setSheetState(SheetState ss);
bool isHidden() const; bool isHidden() const;
bool isVisible() const;
void setHidden(bool hidden);
void setVisible(bool visible);
Workbook *workbook() const; Workbook *workbook() const;
protected: protected:
@ -55,7 +66,6 @@ protected:
AbstractSheet(const QString &sheetName, int sheetId, Workbook *book, AbstractSheetPrivate *d); AbstractSheet(const QString &sheetName, int sheetId, Workbook *book, AbstractSheetPrivate *d);
virtual AbstractSheet *copy(const QString &distName, int distId) const = 0; virtual AbstractSheet *copy(const QString &distName, int distId) const = 0;
void setSheetName(const QString &sheetName); void setSheetName(const QString &sheetName);
void setHidden(bool hidden);
void setSheetType(SheetType type); void setSheetType(SheetType type);
int sheetId() const; int sheetId() const;

2
src/xlsx/xlsxabstractsheet_p.h

@ -56,7 +56,7 @@ public:
QString name; QString name;
int id; int id;
bool hidden; AbstractSheet::SheetState sheetState;
AbstractSheet::SheetType type; AbstractSheet::SheetType type;
}; };

17
src/xlsx/xlsxworkbook.cpp

@ -486,8 +486,10 @@ void Workbook::saveToXmlFile(QIODevice *device) const
writer.writeEmptyElement(QStringLiteral("sheet")); writer.writeEmptyElement(QStringLiteral("sheet"));
writer.writeAttribute(QStringLiteral("name"), sheet->sheetName()); writer.writeAttribute(QStringLiteral("name"), sheet->sheetName());
writer.writeAttribute(QStringLiteral("sheetId"), QString::number(sheet->sheetId())); writer.writeAttribute(QStringLiteral("sheetId"), QString::number(sheet->sheetId()));
if (sheet->isHidden()) if (sheet->sheetState() == AbstractSheet::SS_Hidden)
writer.writeAttribute(QStringLiteral("state"), QStringLiteral("hidden")); writer.writeAttribute(QStringLiteral("state"), QStringLiteral("hidden"));
else if (sheet->sheetState() == AbstractSheet::SS_VeryHidden)
writer.writeAttribute(QStringLiteral("state"), QStringLiteral("veryHidden"));
d->relationships->addDocumentRelationship(QStringLiteral("/worksheet"), QStringLiteral("worksheets/sheet%1.xml").arg(i+1)); d->relationships->addDocumentRelationship(QStringLiteral("/worksheet"), QStringLiteral("worksheets/sheet%1.xml").arg(i+1));
writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(d->relationships->count())); writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(d->relationships->count()));
@ -500,8 +502,10 @@ void Workbook::saveToXmlFile(QIODevice *device) const
writer.writeEmptyElement(QStringLiteral("sheet")); writer.writeEmptyElement(QStringLiteral("sheet"));
writer.writeAttribute(QStringLiteral("name"), sheet->sheetName()); writer.writeAttribute(QStringLiteral("name"), sheet->sheetName());
writer.writeAttribute(QStringLiteral("sheetId"), QString::number(sheet->sheetId())); writer.writeAttribute(QStringLiteral("sheetId"), QString::number(sheet->sheetId()));
if (sheet->isHidden()) if (sheet->sheetState() == AbstractSheet::SS_Hidden)
writer.writeAttribute(QStringLiteral("state"), QStringLiteral("hidden")); writer.writeAttribute(QStringLiteral("state"), QStringLiteral("hidden"));
else if (sheet->sheetState() == AbstractSheet::SS_VeryHidden)
writer.writeAttribute(QStringLiteral("state"), QStringLiteral("veryHidden"));
d->relationships->addDocumentRelationship(QStringLiteral("/chartsheet"), QStringLiteral("chartsheets/sheet%1.xml").arg(i+1)); d->relationships->addDocumentRelationship(QStringLiteral("/chartsheet"), QStringLiteral("chartsheets/sheet%1.xml").arg(i+1));
writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(d->relationships->count())); writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(d->relationships->count()));
@ -566,8 +570,12 @@ bool Workbook::loadFromXmlFile(QIODevice *device)
const QString name = attributes.value(QLatin1String("name")).toString(); const QString name = attributes.value(QLatin1String("name")).toString();
int sheetId = attributes.value(QLatin1String("sheetId")).toString().toInt(); int sheetId = attributes.value(QLatin1String("sheetId")).toString().toInt();
const QString rId = attributes.value(QLatin1String("r:id")).toString(); const QString rId = attributes.value(QLatin1String("r:id")).toString();
// if (attributes.hasAttribute(QLatin1String("state"))) QStringRef &stateString = attributes.value(QLatin1String("state"));
// QString state = attributes.value(QLatin1String("state")).toString(); AbstractSheet::SheetState state = AbstractSheet::SS_Visible;
if (stateString == QLatin1String("hidden"))
state = AbstractSheet::SS_Hidden;
else if (stateString == QLatin1String("veryHidden"))
state = AbstractSheet::SS_VeryHidden;
XlsxRelationship relationship = d->relationships->getRelationshipById(rId); XlsxRelationship relationship = d->relationships->getRelationshipById(rId);
@ -584,6 +592,7 @@ bool Workbook::loadFromXmlFile(QIODevice *device)
qWarning("unknown sheet type"); qWarning("unknown sheet type");
AbstractSheet *sheet = addSheet(name, sheetId, type); AbstractSheet *sheet = addSheet(name, sheetId, type);
sheet->setSheetState(state);
const QString fullPath = QDir::cleanPath(splitPath(filePath())[0] +QLatin1String("/")+ relationship.target); const QString fullPath = QDir::cleanPath(splitPath(filePath())[0] +QLatin1String("/")+ relationship.target);
sheet->setFilePath(fullPath); sheet->setFilePath(fullPath);
} else if (reader.name() == QLatin1String("workbookPr")) { } else if (reader.name() == QLatin1String("workbookPr")) {

Loading…
Cancel
Save