From 964c3980a377293558abdc7392e19775cbc0c4b5 Mon Sep 17 00:00:00 2001 From: Debao Zhang Date: Tue, 25 Feb 2014 15:31:16 +0800 Subject: [PATCH] Change API to support AbstractSheet --- examples/xlsx/demo/main.cpp | 12 +- examples/xlsx/formulas/main.cpp | 2 +- examples/xlsx/numberformat/main.cpp | 2 +- examples/xlsx/worksheetoperations/main.cpp | 14 +- examples/xlsx/xlsxwidget/main.cpp | 16 ++- src/xlsx/xlsxabstractsheet.cpp | 10 ++ src/xlsx/xlsxabstractsheet.h | 6 +- src/xlsx/xlsxabstractsheet_p.h | 1 + src/xlsx/xlsxdocument.cpp | 136 +++++++++----------- src/xlsx/xlsxdocument.h | 24 ++-- src/xlsx/xlsxworkbook.cpp | 142 +++++++++++---------- src/xlsx/xlsxworkbook.h | 25 ++-- src/xlsx/xlsxworkbook_p.h | 4 +- src/xlsx/xlsxworksheet.cpp | 16 +-- src/xlsx/xlsxworksheet.h | 3 +- src/xlsx/xlsxworksheet_p.h | 2 - tests/auto/document/tst_documenttest.cpp | 46 +++---- 17 files changed, 223 insertions(+), 238 deletions(-) diff --git a/examples/xlsx/demo/main.cpp b/examples/xlsx/demo/main.cpp index 1bb732b..170f698 100644 --- a/examples/xlsx/demo/main.cpp +++ b/examples/xlsx/demo/main.cpp @@ -94,7 +94,7 @@ int main() //--------------------------------------------------------------- //The default sheet is "Sheet1" - xlsx.renameWorksheet("Sheet1", "Aligns & Borders"); + xlsx.renameSheet("Sheet1", "Aligns & Borders"); xlsx.setColumn("B", "B", 20); xlsx.setColumn("H", "H", 12); xlsx.currentWorksheet()->setGridLinesVisible(false); @@ -138,7 +138,7 @@ int main() //--------------------------------------------------------------- //Create the second sheet. - xlsx.addWorksheet("Fonts"); + xlsx.addSheet("Fonts"); xlsx.write("B3", "Normal"); Format font_bold; @@ -178,7 +178,7 @@ int main() //--------------------------------------------------------------- //Create the third sheet. - xlsx.addWorksheet("Formulas"); + xlsx.addSheet("Formulas"); xlsx.setColumn("A", "B", 40); Format rAlign; rAlign.setHorizontalAlignment(Format::AlignRight); @@ -241,7 +241,7 @@ int main() //--------------------------------------------------------------- //Create the fourth sheet. - xlsx.addWorksheet("NumFormats"); + xlsx.addSheet("NumFormats"); xlsx.setColumn("B", "B", 40); writeInternalNumFormatsCell(xlsx, 4, 2.5681, 2); writeInternalNumFormatsCell(xlsx, 5, 2500000, 3); @@ -259,7 +259,7 @@ int main() //--------------------------------------------------------------- //Create the fifth sheet. - xlsx.addWorksheet("Merging"); + xlsx.addSheet("Merging"); Format centerAlign; centerAlign.setHorizontalAlignment(Format::AlignHCenter); centerAlign.setVerticalAlignment(Format::AlignVCenter); @@ -272,7 +272,7 @@ int main() //--------------------------------------------------------------- //Create the fifth sheet. - xlsx.addWorksheet("Grouping"); + xlsx.addSheet("Grouping"); qsrand(QDateTime::currentMSecsSinceEpoch()); for (int row=2; row<31; ++row) { for (int col=1; col<=10; ++col) diff --git a/examples/xlsx/formulas/main.cpp b/examples/xlsx/formulas/main.cpp index e779d76..b75e5d3 100644 --- a/examples/xlsx/formulas/main.cpp +++ b/examples/xlsx/formulas/main.cpp @@ -50,7 +50,7 @@ int main() //![1] //![2] - xlsx.addWorksheet("ArrayFormula"); + xlsx.addSheet("ArrayFormula"); Worksheet *sheet = xlsx.currentWorksheet(); for (int row=2; row<20; ++row) { diff --git a/examples/xlsx/numberformat/main.cpp b/examples/xlsx/numberformat/main.cpp index c4e13d1..af3a82c 100644 --- a/examples/xlsx/numberformat/main.cpp +++ b/examples/xlsx/numberformat/main.cpp @@ -32,7 +32,7 @@ int main(int argc, char** argv) } //Builtin number formats - xlsx.addWorksheet(); + xlsx.addSheet(); xlsx.setColumn(1, 4, 20.0); xlsx.write(1, 1, "Raw data", header); xlsx.write(1, 2, "Builtin Format", header); diff --git a/examples/xlsx/worksheetoperations/main.cpp b/examples/xlsx/worksheetoperations/main.cpp index 3f1647d..fca8fec 100644 --- a/examples/xlsx/worksheetoperations/main.cpp +++ b/examples/xlsx/worksheetoperations/main.cpp @@ -5,27 +5,27 @@ int main() { QXlsx::Document xlsx; - xlsx.renameWorksheet("Sheet1", "TheFirstSheet"); + xlsx.renameSheet("Sheet1", "TheFirstSheet"); for (int i=1; i<20; ++i) { for (int j=1; j<15; ++j) xlsx.write(i, j, QString("R %1 C %2").arg(i).arg(j)); } - xlsx.addWorksheet("TheSecondSheet"); + xlsx.addSheet("TheSecondSheet"); xlsx.write(2, 2, "Hello Qt Xlsx"); - xlsx.copyWorksheet("TheFirstSheet", "CopyOfTheFirst"); + xlsx.copySheet("TheFirstSheet", "CopyOfTheFirst"); - xlsx.addWorksheet("TheForthSheet"); + xlsx.addSheet("TheForthSheet"); xlsx.write(3, 3, "This will be deleted..."); - xlsx.selectWorksheet("CopyOfTheFirst"); + xlsx.selectSheet("CopyOfTheFirst"); xlsx.write(25, 2, "On the Copy Sheet"); - xlsx.deleteWorksheet("TheForthSheet"); + xlsx.deleteSheet("TheForthSheet"); - xlsx.moveWorksheet("TheSecondSheet", 0); + xlsx.moveSheet("TheSecondSheet", 0); xlsx.save(); diff --git a/examples/xlsx/xlsxwidget/main.cpp b/examples/xlsx/xlsxwidget/main.cpp index 9d339c2..fe5ca15 100644 --- a/examples/xlsx/xlsxwidget/main.cpp +++ b/examples/xlsx/xlsxwidget/main.cpp @@ -24,13 +24,15 @@ int main(int argc, char **argv) //![2] Document xlsx(filePath); - foreach (QString sheetName, xlsx.worksheetNames()) { - Worksheet *sheet = xlsx.worksheet(sheetName); - QTableView *view = new QTableView(&tabWidget); - view->setModel(new SheetModel(sheet, view)); - foreach (CellRange range, sheet->mergedCells()) - view->setSpan(range.firstRow()-1, range.firstColumn()-1, range.rowCount(), range.columnCount()); - tabWidget.addTab(view, sheetName); + foreach (QString sheetName, xlsx.sheetNames()) { + Worksheet *sheet = dynamic_cast(xlsx.sheet(sheetName)); + if (sheet) { + QTableView *view = new QTableView(&tabWidget); + view->setModel(new SheetModel(sheet, view)); + foreach (CellRange range, sheet->mergedCells()) + view->setSpan(range.firstRow()-1, range.firstColumn()-1, range.rowCount(), range.columnCount()); + tabWidget.addTab(view, sheetName); + } } //![2] diff --git a/src/xlsx/xlsxabstractsheet.cpp b/src/xlsx/xlsxabstractsheet.cpp index 6b101b1..d8896e3 100644 --- a/src/xlsx/xlsxabstractsheet.cpp +++ b/src/xlsx/xlsxabstractsheet.cpp @@ -146,4 +146,14 @@ Workbook *AbstractSheet::workbook() const Q_D(const AbstractSheet); return d->workbook; } + +/*! + * \internal + */ +Relationships &AbstractSheet::relationships() +{ + Q_D(AbstractSheet); + return d->relationships; +} + QT_END_NAMESPACE_XLSX diff --git a/src/xlsx/xlsxabstractsheet.h b/src/xlsx/xlsxabstractsheet.h index 0f40d80..1f94a3f 100644 --- a/src/xlsx/xlsxabstractsheet.h +++ b/src/xlsx/xlsxabstractsheet.h @@ -32,6 +32,7 @@ QT_BEGIN_NAMESPACE_XLSX class Workbook; class Drawing; +class Relationships; class AbstractSheetPrivate; class Q_XLSX_EXPORT AbstractSheet : public OOXmlFile { @@ -49,11 +50,12 @@ public: QString sheetName() const; bool isHidden() const; Workbook *workbook() const; - //Relationships &relationships(); + Relationships &relationships(); protected: + friend class Workbook; AbstractSheet(const QString &sheetName, int sheetId, Workbook *book, AbstractSheetPrivate *d); - //QSharedPointer copy(const QString &distName, int distId) const; + virtual AbstractSheet *copy(const QString &distName, int distId) const = 0; void setSheetName(const QString &sheetName); void setHidden(bool hidden); void setSheetType(SheetType type); diff --git a/src/xlsx/xlsxabstractsheet_p.h b/src/xlsx/xlsxabstractsheet_p.h index a0cc450..47111f7 100644 --- a/src/xlsx/xlsxabstractsheet_p.h +++ b/src/xlsx/xlsxabstractsheet_p.h @@ -54,6 +54,7 @@ public: Workbook *workbook; QSharedPointer drawing; + mutable Relationships relationships; QString name; int id; diff --git a/src/xlsx/xlsxdocument.cpp b/src/xlsx/xlsxdocument.cpp index caf2331..31f5a3e 100644 --- a/src/xlsx/xlsxdocument.cpp +++ b/src/xlsx/xlsxdocument.cpp @@ -92,8 +92,8 @@ DocumentPrivate::DocumentPrivate(Document *p) : void DocumentPrivate::init() { - if (workbook->worksheetCount() == 0) - workbook->addWorksheet(); + if (workbook->sheetCount() == 0) + workbook->addSheet(); } bool DocumentPrivate::loadPackage(QIODevice *device) @@ -179,9 +179,9 @@ bool DocumentPrivate::loadPackage(QIODevice *device) workbook->theme()->loadFromXmlData(zipReader.fileData(path)); } - //load worksheets - for (int i=0; iworksheetCount(); ++i) { - Worksheet *sheet = workbook->worksheet(i); + //load sheets + for (int i=0; isheetCount(); ++i) { + AbstractSheet *sheet = workbook->sheet(i); QString rel_path = getRelFilePath(sheet->filePath()); //If the .rel file exists, load it. if (zipReader.filePaths().contains(rel_path)) @@ -229,16 +229,18 @@ bool DocumentPrivate::savePackage(QIODevice *device) const DocPropsApp docPropsApp; DocPropsCore docPropsCore; - // save worksheet xml files - for (int i=0; iworksheetCount(); ++i) { - Worksheet *sheet = workbook->worksheet(i); - contentTypes.addWorksheetName(QStringLiteral("sheet%1").arg(i+1)); - docPropsApp.addPartTitle(sheet->sheetName()); - - zipWriter.addFile(QStringLiteral("xl/worksheets/sheet%1.xml").arg(i+1), sheet->saveToXmlData()); - Relationships &rel = sheet->relationships(); - if (!rel.isEmpty()) - zipWriter.addFile(QStringLiteral("xl/worksheets/_rels/sheet%1.xml.rels").arg(i+1), rel.saveToXmlData()); + // save sheet xml files + for (int i=0; isheetCount(); ++i) { + AbstractSheet *sheet = workbook->sheet(i); + if (sheet->sheetType() == AbstractSheet::ST_WorkSheet) { + contentTypes.addWorksheetName(QStringLiteral("sheet%1").arg(i+1)); + docPropsApp.addPartTitle(sheet->sheetName()); + + zipWriter.addFile(QStringLiteral("xl/worksheets/sheet%1.xml").arg(i+1), sheet->saveToXmlData()); + Relationships &rel = sheet->relationships(); + if (!rel.isEmpty()) + zipWriter.addFile(QStringLiteral("xl/worksheets/_rels/sheet%1.xml.rels").arg(i+1), rel.saveToXmlData()); + } } // save workbook xml file @@ -261,8 +263,8 @@ bool DocumentPrivate::savePackage(QIODevice *device) const docPropsApp.setProperty(name, q->documentProperty(name)); docPropsCore.setProperty(name, q->documentProperty(name)); } - if (workbook->worksheetCount()) - docPropsApp.addHeadingPair(QStringLiteral("Worksheets"), workbook->worksheetCount()); + if (workbook->sheetCount()) + docPropsApp.addHeadingPair(QStringLiteral("Worksheets"), workbook->sheetCount()); contentTypes.addDocPropApp(); contentTypes.addDocPropCore(); zipWriter.addFile(QStringLiteral("docProps/app.xml"), docPropsApp.saveToXmlData()); @@ -414,17 +416,6 @@ Chart *Document::insertChart(int row, int col, const QSize &size) return currentWorksheet()->insertChart(row, col, size); } -/*! - * \overload - * \deprecated - * Insert an \a image to current active worksheet to the position \a row, \a column with the given - * \a xOffset, \a yOffset, \a xScale and \a yScale. - */ -int Document::insertImage(int row, int column, const QImage &image, double /*xOffset*/, double /*yOffset*/, double /*xScale*/, double /*yScale*/) -{ - return currentWorksheet()->insertImage(row, column, image); -} - /*! Merge a \a range of cells. The first cell should contain the data and the others should be blank. All cells will be applied the same style if a valid \a format is given. @@ -635,138 +626,129 @@ Workbook *Document::workbook() const return d->workbook.data(); } +/*! + * Returns the sheet object named \a sheetName. + */ +AbstractSheet *Document::sheet(const QString &sheetName) const +{ + Q_D(const Document); + return d->workbook->sheet(sheetNames().indexOf(sheetName)); +} + /*! * Returns the worksheet object named \a sheetName. + * If the type of sheet is not AbstractSheet::ST_WorkSheet, then 0 will be returned. */ Worksheet *Document::worksheet(const QString &sheetName) const { - Q_D(const Document); - return d->workbook->worksheet(worksheetNames().indexOf(sheetName)); + AbstractSheet *st = sheet(sheetName); + if (st && st->sheetType() == AbstractSheet::ST_WorkSheet) + return static_cast(st); + else + return 0; } /*! * Creates and append an document with name \a name. * Return true if success. */ -bool Document::addWorksheet(const QString &name) +bool Document::addSheet(const QString &name, AbstractSheet::SheetType type) { Q_D(Document); - return d->workbook->addWorksheet(name); + return d->workbook->addSheet(name, type); } /*! * Creates and inserts an document with name \a name at the \a index. * Returns false if the \a name already used. */ -bool Document::insertWorkSheet(int index, const QString &name) +bool Document::insertSheet(int index, const QString &name, AbstractSheet::SheetType type) { Q_D(Document); - return d->workbook->insertWorkSheet(index, name); + return d->workbook->insertSheet(index, name, type); } /*! Rename the worksheet from \a oldName to \a newName. Returns true if the success. */ -bool Document::renameWorksheet(const QString &oldName, const QString &newName) +bool Document::renameSheet(const QString &oldName, const QString &newName) { Q_D(Document); if (oldName == newName) return false; - return d->workbook->renameWorksheet(worksheetNames().indexOf(oldName), newName); + return d->workbook->renameSheet(sheetNames().indexOf(oldName), newName); } /*! Make a copy of the worksheet \a srcName with the new name \a distName. Returns true if the success. */ -bool Document::copyWorksheet(const QString &srcName, const QString &distName) +bool Document::copySheet(const QString &srcName, const QString &distName) { Q_D(Document); if (srcName == distName) return false; - return d->workbook->copyWorksheet(worksheetNames().indexOf(srcName), distName); + return d->workbook->copySheet(sheetNames().indexOf(srcName), distName); } /*! Move the worksheet \a srcName to the new pos \a distIndex. Returns true if the success. */ -bool Document::moveWorksheet(const QString &srcName, int distIndex) +bool Document::moveSheet(const QString &srcName, int distIndex) { Q_D(Document); - return d->workbook->moveWorksheet(worksheetNames().indexOf(srcName), distIndex); + return d->workbook->moveSheet(sheetNames().indexOf(srcName), distIndex); } /*! Delete the worksheet \a name. Returns true if current sheet was deleted successfully. */ -bool Document::deleteWorksheet(const QString &name) +bool Document::deleteSheet(const QString &name) { Q_D(Document); - return d->workbook->deleteWorksheet(worksheetNames().indexOf(name)); + return d->workbook->deleteSheet(sheetNames().indexOf(name)); } /*! - \deprecated - Rename current worksheet to new \a name. - Returns true if the name defined successful. - - \sa renameWorksheet() + * \brief Return pointer of current sheet. */ -bool Document::setSheetName(const QString &name) +AbstractSheet *Document::currentSheet() const { - return renameWorksheet(currentWorksheet()->sheetName(), name); + Q_D(const Document); + + return d->workbook->activeSheet(); } /*! * \brief Return pointer of current worksheet. + * If the type of sheet is not AbstractSheet::ST_WorkSheet, then 0 will be returned. */ Worksheet *Document::currentWorksheet() const { - Q_D(const Document); - if (d->workbook->worksheetCount() == 0) + AbstractSheet *st = currentSheet(); + if (st && st->sheetType() == AbstractSheet::ST_WorkSheet) + return static_cast(st); + else return 0; - - return d->workbook->activeWorksheet(); -} - -/*! - * \deprecated - * Set current worksheet to be the sheet at \a index. - * \sa selectWorksheet() - */ -void Document::setCurrentWorksheet(int index) -{ - Q_D(Document); - d->workbook->setActiveWorksheet(index); -} - -/*! - * \deprecated - * Set current selected worksheet to be the sheet named \a name. - * \sa selectWorksheet() - */ -void Document::setCurrentWorksheet(const QString &name) -{ - selectWorksheet(name); } /*! * \brief Set worksheet named \a name to be active sheet. * Returns true if success. */ -bool Document::selectWorksheet(const QString &name) +bool Document::selectSheet(const QString &name) { Q_D(Document); - return d->workbook->setActiveWorksheet(worksheetNames().indexOf(name)); + return d->workbook->setActiveSheet(sheetNames().indexOf(name)); } /*! * Returns the names of worksheets contained in current document. */ -QStringList Document::worksheetNames() const +QStringList Document::sheetNames() const { Q_D(const Document); return d->workbook->worksheetNames(); diff --git a/src/xlsx/xlsxdocument.h b/src/xlsx/xlsxdocument.h index 9d1db41..df89ceb 100644 --- a/src/xlsx/xlsxdocument.h +++ b/src/xlsx/xlsxdocument.h @@ -28,6 +28,7 @@ #include "xlsxglobal.h" #include "xlsxformat.h" +#include "xlsxworksheet.h" #include #include class QIODevice; @@ -36,7 +37,6 @@ class QImage; QT_BEGIN_NAMESPACE_XLSX class Workbook; -class Worksheet; class Cell; class CellRange; class DataValidation; @@ -60,7 +60,6 @@ public: QVariant read(const QString &cell) const; QVariant read(int row, int col) const; bool insertImage(int row, int col, const QImage &image); - Q_DECL_DEPRECATED int insertImage(int row, int column, const QImage &image, double xOffset, double yOffset, double xScale=1, double yScale=1); Chart *insertChart(int row, int col, const QSize &size); int mergeCells(const CellRange &range, const Format &format=Format()); int mergeCells(const QString &range, const Format &format=Format()); @@ -85,21 +84,20 @@ public: void setDocumentProperty(const QString &name, const QString &property); QStringList documentPropertyNames() const; - QStringList worksheetNames() const; - bool addWorksheet(const QString &name = QString()); - bool insertWorkSheet(int index, const QString &name = QString()); - bool selectWorksheet(const QString &name); - bool renameWorksheet(const QString &oldName, const QString &newName); - bool copyWorksheet(const QString &srcName, const QString &distName = QString()); - bool moveWorksheet(const QString &srcName, int distIndex); - bool deleteWorksheet(const QString &name); + QStringList sheetNames() const; + bool addSheet(const QString &name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet); + bool insertSheet(int index, const QString &name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet); + bool selectSheet(const QString &name); + bool renameSheet(const QString &oldName, const QString &newName); + bool copySheet(const QString &srcName, const QString &distName = QString()); + bool moveSheet(const QString &srcName, int distIndex); + bool deleteSheet(const QString &name); Workbook *workbook() const; + AbstractSheet *sheet(const QString &sheetName) const; + AbstractSheet *currentSheet() const; Worksheet *worksheet(const QString &sheetName) const; Worksheet *currentWorksheet() const; - Q_DECL_DEPRECATED bool setSheetName(const QString &name); - Q_DECL_DEPRECATED void setCurrentWorksheet(int index); - Q_DECL_DEPRECATED void setCurrentWorksheet(const QString &name); bool save() const; bool saveAs(const QString &xlsXname) const; diff --git a/src/xlsx/xlsxworkbook.cpp b/src/xlsx/xlsxworkbook.cpp index 9f92f1f..aaa7f35 100755 --- a/src/xlsx/xlsxworkbook.cpp +++ b/src/xlsx/xlsxworkbook.cpp @@ -159,9 +159,9 @@ bool Workbook::defineName(const QString &name, const QString &formula, const QSt int id=-1; if (!scope.isEmpty()) { - for (int i=0; iworksheets.size(); ++i) { - if (d->worksheets[i]->sheetName() == scope) { - id = d->worksheets[i]->sheetId(); + for (int i=0; isheets.size(); ++i) { + if (d->sheets[i]->sheetName() == scope) { + id = d->sheets[i]->sheetId(); break; } } @@ -171,10 +171,10 @@ bool Workbook::defineName(const QString &name, const QString &formula, const QSt return true; } -Worksheet *Workbook::addWorksheet(const QString &name) +AbstractSheet *Workbook::addSheet(const QString &name, AbstractSheet::SheetType type) { Q_D(Workbook); - return insertWorkSheet(d->worksheets.size(), name); + return insertSheet(d->sheets.size(), name, type); } /*! @@ -183,44 +183,44 @@ Worksheet *Workbook::addWorksheet(const QString &name) QStringList Workbook::worksheetNames() const { Q_D(const Workbook); - return d->worksheetNames; + return d->sheetNames; } /*! * \internal * Used only when load the xlsx file!! */ -Worksheet *Workbook::addWorksheet(const QString &name, int sheetId) +AbstractSheet *Workbook::addSheet(const QString &name, int sheetId, AbstractSheet::SheetType type) { Q_D(Workbook); if (sheetId > d->last_sheet_id) d->last_sheet_id = sheetId; Worksheet *sheet = new Worksheet(name, sheetId, this); - d->worksheets.append(QSharedPointer(sheet)); - d->worksheetNames.append(name); + d->sheets.append(QSharedPointer(sheet)); + d->sheetNames.append(name); return sheet; } -Worksheet *Workbook::insertWorkSheet(int index, const QString &name) +AbstractSheet *Workbook::insertSheet(int index, const QString &name, AbstractSheet::SheetType type) { Q_D(Workbook); QString worksheetName = createSafeSheetName(name); if (!worksheetName.isEmpty()) { //If user given an already in-used name, we should not continue any more! - if (d->worksheetNames.contains(worksheetName)) + if (d->sheetNames.contains(worksheetName)) return 0; } else { do { ++d->last_sheet_index; worksheetName = QStringLiteral("Sheet%1").arg(d->last_sheet_index); - } while (d->worksheetNames.contains(worksheetName)); + } while (d->sheetNames.contains(worksheetName)); } ++d->last_sheet_id; Worksheet *sheet = new Worksheet(worksheetName, d->last_sheet_id, this); - d->worksheets.insert(index, QSharedPointer(sheet)); - d->worksheetNames.insert(index, worksheetName); + d->sheets.insert(index, QSharedPointer(sheet)); + d->sheetNames.insert(index, worksheetName); d->activesheetIndex = index; return sheet; } @@ -228,16 +228,16 @@ Worksheet *Workbook::insertWorkSheet(int index, const QString &name) /*! * Returns current active worksheet. */ -Worksheet *Workbook::activeWorksheet() const +AbstractSheet *Workbook::activeSheet() const { Q_D(const Workbook); - return d->worksheets[d->activesheetIndex].data(); + return d->sheets[d->activesheetIndex].data(); } -bool Workbook::setActiveWorksheet(int index) +bool Workbook::setActiveSheet(int index) { Q_D(Workbook); - if (index < 0 || index >= d->worksheets.size()) { + if (index < 0 || index >= d->sheets.size()) { //warning return false; } @@ -248,113 +248,104 @@ bool Workbook::setActiveWorksheet(int index) /*! * Rename the worksheet at the \a index to \a name. */ -bool Workbook::renameWorksheet(int index, const QString &name) +bool Workbook::renameSheet(int index, const QString &name) { Q_D(Workbook); //If user given an already in-used name, return false - for (int i=0; iworksheets.size(); ++i) { - if (d->worksheets[i]->sheetName() == name) + for (int i=0; isheets.size(); ++i) { + if (d->sheets[i]->sheetName() == name) return false; } - d->worksheets[index]->setSheetName(name); - d->worksheetNames[index] = name; + d->sheets[index]->setSheetName(name); + d->sheetNames[index] = name; return true; } /*! * Remove the worksheet at pos \a index. */ -bool Workbook::deleteWorksheet(int index) +bool Workbook::deleteSheet(int index) { Q_D(Workbook); - if (d->worksheets.size() <= 1) + if (d->sheets.size() <= 1) return false; - if (index < 0 || index >= d->worksheets.size()) + if (index < 0 || index >= d->sheets.size()) return false; - d->worksheets.removeAt(index); - d->worksheetNames.removeAt(index); + d->sheets.removeAt(index); + d->sheetNames.removeAt(index); return true; } /*! * Moves the worksheet form \a srcIndex to \a distIndex. */ -bool Workbook::moveWorksheet(int srcIndex, int distIndex) +bool Workbook::moveSheet(int srcIndex, int distIndex) { Q_D(Workbook); if (srcIndex == distIndex) return false; - if (srcIndex < 0 || srcIndex >= d->worksheets.size()) + if (srcIndex < 0 || srcIndex >= d->sheets.size()) return false; - QSharedPointer sheet = d->worksheets.takeAt(srcIndex); - d->worksheetNames.takeAt(srcIndex); - if (distIndex >= 0 || distIndex <= d->worksheets.size()) { - d->worksheets.insert(distIndex, sheet); - d->worksheetNames.insert(distIndex, sheet->sheetName()); + QSharedPointer sheet = d->sheets.takeAt(srcIndex); + d->sheetNames.takeAt(srcIndex); + if (distIndex >= 0 || distIndex <= d->sheets.size()) { + d->sheets.insert(distIndex, sheet); + d->sheetNames.insert(distIndex, sheet->sheetName()); } else { - d->worksheets.append(sheet); - d->worksheetNames.append(sheet->sheetName()); + d->sheets.append(sheet); + d->sheetNames.append(sheet->sheetName()); } return true; } -bool Workbook::copyWorksheet(int index, const QString &newName) +bool Workbook::copySheet(int index, const QString &newName) { Q_D(Workbook); - if (index < 0 || index >= d->worksheets.size()) + if (index < 0 || index >= d->sheets.size()) return false; QString worksheetName = newName; if (!newName.isEmpty()) { //If user given an already in-used name, we should not continue any more! - if (d->worksheetNames.contains(newName)) + if (d->sheetNames.contains(newName)) return false; } else { int copy_index = 1; do { ++copy_index; - worksheetName = QStringLiteral("%1(%2)").arg(d->worksheets[index]->sheetName()).arg(copy_index); - } while (d->worksheetNames.contains(worksheetName)); + worksheetName = QStringLiteral("%1(%2)").arg(d->sheets[index]->sheetName()).arg(copy_index); + } while (d->sheetNames.contains(worksheetName)); } ++d->last_sheet_id; - QSharedPointer sheet = d->worksheets[index]->copy(worksheetName, d->last_sheet_id); - d->worksheets.append(sheet); - d->worksheetNames.append(sheet->sheetName()); + AbstractSheet *sheet = d->sheets[index]->copy(worksheetName, d->last_sheet_id); + d->sheets.append(QSharedPointer (sheet)); + d->sheetNames.append(sheet->sheetName()); return false; } -/*! - \deprecated -*/ -QList > Workbook::worksheets() const -{ - Q_D(const Workbook); - return d->worksheets; -} - /*! * Returns count of worksheets. */ -int Workbook::worksheetCount() const +int Workbook::sheetCount() const { Q_D(const Workbook); - return d->worksheets.count(); + return d->sheets.count(); } /*! * Returns the sheet object at index \a sheetIndex. */ -Worksheet *Workbook::worksheet(int index) const +AbstractSheet *Workbook::sheet(int index) const { Q_D(const Workbook); - if (index < 0 || index >= d->worksheets.size()) + if (index < 0 || index >= d->sheets.size()) return 0; - return d->worksheets.at(index).data(); + return d->sheets.at(index).data(); } SharedStrings *Workbook::sharedStrings() const @@ -384,8 +375,8 @@ QList Workbook::drawings() { Q_D(Workbook); QList ds; - for (int i=0; iworksheets.size(); ++i) { - QSharedPointer sheet = d->worksheets[i]; + for (int i=0; isheets.size(); ++i) { + QSharedPointer sheet = d->sheets[i]; if (sheet->drawing()) ds.append(sheet->drawing()); } @@ -398,7 +389,7 @@ void Workbook::saveToXmlFile(QIODevice *device) const Q_D(const Workbook); d->relationships.clear(); - for (int i=0; irelationships.addDocumentRelationship(QStringLiteral("/worksheet"), QStringLiteral("worksheets/sheet%1.xml").arg(i+1)); d->relationships.addDocumentRelationship(QStringLiteral("/theme"), QStringLiteral("theme/theme1.xml")); d->relationships.addDocumentRelationship(QStringLiteral("/styles"), QStringLiteral("styles.xml")); @@ -440,8 +431,8 @@ void Workbook::saveToXmlFile(QIODevice *device) const writer.writeEndElement();//bookViews writer.writeStartElement(QStringLiteral("sheets")); - for (int i=0; iworksheets.size(); ++i) { - QSharedPointer sheet = d->worksheets[i]; + for (int i=0; isheets.size(); ++i) { + QSharedPointer sheet = d->sheets[i]; writer.writeEmptyElement(QStringLiteral("sheet")); writer.writeAttribute(QStringLiteral("name"), sheet->sheetName()); writer.writeAttribute(QStringLiteral("sheetId"), QString::number(sheet->sheetId())); @@ -460,8 +451,8 @@ void Workbook::saveToXmlFile(QIODevice *device) const writer.writeAttribute(QStringLiteral("comment"), data.comment); if (data.sheetId != -1) { //find the local index of the sheet. - for (int i=0; iworksheets.size(); ++i) { - if (d->worksheets[i]->sheetId() == data.sheetId) { + for (int i=0; isheets.size(); ++i) { + if (d->sheets[i]->sheetId() == data.sheetId) { writer.writeAttribute(QStringLiteral("localSheetId"), QString::number(i)); break; } @@ -497,9 +488,22 @@ bool Workbook::loadFromXmlFile(QIODevice *device) // if (attributes.hasAttribute(QLatin1String("state"))) // QString state = attributes.value(QLatin1String("state")).toString(); - Worksheet *sheet = addWorksheet(name, sheetId); - const QString path = d->relationships.getRelationshipById(rId).target; - const QString fullPath = QDir::cleanPath(splitPath(filePath())[0] +QLatin1String("/")+ path); + XlsxRelationship relationship = d->relationships.getRelationshipById(rId); + + AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet; + if (relationship.type.endsWith(QLatin1String("/worksheet"))) + type = AbstractSheet::ST_WorkSheet; + else if (relationship.type.endsWith(QLatin1String("/chartsheet"))) + type = AbstractSheet::ST_ChartSheet; + else if (relationship.type.endsWith(QLatin1String("/dialogsheet"))) + type = AbstractSheet::ST_DialogSheet; + else if (relationship.type.endsWith(QLatin1String("/xlMacrosheet"))) + type = AbstractSheet::ST_MacroSheet; + else + qWarning("unknown sheet type"); + + AbstractSheet *sheet = addSheet(name, sheetId, type); + const QString fullPath = QDir::cleanPath(splitPath(filePath())[0] +QLatin1String("/")+ relationship.target); sheet->setFilePath(fullPath); } else if (reader.name() == QLatin1String("workbookPr")) { QXmlStreamAttributes attrs = reader.attributes(); diff --git a/src/xlsx/xlsxworkbook.h b/src/xlsx/xlsxworkbook.h index 9e69a83..d5ed724 100755 --- a/src/xlsx/xlsxworkbook.h +++ b/src/xlsx/xlsxworkbook.h @@ -27,6 +27,7 @@ #include "xlsxglobal.h" #include "xlsxooxmlfile.h" +#include "xlsxabstractsheet.h" #include #include #include @@ -35,7 +36,6 @@ class QIODevice; QT_BEGIN_NAMESPACE_XLSX -class Worksheet; class SharedStrings; class Styles; class Drawing; @@ -53,19 +53,18 @@ class Q_XLSX_EXPORT Workbook : public OOXmlFile public: ~Workbook(); - Q_DECL_DEPRECATED QList > worksheets() const; - int worksheetCount() const; - Worksheet *worksheet(int sheetIndex) const; + int sheetCount() const; + AbstractSheet *sheet(int index) const; - Worksheet *addWorksheet(const QString &name = QString()); - Worksheet *insertWorkSheet(int index, const QString &name = QString()); - bool renameWorksheet(int index, const QString &name); - bool deleteWorksheet(int index); - bool copyWorksheet(int index, const QString &newName=QString()); - bool moveWorksheet(int srcIndex, int distIndex); + AbstractSheet *addSheet(const QString &name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet); + AbstractSheet *insertSheet(int index, const QString &name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet); + bool renameSheet(int index, const QString &name); + bool deleteSheet(int index); + bool copySheet(int index, const QString &newName=QString()); + bool moveSheet(int srcIndex, int distIndex); - Worksheet *activeWorksheet() const; - bool setActiveWorksheet(int index); + AbstractSheet *activeSheet() const; + bool setActiveSheet(int index); // void addChart(); bool defineName(const QString &name, const QString &formula, const QString &comment=QString(), const QString &scope=QString()); @@ -102,7 +101,7 @@ private: QList images(); QList drawings(); QStringList worksheetNames() const; - Worksheet *addWorksheet(const QString &name, int sheetId); + AbstractSheet *addSheet(const QString &name, int sheetId, AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet); }; QT_END_NAMESPACE_XLSX diff --git a/src/xlsx/xlsxworkbook_p.h b/src/xlsx/xlsxworkbook_p.h index 1f40bbc..f6abc0f 100644 --- a/src/xlsx/xlsxworkbook_p.h +++ b/src/xlsx/xlsxworkbook_p.h @@ -83,8 +83,8 @@ public: mutable Relationships relationships; QSharedPointer sharedStrings; - QList > worksheets; - QStringList worksheetNames; + QList > sheets; + QStringList sheetNames; QSharedPointer styles; QSharedPointer theme; QList > mediaFiles; diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index e611623..4c75294 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/src/xlsx/xlsxworksheet.cpp @@ -184,11 +184,10 @@ Worksheet::Worksheet(const QString &name, int id, Workbook *workbook) * Make a copy of this sheet. */ -QSharedPointer Worksheet::copy(const QString &distName, int distId) const +Worksheet *Worksheet::copy(const QString &distName, int distId) const { Q_D(const Worksheet); - QSharedPointer sheet(new Worksheet(distName, distId, d->workbook)); - + Worksheet *sheet = new Worksheet(distName, distId, d->workbook); WorksheetPrivate *sheet_d = sheet->d_func(); sheet_d->dimension = d->dimension; @@ -203,7 +202,7 @@ QSharedPointer Worksheet::copy(const QString &distName, int distId) c int col = it2.key(); QSharedPointer cell(new Cell(it2.value().data())); - cell->d_ptr->parent = sheet.data(); + cell->d_ptr->parent = sheet; if (cell->dataType() == Cell::String) d->workbook->sharedStrings()->addSharedString(cell->d_ptr->richString); @@ -229,15 +228,6 @@ Worksheet::~Worksheet() { } -/*! - * \internal - */ -Relationships &Worksheet::relationships() -{ - Q_D(Worksheet); - return d->relationships; -} - /*! * Returns whether sheet is protected. */ diff --git a/src/xlsx/xlsxworksheet.h b/src/xlsx/xlsxworksheet.h index 929d695..db18dd9 100755 --- a/src/xlsx/xlsxworksheet.h +++ b/src/xlsx/xlsxworksheet.h @@ -129,14 +129,13 @@ public: bool isWhiteSpaceVisible() const; void setWhiteSpaceVisible(bool visible); - Relationships &relationships(); ~Worksheet(); private: friend class DocumentPrivate; friend class Workbook; friend class ::WorksheetTest; Worksheet(const QString &sheetName, int sheetId, Workbook *book); - QSharedPointer copy(const QString &distName, int distId) const; + Worksheet *copy(const QString &distName, int distId) const; void saveToXmlFile(QIODevice *device) const; bool loadFromXmlFile(QIODevice *device); diff --git a/src/xlsx/xlsxworksheet_p.h b/src/xlsx/xlsxworksheet_p.h index 96226c2..816875e 100644 --- a/src/xlsx/xlsxworksheet_p.h +++ b/src/xlsx/xlsxworksheet_p.h @@ -41,7 +41,6 @@ #include "xlsxcell.h" #include "xlsxdatavalidation.h" #include "xlsxconditionalformatting.h" -#include "xlsxrelationships_p.h" #include #include @@ -143,7 +142,6 @@ public: SharedStrings *sharedStrings() const; - mutable Relationships relationships; QMap > > cellTable; QMap > comments; QMap > > urlTable; diff --git a/tests/auto/document/tst_documenttest.cpp b/tests/auto/document/tst_documenttest.cpp index 6de637e..8163256 100644 --- a/tests/auto/document/tst_documenttest.cpp +++ b/tests/auto/document/tst_documenttest.cpp @@ -308,32 +308,32 @@ void DocumentTest::testReadWriteTime() void DocumentTest::testMoveWorksheet() { Document xlsx1; - xlsx1.addWorksheet(); + xlsx1.addSheet(); - QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2"); - xlsx1.moveWorksheet("Sheet2", 0); - QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet2"<<"Sheet1"); - xlsx1.moveWorksheet("Sheet2", 1); - QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2"); + QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet2"); + xlsx1.moveSheet("Sheet2", 0); + QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet2"<<"Sheet1"); + xlsx1.moveSheet("Sheet2", 1); + QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet2"); } void DocumentTest::testCopyWorksheet() { Document xlsx1; - xlsx1.addWorksheet(); + xlsx1.addSheet(); xlsx1.write("A1", "String"); xlsx1.write("A2", 999); xlsx1.write("A3", true); - xlsx1.addWorksheet(); - QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3"); + xlsx1.addSheet(); + QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3"); - xlsx1.copyWorksheet("Sheet2"); - QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3"<<"Sheet2(2)"); + xlsx1.copySheet("Sheet2"); + QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3"<<"Sheet2(2)"); - xlsx1.deleteWorksheet("Sheet2"); - QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet3"<<"Sheet2(2)"); + xlsx1.deleteSheet("Sheet2"); + QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet3"<<"Sheet2(2)"); - xlsx1.selectWorksheet("Sheet2(2)"); + xlsx1.selectSheet("Sheet2(2)"); QCOMPARE(xlsx1.read("A1").toString(), QString("String")); QCOMPARE(xlsx1.read("A2").toInt(), 999); QCOMPARE(xlsx1.read("A3").toBool(), true); @@ -342,18 +342,18 @@ void DocumentTest::testCopyWorksheet() void DocumentTest::testDeleteWorksheet() { Document xlsx1; - xlsx1.addWorksheet(); - xlsx1.addWorksheet(); + xlsx1.addSheet(); + xlsx1.addSheet(); - QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3"); - xlsx1.deleteWorksheet("Sheet2"); - QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet3"); - xlsx1.deleteWorksheet("Sheet1"); - QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet3"); + QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3"); + xlsx1.deleteSheet("Sheet2"); + QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet3"); + xlsx1.deleteSheet("Sheet1"); + QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet3"); //Cann't delete the last worksheet - xlsx1.deleteWorksheet("Sheet3"); - QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet3"); + xlsx1.deleteSheet("Sheet3"); + QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet3"); } QTEST_APPLESS_MAIN(DocumentTest)