From 176160d2a8bc5a8cd7e814443a14ae1c282af7cd Mon Sep 17 00:00:00 2001 From: Debao Zhang Date: Fri, 22 Nov 2013 00:43:08 +0800 Subject: [PATCH] API change: Using Format instead of Format * --- examples/xlsx/demo/main.cpp | 94 +++--- examples/xlsx/formulas/main.cpp | 8 +- examples/xlsx/mergecells/main.cpp | 6 +- examples/xlsx/numberformat/main.cpp | 14 +- examples/xlsx/rowcolumn/main.cpp | 14 +- examples/xlsx/style/main.cpp | 36 +- src/xlsx/xlsxcell.cpp | 6 +- src/xlsx/xlsxcell.h | 5 +- src/xlsx/xlsxcell_p.h | 2 +- src/xlsx/xlsxdocument.cpp | 23 +- src/xlsx/xlsxdocument.h | 17 +- src/xlsx/xlsxformat.cpp | 18 +- src/xlsx/xlsxformat.h | 1 + src/xlsx/xlsxstyles.cpp | 405 +++++++++++------------ src/xlsx/xlsxstyles_p.h | 29 +- src/xlsx/xlsxworkbook.cpp | 6 - src/xlsx/xlsxworkbook.h | 2 - src/xlsx/xlsxworksheet.cpp | 155 +++++---- src/xlsx/xlsxworksheet.h | 50 +-- src/xlsx/xlsxworksheet_p.h | 10 +- tests/auto/document/tst_documenttest.cpp | 80 ++--- tests/auto/styles/tst_stylestest.cpp | 34 +- tests/auto/worksheet/tst_worksheet.cpp | 2 +- 23 files changed, 499 insertions(+), 518 deletions(-) diff --git a/examples/xlsx/demo/main.cpp b/examples/xlsx/demo/main.cpp index 2dad0fd..d565e1b 100644 --- a/examples/xlsx/demo/main.cpp +++ b/examples/xlsx/demo/main.cpp @@ -8,17 +8,17 @@ QTXLSX_USE_NAMESPACE void writeHorizontalAlignCell(Document &xlsx, const QString &cell, const QString &text, Format::HorizontalAlignment align) { - Format *format = xlsx.createFormat(); - format->setHorizontalAlignment(align); - format->setBorderStyle(Format::BorderThin); + Format format; + format.setHorizontalAlignment(align); + format.setBorderStyle(Format::BorderThin); xlsx.write(cell, text, format); } void writeVerticalAlignCell(Document &xlsx, const QString &range, const QString &text, Format::VerticalAlignment align) { - Format *format = xlsx.createFormat(); - format->setVerticalAlignment(align); - format->setBorderStyle(Format::BorderThin); + Format format; + format.setVerticalAlignment(align); + format.setBorderStyle(Format::BorderThin); CellRange r(range); xlsx.write(r.firstRow(), r.firstColumn(), text); xlsx.mergeCells(r, format); @@ -26,54 +26,54 @@ void writeVerticalAlignCell(Document &xlsx, const QString &range, const QString void writeBorderStyleCell(Document &xlsx, const QString &cell, const QString &text, Format::BorderStyle bs) { - Format *format = xlsx.createFormat(); - format->setBorderStyle(bs); + Format format; + format.setBorderStyle(bs); xlsx.write(cell, text, format); } void writeSolidFillCell(Document &xlsx, const QString &cell, const QColor &color) { - Format *format = xlsx.createFormat(); - format->setPatternBackgroundColor(color); + Format format; + format.setPatternBackgroundColor(color); xlsx.write(cell, QVariant(), format); } void writePatternFillCell(Document &xlsx, const QString &cell, Format::FillPattern pattern, const QColor &color) { - Format *format = xlsx.createFormat(); - format->setPatternForegroundColor(color); - format->setFillPattern(pattern); + Format format; + format.setPatternForegroundColor(color); + format.setFillPattern(pattern); xlsx.write(cell, QVariant(), format); } void writeBorderAndFontColorCell(Document &xlsx, const QString &cell, const QString &text, const QColor &color) { - Format *format = xlsx.createFormat(); - format->setBorderStyle(Format::BorderThin); - format->setBorderColor(color); - format->setFontColor(color); + Format format; + format.setBorderStyle(Format::BorderThin); + format.setBorderColor(color); + format.setFontColor(color); xlsx.write(cell, text, format); } void writeFontNameCell(Document &xlsx, const QString &cell, const QString &text) { - Format *format = xlsx.createFormat(); - format->setFontName(text); - format->setFontSize(16); + Format format; + format.setFontName(text); + format.setFontSize(16); xlsx.write(cell, text, format); } void writeFontSizeCell(Document &xlsx, const QString &cell, int size) { - Format *format = xlsx.createFormat(); - format->setFontSize(size); + Format format; + format.setFontSize(size); xlsx.write(cell, "Qt Xlsx", format); } void writeInternalNumFormatsCell(Document &xlsx, int row, double value, int numFmt) { - Format *format = xlsx.createFormat(); - format->setNumberFormatIndex(numFmt); + Format format; + format.setNumberFormatIndex(numFmt); xlsx.write(row, 1, value); xlsx.write(row, 2, QString("Builtin NumFmt %1").arg(numFmt)); xlsx.write(row, 3, value, format); @@ -81,8 +81,8 @@ void writeInternalNumFormatsCell(Document &xlsx, int row, double value, int numF void writeCustomNumFormatsCell(Document &xlsx, int row, double value, const QString &numFmt) { - Format *format = xlsx.createFormat(); - format->setNumberFormat(numFmt); + Format format; + format.setNumberFormat(numFmt); xlsx.write(row, 1, value); xlsx.write(row, 2, numFmt); xlsx.write(row, 3, value, format); @@ -141,17 +141,17 @@ int main() xlsx.addWorksheet("Fonts"); xlsx.write("B3", "Normal"); - Format *font_bold = xlsx.createFormat(); - font_bold->setFontBold(true); + Format font_bold; + font_bold.setFontBold(true); xlsx.write("B4", "Bold", font_bold); - Format *font_italic = xlsx.createFormat(); - font_italic->setFontItalic(true); + Format font_italic; + font_italic.setFontItalic(true); xlsx.write("B5", "Italic", font_italic); - Format *font_underline = xlsx.createFormat(); - font_underline->setFontUnderline(Format::FontUnderlineSingle); + Format font_underline; + font_underline.setFontUnderline(Format::FontUnderlineSingle); xlsx.write("B6", "Underline", font_underline); - Format *font_strikeout = xlsx.createFormat(); - font_strikeout->setFontStrikeOut(true); + Format font_strikeout; + font_strikeout.setFontStrikeOut(true); xlsx.write("B7", "StrikeOut", font_strikeout); writeFontNameCell(xlsx, "D3", "Arial"); @@ -170,9 +170,9 @@ int main() writeFontSizeCell(xlsx, "G8", 20); writeFontSizeCell(xlsx, "G9", 25); - Format *font_vertical = xlsx.createFormat(); - font_vertical->setRotation(255); - font_vertical->setFontSize(16); + Format font_vertical; + font_vertical.setRotation(255); + font_vertical.setFontSize(16); xlsx.write("J3", "vertical", font_vertical); xlsx.mergeCells("J3:J9"); @@ -180,10 +180,10 @@ int main() //Create the third sheet. xlsx.addWorksheet("Formulas"); xlsx.setColumn("A", "B", 40); - Format *rAlign = xlsx.createFormat(); - rAlign->setHorizontalAlignment(Format::AlignRight); - Format *lAlign = xlsx.createFormat(); - lAlign->setHorizontalAlignment(Format::AlignLeft); + Format rAlign; + rAlign.setHorizontalAlignment(Format::AlignRight); + Format lAlign; + lAlign.setHorizontalAlignment(Format::AlignLeft); xlsx.write("B3", 40, lAlign); xlsx.write("B4", 30, lAlign); xlsx.write("B5", 50, lAlign); @@ -215,9 +215,9 @@ int main() xlsx.write("A21", "LEN(\"Hello Qt!\")=", rAlign); xlsx.write("B21", "=LEN(\"Hello Qt!\")", lAlign); - Format *dateFormat = xlsx.createFormat(); - dateFormat->setHorizontalAlignment(Format::AlignLeft); - dateFormat->setNumberFormat("yyyy-mm-dd"); + Format dateFormat; + dateFormat.setHorizontalAlignment(Format::AlignLeft); + dateFormat.setNumberFormat("yyyy-mm-dd"); xlsx.write("A23", "DATE(2013,8,13)=", rAlign); xlsx.write("B23", "=DATE(2013,8,13)", dateFormat); xlsx.write("A24", "DAY(B23)=", rAlign); @@ -260,9 +260,9 @@ int main() //--------------------------------------------------------------- //Create the fifth sheet. xlsx.addWorksheet("Merging"); - Format *centerAlign = xlsx.createFormat(); - centerAlign->setHorizontalAlignment(Format::AlignHCenter); - centerAlign->setVerticalAlignment(Format::AlignVCenter); + Format centerAlign; + centerAlign.setHorizontalAlignment(Format::AlignHCenter); + centerAlign.setVerticalAlignment(Format::AlignVCenter); xlsx.write("B4", "Hello Qt!"); xlsx.mergeCells("B4:F6", centerAlign); xlsx.write("B8", 1); diff --git a/examples/xlsx/formulas/main.cpp b/examples/xlsx/formulas/main.cpp index 7d0bbda..68ceddc 100644 --- a/examples/xlsx/formulas/main.cpp +++ b/examples/xlsx/formulas/main.cpp @@ -13,10 +13,10 @@ int main() //![1] xlsx.setColumn("A", "B", 40); - Format *rAlign = xlsx.createFormat(); - rAlign->setHorizontalAlignment(Format::AlignRight); - Format *lAlign = xlsx.createFormat(); - lAlign->setHorizontalAlignment(Format::AlignLeft); + Format rAlign; + rAlign.setHorizontalAlignment(Format::AlignRight); + Format lAlign; + lAlign.setHorizontalAlignment(Format::AlignLeft); xlsx.write("B3", 40, lAlign); xlsx.write("B4", 30, lAlign); xlsx.write("B5", 50, lAlign); diff --git a/examples/xlsx/mergecells/main.cpp b/examples/xlsx/mergecells/main.cpp index 076994a..28e5e9d 100644 --- a/examples/xlsx/mergecells/main.cpp +++ b/examples/xlsx/mergecells/main.cpp @@ -7,9 +7,9 @@ int main() { Document xlsx; //![0] - Format *format = xlsx.createFormat(); - format->setHorizontalAlignment(Format::AlignHCenter); - format->setVerticalAlignment(Format::AlignVCenter); + Format format; + format.setHorizontalAlignment(Format::AlignHCenter); + format.setVerticalAlignment(Format::AlignVCenter); //![0] //![1] xlsx.write("B4", "Hello Qt!"); diff --git a/examples/xlsx/numberformat/main.cpp b/examples/xlsx/numberformat/main.cpp index e099811..c4e13d1 100644 --- a/examples/xlsx/numberformat/main.cpp +++ b/examples/xlsx/numberformat/main.cpp @@ -9,9 +9,9 @@ int main(int argc, char** argv) QXlsx::Document xlsx; xlsx.setColumn(1, 4, 20.0); - QXlsx::Format *header = xlsx.createFormat(); - header->setFontBold(true); - header->setFontSize(20); + QXlsx::Format header; + header.setFontBold(true); + header.setFontSize(20); //Custom number formats QStringList numFormats; @@ -26,8 +26,8 @@ int main(int argc, char** argv) int row = i+2; xlsx.write(row, 1, 100.0); xlsx.write(row, 2, numFormats[i]); - QXlsx::Format *format = xlsx.createFormat(); - format->setNumberFormat(numFormats[i]); + QXlsx::Format format; + format.setNumberFormat(numFormats[i]); xlsx.write(row, 3, 100.0, format); } @@ -42,8 +42,8 @@ int main(int argc, char** argv) int numFmt = i; xlsx.write(row, 1, 100.0); xlsx.write(row, 2, numFmt); - QXlsx::Format *format = xlsx.createFormat(); - format->setNumberFormatIndex(numFmt); + QXlsx::Format format; + format.setNumberFormatIndex(numFmt); xlsx.write(row, 3, 100.0, format); } diff --git a/examples/xlsx/rowcolumn/main.cpp b/examples/xlsx/rowcolumn/main.cpp index 7e20062..2c2278a 100644 --- a/examples/xlsx/rowcolumn/main.cpp +++ b/examples/xlsx/rowcolumn/main.cpp @@ -14,18 +14,18 @@ int main() xlsx.setColumn(3, 3, 40.0); //Set style for the row 11th. - QXlsx::Format *format1 = xlsx.createFormat(); - format1->setFontBold(true); - format1->setFontColor(QColor(Qt::blue)); - format1->setFontSize(20); + QXlsx::Format format1; + format1.setFontBold(true); + format1.setFontColor(QColor(Qt::blue)); + format1.setFontSize(20); xlsx.write(11, 1, "Hello Row Style"); xlsx.write(11, 6, "Blue Color"); xlsx.setRow(11, 41, format1); //Set style for the col [9th, 16th) - QXlsx::Format *format2 = xlsx.createFormat(); - format2->setFontBold(true); - format2->setFontColor(QColor(Qt::magenta)); + QXlsx::Format format2; + format2.setFontBold(true); + format2.setFontColor(QColor(Qt::magenta)); for (int row=12; row<=30; row++) for (int col=9; col<=15; col++) xlsx.write(row, col, row+col); diff --git a/examples/xlsx/style/main.cpp b/examples/xlsx/style/main.cpp index 5e9eb0f..b2eedd7 100644 --- a/examples/xlsx/style/main.cpp +++ b/examples/xlsx/style/main.cpp @@ -5,32 +5,32 @@ int main() { QXlsx::Document xlsx; - QXlsx::Format *format1 = xlsx.createFormat(); - format1->setFontColor(QColor(Qt::red)); - format1->setFontSize(15); - format1->setHorizontalAlignment(QXlsx::Format::AlignHCenter); - format1->setBorderStyle(QXlsx::Format::BorderDashDotDot); + QXlsx::Format format1; + format1.setFontColor(QColor(Qt::red)); + format1.setFontSize(15); + format1.setHorizontalAlignment(QXlsx::Format::AlignHCenter); + format1.setBorderStyle(QXlsx::Format::BorderDashDotDot); xlsx.write("A1", "Hello Qt!", format1); xlsx.write("B3", 12345, format1); - QXlsx::Format *format2 = xlsx.createFormat(); - format2->setFontBold(true); - format2->setFontUnderline(QXlsx::Format::FontUnderlineDouble); - format2->setFillPattern(QXlsx::Format::PatternLightUp); + QXlsx::Format format2; + format2.setFontBold(true); + format2.setFontUnderline(QXlsx::Format::FontUnderlineDouble); + format2.setFillPattern(QXlsx::Format::PatternLightUp); xlsx.write("C5", "=44+33", format2); xlsx.write("D7", true, format2); - QXlsx::Format *format3 = xlsx.createFormat(); - format3->setFontBold(true); - format3->setFontColor(QColor(Qt::blue)); - format3->setFontSize(20); + QXlsx::Format format3; + format3.setFontBold(true); + format3.setFontColor(QColor(Qt::blue)); + format3.setFontSize(20); xlsx.write(11, 1, "Hello Row Style"); xlsx.write(11, 6, "Blue Color"); xlsx.setRow(11, 41, format3); - QXlsx::Format *format4 = xlsx.createFormat(); - format4->setFontBold(true); - format4->setFontColor(QColor(Qt::magenta)); + QXlsx::Format format4; + format4.setFontBold(true); + format4.setFontColor(QColor(Qt::magenta)); for (int row=21; row<=40; row++) for (int col=9; col<16; col++) xlsx.write(row, col, row+col); @@ -38,8 +38,8 @@ int main() xlsx.write("A5", QDate(2013, 8, 29)); - QXlsx::Format *format6 = xlsx.createFormat(); - format6->setPatternBackgroundColor(QColor(Qt::green)); + QXlsx::Format format6; + format6.setPatternBackgroundColor(QColor(Qt::green)); xlsx.write("A6", "Background color: green", format6); xlsx.saveAs("book1.xlsx"); diff --git a/src/xlsx/xlsxcell.cpp b/src/xlsx/xlsxcell.cpp index bca708a..16f9de1 100644 --- a/src/xlsx/xlsxcell.cpp +++ b/src/xlsx/xlsxcell.cpp @@ -50,7 +50,7 @@ CellPrivate::CellPrivate(Cell *p) : * \internal * Created by Worksheet only. */ -Cell::Cell(const QVariant &data, DataType type, Format *format, Worksheet *parent) : +Cell::Cell(const QVariant &data, DataType type, const Format &format, Worksheet *parent) : d_ptr(new CellPrivate(this)) { d_ptr->value = data; @@ -88,7 +88,7 @@ QVariant Cell::value() const /*! * Return the style used by this Cell. If no style used, 0 will be returned. */ -Format *Cell::format() const +Format Cell::format() const { Q_D(const Cell); return d->format; @@ -109,7 +109,7 @@ QString Cell::formula() const bool Cell::isDateTime() const { Q_D(const Cell); - if (d->dataType == Numeric && d->format && d->format->isDateTimeFormat()) + if (d->dataType == Numeric && d->format.isValid() && d->format.isDateTimeFormat()) return true; return false; } diff --git a/src/xlsx/xlsxcell.h b/src/xlsx/xlsxcell.h index fcb67c9..96cca9d 100644 --- a/src/xlsx/xlsxcell.h +++ b/src/xlsx/xlsxcell.h @@ -26,6 +26,7 @@ #define QXLSX_XLSXCELL_H #include "xlsxglobal.h" +#include "xlsxformat.h" #include QT_BEGIN_NAMESPACE_XLSX @@ -52,7 +53,7 @@ public: DataType dataType() const; QVariant value() const; - Format * format() const; + Format format() const; QString formula() const; bool isDateTime() const; @@ -65,7 +66,7 @@ private: friend class Worksheet; friend class WorksheetPrivate; - Cell(const QVariant &data=QVariant(), DataType type=Blank, Format *format=0, Worksheet *parent=0); + Cell(const QVariant &data=QVariant(), DataType type=Blank, const Format &format=Format(), Worksheet *parent=0); CellPrivate * const d_ptr; }; diff --git a/src/xlsx/xlsxcell_p.h b/src/xlsx/xlsxcell_p.h index 11e9096..ab122ec 100644 --- a/src/xlsx/xlsxcell_p.h +++ b/src/xlsx/xlsxcell_p.h @@ -43,7 +43,7 @@ public: QVariant value; QString formula; Cell::DataType dataType; - Format *format; + Format format; CellRange range; //used for arrayFormula RichString richString; diff --git a/src/xlsx/xlsxdocument.cpp b/src/xlsx/xlsxdocument.cpp index 55cee33..723f8b6 100644 --- a/src/xlsx/xlsxdocument.cpp +++ b/src/xlsx/xlsxdocument.cpp @@ -101,21 +101,12 @@ Document::Document(QIODevice *device, QObject *parent) : d_ptr->init(); } -/*! - * Create a new format used by sheet cells. - */ -Format *Document::createFormat() -{ - Q_D(Document); - return d->workbook->createFormat(); -} - /*! \overload Write \a value to cell \a row_column with the \a format. */ -int Document::write(const QString &row_column, const QVariant &value, Format *format) +int Document::write(const QString &row_column, const QVariant &value, const Format &format) { return currentWorksheet()->write(row_column, value, format); } @@ -123,7 +114,7 @@ int Document::write(const QString &row_column, const QVariant &value, Format *fo /*! * Write \a value to cell (\a row, \a col) with the \a format. */ -int Document::write(int row, int col, const QVariant &value, Format *format) +int Document::write(int row, int col, const QVariant &value, const Format &format) { return currentWorksheet()->write(row, col, value, format); } @@ -165,7 +156,7 @@ int Document::insertImage(int row, int column, const QImage &image, double xOffs \note All cells except the top-left one will be cleared. */ -int Document::mergeCells(const CellRange &range, Format *format) +int Document::mergeCells(const CellRange &range, const Format &format) { return currentWorksheet()->mergeCells(range, format); } @@ -177,7 +168,7 @@ int Document::mergeCells(const CellRange &range, Format *format) \note All cells except the top-left one will be cleared. */ -int Document::mergeCells(const QString &range, Format *format) +int Document::mergeCells(const QString &range, const Format &format) { return currentWorksheet()->mergeCells(range, format); } @@ -202,7 +193,7 @@ int Document::unmergeCells(const CellRange &range) Sets row \a height and \a format. Row height measured in point size. If format equals 0 then format is ignored. \a row is 1-indexed. */ -bool Document::setRow(int row, double height, Format *format, bool hidden) +bool Document::setRow(int row, double height, const Format &format, bool hidden) { return currentWorksheet()->setRow(row, height, format, hidden); } @@ -213,7 +204,7 @@ bool Document::setRow(int row, double height, Format *format, bool hidden) numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. If format equals 0 then format is ignored. \a colFirst and \a colLast are all 1-indexed. */ -bool Document::setColumn(int colFirst, int colLast, double width, Format *format, bool hidden) +bool Document::setColumn(int colFirst, int colLast, double width, const Format &format, bool hidden) { return currentWorksheet()->setColumn(colFirst, colLast, width, format, hidden); } @@ -224,7 +215,7 @@ bool Document::setColumn(int colFirst, int colLast, double width, Format *format numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. If format equals 0 then format is ignored. \a colFirst and \a colLast should be "A", "B", "C", ... */ -bool Document::setColumn(const QString &colFirst, const QString &colLast, double width, Format *format, bool hidden) +bool Document::setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format, bool hidden) { return currentWorksheet()->setColumn(colFirst, colLast, width, format, hidden); } diff --git a/src/xlsx/xlsxdocument.h b/src/xlsx/xlsxdocument.h index 6d3c66e..d8c369e 100644 --- a/src/xlsx/xlsxdocument.h +++ b/src/xlsx/xlsxdocument.h @@ -27,6 +27,7 @@ #define QXLSX_XLSXDOCUMENT_H #include "xlsxglobal.h" +#include "xlsxformat.h" #include #include class QIODevice; @@ -37,7 +38,6 @@ QT_BEGIN_NAMESPACE_XLSX class Workbook; class Worksheet; class Package; -class Format; class Cell; class CellRange; class DataValidation; @@ -54,19 +54,18 @@ public: Document(QIODevice *device, QObject *parent=0); ~Document(); - Format *createFormat(); - int write(const QString &cell, const QVariant &value, Format *format=0); - int write(int row, int col, const QVariant &value, Format *format=0); + int write(const QString &cell, const QVariant &value, const Format &format=Format()); + int write(int row, int col, const QVariant &value, const Format &format=Format()); QVariant read(const QString &cell) const; QVariant read(int row, int col) const; int insertImage(int row, int column, const QImage &image, double xOffset=0, double yOffset=0, double xScale=1, double yScale=1); - int mergeCells(const CellRange &range, Format *format=0); - int mergeCells(const QString &range, Format *format=0); + int mergeCells(const CellRange &range, const Format &format=Format()); + int mergeCells(const QString &range, const Format &format=Format()); int unmergeCells(const CellRange &range); int unmergeCells(const QString &range); - bool setRow(int row, double height, Format* format=0, bool hidden=false); - bool setColumn(int colFirst, int colLast, double width, Format* format=0, bool hidden=false); - bool setColumn(const QString &colFirst, const QString &colLast, double width, Format* format=0, bool hidden=false); + bool setRow(int row, double height, const Format &format=Format(), bool hidden=false); + bool setColumn(int colFirst, int colLast, double width, const Format &format=Format(), bool hidden=false); + bool setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format=Format(), bool hidden=false); bool groupRows(int rowFirst, int rowLast, bool collapsed = true); bool groupColumns(int colFirst, int colLast, bool collapsed = true); bool addDataValidation(const DataValidation &validation); diff --git a/src/xlsx/xlsxformat.cpp b/src/xlsx/xlsxformat.cpp index 652a3b2..e003e19 100755 --- a/src/xlsx/xlsxformat.cpp +++ b/src/xlsx/xlsxformat.cpp @@ -790,7 +790,19 @@ void Format::setLocked(bool locked) */ bool Format::isValid() const { - return !d->property.isEmpty(); + if (!d->property.isEmpty()) + return true; + if (d->xf_indexValid || d->font_index_valid || d->fill_index_valid || d->border_index_valid) + return true; + return false; +} + +/*! + Returns true if the format is empty; otherwise returns false. + */ +bool Format::isEmpty() const +{ + return d->property.isEmpty(); } QByteArray Format::formatKey() const @@ -823,7 +835,7 @@ int Format::xfIndex() const bool Format::xfIndexValid() const { - return !d->dirty && d->xf_indexValid; + return d->xf_indexValid; } void Format::setDxfIndex(int index) @@ -839,7 +851,7 @@ int Format::dxfIndex() const bool Format::dxfIndexValid() const { - return !d->dirty && d->dxf_indexValid; + return d->dxf_indexValid; } bool Format::operator ==(const Format &format) const diff --git a/src/xlsx/xlsxformat.h b/src/xlsx/xlsxformat.h index b621c16..a29ade0 100755 --- a/src/xlsx/xlsxformat.h +++ b/src/xlsx/xlsxformat.h @@ -214,6 +214,7 @@ public: void setHidden(bool hidden); bool isValid() const; + bool isEmpty() const; bool operator == (const Format &format) const; bool operator != (const Format &format) const; diff --git a/src/xlsx/xlsxstyles.cpp b/src/xlsx/xlsxstyles.cpp index df4da91..e9906f9 100755 --- a/src/xlsx/xlsxstyles.cpp +++ b/src/xlsx/xlsxstyles.cpp @@ -46,13 +46,14 @@ Styles::Styles(bool createEmpty) if (!createEmpty) { //Add default Format - addFormat(createFormat()); - //Add another fill format - Format *format = createFormat(); - format->setFillPattern(Format::PatternGray125); + Format defaultFmt; + addFormat(defaultFmt); - m_fillsList.append(format); - m_fillsHash.insert(format->fillKey(), format); + //Add another fill format + Format fillFmt; + fillFmt.setFillPattern(Format::PatternGray125); + m_fillsList.append(fillFmt); + m_fillsHash.insert(fillFmt.fillKey(), fillFmt); } } @@ -60,18 +61,10 @@ Styles::~Styles() { } -Format *Styles::createFormat() -{ - Format *format = new Format(); - m_createdFormatsList.append(QSharedPointer(format)); - - return format; -} - -Format *Styles::xfFormat(int idx) const +Format Styles::xfFormat(int idx) const { if (idx <0 || idx >= m_xf_formatsList.size()) - return 0; + return Format(); return m_xf_formatsList[idx]; } @@ -83,13 +76,10 @@ Format *Styles::xfFormat(int idx) const the same key have been in. This is useful when reading existing .xlsx files which may contains duplicated formats. */ -void Styles::addFormat(Format *format, bool force) +void Styles::addFormat(const Format &format, bool force) { - if (!format) - return; - //numFmt - if (format->hasProperty(FormatPrivate::P_NumFmt_FormatCode) && !format->hasProperty(FormatPrivate::P_NumFmt_Id)) { + if (format.hasProperty(FormatPrivate::P_NumFmt_FormatCode) && !format.hasProperty(FormatPrivate::P_NumFmt_Id)) { if (m_builtinNumFmtsHash.isEmpty()) { m_builtinNumFmtsHash.insert(QStringLiteral("General"), 0); m_builtinNumFmtsHash.insert(QStringLiteral("0"), 1); @@ -129,15 +119,15 @@ void Styles::addFormat(Format *format, bool force) m_builtinNumFmtsHash.insert(QStringLiteral("##0.0E+0"), 48); m_builtinNumFmtsHash.insert(QStringLiteral("@"), 49); } - const QString str = format->numberFormat(); + const QString str = format.numberFormat(); //Assign proper number format index if (m_builtinNumFmtsHash.contains(str)) { - format->setNumberFormat(m_builtinNumFmtsHash[str], str); + const_cast(&format)->setNumberFormat(m_builtinNumFmtsHash[str], str); } else if (m_customNumFmtsHash.contains(str)) { - format->setNumberFormat(m_customNumFmtsHash[str]->formatIndex, str); + const_cast(&format)->setNumberFormat(m_customNumFmtsHash[str]->formatIndex, str); } else { //Assign a new fmt Id. - format->setNumberFormat(m_nextCustomNumFmtId, str); + const_cast(&format)->setNumberFormat(m_nextCustomNumFmtId, str); QSharedPointer fmt(new XlsxFormatNumberData); fmt->formatIndex = m_nextCustomNumFmtId; @@ -150,57 +140,57 @@ void Styles::addFormat(Format *format, bool force) } //Font - if (!format->fontIndexValid()) { - if (!m_fontsHash.contains(format->fontKey())) { - format->setFontIndex(m_fontsList.size()); //Assign proper index + if (!format.fontIndexValid()) { + if (!m_fontsHash.contains(format.fontKey())) { + const_cast(&format)->setFontIndex(m_fontsList.size()); //Assign proper index m_fontsList.append(format); - m_fontsHash[format->fontKey()] = format; + m_fontsHash[format.fontKey()] = format; } else { - format->setFontIndex(m_fontsHash[format->fontKey()]->fontIndex()); + const_cast(&format)->setFontIndex(m_fontsHash[format.fontKey()].fontIndex()); } } //Fill - if (!format->fillIndexValid()) { - if (!m_fillsHash.contains(format->fillKey())) { - format->setFillIndex(m_fillsList.size()); //Assign proper index + if (!format.fillIndexValid()) { + if (!m_fillsHash.contains(format.fillKey())) { + const_cast(&format)->setFillIndex(m_fillsList.size()); //Assign proper index m_fillsList.append(format); - m_fillsHash[format->fillKey()] = format; + m_fillsHash[format.fillKey()] = format; } else { - format->setFillIndex(m_fillsHash[format->fillKey()]->fillIndex()); + const_cast(&format)->setFillIndex(m_fillsHash[format.fillKey()].fillIndex()); } } //Border - if (!format->borderIndexValid()) { - if (!m_bordersHash.contains(format->borderKey())) { - format->setBorderIndex(m_bordersList.size()); //Assign proper index + if (!format.borderIndexValid()) { + if (!m_bordersHash.contains(format.borderKey())) { + const_cast(&format)->setBorderIndex(m_bordersList.size()); //Assign proper index m_bordersList.append(format); - m_bordersHash[format->borderKey()] = format; + m_bordersHash[format.borderKey()] = format; } else { - format->setBorderIndex(m_bordersHash[format->borderKey()]->borderIndex()); + const_cast(&format)->setBorderIndex(m_bordersHash[format.borderKey()].borderIndex()); } } //Format - if (format->isDxfFormat()) { - if (!format->dxfIndexValid()) { - if (!m_dxf_formatsHash.contains(format->formatKey())) { - format->setDxfIndex(m_dxf_formatsList.size()); + if (format.isDxfFormat()) { + if (!format.dxfIndexValid()) { + if (!m_dxf_formatsHash.contains(format.formatKey())) { + const_cast(&format)->setDxfIndex(m_dxf_formatsList.size()); m_dxf_formatsList.append(format); - m_dxf_formatsHash[format->formatKey()] = format; + m_dxf_formatsHash[format.formatKey()] = format; } else { - format->setDxfIndex(m_dxf_formatsHash[format->formatKey()]->dxfIndex()); + const_cast(&format)->setDxfIndex(m_dxf_formatsHash[format.formatKey()].dxfIndex()); } } } else { - if (!format->xfIndexValid()) { - if (!m_xf_formatsHash.contains(format->formatKey()) || force) { - format->setXfIndex(m_xf_formatsList.size()); + if (!format.xfIndexValid()) { + if (!m_xf_formatsHash.contains(format.formatKey()) || force) { + const_cast(&format)->setXfIndex(m_xf_formatsList.size()); m_xf_formatsList.append(format); - m_xf_formatsHash[format->formatKey()] = format; + m_xf_formatsHash[format.formatKey()] = format; } else { - format->setXfIndex(m_xf_formatsHash[format->formatKey()]->xfIndex()); + const_cast(&format)->setXfIndex(m_xf_formatsHash[format.formatKey()].xfIndex()); } } } @@ -288,21 +278,21 @@ void Styles::writeFonts(XmlStreamWriter &writer) writer.writeStartElement(QStringLiteral("fonts")); writer.writeAttribute(QStringLiteral("count"), QString::number(m_fontsList.count())); for (int i=0; ifontBold()) + if (format.fontBold()) writer.writeEmptyElement(QStringLiteral("b")); - if (format->fontItalic()) + if (format.fontItalic()) writer.writeEmptyElement(QStringLiteral("i")); - if (format->fontStrikeOut()) + if (format.fontStrikeOut()) writer.writeEmptyElement(QStringLiteral("strike")); - if (format->fontOutline()) + if (format.fontOutline()) writer.writeEmptyElement(QStringLiteral("outline")); - if (format->boolProperty(FormatPrivate::P_Font_Shadow)) + if (format.boolProperty(FormatPrivate::P_Font_Shadow)) writer.writeEmptyElement(QStringLiteral("shadow")); - if (format->hasProperty(FormatPrivate::P_Font_Underline)) { - Format::FontUnderline u = format->fontUnderline(); + if (format.hasProperty(FormatPrivate::P_Font_Underline)) { + Format::FontUnderline u = format.fontUnderline(); if (u != Format::FontUnderlineNone) { writer.writeEmptyElement(QStringLiteral("u")); if (u== Format::FontUnderlineDouble) @@ -313,8 +303,8 @@ void Styles::writeFonts(XmlStreamWriter &writer) writer.writeAttribute(QStringLiteral("val"), QStringLiteral("doubleAccounting")); } } - if (format->hasProperty(FormatPrivate::P_Font_Script)) { - Format::FontScript s = format->fontScript(); + if (format.hasProperty(FormatPrivate::P_Font_Script)) { + Format::FontScript s = format.fontScript(); if (s != Format::FontScriptNormal) { writer.writeEmptyElement(QStringLiteral("vertAlign")); if (s == Format::FontScriptSuper) @@ -324,49 +314,49 @@ void Styles::writeFonts(XmlStreamWriter &writer) } } - if (format->hasProperty(FormatPrivate::P_Font_Size)) { + if (format.hasProperty(FormatPrivate::P_Font_Size)) { writer.writeEmptyElement(QStringLiteral("sz")); - writer.writeAttribute(QStringLiteral("val"), QString::number(format->fontSize())); + writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontSize())); } - if (format->fontColor().isValid()) { + if (format.fontColor().isValid()) { writer.writeEmptyElement(QStringLiteral("color")); - QString color = format->fontColor().name(); + QString color = format.fontColor().name(); writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.mid(1));//remove # - } else if (format->hasProperty(FormatPrivate::P_Font_ThemeColor)) { + } else if (format.hasProperty(FormatPrivate::P_Font_ThemeColor)) { writer.writeEmptyElement(QStringLiteral("color")); - QStringList themes = format->stringProperty(FormatPrivate::P_Font_ThemeColor).split(QLatin1Char(':')); + QStringList themes = format.stringProperty(FormatPrivate::P_Font_ThemeColor).split(QLatin1Char(':')); writer.writeAttribute(QStringLiteral("theme"), themes[0]); if (!themes[1].isEmpty()) writer.writeAttribute(QStringLiteral("tint"), themes[1]); } - if (!format->fontName().isEmpty()) { + if (!format.fontName().isEmpty()) { writer.writeEmptyElement(QStringLiteral("name")); - writer.writeAttribute(QStringLiteral("val"), format->fontName()); + writer.writeAttribute(QStringLiteral("val"), format.fontName()); } - if (format->hasProperty(FormatPrivate::P_Font_Family)) { + if (format.hasProperty(FormatPrivate::P_Font_Family)) { writer.writeEmptyElement(QStringLiteral("family")); - writer.writeAttribute(QStringLiteral("val"), QString::number(format->intProperty(FormatPrivate::P_Font_Family))); + writer.writeAttribute(QStringLiteral("val"), QString::number(format.intProperty(FormatPrivate::P_Font_Family))); } - if (format->hasProperty(FormatPrivate::P_Font_Scheme)) { + if (format.hasProperty(FormatPrivate::P_Font_Scheme)) { writer.writeEmptyElement(QStringLiteral("scheme")); - writer.writeAttribute(QStringLiteral("val"), format->stringProperty(FormatPrivate::P_Font_Scheme)); + writer.writeAttribute(QStringLiteral("val"), format.stringProperty(FormatPrivate::P_Font_Scheme)); } -// if (!format->isDxfFormat()) { +// if (!format.isDxfFormat()) { // writer.writeEmptyElement(QStringLiteral("sz")); -// writer.writeAttribute(QStringLiteral("val"), QString::number(format->fontSize())); +// writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontSize())); // } -// if (!format->isDxfFormat()) { +// if (!format.isDxfFormat()) { // writer.writeEmptyElement(QStringLiteral("name")); -// writer.writeAttribute(QStringLiteral("val"), format->fontName()); +// writer.writeAttribute(QStringLiteral("val"), format.fontName()); // writer.writeEmptyElement(QStringLiteral("family")); -// writer.writeAttribute(QStringLiteral("val"), QString::number(format->fontFamily())); -// if (format->fontName() == QLatin1String("Calibri")) { +// writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontFamily())); +// if (format.fontName() == QLatin1String("Calibri")) { // writer.writeEmptyElement(QStringLiteral("scheme")); -// writer.writeAttribute(QStringLiteral("val"), format->fontScheme()); +// writer.writeAttribute(QStringLiteral("val"), format.fontScheme()); // } // } writer.writeEndElement(); //font @@ -379,14 +369,13 @@ void Styles::writeFills(XmlStreamWriter &writer) writer.writeStartElement(QStringLiteral("fills")); writer.writeAttribute(QStringLiteral("count"), QString::number(m_fillsList.size())); - for (int i=0; i patternStrings; if (patternStrings.isEmpty()) { @@ -413,24 +402,24 @@ void Styles::writeFill(XmlStreamWriter &writer, Format *fill) writer.writeStartElement(QStringLiteral("fill")); writer.writeStartElement(QStringLiteral("patternFill")); - writer.writeAttribute(QStringLiteral("patternType"), patternStrings[fill->fillPattern()]); + writer.writeAttribute(QStringLiteral("patternType"), patternStrings[fill.fillPattern()]); // For a solid fill, Excel reverses the role of foreground and background colours - if (fill->patternForegroundColor().isValid()) { - writer.writeEmptyElement(fill->fillPattern() == Format::PatternSolid ? QStringLiteral("bgColor") : QStringLiteral("fgColor")); - writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill->patternForegroundColor().name().mid(1)); - } else if (!fill->stringProperty(FormatPrivate::P_Fill_FgThemeColor).isEmpty()) { + if (fill.patternForegroundColor().isValid()) { + writer.writeEmptyElement(fill.fillPattern() == Format::PatternSolid ? QStringLiteral("bgColor") : QStringLiteral("fgColor")); + writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill.patternForegroundColor().name().mid(1)); + } else if (!fill.stringProperty(FormatPrivate::P_Fill_FgThemeColor).isEmpty()) { writer.writeEmptyElement(QStringLiteral("fgColor")); - QStringList themes = fill->stringProperty(FormatPrivate::P_Fill_FgThemeColor).split(QLatin1Char(':')); + QStringList themes = fill.stringProperty(FormatPrivate::P_Fill_FgThemeColor).split(QLatin1Char(':')); writer.writeAttribute(QStringLiteral("theme"), themes[0]); if (!themes[1].isEmpty()) writer.writeAttribute(QStringLiteral("tint"), themes[1]); } - if (fill->patternBackgroundColor().isValid()) { - writer.writeEmptyElement(fill->fillPattern() == Format::PatternSolid ? QStringLiteral("fgColor") : QStringLiteral("bgColor")); - writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill->patternBackgroundColor().name().mid(1)); - } else if (!fill->stringProperty(FormatPrivate::P_Fill_BgThemeColor).isEmpty()) { + if (fill.patternBackgroundColor().isValid()) { + writer.writeEmptyElement(fill.fillPattern() == Format::PatternSolid ? QStringLiteral("fgColor") : QStringLiteral("bgColor")); + writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill.patternBackgroundColor().name().mid(1)); + } else if (!fill.stringProperty(FormatPrivate::P_Fill_BgThemeColor).isEmpty()) { writer.writeEmptyElement(QStringLiteral("bgColor")); - QStringList themes = fill->stringProperty(FormatPrivate::P_Fill_BgThemeColor).split(QLatin1Char(':')); + QStringList themes = fill.stringProperty(FormatPrivate::P_Fill_BgThemeColor).split(QLatin1Char(':')); writer.writeAttribute(QStringLiteral("theme"), themes[0]); if (!themes[1].isEmpty()) writer.writeAttribute(QStringLiteral("tint"), themes[1]); @@ -445,11 +434,11 @@ void Styles::writeBorders(XmlStreamWriter &writer) writer.writeStartElement(QStringLiteral("borders")); writer.writeAttribute(QStringLiteral("count"), QString::number(m_bordersList.count())); for (int i=0; ihasProperty(FormatPrivate::P_Border_DiagonalType)) { - Format::DiagonalBorderType t = border->diagonalBorderType(); + if (border.hasProperty(FormatPrivate::P_Border_DiagonalType)) { + Format::DiagonalBorderType t = border.diagonalBorderType(); if (t == Format::DiagonalBorderUp) { writer.writeAttribute(QStringLiteral("diagonalUp"), QStringLiteral("1")); } else if (t == Format::DiagonalBorderDown) { @@ -459,18 +448,18 @@ void Styles::writeBorders(XmlStreamWriter &writer) writer.writeAttribute(QStringLiteral("diagonalDown"), QStringLiteral("1")); } } - if (border->hasProperty(FormatPrivate::P_Border_LeftStyle)) - writeSubBorder(writer, QStringLiteral("left"), border->leftBorderStyle(), border->leftBorderColor(), border->stringProperty(FormatPrivate::P_Border_ThemeLeftColor)); - if (border->hasProperty(FormatPrivate::P_Border_RightStyle)) - writeSubBorder(writer, QStringLiteral("right"), border->rightBorderStyle(), border->rightBorderColor(), border->stringProperty(FormatPrivate::P_Border_ThemeRightColor)); - if (border->hasProperty(FormatPrivate::P_Border_TopStyle)) - writeSubBorder(writer, QStringLiteral("top"), border->topBorderStyle(), border->topBorderColor(), border->stringProperty(FormatPrivate::P_Border_ThemeTopColor)); - if (border->hasProperty(FormatPrivate::P_Border_BottomStyle)) - writeSubBorder(writer, QStringLiteral("bottom"), border->bottomBorderStyle(), border->bottomBorderColor(), border->stringProperty(FormatPrivate::P_Border_ThemeBottomColor)); - -// if (!format->isDxfFormat()) { - if (border->hasProperty(FormatPrivate::P_Border_DiagonalStyle)) - writeSubBorder(writer, QStringLiteral("diagonal"), border->diagonalBorderStyle(), border->diagonalBorderColor(), border->stringProperty(FormatPrivate::P_Border_ThemeDiagonalColor)); + if (border.hasProperty(FormatPrivate::P_Border_LeftStyle)) + writeSubBorder(writer, QStringLiteral("left"), border.leftBorderStyle(), border.leftBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeLeftColor)); + if (border.hasProperty(FormatPrivate::P_Border_RightStyle)) + writeSubBorder(writer, QStringLiteral("right"), border.rightBorderStyle(), border.rightBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeRightColor)); + if (border.hasProperty(FormatPrivate::P_Border_TopStyle)) + writeSubBorder(writer, QStringLiteral("top"), border.topBorderStyle(), border.topBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeTopColor)); + if (border.hasProperty(FormatPrivate::P_Border_BottomStyle)) + writeSubBorder(writer, QStringLiteral("bottom"), border.bottomBorderStyle(), border.bottomBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeBottomColor)); + +// if (!format.isDxfFormat()) { + if (border.hasProperty(FormatPrivate::P_Border_DiagonalStyle)) + writeSubBorder(writer, QStringLiteral("diagonal"), border.diagonalBorderStyle(), border.diagonalBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeDiagonalColor)); // } writer.writeEndElement();//border } @@ -522,11 +511,11 @@ void Styles::writeCellXfs(XmlStreamWriter &writer) { writer.writeStartElement(QStringLiteral("cellXfs")); writer.writeAttribute(QStringLiteral("count"), QString::number(m_xf_formatsList.size())); - foreach (Format *format, m_xf_formatsList) { - int num_fmt_id = format->numberFormatIndex(); - int font_id = format->fontIndex(); - int fill_id = format->fillIndex(); - int border_id = format->borderIndex(); + foreach (const Format &format, m_xf_formatsList) { + int num_fmt_id = format.numberFormatIndex(); + int font_id = format.fontIndex(); + int fill_id = format.fillIndex(); + int border_id = format.borderIndex(); int xf_id = 0; writer.writeStartElement(QStringLiteral("xf")); writer.writeAttribute(QStringLiteral("numFmtId"), QString::number(num_fmt_id)); @@ -534,21 +523,21 @@ void Styles::writeCellXfs(XmlStreamWriter &writer) writer.writeAttribute(QStringLiteral("fillId"), QString::number(fill_id)); writer.writeAttribute(QStringLiteral("borderId"), QString::number(border_id)); writer.writeAttribute(QStringLiteral("xfId"), QString::number(xf_id)); - if (format->numberFormatIndex() > 0) + if (format.numberFormatIndex() > 0) writer.writeAttribute(QStringLiteral("applyNumberFormat"), QStringLiteral("1")); - if (format->fontIndex() > 0) + if (format.fontIndex() > 0) writer.writeAttribute(QStringLiteral("applyFont"), QStringLiteral("1")); - if (format->borderIndex() > 0) + if (format.borderIndex() > 0) writer.writeAttribute(QStringLiteral("applyBorder"), QStringLiteral("1")); - if (format->fillIndex() > 0) + if (format.fillIndex() > 0) writer.writeAttribute(QStringLiteral("applyFill"), QStringLiteral("1")); - if (format->hasAlignmentData()) + if (format.hasAlignmentData()) writer.writeAttribute(QStringLiteral("applyAlignment"), QStringLiteral("1")); - if (format->hasAlignmentData()) { + if (format.hasAlignmentData()) { writer.writeEmptyElement(QStringLiteral("alignment")); - if (format->hasProperty(FormatPrivate::P_Alignment_AlignH)) { - switch (format->horizontalAlignment()) { + if (format.hasProperty(FormatPrivate::P_Alignment_AlignH)) { + switch (format.horizontalAlignment()) { case Format::AlignLeft: writer.writeAttribute(QStringLiteral("horizontal"), QStringLiteral("left")); break; @@ -575,8 +564,8 @@ void Styles::writeCellXfs(XmlStreamWriter &writer) } } - if (format->hasProperty(FormatPrivate::P_Alignment_AlignV)) { - switch (format->verticalAlignment()) { + if (format.hasProperty(FormatPrivate::P_Alignment_AlignV)) { + switch (format.verticalAlignment()) { case Format::AlignTop: writer.writeAttribute(QStringLiteral("vertical"), QStringLiteral("top")); break; @@ -593,14 +582,14 @@ void Styles::writeCellXfs(XmlStreamWriter &writer) break; } } - if (format->hasProperty(FormatPrivate::P_Alignment_Indent)) - writer.writeAttribute(QStringLiteral("indent"), QString::number(format->indent())); - if (format->hasProperty(FormatPrivate::P_Alignment_Wrap) && format->textWrap()) + if (format.hasProperty(FormatPrivate::P_Alignment_Indent)) + writer.writeAttribute(QStringLiteral("indent"), QString::number(format.indent())); + if (format.hasProperty(FormatPrivate::P_Alignment_Wrap) && format.textWrap()) writer.writeAttribute(QStringLiteral("wrapText"), QStringLiteral("1")); - if (format->hasProperty(FormatPrivate::P_Alignment_ShinkToFit) && format->shrinkToFit()) + if (format.hasProperty(FormatPrivate::P_Alignment_ShinkToFit) && format.shrinkToFit()) writer.writeAttribute(QStringLiteral("shrinkToFit"), QStringLiteral("1")); - if (format->hasProperty(FormatPrivate::P_Alignment_Rotation)) - writer.writeAttribute(QStringLiteral("textRotation"), QString::number(format->rotation())); + if (format.hasProperty(FormatPrivate::P_Alignment_Rotation)) + writer.writeAttribute(QStringLiteral("textRotation"), QString::number(format.rotation())); } writer.writeEndElement();//xf @@ -612,7 +601,7 @@ void Styles::writeDxfs(XmlStreamWriter &writer) { writer.writeStartElement(QStringLiteral("dxfs")); writer.writeAttribute(QStringLiteral("count"), QString::number(m_dxf_formatsList.size())); - foreach (Format *format, m_dxf_formatsList) { + foreach (const Format &format, m_dxf_formatsList) { writer.writeStartElement(QStringLiteral("dxf")); Q_UNUSED(format) writer.writeEndElement();//dxf @@ -653,63 +642,63 @@ bool Styles::readFonts(XmlStreamReader &reader) reader.readNextStartElement(); if (reader.name() != QLatin1String("font")) return false; - Format *format = createFormat(); + Format format; while((reader.readNextStartElement(),true)) { //read until font endelement. if (reader.tokenType() == QXmlStreamReader::StartElement) { QXmlStreamAttributes attributes = reader.attributes(); if (reader.name() == QLatin1String("name")) { - format->setFontName(attributes.value(QLatin1String("val")).toString()); + format.setFontName(attributes.value(QLatin1String("val")).toString()); } else if (reader.name() == QLatin1String("charset")) { - format->setProperty(FormatPrivate::P_Font_Charset, attributes.value(QLatin1String("val")).toString().toInt()); + format.setProperty(FormatPrivate::P_Font_Charset, attributes.value(QLatin1String("val")).toString().toInt()); } else if (reader.name() == QLatin1String("family")) { - format->setProperty(FormatPrivate::P_Font_Family, attributes.value(QLatin1String("val")).toString().toInt()); + format.setProperty(FormatPrivate::P_Font_Family, attributes.value(QLatin1String("val")).toString().toInt()); } else if (reader.name() == QLatin1String("b")) { - format->setFontBold(true); + format.setFontBold(true); } else if (reader.name() == QLatin1String("i")) { - format->setFontItalic(true); + format.setFontItalic(true); } else if (reader.name() == QLatin1String("strike")) { - format->setFontStrikeOut(true); + format.setFontStrikeOut(true); } else if (reader.name() == QLatin1String("outline")) { - format->setFontOutline(true); + format.setFontOutline(true); } else if (reader.name() == QLatin1String("shadow")) { - format->setProperty(FormatPrivate::P_Font_Shadow, true); + format.setProperty(FormatPrivate::P_Font_Shadow, true); } else if (reader.name() == QLatin1String("condense")) { - format->setProperty(FormatPrivate::P_Font_Condense, attributes.value(QLatin1String("val")).toString().toInt()); + format.setProperty(FormatPrivate::P_Font_Condense, attributes.value(QLatin1String("val")).toString().toInt()); } else if (reader.name() == QLatin1String("extend")) { - format->setProperty(FormatPrivate::P_Font_Extend, attributes.value(QLatin1String("val")).toString().toInt()); + format.setProperty(FormatPrivate::P_Font_Extend, attributes.value(QLatin1String("val")).toString().toInt()); } else if (reader.name() == QLatin1String("color")) { if (attributes.hasAttribute(QLatin1String("rgb"))) { QString colorString = attributes.value(QLatin1String("rgb")).toString(); - format->setFontColor(fromARGBString(colorString)); + format.setFontColor(fromARGBString(colorString)); } else if (attributes.hasAttribute(QLatin1String("indexed"))) { QColor color = getColorByIndex(attributes.value(QLatin1String("indexed")).toString().toInt()); - format->setFontColor(color); + format.setFontColor(color); } else if (attributes.hasAttribute(QLatin1String("theme"))) { QString theme = attributes.value(QLatin1String("theme")).toString(); QString tint = attributes.value(QLatin1String("tint")).toString(); - format->setProperty(FormatPrivate::P_Font_ThemeColor, QString(theme + QLatin1Char(':') + tint)); + format.setProperty(FormatPrivate::P_Font_ThemeColor, QString(theme + QLatin1Char(':') + tint)); } } else if (reader.name() == QLatin1String("sz")) { int sz = attributes.value(QLatin1String("val")).toString().toInt(); - format->setFontSize(sz); + format.setFontSize(sz); } else if (reader.name() == QLatin1String("u")) { QString value = attributes.value(QLatin1String("val")).toString(); if (value == QLatin1String("double")) - format->setFontUnderline(Format::FontUnderlineDouble); + format.setFontUnderline(Format::FontUnderlineDouble); else if (value == QLatin1String("doubleAccounting")) - format->setFontUnderline(Format::FontUnderlineDoubleAccounting); + format.setFontUnderline(Format::FontUnderlineDoubleAccounting); else if (value == QLatin1String("singleAccounting")) - format->setFontUnderline(Format::FontUnderlineSingleAccounting); + format.setFontUnderline(Format::FontUnderlineSingleAccounting); else - format->setFontUnderline(Format::FontUnderlineSingle); + format.setFontUnderline(Format::FontUnderlineSingle); } else if (reader.name() == QLatin1String("vertAlign")) { QString value = attributes.value(QLatin1String("val")).toString(); if (value == QLatin1String("superscript")) - format->setFontScript(Format::FontScriptSuper); + format.setFontScript(Format::FontScriptSuper); else if (value == QLatin1String("subscript")) - format->setFontScript(Format::FontScriptSub); + format.setFontScript(Format::FontScriptSub); } else if (reader.name() == QLatin1String("scheme")) { - format->setProperty(FormatPrivate::P_Font_Scheme, attributes.value(QLatin1String("val")).toString()); + format.setProperty(FormatPrivate::P_Font_Scheme, attributes.value(QLatin1String("val")).toString()); } } @@ -717,8 +706,8 @@ bool Styles::readFonts(XmlStreamReader &reader) break; } m_fontsList.append(format); - m_fontsHash.insert(format->fontKey(), format); - format->setFontIndex(m_fontsList.size()-1); + m_fontsHash.insert(format.fontKey(), format); + format.setFontIndex(m_fontsList.size()-1); } return true; } @@ -765,14 +754,14 @@ bool Styles::readFill(XmlStreamReader &reader) patternValues[QStringLiteral("lightGrid")] = Format::PatternLightGrid; } - Format *fill = createFormat(); + Format fill; while((reader.readNextStartElement(), true)) { //read until fill endelement if (reader.tokenType() == QXmlStreamReader::StartElement) { if (reader.name() == QLatin1String("patternFill")) { QXmlStreamAttributes attributes = reader.attributes(); if (attributes.hasAttribute(QLatin1String("patternType"))) { QString pattern = attributes.value(QLatin1String("patternType")).toString(); - fill->setFillPattern(patternValues.contains(pattern) ? patternValues[pattern] : Format::PatternNone); + fill.setFillPattern(patternValues.contains(pattern) ? patternValues[pattern] : Format::PatternNone); } } else if (reader.name() == QLatin1String("fgColor")) { QXmlStreamAttributes attributes = reader.attributes(); @@ -784,12 +773,12 @@ bool Styles::readFill(XmlStreamReader &reader) } else if (attributes.hasAttribute(QLatin1String("theme"))) { QString theme = attributes.value(QLatin1String("theme")).toString(); QString tint = attributes.value(QLatin1String("tint")).toString(); - fill->setProperty(FormatPrivate::P_Fill_FgThemeColor, QString(theme + QLatin1Char(':') + tint)); + fill.setProperty(FormatPrivate::P_Fill_FgThemeColor, QString(theme + QLatin1Char(':') + tint)); } - if (fill->fillPattern() == Format::PatternSolid) - fill->setPatternBackgroundColor(c); + if (fill.fillPattern() == Format::PatternSolid) + fill.setPatternBackgroundColor(c); else - fill->setPatternForegroundColor(c); + fill.setPatternForegroundColor(c); } else if (reader.name() == QLatin1String("bgColor")) { QXmlStreamAttributes attributes = reader.attributes(); QColor c; @@ -800,12 +789,12 @@ bool Styles::readFill(XmlStreamReader &reader) } else if (attributes.hasAttribute(QLatin1String("theme"))) { QString theme = attributes.value(QLatin1String("theme")).toString(); QString tint = attributes.value(QLatin1String("tint")).toString(); - fill->setProperty(FormatPrivate::P_Fill_BgThemeColor, QString(theme + QLatin1Char(':') + tint)); + fill.setProperty(FormatPrivate::P_Fill_BgThemeColor, QString(theme + QLatin1Char(':') + tint)); } - if (fill->fillPattern() == Format::PatternSolid) - fill->setPatternForegroundColor(c); + if (fill.fillPattern() == Format::PatternSolid) + fill.setPatternForegroundColor(c); else - fill->setPatternBackgroundColor(c); + fill.setPatternBackgroundColor(c); } } @@ -814,8 +803,8 @@ bool Styles::readFill(XmlStreamReader &reader) } m_fillsList.append(fill); - m_fillsHash.insert(fill->fillKey(), fill); - fill->setFillIndex(m_fillsList.size()-1);//first call key(), then setIndex() + m_fillsHash.insert(fill.fillKey(), fill); + fill.setFillIndex(m_fillsList.size()-1);//first call key(), then setIndex() return true; } @@ -838,17 +827,17 @@ bool Styles::readBorders(XmlStreamReader &reader) bool Styles::readBorder(XmlStreamReader &reader) { Q_ASSERT(reader.name() == QLatin1String("border")); - Format *border = createFormat(); + Format border; QXmlStreamAttributes attributes = reader.attributes(); bool isUp = attributes.hasAttribute(QLatin1String("diagonalUp")); bool isDown = attributes.hasAttribute(QLatin1String("diagonalUp")); if (isUp && isDown) - border->setDiagonalBorderType(Format::DiagnoalBorderBoth); + border.setDiagonalBorderType(Format::DiagnoalBorderBoth); else if (isUp) - border->setDiagonalBorderType(Format::DiagonalBorderUp); + border.setDiagonalBorderType(Format::DiagonalBorderUp); else if (isDown) - border->setDiagonalBorderType(Format::DiagonalBorderDown); + border.setDiagonalBorderType(Format::DiagonalBorderDown); while((reader.readNextStartElement(), true)) { //read until border endelement if (reader.tokenType() == QXmlStreamReader::StartElement) { @@ -861,35 +850,35 @@ bool Styles::readBorder(XmlStreamReader &reader) readSubBorder(reader, reader.name().toString(), style, color, themeColor); if (reader.name() == QLatin1String("left")) { - border->setLeftBorderStyle(style); + border.setLeftBorderStyle(style); if (color.isValid()) - border->setLeftBorderColor(color); + border.setLeftBorderColor(color); else if (!themeColor.isEmpty()) - border->setProperty(FormatPrivate::P_Border_ThemeLeftColor, themeColor); + border.setProperty(FormatPrivate::P_Border_ThemeLeftColor, themeColor); } else if (reader.name() == QLatin1String("right")) { - border->setRightBorderStyle(style); + border.setRightBorderStyle(style); if (color.isValid()) - border->setRightBorderColor(color); + border.setRightBorderColor(color); else if (!themeColor.isEmpty()) - border->setProperty(FormatPrivate::P_Border_ThemeRightColor, themeColor); + border.setProperty(FormatPrivate::P_Border_ThemeRightColor, themeColor); } else if (reader.name() == QLatin1String("top")) { - border->setTopBorderStyle(style); + border.setTopBorderStyle(style); if (color.isValid()) - border->setTopBorderColor(color); + border.setTopBorderColor(color); else if (!themeColor.isEmpty()) - border->setProperty(FormatPrivate::P_Border_ThemeTopColor, themeColor); + border.setProperty(FormatPrivate::P_Border_ThemeTopColor, themeColor); } else if (reader.name() == QLatin1String("bottom")) { - border->setBottomBorderStyle(style); + border.setBottomBorderStyle(style); if (color.isValid()) - border->setBottomBorderColor(color); + border.setBottomBorderColor(color); else if (!themeColor.isEmpty()) - border->setProperty(FormatPrivate::P_Border_ThemeBottomColor, themeColor); + border.setProperty(FormatPrivate::P_Border_ThemeBottomColor, themeColor); } else if (reader.name() == QLatin1String("diagonal")) { - border->setDiagonalBorderStyle(style); + border.setDiagonalBorderStyle(style); if (color.isValid()) - border->setDiagonalBorderColor(color); + border.setDiagonalBorderColor(color); else if (!themeColor.isEmpty()) - border->setProperty(FormatPrivate::P_Border_ThemeDiagonalColor, themeColor); + border.setProperty(FormatPrivate::P_Border_ThemeDiagonalColor, themeColor); } } } @@ -899,8 +888,8 @@ bool Styles::readBorder(XmlStreamReader &reader) } m_bordersList.append(border); - m_bordersHash.insert(border->borderKey(), border); - border->setBorderIndex(m_bordersList.size()-1);//first call key(), then setIndex() + m_bordersHash.insert(border.borderKey(), border); + border.setBorderIndex(m_bordersList.size()-1);//first call key(), then setIndex() return true; } @@ -970,7 +959,7 @@ bool Styles::readCellXfs(XmlStreamReader &reader) reader.readNextStartElement(); if (reader.name() != QLatin1String("xf")) return false; - Format *format = createFormat(); + Format format; QXmlStreamAttributes xfAttrs = reader.attributes(); // qDebug()<setNumberFormatIndex(numFmtIndex); + format.setNumberFormatIndex(numFmtIndex); else - format->setNumberFormat(numFmtIndex, m_customNumFmtIdMap[numFmtIndex]->formatString); + format.setNumberFormat(numFmtIndex, m_customNumFmtIdMap[numFmtIndex]->formatString); } if (xfAttrs.hasAttribute(QLatin1String("applyFont"))) { @@ -990,10 +979,10 @@ bool Styles::readCellXfs(XmlStreamReader &reader) if (fontIndex >= m_fontsList.size()) { qDebug("Error read styles.xml, cellXfs fontId"); } else { - Format *fontFormat = m_fontsList[fontIndex]; + Format fontFormat = m_fontsList[fontIndex]; for (int i=FormatPrivate::P_Font_STARTID; ihasProperty(i)) - format->setProperty(i, fontFormat->property(i)); + if (fontFormat.hasProperty(i)) + format.setProperty(i, fontFormat.property(i)); } } } @@ -1003,10 +992,10 @@ bool Styles::readCellXfs(XmlStreamReader &reader) if (id >= m_fillsList.size()) { qDebug("Error read styles.xml, cellXfs fillId"); } else { - Format *fillFormat = m_fillsList[id]; + Format fillFormat = m_fillsList[id]; for (int i=FormatPrivate::P_Fill_STARTID; ihasProperty(i)) - format->setProperty(i, fillFormat->property(i)); + if (fillFormat.hasProperty(i)) + format.setProperty(i, fillFormat.property(i)); } } } @@ -1016,10 +1005,10 @@ bool Styles::readCellXfs(XmlStreamReader &reader) if (id >= m_bordersList.size()) { qDebug("Error read styles.xml, cellXfs borderId"); } else { - Format *borderFormat = m_bordersList[id]; + Format borderFormat = m_bordersList[id]; for (int i=FormatPrivate::P_Border_STARTID; ihasProperty(i)) - format->setProperty(i, borderFormat->property(i)); + if (borderFormat.hasProperty(i)) + format.setProperty(i, borderFormat.property(i)); } } } @@ -1041,7 +1030,7 @@ bool Styles::readCellXfs(XmlStreamReader &reader) } QString str = alignAttrs.value(QLatin1String("horizontal")).toString(); if (alignStringMap.contains(str)) - format->setHorizontalAlignment(alignStringMap[str]); + format.setHorizontalAlignment(alignStringMap[str]); } if (alignAttrs.hasAttribute(QLatin1String("vertical"))) { @@ -1054,24 +1043,24 @@ bool Styles::readCellXfs(XmlStreamReader &reader) } QString str = alignAttrs.value(QLatin1String("vertical")).toString(); if (alignStringMap.contains(str)) - format->setVerticalAlignment(alignStringMap[str]); + format.setVerticalAlignment(alignStringMap[str]); } if (alignAttrs.hasAttribute(QLatin1String("indent"))) { int indent = alignAttrs.value(QLatin1String("indent")).toString().toInt(); - format->setIndent(indent); + format.setIndent(indent); } if (alignAttrs.hasAttribute(QLatin1String("textRotation"))) { int rotation = alignAttrs.value(QLatin1String("textRotation")).toString().toInt(); - format->setRotation(rotation); + format.setRotation(rotation); } if (alignAttrs.hasAttribute(QLatin1String("wrapText"))) - format->setTextWarp(true); + format.setTextWarp(true); if (alignAttrs.hasAttribute(QLatin1String("shrinkToFit"))) - format->setShrinkToFit(true); + format.setShrinkToFit(true); } } diff --git a/src/xlsx/xlsxstyles_p.h b/src/xlsx/xlsxstyles_p.h index e4c9ce4..4f57557 100755 --- a/src/xlsx/xlsxstyles_p.h +++ b/src/xlsx/xlsxstyles_p.h @@ -56,9 +56,8 @@ class XLSX_AUTOTEST_EXPORT Styles public: Styles(bool createEmpty=false); ~Styles(); - Format *createFormat(); - void addFormat(Format *format, bool force=false); - Format *xfFormat(int idx) const; + void addFormat(const Format &format, bool force=false); + Format xfFormat(int idx) const; QByteArray saveToXmlData(); void saveToXmlFile(QIODevice *device); @@ -72,7 +71,7 @@ private: void writeNumFmts(XmlStreamWriter &writer); void writeFonts(XmlStreamWriter &writer); void writeFills(XmlStreamWriter &writer); - void writeFill(XmlStreamWriter &writer, Format *fill); + void writeFill(XmlStreamWriter &writer, const Format &fill); void writeBorders(XmlStreamWriter &writer); void writeSubBorder(XmlStreamWriter &writer, const QString &type, int style, const QColor &color, const QString &themeColor); void writeCellXfs(XmlStreamWriter &writer); @@ -95,22 +94,20 @@ private: QMap > m_customNumFmtIdMap; QHash > m_customNumFmtsHash; int m_nextCustomNumFmtId; - QList m_fontsList; - QList m_fillsList; - QList m_bordersList; - QHash m_fontsHash; - QHash m_fillsHash; - QHash m_bordersHash; + QList m_fontsList; + QList m_fillsList; + QList m_bordersList; + QHash m_fontsHash; + QHash m_fillsHash; + QHash m_bordersHash; QVector m_indexedColors; - QList > m_createdFormatsList; //All created formats + QList m_xf_formatsList; + QHash m_xf_formatsHash; - QList m_xf_formatsList; - QHash m_xf_formatsHash; - - QList m_dxf_formatsList; - QHash m_dxf_formatsHash; + QList m_dxf_formatsList; + QHash m_dxf_formatsHash; }; } diff --git a/src/xlsx/xlsxworkbook.cpp b/src/xlsx/xlsxworkbook.cpp index e889da4..0e542ec 100755 --- a/src/xlsx/xlsxworkbook.cpp +++ b/src/xlsx/xlsxworkbook.cpp @@ -225,12 +225,6 @@ void Workbook::setActiveWorksheet(int index) d->activesheet = index; } -Format *Workbook::createFormat() -{ - Q_D(Workbook); - return d->styles->createFormat(); -} - QList > Workbook::worksheets() const { Q_D(const Workbook); diff --git a/src/xlsx/xlsxworkbook.h b/src/xlsx/xlsxworkbook.h index 97a437e..64932b7 100755 --- a/src/xlsx/xlsxworkbook.h +++ b/src/xlsx/xlsxworkbook.h @@ -35,7 +35,6 @@ class QIODevice; QT_BEGIN_NAMESPACE_XLSX class Worksheet; -class Format; class SharedStrings; class Styles; class Package; @@ -57,7 +56,6 @@ public: int activeWorksheet() const; void setActiveWorksheet(int index); - Format *createFormat(); // void addChart(); bool defineName(const QString &name, const QString &formula, const QString &comment=QString(), const QString &scope=QString()); bool isDate1904() const; diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index 9cd4c72..7fcc214 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/src/xlsx/xlsxworksheet.cpp @@ -430,7 +430,7 @@ QList > Worksheet::drawingLinks() const * Write \a value to cell (\a row, \a column) with the \a format. * Both \a row and \a column are all 1-indexed value. */ -int Worksheet::write(int row, int column, const QVariant &value, Format *format) +int Worksheet::write(int row, int column, const QVariant &value, const Format &format) { Q_D(Worksheet); bool ok; @@ -477,7 +477,7 @@ int Worksheet::write(int row, int column, const QVariant &value, Format *format) /*! \overload */ -int Worksheet::write(const QString &row_column, const QVariant &value, Format *format) +int Worksheet::write(const QString &row_column, const QVariant &value, const Format &format) { //convert the "A1" notation to row/column notation QPoint pos = xl_cell_to_rowcol(row_column); @@ -533,19 +533,19 @@ Cell *Worksheet::cellAt(int row, int column) const return d->cellTable[row][column].data(); } -Format *WorksheetPrivate::cellFormat(int row, int col) const +Format WorksheetPrivate::cellFormat(int row, int col) const { if (!cellTable.contains(row)) - return 0; + return Format(); if (!cellTable[row].contains(col)) - return 0; + return Format(); return cellTable[row][col]->format(); } /*! \overload */ -int Worksheet::writeString(const QString &row_column, const QString &value, Format *format) +int Worksheet::writeString(const QString &row_column, const QString &value, const Format &format) { //convert the "A1" notation to row/column notation QPoint pos = xl_cell_to_rowcol(row_column); @@ -558,7 +558,7 @@ int Worksheet::writeString(const QString &row_column, const QString &value, Form /*! Write string \a value to the cell (\a row, \a column) with the \a format */ -int Worksheet::writeString(int row, int column, const QString &value, Format *format) +int Worksheet::writeString(int row, int column, const QString &value, const Format &format) { Q_D(Worksheet); int error = 0; @@ -572,16 +572,16 @@ int Worksheet::writeString(int row, int column, const QString &value, Format *fo } d->sharedStrings()->addSharedString(content); - format = format ? format : d->cellFormat(row, column); - d->cellTable[row][column] = QSharedPointer(new Cell(content, Cell::String, format, this)); - d->workbook->styles()->addFormat(format); + Format fmt = format.isValid() ? format : d->cellFormat(row, column); + d->workbook->styles()->addFormat(fmt); + d->cellTable[row][column] = QSharedPointer(new Cell(content, Cell::String, fmt, this)); return error; } /*! \overload */ -int Worksheet::writeInlineString(const QString &row_column, const QString &value, Format *format) +int Worksheet::writeInlineString(const QString &row_column, const QString &value, const Format &format) { //convert the "A1" notation to row/column notation QPoint pos = xl_cell_to_rowcol(row_column); @@ -594,7 +594,7 @@ int Worksheet::writeInlineString(const QString &row_column, const QString &value /*! Write string \a value to the cell (\a row, \a column) with the \a format */ -int Worksheet::writeInlineString(int row, int column, const QString &value, Format *format) +int Worksheet::writeInlineString(int row, int column, const QString &value, const Format &format) { Q_D(Worksheet); int error = 0; @@ -607,16 +607,16 @@ int Worksheet::writeInlineString(int row, int column, const QString &value, Form error = -2; } - format = format ? format : d->cellFormat(row, column); - d->cellTable[row][column] = QSharedPointer(new Cell(value, Cell::InlineString, format, this)); - d->workbook->styles()->addFormat(format); + Format fmt = format.isValid() ? format : d->cellFormat(row, column); + d->workbook->styles()->addFormat(fmt); + d->cellTable[row][column] = QSharedPointer(new Cell(value, Cell::InlineString, fmt, this)); return error; } /*! \overload */ -int Worksheet::writeNumeric(const QString &row_column, double value, Format *format) +int Worksheet::writeNumeric(const QString &row_column, double value, const Format &format) { //convert the "A1" notation to row/column notation QPoint pos = xl_cell_to_rowcol(row_column); @@ -629,22 +629,22 @@ int Worksheet::writeNumeric(const QString &row_column, double value, Format *for /*! Write numeric \a value to the cell (\a row, \a column) with the \a format */ -int Worksheet::writeNumeric(int row, int column, double value, Format *format) +int Worksheet::writeNumeric(int row, int column, double value, const Format &format) { Q_D(Worksheet); if (d->checkDimensions(row, column)) return -1; - format = format ? format : d->cellFormat(row, column); - d->cellTable[row][column] = QSharedPointer(new Cell(value, Cell::Numeric, format, this)); - d->workbook->styles()->addFormat(format); + Format fmt = format.isValid() ? format : d->cellFormat(row, column); + d->workbook->styles()->addFormat(fmt); + d->cellTable[row][column] = QSharedPointer(new Cell(value, Cell::Numeric, fmt, this)); return 0; } /*! \overload */ -int Worksheet::writeFormula(const QString &row_column, const QString &formula, Format *format, double result) +int Worksheet::writeFormula(const QString &row_column, const QString &formula, const Format &format, double result) { //convert the "A1" notation to row/column notation QPoint pos = xl_cell_to_rowcol(row_column); @@ -657,7 +657,7 @@ int Worksheet::writeFormula(const QString &row_column, const QString &formula, F /*! Write \a formula to the cell (\a row, \a column) with the \a format */ -int Worksheet::writeFormula(int row, int column, const QString &formula, Format *format, double result) +int Worksheet::writeFormula(int row, int column, const QString &formula, const Format &format, double result) { Q_D(Worksheet); int error = 0; @@ -669,11 +669,11 @@ int Worksheet::writeFormula(int row, int column, const QString &formula, Format if (_formula.startsWith(QLatin1String("="))) _formula.remove(0,1); - format = format ? format : d->cellFormat(row, column); - Cell *data = new Cell(result, Cell::Formula, format, this); + Format fmt = format.isValid() ? format : d->cellFormat(row, column); + d->workbook->styles()->addFormat(fmt); + Cell *data = new Cell(result, Cell::Formula, fmt, this); data->d_ptr->formula = _formula; d->cellTable[row][column] = QSharedPointer(data); - d->workbook->styles()->addFormat(format); return error; } @@ -681,7 +681,7 @@ int Worksheet::writeFormula(int row, int column, const QString &formula, Format /*! Write \a formula to the \a range with the \a format */ -int Worksheet::writeArrayFormula(const CellRange &range, const QString &formula, Format *format) +int Worksheet::writeArrayFormula(const CellRange &range, const QString &formula, const Format &format) { Q_D(Worksheet); int error = 0; @@ -699,7 +699,8 @@ int Worksheet::writeArrayFormula(const CellRange &range, const QString &formula, for (int row=range.firstRow(); row<=range.lastRow(); ++row) { for (int column=range.firstColumn(); column<=range.lastColumn(); ++column) { - Format *_format = format ? format : d->cellFormat(row, column); + Format _format = format.isValid() ? format : d->cellFormat(row, column); + d->workbook->styles()->addFormat(_format); if (row == range.firstRow() && column == range.firstColumn()) { QSharedPointer data(new Cell(0, Cell::ArrayFormula, _format, this)); data->d_ptr->formula = _formula; @@ -708,7 +709,6 @@ int Worksheet::writeArrayFormula(const CellRange &range, const QString &formula, } else { d->cellTable[row][column] = QSharedPointer(new Cell(0, Cell::Numeric, _format, this)); } - d->workbook->styles()->addFormat(_format); } } @@ -718,7 +718,7 @@ int Worksheet::writeArrayFormula(const CellRange &range, const QString &formula, /*! \overload */ -int Worksheet::writeArrayFormula(const QString &range, const QString &formula, Format *format) +int Worksheet::writeArrayFormula(const QString &range, const QString &formula, const Format &format) { return writeArrayFormula(CellRange(range), formula, format); } @@ -726,7 +726,7 @@ int Worksheet::writeArrayFormula(const QString &range, const QString &formula, F /*! \overload */ -int Worksheet::writeBlank(const QString &row_column, Format *format) +int Worksheet::writeBlank(const QString &row_column, const Format &format) { //convert the "A1" notation to row/column notation QPoint pos = xl_cell_to_rowcol(row_column); @@ -739,22 +739,23 @@ int Worksheet::writeBlank(const QString &row_column, Format *format) /*! Write a empty cell (\a row, \a column) with the \a format */ -int Worksheet::writeBlank(int row, int column, Format *format) +int Worksheet::writeBlank(int row, int column, const Format &format) { Q_D(Worksheet); if (d->checkDimensions(row, column)) return -1; - format = format ? format : d->cellFormat(row, column); - d->cellTable[row][column] = QSharedPointer(new Cell(QVariant(), Cell::Blank, format, this)); - d->workbook->styles()->addFormat(format); + Format fmt = format.isValid() ? format : d->cellFormat(row, column); + d->workbook->styles()->addFormat(fmt); + + d->cellTable[row][column] = QSharedPointer(new Cell(QVariant(), Cell::Blank, fmt, this)); return 0; } /*! \overload */ -int Worksheet::writeBool(const QString &row_column, bool value, Format *format) +int Worksheet::writeBool(const QString &row_column, bool value, const Format &format) { //convert the "A1" notation to row/column notation QPoint pos = xl_cell_to_rowcol(row_column); @@ -767,22 +768,22 @@ int Worksheet::writeBool(const QString &row_column, bool value, Format *format) /*! Write a bool \a value to the cell (\a row, \a column) with the \a format */ -int Worksheet::writeBool(int row, int column, bool value, Format *format) +int Worksheet::writeBool(int row, int column, bool value, const Format &format) { Q_D(Worksheet); if (d->checkDimensions(row, column)) return -1; - format = format ? format : d->cellFormat(row, column); - d->cellTable[row][column] = QSharedPointer(new Cell(value, Cell::Boolean, format, this)); - d->workbook->styles()->addFormat(format); + Format fmt = format.isValid() ? format : d->cellFormat(row, column); + d->workbook->styles()->addFormat(fmt); + d->cellTable[row][column] = QSharedPointer(new Cell(value, Cell::Boolean, fmt, this)); return 0; } /*! \overload */ -int Worksheet::writeDateTime(const QString &row_column, const QDateTime &dt, Format *format) +int Worksheet::writeDateTime(const QString &row_column, const QDateTime &dt, const Format &format) { //convert the "A1" notation to row/column notation QPoint pos = xl_cell_to_rowcol(row_column); @@ -795,21 +796,20 @@ int Worksheet::writeDateTime(const QString &row_column, const QDateTime &dt, For /*! Write a QDateTime \a value to the cell (\a row, \a column) with the \a format */ -int Worksheet::writeDateTime(int row, int column, const QDateTime &dt, Format *format) +int Worksheet::writeDateTime(int row, int column, const QDateTime &dt, const Format &format) { Q_D(Worksheet); if (d->checkDimensions(row, column)) return -1; - if (!format) { - format = d->workbook->createFormat(); - format->setNumberFormat(d->workbook->defaultDateFormat()); - } + Format fmt = format; + if (!fmt.isValid()) + fmt.setNumberFormat(d->workbook->defaultDateFormat()); + d->workbook->styles()->addFormat(fmt); double value = datetimeToNumber(dt, d->workbook->isDate1904()); - d->cellTable[row][column] = QSharedPointer(new Cell(value, Cell::Numeric, format, this)); - d->workbook->styles()->addFormat(format); + d->cellTable[row][column] = QSharedPointer(new Cell(value, Cell::Numeric, fmt, this)); return 0; } @@ -817,7 +817,7 @@ int Worksheet::writeDateTime(int row, int column, const QDateTime &dt, Format *f /*! \overload */ -int Worksheet::writeHyperlink(const QString &row_column, const QUrl &url, Format *format, const QString &display, const QString &tip) +int Worksheet::writeHyperlink(const QString &row_column, const QUrl &url, const Format &format, const QString &display, const QString &tip) { //convert the "A1" notation to row/column notation QPoint pos = xl_cell_to_rowcol(row_column); @@ -830,7 +830,7 @@ int Worksheet::writeHyperlink(const QString &row_column, const QUrl &url, Format /*! Write a QUrl \a value to the cell (\a row, \a column) with the \a format */ -int Worksheet::writeHyperlink(int row, int column, const QUrl &url, Format *format, const QString &display, const QString &tip) +int Worksheet::writeHyperlink(int row, int column, const QUrl &url, const Format &format, const QString &display, const QString &tip) { Q_D(Worksheet); if (d->checkDimensions(row, column)) @@ -863,18 +863,17 @@ int Worksheet::writeHyperlink(int row, int column, const QUrl &url, Format *form urlString = url.toString(QUrl::RemoveFragment); } + Format fmt = format.isValid() ? format : d->cellFormat(row, column); //Given a default style for hyperlink - if (!format) { - format = d->workbook->createFormat(); - format->setFontColor(Qt::blue); - format->setFontUnderline(Format::FontUnderlineSingle); + if (!fmt.isValid()) { + fmt.setFontColor(Qt::blue); + fmt.setFontUnderline(Format::FontUnderlineSingle); } + d->workbook->styles()->addFormat(fmt); //Write the hyperlink string as normal string. d->sharedStrings()->addSharedString(displayString); - format = format ? format : d->cellFormat(row, column); - d->cellTable[row][column] = QSharedPointer(new Cell(displayString, Cell::String, format, this)); - d->workbook->styles()->addFormat(format); + d->cellTable[row][column] = QSharedPointer(new Cell(displayString, Cell::String, fmt, this)); //Store the hyperlink data in a separate table d->urlTable[row][column] = new XlsxUrlData(XlsxUrlData::External, urlString, locationString, tip); @@ -906,7 +905,7 @@ int Worksheet::insertImage(int row, int column, const QImage &image, const QPoin \note All cells except the top-left one will be cleared. */ -int Worksheet::mergeCells(const CellRange &range, Format *format) +int Worksheet::mergeCells(const CellRange &range, const Format &format) { Q_D(Worksheet); if (range.rowCount() < 2 && range.columnCount() < 2) @@ -915,12 +914,15 @@ int Worksheet::mergeCells(const CellRange &range, Format *format) if (d->checkDimensions(range.firstRow(), range.firstColumn())) return -1; + if (format.isValid()) + d->workbook->styles()->addFormat(format); + for (int row = range.firstRow(); row <= range.lastRow(); ++row) { for (int col = range.firstColumn(); col <= range.lastColumn(); ++col) { if (row == range.firstRow() && col == range.firstColumn()) { Cell *cell = cellAt(row, col); if (cell) { - if (format) + if (format.isValid()) cell->d_ptr->format = format; } else { writeBlank(row, col, format); @@ -931,9 +933,6 @@ int Worksheet::mergeCells(const CellRange &range, Format *format) } } - if (format) - d->workbook->styles()->addFormat(format); - d->merges.append(range); return 0; } @@ -945,7 +944,7 @@ int Worksheet::mergeCells(const CellRange &range, Format *format) \note All cells except the top-left one will be cleared. */ -int Worksheet::mergeCells(const QString &range, Format *format) +int Worksheet::mergeCells(const QString &range, const Format &format) { QStringList cells = range.split(QLatin1Char(':')); if (cells.size() != 2) @@ -1060,8 +1059,8 @@ void Worksheet::saveToXmlFile(QIODevice *device) writer.writeAttribute(QStringLiteral("max"), QString::number(col_info->lastColumn)); if (col_info->width) writer.writeAttribute(QStringLiteral("width"), QString::number(col_info->width, 'g', 15)); - if (col_info->format) - writer.writeAttribute(QStringLiteral("style"), QString::number(col_info->format->xfIndex())); + if (!col_info->format.isEmpty()) + writer.writeAttribute(QStringLiteral("style"), QString::number(col_info->format.xfIndex())); if (col_info->hidden) writer.writeAttribute(QStringLiteral("hidden"), QStringLiteral("1")); if (col_info->width) @@ -1112,8 +1111,8 @@ void WorksheetPrivate::writeSheetData(XmlStreamWriter &writer) if (rowsInfo.contains(row_num)) { QSharedPointer rowInfo = rowsInfo[row_num]; - if (rowInfo->format) { - writer.writeAttribute(QStringLiteral("s"), QString::number(rowInfo->format->xfIndex())); + if (!rowInfo->format.isEmpty()) { + writer.writeAttribute(QStringLiteral("s"), QString::number(rowInfo->format.xfIndex())); writer.writeAttribute(QStringLiteral("customFormat"), QStringLiteral("1")); } if (rowInfo->height != 15 && rowInfo->height != 0) { @@ -1151,12 +1150,12 @@ void WorksheetPrivate::writeCellData(XmlStreamWriter &writer, int row, int col, writer.writeAttribute(QStringLiteral("r"), cell_pos); //Style used by the cell, row or col - if (cell->format()) - writer.writeAttribute(QStringLiteral("s"), QString::number(cell->format()->xfIndex())); - else if (rowsInfo.contains(row) && rowsInfo[row]->format) - writer.writeAttribute(QStringLiteral("s"), QString::number(rowsInfo[row]->format->xfIndex())); - else if (colsInfoHelper.contains(col) && colsInfoHelper[col]->format) - writer.writeAttribute(QStringLiteral("s"), QString::number(colsInfoHelper[col]->format->xfIndex())); + if (!cell->format().isEmpty()) + writer.writeAttribute(QStringLiteral("s"), QString::number(cell->format().xfIndex())); + else if (rowsInfo.contains(row) && !rowsInfo[row]->format.isEmpty()) + writer.writeAttribute(QStringLiteral("s"), QString::number(rowsInfo[row]->format.xfIndex())); + else if (colsInfoHelper.contains(col) && !colsInfoHelper[col]->format.isEmpty()) + writer.writeAttribute(QStringLiteral("s"), QString::number(colsInfoHelper[col]->format.xfIndex())); if (cell->dataType() == Cell::String) { int sst_idx; @@ -1367,7 +1366,7 @@ void WorksheetPrivate::writeDrawings(XmlStreamWriter &writer) Sets row \a height and \a format. Row height measured in point size. If format equals 0 then format is ignored. \a row is 1-indexed. */ -bool Worksheet::setRow(int row, double height, Format *format, bool hidden) +bool Worksheet::setRow(int row, double height, const Format &format, bool hidden) { Q_D(Worksheet); int min_col = d->dimension.firstColumn() < 0 ? 0 : d->dimension.firstColumn(); @@ -1428,11 +1427,11 @@ void WorksheetPrivate::splitColsInfo(int colFirst, int colLast) numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. If format equals 0 then format is ignored. Both \a colFirst and \a colLast are all 1-indexed. */ -bool Worksheet::setColumn(int colFirst, int colLast, double width, Format *format, bool hidden) +bool Worksheet::setColumn(int colFirst, int colLast, double width, const Format &format, bool hidden) { Q_D(Worksheet); bool ignore_row = true; - bool ignore_col = (format || (width && hidden)) ? false : true; + bool ignore_col = (format.isValid() || (width && hidden)) ? false : true; if (colFirst > colLast) return false; @@ -1482,7 +1481,7 @@ bool Worksheet::setColumn(int colFirst, int colLast, double width, Format *forma numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. If format equals 0 then format is ignored. \a colFirst and \a colLast should be "A", "B", "C", ... */ -bool Worksheet::setColumn(const QString &colFirst, const QString &colLast, double width, Format *format, bool hidden) +bool Worksheet::setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format, bool hidden) { int col1 = xl_col_name_to_value(colFirst); int col2 = xl_col_name_to_value(colLast); @@ -1882,11 +1881,11 @@ void WorksheetPrivate::readSheetData(XmlStreamReader &reader) QPoint pos = xl_cell_to_rowcol(r); //get format - Format *format = 0; + Format format; if (attributes.hasAttribute(QLatin1String("s"))) { int idx = attributes.value(QLatin1String("s")).toString().toInt(); format = workbook->styles()->xfFormat(idx); - if (!format) + if (!format.isValid()) qDebug()<Invalid style index: ").arg(idx)<