Browse Source

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

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

67
src/xlsx/xlsxabstractsheet.cpp

@ -31,8 +31,8 @@ QT_BEGIN_NAMESPACE_XLSX
AbstractSheetPrivate::AbstractSheetPrivate(AbstractSheet *p, AbstractSheet::CreateFlag flag)
: AbstractOOXmlFilePrivate(p, flag)
{
hidden = false;
type = AbstractSheet::ST_WorkSheet;
sheetState = AbstractSheet::SS_Visible;
}
AbstractSheetPrivate::~AbstractSheetPrivate()
@ -48,11 +48,19 @@ AbstractSheetPrivate::~AbstractSheetPrivate()
/*!
\enum AbstractSheet::SheetType
\value ST_WorkSheet,
\value ST_ChartSheet,
\omitvalue ST_DialogSheet,
\value ST_WorkSheet
\value ST_ChartSheet
\omitvalue ST_DialogSheet
\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
@ -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
{
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)
{
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)
public:
enum SheetType
{
enum SheetType {
ST_WorkSheet,
ST_ChartSheet,
ST_DialogSheet,
ST_MacroSheet
};
SheetType sheetType() const;
enum SheetState {
SS_Visible,
SS_Hidden,
SS_VeryHidden
};
QString sheetName() const;
SheetType sheetType() const;
SheetState sheetState() const;
void setSheetState(SheetState ss);
bool isHidden() const;
bool isVisible() const;
void setHidden(bool hidden);
void setVisible(bool visible);
Workbook *workbook() const;
protected:
@ -55,7 +66,6 @@ protected:
AbstractSheet(const QString &sheetName, int sheetId, Workbook *book, AbstractSheetPrivate *d);
virtual AbstractSheet *copy(const QString &distName, int distId) const = 0;
void setSheetName(const QString &sheetName);
void setHidden(bool hidden);
void setSheetType(SheetType type);
int sheetId() const;

2
src/xlsx/xlsxabstractsheet_p.h

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

17
src/xlsx/xlsxworkbook.cpp

@ -486,8 +486,10 @@ void Workbook::saveToXmlFile(QIODevice *device) const
writer.writeEmptyElement(QStringLiteral("sheet"));
writer.writeAttribute(QStringLiteral("name"), sheet->sheetName());
writer.writeAttribute(QStringLiteral("sheetId"), QString::number(sheet->sheetId()));
if (sheet->isHidden())
if (sheet->sheetState() == AbstractSheet::SS_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));
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.writeAttribute(QStringLiteral("name"), sheet->sheetName());
writer.writeAttribute(QStringLiteral("sheetId"), QString::number(sheet->sheetId()));
if (sheet->isHidden())
if (sheet->sheetState() == AbstractSheet::SS_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));
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();
int sheetId = attributes.value(QLatin1String("sheetId")).toString().toInt();
const QString rId = attributes.value(QLatin1String("r:id")).toString();
// if (attributes.hasAttribute(QLatin1String("state")))
// QString state = attributes.value(QLatin1String("state")).toString();
QStringRef &stateString = attributes.value(QLatin1String("state"));
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);
@ -584,6 +592,7 @@ bool Workbook::loadFromXmlFile(QIODevice *device)
qWarning("unknown sheet type");
AbstractSheet *sheet = addSheet(name, sheetId, type);
sheet->setSheetState(state);
const QString fullPath = QDir::cleanPath(splitPath(filePath())[0] +QLatin1String("/")+ relationship.target);
sheet->setFilePath(fullPath);
} else if (reader.name() == QLatin1String("workbookPr")) {

Loading…
Cancel
Save