Browse Source

API change: Using Format instead of Format *

master
Debao Zhang 12 years ago
parent
commit
176160d2a8
  1. 94
      examples/xlsx/demo/main.cpp
  2. 8
      examples/xlsx/formulas/main.cpp
  3. 6
      examples/xlsx/mergecells/main.cpp
  4. 14
      examples/xlsx/numberformat/main.cpp
  5. 14
      examples/xlsx/rowcolumn/main.cpp
  6. 36
      examples/xlsx/style/main.cpp
  7. 6
      src/xlsx/xlsxcell.cpp
  8. 5
      src/xlsx/xlsxcell.h
  9. 2
      src/xlsx/xlsxcell_p.h
  10. 23
      src/xlsx/xlsxdocument.cpp
  11. 17
      src/xlsx/xlsxdocument.h
  12. 18
      src/xlsx/xlsxformat.cpp
  13. 1
      src/xlsx/xlsxformat.h
  14. 405
      src/xlsx/xlsxstyles.cpp
  15. 29
      src/xlsx/xlsxstyles_p.h
  16. 6
      src/xlsx/xlsxworkbook.cpp
  17. 2
      src/xlsx/xlsxworkbook.h
  18. 155
      src/xlsx/xlsxworksheet.cpp
  19. 50
      src/xlsx/xlsxworksheet.h
  20. 10
      src/xlsx/xlsxworksheet_p.h
  21. 80
      tests/auto/document/tst_documenttest.cpp
  22. 34
      tests/auto/styles/tst_stylestest.cpp
  23. 2
      tests/auto/worksheet/tst_worksheet.cpp

94
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) void writeHorizontalAlignCell(Document &xlsx, const QString &cell, const QString &text, Format::HorizontalAlignment align)
{ {
Format *format = xlsx.createFormat(); Format format;
format->setHorizontalAlignment(align); format.setHorizontalAlignment(align);
format->setBorderStyle(Format::BorderThin); format.setBorderStyle(Format::BorderThin);
xlsx.write(cell, text, format); xlsx.write(cell, text, format);
} }
void writeVerticalAlignCell(Document &xlsx, const QString &range, const QString &text, Format::VerticalAlignment align) void writeVerticalAlignCell(Document &xlsx, const QString &range, const QString &text, Format::VerticalAlignment align)
{ {
Format *format = xlsx.createFormat(); Format format;
format->setVerticalAlignment(align); format.setVerticalAlignment(align);
format->setBorderStyle(Format::BorderThin); format.setBorderStyle(Format::BorderThin);
CellRange r(range); CellRange r(range);
xlsx.write(r.firstRow(), r.firstColumn(), text); xlsx.write(r.firstRow(), r.firstColumn(), text);
xlsx.mergeCells(r, format); 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) void writeBorderStyleCell(Document &xlsx, const QString &cell, const QString &text, Format::BorderStyle bs)
{ {
Format *format = xlsx.createFormat(); Format format;
format->setBorderStyle(bs); format.setBorderStyle(bs);
xlsx.write(cell, text, format); xlsx.write(cell, text, format);
} }
void writeSolidFillCell(Document &xlsx, const QString &cell, const QColor &color) void writeSolidFillCell(Document &xlsx, const QString &cell, const QColor &color)
{ {
Format *format = xlsx.createFormat(); Format format;
format->setPatternBackgroundColor(color); format.setPatternBackgroundColor(color);
xlsx.write(cell, QVariant(), format); xlsx.write(cell, QVariant(), format);
} }
void writePatternFillCell(Document &xlsx, const QString &cell, Format::FillPattern pattern, const QColor &color) void writePatternFillCell(Document &xlsx, const QString &cell, Format::FillPattern pattern, const QColor &color)
{ {
Format *format = xlsx.createFormat(); Format format;
format->setPatternForegroundColor(color); format.setPatternForegroundColor(color);
format->setFillPattern(pattern); format.setFillPattern(pattern);
xlsx.write(cell, QVariant(), format); xlsx.write(cell, QVariant(), format);
} }
void writeBorderAndFontColorCell(Document &xlsx, const QString &cell, const QString &text, const QColor &color) void writeBorderAndFontColorCell(Document &xlsx, const QString &cell, const QString &text, const QColor &color)
{ {
Format *format = xlsx.createFormat(); Format format;
format->setBorderStyle(Format::BorderThin); format.setBorderStyle(Format::BorderThin);
format->setBorderColor(color); format.setBorderColor(color);
format->setFontColor(color); format.setFontColor(color);
xlsx.write(cell, text, format); xlsx.write(cell, text, format);
} }
void writeFontNameCell(Document &xlsx, const QString &cell, const QString &text) void writeFontNameCell(Document &xlsx, const QString &cell, const QString &text)
{ {
Format *format = xlsx.createFormat(); Format format;
format->setFontName(text); format.setFontName(text);
format->setFontSize(16); format.setFontSize(16);
xlsx.write(cell, text, format); xlsx.write(cell, text, format);
} }
void writeFontSizeCell(Document &xlsx, const QString &cell, int size) void writeFontSizeCell(Document &xlsx, const QString &cell, int size)
{ {
Format *format = xlsx.createFormat(); Format format;
format->setFontSize(size); format.setFontSize(size);
xlsx.write(cell, "Qt Xlsx", format); xlsx.write(cell, "Qt Xlsx", format);
} }
void writeInternalNumFormatsCell(Document &xlsx, int row, double value, int numFmt) void writeInternalNumFormatsCell(Document &xlsx, int row, double value, int numFmt)
{ {
Format *format = xlsx.createFormat(); Format format;
format->setNumberFormatIndex(numFmt); format.setNumberFormatIndex(numFmt);
xlsx.write(row, 1, value); xlsx.write(row, 1, value);
xlsx.write(row, 2, QString("Builtin NumFmt %1").arg(numFmt)); xlsx.write(row, 2, QString("Builtin NumFmt %1").arg(numFmt));
xlsx.write(row, 3, value, format); 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) void writeCustomNumFormatsCell(Document &xlsx, int row, double value, const QString &numFmt)
{ {
Format *format = xlsx.createFormat(); Format format;
format->setNumberFormat(numFmt); format.setNumberFormat(numFmt);
xlsx.write(row, 1, value); xlsx.write(row, 1, value);
xlsx.write(row, 2, numFmt); xlsx.write(row, 2, numFmt);
xlsx.write(row, 3, value, format); xlsx.write(row, 3, value, format);
@ -141,17 +141,17 @@ int main()
xlsx.addWorksheet("Fonts"); xlsx.addWorksheet("Fonts");
xlsx.write("B3", "Normal"); xlsx.write("B3", "Normal");
Format *font_bold = xlsx.createFormat(); Format font_bold;
font_bold->setFontBold(true); font_bold.setFontBold(true);
xlsx.write("B4", "Bold", font_bold); xlsx.write("B4", "Bold", font_bold);
Format *font_italic = xlsx.createFormat(); Format font_italic;
font_italic->setFontItalic(true); font_italic.setFontItalic(true);
xlsx.write("B5", "Italic", font_italic); xlsx.write("B5", "Italic", font_italic);
Format *font_underline = xlsx.createFormat(); Format font_underline;
font_underline->setFontUnderline(Format::FontUnderlineSingle); font_underline.setFontUnderline(Format::FontUnderlineSingle);
xlsx.write("B6", "Underline", font_underline); xlsx.write("B6", "Underline", font_underline);
Format *font_strikeout = xlsx.createFormat(); Format font_strikeout;
font_strikeout->setFontStrikeOut(true); font_strikeout.setFontStrikeOut(true);
xlsx.write("B7", "StrikeOut", font_strikeout); xlsx.write("B7", "StrikeOut", font_strikeout);
writeFontNameCell(xlsx, "D3", "Arial"); writeFontNameCell(xlsx, "D3", "Arial");
@ -170,9 +170,9 @@ int main()
writeFontSizeCell(xlsx, "G8", 20); writeFontSizeCell(xlsx, "G8", 20);
writeFontSizeCell(xlsx, "G9", 25); writeFontSizeCell(xlsx, "G9", 25);
Format *font_vertical = xlsx.createFormat(); Format font_vertical;
font_vertical->setRotation(255); font_vertical.setRotation(255);
font_vertical->setFontSize(16); font_vertical.setFontSize(16);
xlsx.write("J3", "vertical", font_vertical); xlsx.write("J3", "vertical", font_vertical);
xlsx.mergeCells("J3:J9"); xlsx.mergeCells("J3:J9");
@ -180,10 +180,10 @@ int main()
//Create the third sheet. //Create the third sheet.
xlsx.addWorksheet("Formulas"); xlsx.addWorksheet("Formulas");
xlsx.setColumn("A", "B", 40); xlsx.setColumn("A", "B", 40);
Format *rAlign = xlsx.createFormat(); Format rAlign;
rAlign->setHorizontalAlignment(Format::AlignRight); rAlign.setHorizontalAlignment(Format::AlignRight);
Format *lAlign = xlsx.createFormat(); Format lAlign;
lAlign->setHorizontalAlignment(Format::AlignLeft); lAlign.setHorizontalAlignment(Format::AlignLeft);
xlsx.write("B3", 40, lAlign); xlsx.write("B3", 40, lAlign);
xlsx.write("B4", 30, lAlign); xlsx.write("B4", 30, lAlign);
xlsx.write("B5", 50, lAlign); xlsx.write("B5", 50, lAlign);
@ -215,9 +215,9 @@ int main()
xlsx.write("A21", "LEN(\"Hello Qt!\")=", rAlign); xlsx.write("A21", "LEN(\"Hello Qt!\")=", rAlign);
xlsx.write("B21", "=LEN(\"Hello Qt!\")", lAlign); xlsx.write("B21", "=LEN(\"Hello Qt!\")", lAlign);
Format *dateFormat = xlsx.createFormat(); Format dateFormat;
dateFormat->setHorizontalAlignment(Format::AlignLeft); dateFormat.setHorizontalAlignment(Format::AlignLeft);
dateFormat->setNumberFormat("yyyy-mm-dd"); dateFormat.setNumberFormat("yyyy-mm-dd");
xlsx.write("A23", "DATE(2013,8,13)=", rAlign); xlsx.write("A23", "DATE(2013,8,13)=", rAlign);
xlsx.write("B23", "=DATE(2013,8,13)", dateFormat); xlsx.write("B23", "=DATE(2013,8,13)", dateFormat);
xlsx.write("A24", "DAY(B23)=", rAlign); xlsx.write("A24", "DAY(B23)=", rAlign);
@ -260,9 +260,9 @@ int main()
//--------------------------------------------------------------- //---------------------------------------------------------------
//Create the fifth sheet. //Create the fifth sheet.
xlsx.addWorksheet("Merging"); xlsx.addWorksheet("Merging");
Format *centerAlign = xlsx.createFormat(); Format centerAlign;
centerAlign->setHorizontalAlignment(Format::AlignHCenter); centerAlign.setHorizontalAlignment(Format::AlignHCenter);
centerAlign->setVerticalAlignment(Format::AlignVCenter); centerAlign.setVerticalAlignment(Format::AlignVCenter);
xlsx.write("B4", "Hello Qt!"); xlsx.write("B4", "Hello Qt!");
xlsx.mergeCells("B4:F6", centerAlign); xlsx.mergeCells("B4:F6", centerAlign);
xlsx.write("B8", 1); xlsx.write("B8", 1);

8
examples/xlsx/formulas/main.cpp

@ -13,10 +13,10 @@ int main()
//![1] //![1]
xlsx.setColumn("A", "B", 40); xlsx.setColumn("A", "B", 40);
Format *rAlign = xlsx.createFormat(); Format rAlign;
rAlign->setHorizontalAlignment(Format::AlignRight); rAlign.setHorizontalAlignment(Format::AlignRight);
Format *lAlign = xlsx.createFormat(); Format lAlign;
lAlign->setHorizontalAlignment(Format::AlignLeft); lAlign.setHorizontalAlignment(Format::AlignLeft);
xlsx.write("B3", 40, lAlign); xlsx.write("B3", 40, lAlign);
xlsx.write("B4", 30, lAlign); xlsx.write("B4", 30, lAlign);
xlsx.write("B5", 50, lAlign); xlsx.write("B5", 50, lAlign);

6
examples/xlsx/mergecells/main.cpp

@ -7,9 +7,9 @@ int main()
{ {
Document xlsx; Document xlsx;
//![0] //![0]
Format *format = xlsx.createFormat(); Format format;
format->setHorizontalAlignment(Format::AlignHCenter); format.setHorizontalAlignment(Format::AlignHCenter);
format->setVerticalAlignment(Format::AlignVCenter); format.setVerticalAlignment(Format::AlignVCenter);
//![0] //![0]
//![1] //![1]
xlsx.write("B4", "Hello Qt!"); xlsx.write("B4", "Hello Qt!");

14
examples/xlsx/numberformat/main.cpp

@ -9,9 +9,9 @@ int main(int argc, char** argv)
QXlsx::Document xlsx; QXlsx::Document xlsx;
xlsx.setColumn(1, 4, 20.0); xlsx.setColumn(1, 4, 20.0);
QXlsx::Format *header = xlsx.createFormat(); QXlsx::Format header;
header->setFontBold(true); header.setFontBold(true);
header->setFontSize(20); header.setFontSize(20);
//Custom number formats //Custom number formats
QStringList numFormats; QStringList numFormats;
@ -26,8 +26,8 @@ int main(int argc, char** argv)
int row = i+2; int row = i+2;
xlsx.write(row, 1, 100.0); xlsx.write(row, 1, 100.0);
xlsx.write(row, 2, numFormats[i]); xlsx.write(row, 2, numFormats[i]);
QXlsx::Format *format = xlsx.createFormat(); QXlsx::Format format;
format->setNumberFormat(numFormats[i]); format.setNumberFormat(numFormats[i]);
xlsx.write(row, 3, 100.0, format); xlsx.write(row, 3, 100.0, format);
} }
@ -42,8 +42,8 @@ int main(int argc, char** argv)
int numFmt = i; int numFmt = i;
xlsx.write(row, 1, 100.0); xlsx.write(row, 1, 100.0);
xlsx.write(row, 2, numFmt); xlsx.write(row, 2, numFmt);
QXlsx::Format *format = xlsx.createFormat(); QXlsx::Format format;
format->setNumberFormatIndex(numFmt); format.setNumberFormatIndex(numFmt);
xlsx.write(row, 3, 100.0, format); xlsx.write(row, 3, 100.0, format);
} }

14
examples/xlsx/rowcolumn/main.cpp

@ -14,18 +14,18 @@ int main()
xlsx.setColumn(3, 3, 40.0); xlsx.setColumn(3, 3, 40.0);
//Set style for the row 11th. //Set style for the row 11th.
QXlsx::Format *format1 = xlsx.createFormat(); QXlsx::Format format1;
format1->setFontBold(true); format1.setFontBold(true);
format1->setFontColor(QColor(Qt::blue)); format1.setFontColor(QColor(Qt::blue));
format1->setFontSize(20); format1.setFontSize(20);
xlsx.write(11, 1, "Hello Row Style"); xlsx.write(11, 1, "Hello Row Style");
xlsx.write(11, 6, "Blue Color"); xlsx.write(11, 6, "Blue Color");
xlsx.setRow(11, 41, format1); xlsx.setRow(11, 41, format1);
//Set style for the col [9th, 16th) //Set style for the col [9th, 16th)
QXlsx::Format *format2 = xlsx.createFormat(); QXlsx::Format format2;
format2->setFontBold(true); format2.setFontBold(true);
format2->setFontColor(QColor(Qt::magenta)); format2.setFontColor(QColor(Qt::magenta));
for (int row=12; row<=30; row++) for (int row=12; row<=30; row++)
for (int col=9; col<=15; col++) for (int col=9; col<=15; col++)
xlsx.write(row, col, row+col); xlsx.write(row, col, row+col);

36
examples/xlsx/style/main.cpp

@ -5,32 +5,32 @@
int main() int main()
{ {
QXlsx::Document xlsx; QXlsx::Document xlsx;
QXlsx::Format *format1 = xlsx.createFormat(); QXlsx::Format format1;
format1->setFontColor(QColor(Qt::red)); format1.setFontColor(QColor(Qt::red));
format1->setFontSize(15); format1.setFontSize(15);
format1->setHorizontalAlignment(QXlsx::Format::AlignHCenter); format1.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
format1->setBorderStyle(QXlsx::Format::BorderDashDotDot); format1.setBorderStyle(QXlsx::Format::BorderDashDotDot);
xlsx.write("A1", "Hello Qt!", format1); xlsx.write("A1", "Hello Qt!", format1);
xlsx.write("B3", 12345, format1); xlsx.write("B3", 12345, format1);
QXlsx::Format *format2 = xlsx.createFormat(); QXlsx::Format format2;
format2->setFontBold(true); format2.setFontBold(true);
format2->setFontUnderline(QXlsx::Format::FontUnderlineDouble); format2.setFontUnderline(QXlsx::Format::FontUnderlineDouble);
format2->setFillPattern(QXlsx::Format::PatternLightUp); format2.setFillPattern(QXlsx::Format::PatternLightUp);
xlsx.write("C5", "=44+33", format2); xlsx.write("C5", "=44+33", format2);
xlsx.write("D7", true, format2); xlsx.write("D7", true, format2);
QXlsx::Format *format3 = xlsx.createFormat(); QXlsx::Format format3;
format3->setFontBold(true); format3.setFontBold(true);
format3->setFontColor(QColor(Qt::blue)); format3.setFontColor(QColor(Qt::blue));
format3->setFontSize(20); format3.setFontSize(20);
xlsx.write(11, 1, "Hello Row Style"); xlsx.write(11, 1, "Hello Row Style");
xlsx.write(11, 6, "Blue Color"); xlsx.write(11, 6, "Blue Color");
xlsx.setRow(11, 41, format3); xlsx.setRow(11, 41, format3);
QXlsx::Format *format4 = xlsx.createFormat(); QXlsx::Format format4;
format4->setFontBold(true); format4.setFontBold(true);
format4->setFontColor(QColor(Qt::magenta)); format4.setFontColor(QColor(Qt::magenta));
for (int row=21; row<=40; row++) for (int row=21; row<=40; row++)
for (int col=9; col<16; col++) for (int col=9; col<16; col++)
xlsx.write(row, col, row+col); xlsx.write(row, col, row+col);
@ -38,8 +38,8 @@ int main()
xlsx.write("A5", QDate(2013, 8, 29)); xlsx.write("A5", QDate(2013, 8, 29));
QXlsx::Format *format6 = xlsx.createFormat(); QXlsx::Format format6;
format6->setPatternBackgroundColor(QColor(Qt::green)); format6.setPatternBackgroundColor(QColor(Qt::green));
xlsx.write("A6", "Background color: green", format6); xlsx.write("A6", "Background color: green", format6);
xlsx.saveAs("book1.xlsx"); xlsx.saveAs("book1.xlsx");

6
src/xlsx/xlsxcell.cpp

@ -50,7 +50,7 @@ CellPrivate::CellPrivate(Cell *p) :
* \internal * \internal
* Created by Worksheet only. * 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(new CellPrivate(this))
{ {
d_ptr->value = data; 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. * 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); Q_D(const Cell);
return d->format; return d->format;
@ -109,7 +109,7 @@ QString Cell::formula() const
bool Cell::isDateTime() const bool Cell::isDateTime() const
{ {
Q_D(const Cell); 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 true;
return false; return false;
} }

5
src/xlsx/xlsxcell.h

@ -26,6 +26,7 @@
#define QXLSX_XLSXCELL_H #define QXLSX_XLSXCELL_H
#include "xlsxglobal.h" #include "xlsxglobal.h"
#include "xlsxformat.h"
#include <QVariant> #include <QVariant>
QT_BEGIN_NAMESPACE_XLSX QT_BEGIN_NAMESPACE_XLSX
@ -52,7 +53,7 @@ public:
DataType dataType() const; DataType dataType() const;
QVariant value() const; QVariant value() const;
Format * format() const; Format format() const;
QString formula() const; QString formula() const;
bool isDateTime() const; bool isDateTime() const;
@ -65,7 +66,7 @@ private:
friend class Worksheet; friend class Worksheet;
friend class WorksheetPrivate; 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; CellPrivate * const d_ptr;
}; };

2
src/xlsx/xlsxcell_p.h

@ -43,7 +43,7 @@ public:
QVariant value; QVariant value;
QString formula; QString formula;
Cell::DataType dataType; Cell::DataType dataType;
Format *format; Format format;
CellRange range; //used for arrayFormula CellRange range; //used for arrayFormula
RichString richString; RichString richString;

23
src/xlsx/xlsxdocument.cpp

@ -101,21 +101,12 @@ Document::Document(QIODevice *device, QObject *parent) :
d_ptr->init(); d_ptr->init();
} }
/*!
* Create a new format used by sheet cells.
*/
Format *Document::createFormat()
{
Q_D(Document);
return d->workbook->createFormat();
}
/*! /*!
\overload \overload
Write \a value to cell \a row_column with the \a format. 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); 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. * 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); 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. \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); 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. \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); 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 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. 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); 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 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. 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); 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 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", ... 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); return currentWorksheet()->setColumn(colFirst, colLast, width, format, hidden);
} }

17
src/xlsx/xlsxdocument.h

@ -27,6 +27,7 @@
#define QXLSX_XLSXDOCUMENT_H #define QXLSX_XLSXDOCUMENT_H
#include "xlsxglobal.h" #include "xlsxglobal.h"
#include "xlsxformat.h"
#include <QObject> #include <QObject>
#include <QVariant> #include <QVariant>
class QIODevice; class QIODevice;
@ -37,7 +38,6 @@ QT_BEGIN_NAMESPACE_XLSX
class Workbook; class Workbook;
class Worksheet; class Worksheet;
class Package; class Package;
class Format;
class Cell; class Cell;
class CellRange; class CellRange;
class DataValidation; class DataValidation;
@ -54,19 +54,18 @@ public:
Document(QIODevice *device, QObject *parent=0); Document(QIODevice *device, QObject *parent=0);
~Document(); ~Document();
Format *createFormat(); int write(const QString &cell, const QVariant &value, const Format &format=Format());
int write(const QString &cell, const QVariant &value, Format *format=0); int write(int row, int col, const QVariant &value, const Format &format=Format());
int write(int row, int col, const QVariant &value, Format *format=0);
QVariant read(const QString &cell) const; QVariant read(const QString &cell) const;
QVariant read(int row, int col) 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 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 CellRange &range, const Format &format=Format());
int mergeCells(const QString &range, Format *format=0); int mergeCells(const QString &range, const Format &format=Format());
int unmergeCells(const CellRange &range); int unmergeCells(const CellRange &range);
int unmergeCells(const QString &range); int unmergeCells(const QString &range);
bool setRow(int row, double height, 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, Format* format=0, 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, Format* format=0, 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 groupRows(int rowFirst, int rowLast, bool collapsed = true);
bool groupColumns(int colFirst, int colLast, bool collapsed = true); bool groupColumns(int colFirst, int colLast, bool collapsed = true);
bool addDataValidation(const DataValidation &validation); bool addDataValidation(const DataValidation &validation);

18
src/xlsx/xlsxformat.cpp

@ -790,7 +790,19 @@ void Format::setLocked(bool locked)
*/ */
bool Format::isValid() const 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 QByteArray Format::formatKey() const
@ -823,7 +835,7 @@ int Format::xfIndex() const
bool Format::xfIndexValid() const bool Format::xfIndexValid() const
{ {
return !d->dirty && d->xf_indexValid; return d->xf_indexValid;
} }
void Format::setDxfIndex(int index) void Format::setDxfIndex(int index)
@ -839,7 +851,7 @@ int Format::dxfIndex() const
bool Format::dxfIndexValid() const bool Format::dxfIndexValid() const
{ {
return !d->dirty && d->dxf_indexValid; return d->dxf_indexValid;
} }
bool Format::operator ==(const Format &format) const bool Format::operator ==(const Format &format) const

1
src/xlsx/xlsxformat.h

@ -214,6 +214,7 @@ public:
void setHidden(bool hidden); void setHidden(bool hidden);
bool isValid() const; bool isValid() const;
bool isEmpty() const;
bool operator == (const Format &format) const; bool operator == (const Format &format) const;
bool operator != (const Format &format) const; bool operator != (const Format &format) const;

405
src/xlsx/xlsxstyles.cpp

@ -46,13 +46,14 @@ Styles::Styles(bool createEmpty)
if (!createEmpty) { if (!createEmpty) {
//Add default Format //Add default Format
addFormat(createFormat()); Format defaultFmt;
//Add another fill format addFormat(defaultFmt);
Format *format = createFormat();
format->setFillPattern(Format::PatternGray125);
m_fillsList.append(format); //Add another fill format
m_fillsHash.insert(format->fillKey(), 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 Styles::xfFormat(int idx) const
{
Format *format = new Format();
m_createdFormatsList.append(QSharedPointer<Format>(format));
return format;
}
Format *Styles::xfFormat(int idx) const
{ {
if (idx <0 || idx >= m_xf_formatsList.size()) if (idx <0 || idx >= m_xf_formatsList.size())
return 0; return Format();
return m_xf_formatsList[idx]; return m_xf_formatsList[idx];
} }
@ -83,13 +76,10 @@ Format *Styles::xfFormat(int idx) const
the same key have been in. the same key have been in.
This is useful when reading existing .xlsx files which may contains duplicated formats. 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 //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()) { if (m_builtinNumFmtsHash.isEmpty()) {
m_builtinNumFmtsHash.insert(QStringLiteral("General"), 0); m_builtinNumFmtsHash.insert(QStringLiteral("General"), 0);
m_builtinNumFmtsHash.insert(QStringLiteral("0"), 1); 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("##0.0E+0"), 48);
m_builtinNumFmtsHash.insert(QStringLiteral("@"), 49); m_builtinNumFmtsHash.insert(QStringLiteral("@"), 49);
} }
const QString str = format->numberFormat(); const QString str = format.numberFormat();
//Assign proper number format index //Assign proper number format index
if (m_builtinNumFmtsHash.contains(str)) { if (m_builtinNumFmtsHash.contains(str)) {
format->setNumberFormat(m_builtinNumFmtsHash[str], str); const_cast<Format *>(&format)->setNumberFormat(m_builtinNumFmtsHash[str], str);
} else if (m_customNumFmtsHash.contains(str)) { } else if (m_customNumFmtsHash.contains(str)) {
format->setNumberFormat(m_customNumFmtsHash[str]->formatIndex, str); const_cast<Format *>(&format)->setNumberFormat(m_customNumFmtsHash[str]->formatIndex, str);
} else { } else {
//Assign a new fmt Id. //Assign a new fmt Id.
format->setNumberFormat(m_nextCustomNumFmtId, str); const_cast<Format *>(&format)->setNumberFormat(m_nextCustomNumFmtId, str);
QSharedPointer<XlsxFormatNumberData> fmt(new XlsxFormatNumberData); QSharedPointer<XlsxFormatNumberData> fmt(new XlsxFormatNumberData);
fmt->formatIndex = m_nextCustomNumFmtId; fmt->formatIndex = m_nextCustomNumFmtId;
@ -150,57 +140,57 @@ void Styles::addFormat(Format *format, bool force)
} }
//Font //Font
if (!format->fontIndexValid()) { if (!format.fontIndexValid()) {
if (!m_fontsHash.contains(format->fontKey())) { if (!m_fontsHash.contains(format.fontKey())) {
format->setFontIndex(m_fontsList.size()); //Assign proper index const_cast<Format *>(&format)->setFontIndex(m_fontsList.size()); //Assign proper index
m_fontsList.append(format); m_fontsList.append(format);
m_fontsHash[format->fontKey()] = format; m_fontsHash[format.fontKey()] = format;
} else { } else {
format->setFontIndex(m_fontsHash[format->fontKey()]->fontIndex()); const_cast<Format *>(&format)->setFontIndex(m_fontsHash[format.fontKey()].fontIndex());
} }
} }
//Fill //Fill
if (!format->fillIndexValid()) { if (!format.fillIndexValid()) {
if (!m_fillsHash.contains(format->fillKey())) { if (!m_fillsHash.contains(format.fillKey())) {
format->setFillIndex(m_fillsList.size()); //Assign proper index const_cast<Format *>(&format)->setFillIndex(m_fillsList.size()); //Assign proper index
m_fillsList.append(format); m_fillsList.append(format);
m_fillsHash[format->fillKey()] = format; m_fillsHash[format.fillKey()] = format;
} else { } else {
format->setFillIndex(m_fillsHash[format->fillKey()]->fillIndex()); const_cast<Format *>(&format)->setFillIndex(m_fillsHash[format.fillKey()].fillIndex());
} }
} }
//Border //Border
if (!format->borderIndexValid()) { if (!format.borderIndexValid()) {
if (!m_bordersHash.contains(format->borderKey())) { if (!m_bordersHash.contains(format.borderKey())) {
format->setBorderIndex(m_bordersList.size()); //Assign proper index const_cast<Format *>(&format)->setBorderIndex(m_bordersList.size()); //Assign proper index
m_bordersList.append(format); m_bordersList.append(format);
m_bordersHash[format->borderKey()] = format; m_bordersHash[format.borderKey()] = format;
} else { } else {
format->setBorderIndex(m_bordersHash[format->borderKey()]->borderIndex()); const_cast<Format *>(&format)->setBorderIndex(m_bordersHash[format.borderKey()].borderIndex());
} }
} }
//Format //Format
if (format->isDxfFormat()) { if (format.isDxfFormat()) {
if (!format->dxfIndexValid()) { if (!format.dxfIndexValid()) {
if (!m_dxf_formatsHash.contains(format->formatKey())) { if (!m_dxf_formatsHash.contains(format.formatKey())) {
format->setDxfIndex(m_dxf_formatsList.size()); const_cast<Format *>(&format)->setDxfIndex(m_dxf_formatsList.size());
m_dxf_formatsList.append(format); m_dxf_formatsList.append(format);
m_dxf_formatsHash[format->formatKey()] = format; m_dxf_formatsHash[format.formatKey()] = format;
} else { } else {
format->setDxfIndex(m_dxf_formatsHash[format->formatKey()]->dxfIndex()); const_cast<Format *>(&format)->setDxfIndex(m_dxf_formatsHash[format.formatKey()].dxfIndex());
} }
} }
} else { } else {
if (!format->xfIndexValid()) { if (!format.xfIndexValid()) {
if (!m_xf_formatsHash.contains(format->formatKey()) || force) { if (!m_xf_formatsHash.contains(format.formatKey()) || force) {
format->setXfIndex(m_xf_formatsList.size()); const_cast<Format *>(&format)->setXfIndex(m_xf_formatsList.size());
m_xf_formatsList.append(format); m_xf_formatsList.append(format);
m_xf_formatsHash[format->formatKey()] = format; m_xf_formatsHash[format.formatKey()] = format;
} else { } else {
format->setXfIndex(m_xf_formatsHash[format->formatKey()]->xfIndex()); const_cast<Format *>(&format)->setXfIndex(m_xf_formatsHash[format.formatKey()].xfIndex());
} }
} }
} }
@ -288,21 +278,21 @@ void Styles::writeFonts(XmlStreamWriter &writer)
writer.writeStartElement(QStringLiteral("fonts")); writer.writeStartElement(QStringLiteral("fonts"));
writer.writeAttribute(QStringLiteral("count"), QString::number(m_fontsList.count())); writer.writeAttribute(QStringLiteral("count"), QString::number(m_fontsList.count()));
for (int i=0; i<m_fontsList.size(); ++i) { for (int i=0; i<m_fontsList.size(); ++i) {
Format *format = m_fontsList[i]; const Format &format = m_fontsList[i];
writer.writeStartElement(QStringLiteral("font")); writer.writeStartElement(QStringLiteral("font"));
if (format->fontBold()) if (format.fontBold())
writer.writeEmptyElement(QStringLiteral("b")); writer.writeEmptyElement(QStringLiteral("b"));
if (format->fontItalic()) if (format.fontItalic())
writer.writeEmptyElement(QStringLiteral("i")); writer.writeEmptyElement(QStringLiteral("i"));
if (format->fontStrikeOut()) if (format.fontStrikeOut())
writer.writeEmptyElement(QStringLiteral("strike")); writer.writeEmptyElement(QStringLiteral("strike"));
if (format->fontOutline()) if (format.fontOutline())
writer.writeEmptyElement(QStringLiteral("outline")); writer.writeEmptyElement(QStringLiteral("outline"));
if (format->boolProperty(FormatPrivate::P_Font_Shadow)) if (format.boolProperty(FormatPrivate::P_Font_Shadow))
writer.writeEmptyElement(QStringLiteral("shadow")); writer.writeEmptyElement(QStringLiteral("shadow"));
if (format->hasProperty(FormatPrivate::P_Font_Underline)) { if (format.hasProperty(FormatPrivate::P_Font_Underline)) {
Format::FontUnderline u = format->fontUnderline(); Format::FontUnderline u = format.fontUnderline();
if (u != Format::FontUnderlineNone) { if (u != Format::FontUnderlineNone) {
writer.writeEmptyElement(QStringLiteral("u")); writer.writeEmptyElement(QStringLiteral("u"));
if (u== Format::FontUnderlineDouble) if (u== Format::FontUnderlineDouble)
@ -313,8 +303,8 @@ void Styles::writeFonts(XmlStreamWriter &writer)
writer.writeAttribute(QStringLiteral("val"), QStringLiteral("doubleAccounting")); writer.writeAttribute(QStringLiteral("val"), QStringLiteral("doubleAccounting"));
} }
} }
if (format->hasProperty(FormatPrivate::P_Font_Script)) { if (format.hasProperty(FormatPrivate::P_Font_Script)) {
Format::FontScript s = format->fontScript(); Format::FontScript s = format.fontScript();
if (s != Format::FontScriptNormal) { if (s != Format::FontScriptNormal) {
writer.writeEmptyElement(QStringLiteral("vertAlign")); writer.writeEmptyElement(QStringLiteral("vertAlign"));
if (s == Format::FontScriptSuper) 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.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")); writer.writeEmptyElement(QStringLiteral("color"));
QString color = format->fontColor().name(); QString color = format.fontColor().name();
writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+color.mid(1));//remove # 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")); 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]); writer.writeAttribute(QStringLiteral("theme"), themes[0]);
if (!themes[1].isEmpty()) if (!themes[1].isEmpty())
writer.writeAttribute(QStringLiteral("tint"), themes[1]); writer.writeAttribute(QStringLiteral("tint"), themes[1]);
} }
if (!format->fontName().isEmpty()) { if (!format.fontName().isEmpty()) {
writer.writeEmptyElement(QStringLiteral("name")); 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.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.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.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.writeEmptyElement(QStringLiteral("name"));
// writer.writeAttribute(QStringLiteral("val"), format->fontName()); // writer.writeAttribute(QStringLiteral("val"), format.fontName());
// writer.writeEmptyElement(QStringLiteral("family")); // writer.writeEmptyElement(QStringLiteral("family"));
// writer.writeAttribute(QStringLiteral("val"), QString::number(format->fontFamily())); // writer.writeAttribute(QStringLiteral("val"), QString::number(format.fontFamily()));
// if (format->fontName() == QLatin1String("Calibri")) { // if (format.fontName() == QLatin1String("Calibri")) {
// writer.writeEmptyElement(QStringLiteral("scheme")); // writer.writeEmptyElement(QStringLiteral("scheme"));
// writer.writeAttribute(QStringLiteral("val"), format->fontScheme()); // writer.writeAttribute(QStringLiteral("val"), format.fontScheme());
// } // }
// } // }
writer.writeEndElement(); //font writer.writeEndElement(); //font
@ -379,14 +369,13 @@ void Styles::writeFills(XmlStreamWriter &writer)
writer.writeStartElement(QStringLiteral("fills")); writer.writeStartElement(QStringLiteral("fills"));
writer.writeAttribute(QStringLiteral("count"), QString::number(m_fillsList.size())); writer.writeAttribute(QStringLiteral("count"), QString::number(m_fillsList.size()));
for (int i=0; i<m_fillsList.size(); ++i) { for (int i=0; i<m_fillsList.size(); ++i)
Format *fill = m_fillsList[i]; writeFill(writer, m_fillsList[i]);
writeFill(writer, fill);
}
writer.writeEndElement(); //fills writer.writeEndElement(); //fills
} }
void Styles::writeFill(XmlStreamWriter &writer, Format *fill) void Styles::writeFill(XmlStreamWriter &writer, const Format &fill)
{ {
static QMap<int, QString> patternStrings; static QMap<int, QString> patternStrings;
if (patternStrings.isEmpty()) { if (patternStrings.isEmpty()) {
@ -413,24 +402,24 @@ void Styles::writeFill(XmlStreamWriter &writer, Format *fill)
writer.writeStartElement(QStringLiteral("fill")); writer.writeStartElement(QStringLiteral("fill"));
writer.writeStartElement(QStringLiteral("patternFill")); 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 // For a solid fill, Excel reverses the role of foreground and background colours
if (fill->patternForegroundColor().isValid()) { if (fill.patternForegroundColor().isValid()) {
writer.writeEmptyElement(fill->fillPattern() == Format::PatternSolid ? QStringLiteral("bgColor") : QStringLiteral("fgColor")); writer.writeEmptyElement(fill.fillPattern() == Format::PatternSolid ? QStringLiteral("bgColor") : QStringLiteral("fgColor"));
writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill->patternForegroundColor().name().mid(1)); writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill.patternForegroundColor().name().mid(1));
} else if (!fill->stringProperty(FormatPrivate::P_Fill_FgThemeColor).isEmpty()) { } else if (!fill.stringProperty(FormatPrivate::P_Fill_FgThemeColor).isEmpty()) {
writer.writeEmptyElement(QStringLiteral("fgColor")); 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]); writer.writeAttribute(QStringLiteral("theme"), themes[0]);
if (!themes[1].isEmpty()) if (!themes[1].isEmpty())
writer.writeAttribute(QStringLiteral("tint"), themes[1]); writer.writeAttribute(QStringLiteral("tint"), themes[1]);
} }
if (fill->patternBackgroundColor().isValid()) { if (fill.patternBackgroundColor().isValid()) {
writer.writeEmptyElement(fill->fillPattern() == Format::PatternSolid ? QStringLiteral("fgColor") : QStringLiteral("bgColor")); writer.writeEmptyElement(fill.fillPattern() == Format::PatternSolid ? QStringLiteral("fgColor") : QStringLiteral("bgColor"));
writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill->patternBackgroundColor().name().mid(1)); writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill.patternBackgroundColor().name().mid(1));
} else if (!fill->stringProperty(FormatPrivate::P_Fill_BgThemeColor).isEmpty()) { } else if (!fill.stringProperty(FormatPrivate::P_Fill_BgThemeColor).isEmpty()) {
writer.writeEmptyElement(QStringLiteral("bgColor")); 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]); writer.writeAttribute(QStringLiteral("theme"), themes[0]);
if (!themes[1].isEmpty()) if (!themes[1].isEmpty())
writer.writeAttribute(QStringLiteral("tint"), themes[1]); writer.writeAttribute(QStringLiteral("tint"), themes[1]);
@ -445,11 +434,11 @@ void Styles::writeBorders(XmlStreamWriter &writer)
writer.writeStartElement(QStringLiteral("borders")); writer.writeStartElement(QStringLiteral("borders"));
writer.writeAttribute(QStringLiteral("count"), QString::number(m_bordersList.count())); writer.writeAttribute(QStringLiteral("count"), QString::number(m_bordersList.count()));
for (int i=0; i<m_bordersList.size(); ++i) { for (int i=0; i<m_bordersList.size(); ++i) {
Format *border = m_bordersList[i]; const Format &border = m_bordersList[i];
writer.writeStartElement(QStringLiteral("border")); writer.writeStartElement(QStringLiteral("border"));
if (border->hasProperty(FormatPrivate::P_Border_DiagonalType)) { if (border.hasProperty(FormatPrivate::P_Border_DiagonalType)) {
Format::DiagonalBorderType t = border->diagonalBorderType(); Format::DiagonalBorderType t = border.diagonalBorderType();
if (t == Format::DiagonalBorderUp) { if (t == Format::DiagonalBorderUp) {
writer.writeAttribute(QStringLiteral("diagonalUp"), QStringLiteral("1")); writer.writeAttribute(QStringLiteral("diagonalUp"), QStringLiteral("1"));
} else if (t == Format::DiagonalBorderDown) { } else if (t == Format::DiagonalBorderDown) {
@ -459,18 +448,18 @@ void Styles::writeBorders(XmlStreamWriter &writer)
writer.writeAttribute(QStringLiteral("diagonalDown"), QStringLiteral("1")); writer.writeAttribute(QStringLiteral("diagonalDown"), QStringLiteral("1"));
} }
} }
if (border->hasProperty(FormatPrivate::P_Border_LeftStyle)) if (border.hasProperty(FormatPrivate::P_Border_LeftStyle))
writeSubBorder(writer, QStringLiteral("left"), border->leftBorderStyle(), border->leftBorderColor(), border->stringProperty(FormatPrivate::P_Border_ThemeLeftColor)); writeSubBorder(writer, QStringLiteral("left"), border.leftBorderStyle(), border.leftBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeLeftColor));
if (border->hasProperty(FormatPrivate::P_Border_RightStyle)) if (border.hasProperty(FormatPrivate::P_Border_RightStyle))
writeSubBorder(writer, QStringLiteral("right"), border->rightBorderStyle(), border->rightBorderColor(), border->stringProperty(FormatPrivate::P_Border_ThemeRightColor)); writeSubBorder(writer, QStringLiteral("right"), border.rightBorderStyle(), border.rightBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeRightColor));
if (border->hasProperty(FormatPrivate::P_Border_TopStyle)) if (border.hasProperty(FormatPrivate::P_Border_TopStyle))
writeSubBorder(writer, QStringLiteral("top"), border->topBorderStyle(), border->topBorderColor(), border->stringProperty(FormatPrivate::P_Border_ThemeTopColor)); writeSubBorder(writer, QStringLiteral("top"), border.topBorderStyle(), border.topBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeTopColor));
if (border->hasProperty(FormatPrivate::P_Border_BottomStyle)) if (border.hasProperty(FormatPrivate::P_Border_BottomStyle))
writeSubBorder(writer, QStringLiteral("bottom"), border->bottomBorderStyle(), border->bottomBorderColor(), border->stringProperty(FormatPrivate::P_Border_ThemeBottomColor)); writeSubBorder(writer, QStringLiteral("bottom"), border.bottomBorderStyle(), border.bottomBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeBottomColor));
// if (!format->isDxfFormat()) { // if (!format.isDxfFormat()) {
if (border->hasProperty(FormatPrivate::P_Border_DiagonalStyle)) if (border.hasProperty(FormatPrivate::P_Border_DiagonalStyle))
writeSubBorder(writer, QStringLiteral("diagonal"), border->diagonalBorderStyle(), border->diagonalBorderColor(), border->stringProperty(FormatPrivate::P_Border_ThemeDiagonalColor)); writeSubBorder(writer, QStringLiteral("diagonal"), border.diagonalBorderStyle(), border.diagonalBorderColor(), border.stringProperty(FormatPrivate::P_Border_ThemeDiagonalColor));
// } // }
writer.writeEndElement();//border writer.writeEndElement();//border
} }
@ -522,11 +511,11 @@ void Styles::writeCellXfs(XmlStreamWriter &writer)
{ {
writer.writeStartElement(QStringLiteral("cellXfs")); writer.writeStartElement(QStringLiteral("cellXfs"));
writer.writeAttribute(QStringLiteral("count"), QString::number(m_xf_formatsList.size())); writer.writeAttribute(QStringLiteral("count"), QString::number(m_xf_formatsList.size()));
foreach (Format *format, m_xf_formatsList) { foreach (const Format &format, m_xf_formatsList) {
int num_fmt_id = format->numberFormatIndex(); int num_fmt_id = format.numberFormatIndex();
int font_id = format->fontIndex(); int font_id = format.fontIndex();
int fill_id = format->fillIndex(); int fill_id = format.fillIndex();
int border_id = format->borderIndex(); int border_id = format.borderIndex();
int xf_id = 0; int xf_id = 0;
writer.writeStartElement(QStringLiteral("xf")); writer.writeStartElement(QStringLiteral("xf"));
writer.writeAttribute(QStringLiteral("numFmtId"), QString::number(num_fmt_id)); 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("fillId"), QString::number(fill_id));
writer.writeAttribute(QStringLiteral("borderId"), QString::number(border_id)); writer.writeAttribute(QStringLiteral("borderId"), QString::number(border_id));
writer.writeAttribute(QStringLiteral("xfId"), QString::number(xf_id)); writer.writeAttribute(QStringLiteral("xfId"), QString::number(xf_id));
if (format->numberFormatIndex() > 0) if (format.numberFormatIndex() > 0)
writer.writeAttribute(QStringLiteral("applyNumberFormat"), QStringLiteral("1")); writer.writeAttribute(QStringLiteral("applyNumberFormat"), QStringLiteral("1"));
if (format->fontIndex() > 0) if (format.fontIndex() > 0)
writer.writeAttribute(QStringLiteral("applyFont"), QStringLiteral("1")); writer.writeAttribute(QStringLiteral("applyFont"), QStringLiteral("1"));
if (format->borderIndex() > 0) if (format.borderIndex() > 0)
writer.writeAttribute(QStringLiteral("applyBorder"), QStringLiteral("1")); writer.writeAttribute(QStringLiteral("applyBorder"), QStringLiteral("1"));
if (format->fillIndex() > 0) if (format.fillIndex() > 0)
writer.writeAttribute(QStringLiteral("applyFill"), QStringLiteral("1")); writer.writeAttribute(QStringLiteral("applyFill"), QStringLiteral("1"));
if (format->hasAlignmentData()) if (format.hasAlignmentData())
writer.writeAttribute(QStringLiteral("applyAlignment"), QStringLiteral("1")); writer.writeAttribute(QStringLiteral("applyAlignment"), QStringLiteral("1"));
if (format->hasAlignmentData()) { if (format.hasAlignmentData()) {
writer.writeEmptyElement(QStringLiteral("alignment")); writer.writeEmptyElement(QStringLiteral("alignment"));
if (format->hasProperty(FormatPrivate::P_Alignment_AlignH)) { if (format.hasProperty(FormatPrivate::P_Alignment_AlignH)) {
switch (format->horizontalAlignment()) { switch (format.horizontalAlignment()) {
case Format::AlignLeft: case Format::AlignLeft:
writer.writeAttribute(QStringLiteral("horizontal"), QStringLiteral("left")); writer.writeAttribute(QStringLiteral("horizontal"), QStringLiteral("left"));
break; break;
@ -575,8 +564,8 @@ void Styles::writeCellXfs(XmlStreamWriter &writer)
} }
} }
if (format->hasProperty(FormatPrivate::P_Alignment_AlignV)) { if (format.hasProperty(FormatPrivate::P_Alignment_AlignV)) {
switch (format->verticalAlignment()) { switch (format.verticalAlignment()) {
case Format::AlignTop: case Format::AlignTop:
writer.writeAttribute(QStringLiteral("vertical"), QStringLiteral("top")); writer.writeAttribute(QStringLiteral("vertical"), QStringLiteral("top"));
break; break;
@ -593,14 +582,14 @@ void Styles::writeCellXfs(XmlStreamWriter &writer)
break; break;
} }
} }
if (format->hasProperty(FormatPrivate::P_Alignment_Indent)) if (format.hasProperty(FormatPrivate::P_Alignment_Indent))
writer.writeAttribute(QStringLiteral("indent"), QString::number(format->indent())); writer.writeAttribute(QStringLiteral("indent"), QString::number(format.indent()));
if (format->hasProperty(FormatPrivate::P_Alignment_Wrap) && format->textWrap()) if (format.hasProperty(FormatPrivate::P_Alignment_Wrap) && format.textWrap())
writer.writeAttribute(QStringLiteral("wrapText"), QStringLiteral("1")); 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")); writer.writeAttribute(QStringLiteral("shrinkToFit"), QStringLiteral("1"));
if (format->hasProperty(FormatPrivate::P_Alignment_Rotation)) if (format.hasProperty(FormatPrivate::P_Alignment_Rotation))
writer.writeAttribute(QStringLiteral("textRotation"), QString::number(format->rotation())); writer.writeAttribute(QStringLiteral("textRotation"), QString::number(format.rotation()));
} }
writer.writeEndElement();//xf writer.writeEndElement();//xf
@ -612,7 +601,7 @@ void Styles::writeDxfs(XmlStreamWriter &writer)
{ {
writer.writeStartElement(QStringLiteral("dxfs")); writer.writeStartElement(QStringLiteral("dxfs"));
writer.writeAttribute(QStringLiteral("count"), QString::number(m_dxf_formatsList.size())); 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")); writer.writeStartElement(QStringLiteral("dxf"));
Q_UNUSED(format) Q_UNUSED(format)
writer.writeEndElement();//dxf writer.writeEndElement();//dxf
@ -653,63 +642,63 @@ bool Styles::readFonts(XmlStreamReader &reader)
reader.readNextStartElement(); reader.readNextStartElement();
if (reader.name() != QLatin1String("font")) if (reader.name() != QLatin1String("font"))
return false; return false;
Format *format = createFormat(); Format format;
while((reader.readNextStartElement(),true)) { //read until font endelement. while((reader.readNextStartElement(),true)) { //read until font endelement.
if (reader.tokenType() == QXmlStreamReader::StartElement) { if (reader.tokenType() == QXmlStreamReader::StartElement) {
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
if (reader.name() == QLatin1String("name")) { 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")) { } 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")) { } 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")) { } else if (reader.name() == QLatin1String("b")) {
format->setFontBold(true); format.setFontBold(true);
} else if (reader.name() == QLatin1String("i")) { } else if (reader.name() == QLatin1String("i")) {
format->setFontItalic(true); format.setFontItalic(true);
} else if (reader.name() == QLatin1String("strike")) { } else if (reader.name() == QLatin1String("strike")) {
format->setFontStrikeOut(true); format.setFontStrikeOut(true);
} else if (reader.name() == QLatin1String("outline")) { } else if (reader.name() == QLatin1String("outline")) {
format->setFontOutline(true); format.setFontOutline(true);
} else if (reader.name() == QLatin1String("shadow")) { } 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")) { } 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")) { } 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")) { } else if (reader.name() == QLatin1String("color")) {
if (attributes.hasAttribute(QLatin1String("rgb"))) { if (attributes.hasAttribute(QLatin1String("rgb"))) {
QString colorString = attributes.value(QLatin1String("rgb")).toString(); QString colorString = attributes.value(QLatin1String("rgb")).toString();
format->setFontColor(fromARGBString(colorString)); format.setFontColor(fromARGBString(colorString));
} else if (attributes.hasAttribute(QLatin1String("indexed"))) { } else if (attributes.hasAttribute(QLatin1String("indexed"))) {
QColor color = getColorByIndex(attributes.value(QLatin1String("indexed")).toString().toInt()); QColor color = getColorByIndex(attributes.value(QLatin1String("indexed")).toString().toInt());
format->setFontColor(color); format.setFontColor(color);
} else if (attributes.hasAttribute(QLatin1String("theme"))) { } else if (attributes.hasAttribute(QLatin1String("theme"))) {
QString theme = attributes.value(QLatin1String("theme")).toString(); QString theme = attributes.value(QLatin1String("theme")).toString();
QString tint = attributes.value(QLatin1String("tint")).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")) { } else if (reader.name() == QLatin1String("sz")) {
int sz = attributes.value(QLatin1String("val")).toString().toInt(); int sz = attributes.value(QLatin1String("val")).toString().toInt();
format->setFontSize(sz); format.setFontSize(sz);
} else if (reader.name() == QLatin1String("u")) { } else if (reader.name() == QLatin1String("u")) {
QString value = attributes.value(QLatin1String("val")).toString(); QString value = attributes.value(QLatin1String("val")).toString();
if (value == QLatin1String("double")) if (value == QLatin1String("double"))
format->setFontUnderline(Format::FontUnderlineDouble); format.setFontUnderline(Format::FontUnderlineDouble);
else if (value == QLatin1String("doubleAccounting")) else if (value == QLatin1String("doubleAccounting"))
format->setFontUnderline(Format::FontUnderlineDoubleAccounting); format.setFontUnderline(Format::FontUnderlineDoubleAccounting);
else if (value == QLatin1String("singleAccounting")) else if (value == QLatin1String("singleAccounting"))
format->setFontUnderline(Format::FontUnderlineSingleAccounting); format.setFontUnderline(Format::FontUnderlineSingleAccounting);
else else
format->setFontUnderline(Format::FontUnderlineSingle); format.setFontUnderline(Format::FontUnderlineSingle);
} else if (reader.name() == QLatin1String("vertAlign")) { } else if (reader.name() == QLatin1String("vertAlign")) {
QString value = attributes.value(QLatin1String("val")).toString(); QString value = attributes.value(QLatin1String("val")).toString();
if (value == QLatin1String("superscript")) if (value == QLatin1String("superscript"))
format->setFontScript(Format::FontScriptSuper); format.setFontScript(Format::FontScriptSuper);
else if (value == QLatin1String("subscript")) else if (value == QLatin1String("subscript"))
format->setFontScript(Format::FontScriptSub); format.setFontScript(Format::FontScriptSub);
} else if (reader.name() == QLatin1String("scheme")) { } 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; break;
} }
m_fontsList.append(format); m_fontsList.append(format);
m_fontsHash.insert(format->fontKey(), format); m_fontsHash.insert(format.fontKey(), format);
format->setFontIndex(m_fontsList.size()-1); format.setFontIndex(m_fontsList.size()-1);
} }
return true; return true;
} }
@ -765,14 +754,14 @@ bool Styles::readFill(XmlStreamReader &reader)
patternValues[QStringLiteral("lightGrid")] = Format::PatternLightGrid; patternValues[QStringLiteral("lightGrid")] = Format::PatternLightGrid;
} }
Format *fill = createFormat(); Format fill;
while((reader.readNextStartElement(), true)) { //read until fill endelement while((reader.readNextStartElement(), true)) { //read until fill endelement
if (reader.tokenType() == QXmlStreamReader::StartElement) { if (reader.tokenType() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("patternFill")) { if (reader.name() == QLatin1String("patternFill")) {
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
if (attributes.hasAttribute(QLatin1String("patternType"))) { if (attributes.hasAttribute(QLatin1String("patternType"))) {
QString pattern = attributes.value(QLatin1String("patternType")).toString(); 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")) { } else if (reader.name() == QLatin1String("fgColor")) {
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
@ -784,12 +773,12 @@ bool Styles::readFill(XmlStreamReader &reader)
} else if (attributes.hasAttribute(QLatin1String("theme"))) { } else if (attributes.hasAttribute(QLatin1String("theme"))) {
QString theme = attributes.value(QLatin1String("theme")).toString(); QString theme = attributes.value(QLatin1String("theme")).toString();
QString tint = attributes.value(QLatin1String("tint")).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) if (fill.fillPattern() == Format::PatternSolid)
fill->setPatternBackgroundColor(c); fill.setPatternBackgroundColor(c);
else else
fill->setPatternForegroundColor(c); fill.setPatternForegroundColor(c);
} else if (reader.name() == QLatin1String("bgColor")) { } else if (reader.name() == QLatin1String("bgColor")) {
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
QColor c; QColor c;
@ -800,12 +789,12 @@ bool Styles::readFill(XmlStreamReader &reader)
} else if (attributes.hasAttribute(QLatin1String("theme"))) { } else if (attributes.hasAttribute(QLatin1String("theme"))) {
QString theme = attributes.value(QLatin1String("theme")).toString(); QString theme = attributes.value(QLatin1String("theme")).toString();
QString tint = attributes.value(QLatin1String("tint")).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) if (fill.fillPattern() == Format::PatternSolid)
fill->setPatternForegroundColor(c); fill.setPatternForegroundColor(c);
else else
fill->setPatternBackgroundColor(c); fill.setPatternBackgroundColor(c);
} }
} }
@ -814,8 +803,8 @@ bool Styles::readFill(XmlStreamReader &reader)
} }
m_fillsList.append(fill); m_fillsList.append(fill);
m_fillsHash.insert(fill->fillKey(), fill); m_fillsHash.insert(fill.fillKey(), fill);
fill->setFillIndex(m_fillsList.size()-1);//first call key(), then setIndex() fill.setFillIndex(m_fillsList.size()-1);//first call key(), then setIndex()
return true; return true;
} }
@ -838,17 +827,17 @@ bool Styles::readBorders(XmlStreamReader &reader)
bool Styles::readBorder(XmlStreamReader &reader) bool Styles::readBorder(XmlStreamReader &reader)
{ {
Q_ASSERT(reader.name() == QLatin1String("border")); Q_ASSERT(reader.name() == QLatin1String("border"));
Format *border = createFormat(); Format border;
QXmlStreamAttributes attributes = reader.attributes(); QXmlStreamAttributes attributes = reader.attributes();
bool isUp = attributes.hasAttribute(QLatin1String("diagonalUp")); bool isUp = attributes.hasAttribute(QLatin1String("diagonalUp"));
bool isDown = attributes.hasAttribute(QLatin1String("diagonalUp")); bool isDown = attributes.hasAttribute(QLatin1String("diagonalUp"));
if (isUp && isDown) if (isUp && isDown)
border->setDiagonalBorderType(Format::DiagnoalBorderBoth); border.setDiagonalBorderType(Format::DiagnoalBorderBoth);
else if (isUp) else if (isUp)
border->setDiagonalBorderType(Format::DiagonalBorderUp); border.setDiagonalBorderType(Format::DiagonalBorderUp);
else if (isDown) else if (isDown)
border->setDiagonalBorderType(Format::DiagonalBorderDown); border.setDiagonalBorderType(Format::DiagonalBorderDown);
while((reader.readNextStartElement(), true)) { //read until border endelement while((reader.readNextStartElement(), true)) { //read until border endelement
if (reader.tokenType() == QXmlStreamReader::StartElement) { if (reader.tokenType() == QXmlStreamReader::StartElement) {
@ -861,35 +850,35 @@ bool Styles::readBorder(XmlStreamReader &reader)
readSubBorder(reader, reader.name().toString(), style, color, themeColor); readSubBorder(reader, reader.name().toString(), style, color, themeColor);
if (reader.name() == QLatin1String("left")) { if (reader.name() == QLatin1String("left")) {
border->setLeftBorderStyle(style); border.setLeftBorderStyle(style);
if (color.isValid()) if (color.isValid())
border->setLeftBorderColor(color); border.setLeftBorderColor(color);
else if (!themeColor.isEmpty()) else if (!themeColor.isEmpty())
border->setProperty(FormatPrivate::P_Border_ThemeLeftColor, themeColor); border.setProperty(FormatPrivate::P_Border_ThemeLeftColor, themeColor);
} else if (reader.name() == QLatin1String("right")) { } else if (reader.name() == QLatin1String("right")) {
border->setRightBorderStyle(style); border.setRightBorderStyle(style);
if (color.isValid()) if (color.isValid())
border->setRightBorderColor(color); border.setRightBorderColor(color);
else if (!themeColor.isEmpty()) else if (!themeColor.isEmpty())
border->setProperty(FormatPrivate::P_Border_ThemeRightColor, themeColor); border.setProperty(FormatPrivate::P_Border_ThemeRightColor, themeColor);
} else if (reader.name() == QLatin1String("top")) { } else if (reader.name() == QLatin1String("top")) {
border->setTopBorderStyle(style); border.setTopBorderStyle(style);
if (color.isValid()) if (color.isValid())
border->setTopBorderColor(color); border.setTopBorderColor(color);
else if (!themeColor.isEmpty()) else if (!themeColor.isEmpty())
border->setProperty(FormatPrivate::P_Border_ThemeTopColor, themeColor); border.setProperty(FormatPrivate::P_Border_ThemeTopColor, themeColor);
} else if (reader.name() == QLatin1String("bottom")) { } else if (reader.name() == QLatin1String("bottom")) {
border->setBottomBorderStyle(style); border.setBottomBorderStyle(style);
if (color.isValid()) if (color.isValid())
border->setBottomBorderColor(color); border.setBottomBorderColor(color);
else if (!themeColor.isEmpty()) else if (!themeColor.isEmpty())
border->setProperty(FormatPrivate::P_Border_ThemeBottomColor, themeColor); border.setProperty(FormatPrivate::P_Border_ThemeBottomColor, themeColor);
} else if (reader.name() == QLatin1String("diagonal")) { } else if (reader.name() == QLatin1String("diagonal")) {
border->setDiagonalBorderStyle(style); border.setDiagonalBorderStyle(style);
if (color.isValid()) if (color.isValid())
border->setDiagonalBorderColor(color); border.setDiagonalBorderColor(color);
else if (!themeColor.isEmpty()) 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_bordersList.append(border);
m_bordersHash.insert(border->borderKey(), border); m_bordersHash.insert(border.borderKey(), border);
border->setBorderIndex(m_bordersList.size()-1);//first call key(), then setIndex() border.setBorderIndex(m_bordersList.size()-1);//first call key(), then setIndex()
return true; return true;
} }
@ -970,7 +959,7 @@ bool Styles::readCellXfs(XmlStreamReader &reader)
reader.readNextStartElement(); reader.readNextStartElement();
if (reader.name() != QLatin1String("xf")) if (reader.name() != QLatin1String("xf"))
return false; return false;
Format *format = createFormat(); Format format;
QXmlStreamAttributes xfAttrs = reader.attributes(); QXmlStreamAttributes xfAttrs = reader.attributes();
// qDebug()<<reader.name()<<reader.tokenString()<<" ........."; // qDebug()<<reader.name()<<reader.tokenString()<<" .........";
@ -980,9 +969,9 @@ bool Styles::readCellXfs(XmlStreamReader &reader)
if (xfAttrs.hasAttribute(QLatin1String("applyNumberFormat"))) { if (xfAttrs.hasAttribute(QLatin1String("applyNumberFormat"))) {
int numFmtIndex = xfAttrs.value(QLatin1String("numFmtId")).toString().toInt(); int numFmtIndex = xfAttrs.value(QLatin1String("numFmtId")).toString().toInt();
if (!m_customNumFmtIdMap.contains(numFmtIndex)) if (!m_customNumFmtIdMap.contains(numFmtIndex))
format->setNumberFormatIndex(numFmtIndex); format.setNumberFormatIndex(numFmtIndex);
else else
format->setNumberFormat(numFmtIndex, m_customNumFmtIdMap[numFmtIndex]->formatString); format.setNumberFormat(numFmtIndex, m_customNumFmtIdMap[numFmtIndex]->formatString);
} }
if (xfAttrs.hasAttribute(QLatin1String("applyFont"))) { if (xfAttrs.hasAttribute(QLatin1String("applyFont"))) {
@ -990,10 +979,10 @@ bool Styles::readCellXfs(XmlStreamReader &reader)
if (fontIndex >= m_fontsList.size()) { if (fontIndex >= m_fontsList.size()) {
qDebug("Error read styles.xml, cellXfs fontId"); qDebug("Error read styles.xml, cellXfs fontId");
} else { } else {
Format *fontFormat = m_fontsList[fontIndex]; Format fontFormat = m_fontsList[fontIndex];
for (int i=FormatPrivate::P_Font_STARTID; i<FormatPrivate::P_Font_ENDID; ++i) { for (int i=FormatPrivate::P_Font_STARTID; i<FormatPrivate::P_Font_ENDID; ++i) {
if (fontFormat->hasProperty(i)) if (fontFormat.hasProperty(i))
format->setProperty(i, fontFormat->property(i)); format.setProperty(i, fontFormat.property(i));
} }
} }
} }
@ -1003,10 +992,10 @@ bool Styles::readCellXfs(XmlStreamReader &reader)
if (id >= m_fillsList.size()) { if (id >= m_fillsList.size()) {
qDebug("Error read styles.xml, cellXfs fillId"); qDebug("Error read styles.xml, cellXfs fillId");
} else { } else {
Format *fillFormat = m_fillsList[id]; Format fillFormat = m_fillsList[id];
for (int i=FormatPrivate::P_Fill_STARTID; i<FormatPrivate::P_Fill_ENDID; ++i) { for (int i=FormatPrivate::P_Fill_STARTID; i<FormatPrivate::P_Fill_ENDID; ++i) {
if (fillFormat->hasProperty(i)) if (fillFormat.hasProperty(i))
format->setProperty(i, fillFormat->property(i)); format.setProperty(i, fillFormat.property(i));
} }
} }
} }
@ -1016,10 +1005,10 @@ bool Styles::readCellXfs(XmlStreamReader &reader)
if (id >= m_bordersList.size()) { if (id >= m_bordersList.size()) {
qDebug("Error read styles.xml, cellXfs borderId"); qDebug("Error read styles.xml, cellXfs borderId");
} else { } else {
Format *borderFormat = m_bordersList[id]; Format borderFormat = m_bordersList[id];
for (int i=FormatPrivate::P_Border_STARTID; i<FormatPrivate::P_Border_ENDID; ++i) { for (int i=FormatPrivate::P_Border_STARTID; i<FormatPrivate::P_Border_ENDID; ++i) {
if (borderFormat->hasProperty(i)) if (borderFormat.hasProperty(i))
format->setProperty(i, borderFormat->property(i)); format.setProperty(i, borderFormat.property(i));
} }
} }
} }
@ -1041,7 +1030,7 @@ bool Styles::readCellXfs(XmlStreamReader &reader)
} }
QString str = alignAttrs.value(QLatin1String("horizontal")).toString(); QString str = alignAttrs.value(QLatin1String("horizontal")).toString();
if (alignStringMap.contains(str)) if (alignStringMap.contains(str))
format->setHorizontalAlignment(alignStringMap[str]); format.setHorizontalAlignment(alignStringMap[str]);
} }
if (alignAttrs.hasAttribute(QLatin1String("vertical"))) { if (alignAttrs.hasAttribute(QLatin1String("vertical"))) {
@ -1054,24 +1043,24 @@ bool Styles::readCellXfs(XmlStreamReader &reader)
} }
QString str = alignAttrs.value(QLatin1String("vertical")).toString(); QString str = alignAttrs.value(QLatin1String("vertical")).toString();
if (alignStringMap.contains(str)) if (alignStringMap.contains(str))
format->setVerticalAlignment(alignStringMap[str]); format.setVerticalAlignment(alignStringMap[str]);
} }
if (alignAttrs.hasAttribute(QLatin1String("indent"))) { if (alignAttrs.hasAttribute(QLatin1String("indent"))) {
int indent = alignAttrs.value(QLatin1String("indent")).toString().toInt(); int indent = alignAttrs.value(QLatin1String("indent")).toString().toInt();
format->setIndent(indent); format.setIndent(indent);
} }
if (alignAttrs.hasAttribute(QLatin1String("textRotation"))) { if (alignAttrs.hasAttribute(QLatin1String("textRotation"))) {
int rotation = alignAttrs.value(QLatin1String("textRotation")).toString().toInt(); int rotation = alignAttrs.value(QLatin1String("textRotation")).toString().toInt();
format->setRotation(rotation); format.setRotation(rotation);
} }
if (alignAttrs.hasAttribute(QLatin1String("wrapText"))) if (alignAttrs.hasAttribute(QLatin1String("wrapText")))
format->setTextWarp(true); format.setTextWarp(true);
if (alignAttrs.hasAttribute(QLatin1String("shrinkToFit"))) if (alignAttrs.hasAttribute(QLatin1String("shrinkToFit")))
format->setShrinkToFit(true); format.setShrinkToFit(true);
} }
} }

29
src/xlsx/xlsxstyles_p.h

@ -56,9 +56,8 @@ class XLSX_AUTOTEST_EXPORT Styles
public: public:
Styles(bool createEmpty=false); Styles(bool createEmpty=false);
~Styles(); ~Styles();
Format *createFormat(); void addFormat(const Format &format, bool force=false);
void addFormat(Format *format, bool force=false); Format xfFormat(int idx) const;
Format *xfFormat(int idx) const;
QByteArray saveToXmlData(); QByteArray saveToXmlData();
void saveToXmlFile(QIODevice *device); void saveToXmlFile(QIODevice *device);
@ -72,7 +71,7 @@ private:
void writeNumFmts(XmlStreamWriter &writer); void writeNumFmts(XmlStreamWriter &writer);
void writeFonts(XmlStreamWriter &writer); void writeFonts(XmlStreamWriter &writer);
void writeFills(XmlStreamWriter &writer); void writeFills(XmlStreamWriter &writer);
void writeFill(XmlStreamWriter &writer, Format *fill); void writeFill(XmlStreamWriter &writer, const Format &fill);
void writeBorders(XmlStreamWriter &writer); void writeBorders(XmlStreamWriter &writer);
void writeSubBorder(XmlStreamWriter &writer, const QString &type, int style, const QColor &color, const QString &themeColor); void writeSubBorder(XmlStreamWriter &writer, const QString &type, int style, const QColor &color, const QString &themeColor);
void writeCellXfs(XmlStreamWriter &writer); void writeCellXfs(XmlStreamWriter &writer);
@ -95,22 +94,20 @@ private:
QMap<int, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtIdMap; QMap<int, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtIdMap;
QHash<QString, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtsHash; QHash<QString, QSharedPointer<XlsxFormatNumberData> > m_customNumFmtsHash;
int m_nextCustomNumFmtId; int m_nextCustomNumFmtId;
QList<Format *> m_fontsList; QList<Format> m_fontsList;
QList<Format *> m_fillsList; QList<Format> m_fillsList;
QList<Format *> m_bordersList; QList<Format> m_bordersList;
QHash<QByteArray, Format *> m_fontsHash; QHash<QByteArray, Format> m_fontsHash;
QHash<QByteArray, Format *> m_fillsHash; QHash<QByteArray, Format> m_fillsHash;
QHash<QByteArray, Format *> m_bordersHash; QHash<QByteArray, Format> m_bordersHash;
QVector<QColor> m_indexedColors; QVector<QColor> m_indexedColors;
QList<QSharedPointer<Format> > m_createdFormatsList; //All created formats QList<Format> m_xf_formatsList;
QHash<QByteArray, Format> m_xf_formatsHash;
QList<Format *> m_xf_formatsList; QList<Format> m_dxf_formatsList;
QHash<QByteArray, Format *> m_xf_formatsHash; QHash<QByteArray, Format> m_dxf_formatsHash;
QList<Format *> m_dxf_formatsList;
QHash<QByteArray, Format *> m_dxf_formatsHash;
}; };
} }

6
src/xlsx/xlsxworkbook.cpp

@ -225,12 +225,6 @@ void Workbook::setActiveWorksheet(int index)
d->activesheet = index; d->activesheet = index;
} }
Format *Workbook::createFormat()
{
Q_D(Workbook);
return d->styles->createFormat();
}
QList<QSharedPointer<Worksheet> > Workbook::worksheets() const QList<QSharedPointer<Worksheet> > Workbook::worksheets() const
{ {
Q_D(const Workbook); Q_D(const Workbook);

2
src/xlsx/xlsxworkbook.h

@ -35,7 +35,6 @@ class QIODevice;
QT_BEGIN_NAMESPACE_XLSX QT_BEGIN_NAMESPACE_XLSX
class Worksheet; class Worksheet;
class Format;
class SharedStrings; class SharedStrings;
class Styles; class Styles;
class Package; class Package;
@ -57,7 +56,6 @@ public:
int activeWorksheet() const; int activeWorksheet() const;
void setActiveWorksheet(int index); void setActiveWorksheet(int index);
Format *createFormat();
// void addChart(); // void addChart();
bool defineName(const QString &name, const QString &formula, const QString &comment=QString(), const QString &scope=QString()); bool defineName(const QString &name, const QString &formula, const QString &comment=QString(), const QString &scope=QString());
bool isDate1904() const; bool isDate1904() const;

155
src/xlsx/xlsxworksheet.cpp

@ -430,7 +430,7 @@ QList<QPair<QString, QString> > Worksheet::drawingLinks() const
* Write \a value to cell (\a row, \a column) with the \a format. * Write \a value to cell (\a row, \a column) with the \a format.
* Both \a row and \a column are all 1-indexed value. * 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); Q_D(Worksheet);
bool ok; bool ok;
@ -477,7 +477,7 @@ int Worksheet::write(int row, int column, const QVariant &value, Format *format)
/*! /*!
\overload \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 //convert the "A1" notation to row/column notation
QPoint pos = xl_cell_to_rowcol(row_column); 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(); 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)) if (!cellTable.contains(row))
return 0; return Format();
if (!cellTable[row].contains(col)) if (!cellTable[row].contains(col))
return 0; return Format();
return cellTable[row][col]->format(); return cellTable[row][col]->format();
} }
/*! /*!
\overload \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 //convert the "A1" notation to row/column notation
QPoint pos = xl_cell_to_rowcol(row_column); 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 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); Q_D(Worksheet);
int error = 0; int error = 0;
@ -572,16 +572,16 @@ int Worksheet::writeString(int row, int column, const QString &value, Format *fo
} }
d->sharedStrings()->addSharedString(content); d->sharedStrings()->addSharedString(content);
format = format ? format : d->cellFormat(row, column); Format fmt = format.isValid() ? format : d->cellFormat(row, column);
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(content, Cell::String, format, this)); d->workbook->styles()->addFormat(fmt);
d->workbook->styles()->addFormat(format); d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(content, Cell::String, fmt, this));
return error; return error;
} }
/*! /*!
\overload \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 //convert the "A1" notation to row/column notation
QPoint pos = xl_cell_to_rowcol(row_column); 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 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); Q_D(Worksheet);
int error = 0; int error = 0;
@ -607,16 +607,16 @@ int Worksheet::writeInlineString(int row, int column, const QString &value, Form
error = -2; error = -2;
} }
format = format ? format : d->cellFormat(row, column); Format fmt = format.isValid() ? format : d->cellFormat(row, column);
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::InlineString, format, this)); d->workbook->styles()->addFormat(fmt);
d->workbook->styles()->addFormat(format); d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::InlineString, fmt, this));
return error; return error;
} }
/*! /*!
\overload \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 //convert the "A1" notation to row/column notation
QPoint pos = xl_cell_to_rowcol(row_column); 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 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); Q_D(Worksheet);
if (d->checkDimensions(row, column)) if (d->checkDimensions(row, column))
return -1; return -1;
format = format ? format : d->cellFormat(row, column); Format fmt = format.isValid() ? format : d->cellFormat(row, column);
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Numeric, format, this)); d->workbook->styles()->addFormat(fmt);
d->workbook->styles()->addFormat(format); d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Numeric, fmt, this));
return 0; return 0;
} }
/*! /*!
\overload \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 //convert the "A1" notation to row/column notation
QPoint pos = xl_cell_to_rowcol(row_column); 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 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); Q_D(Worksheet);
int error = 0; int error = 0;
@ -669,11 +669,11 @@ int Worksheet::writeFormula(int row, int column, const QString &formula, Format
if (_formula.startsWith(QLatin1String("="))) if (_formula.startsWith(QLatin1String("=")))
_formula.remove(0,1); _formula.remove(0,1);
format = format ? format : d->cellFormat(row, column); Format fmt = format.isValid() ? format : d->cellFormat(row, column);
Cell *data = new Cell(result, Cell::Formula, format, this); d->workbook->styles()->addFormat(fmt);
Cell *data = new Cell(result, Cell::Formula, fmt, this);
data->d_ptr->formula = _formula; data->d_ptr->formula = _formula;
d->cellTable[row][column] = QSharedPointer<Cell>(data); d->cellTable[row][column] = QSharedPointer<Cell>(data);
d->workbook->styles()->addFormat(format);
return error; 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 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); Q_D(Worksheet);
int error = 0; 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 row=range.firstRow(); row<=range.lastRow(); ++row) {
for (int column=range.firstColumn(); column<=range.lastColumn(); ++column) { 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()) { if (row == range.firstRow() && column == range.firstColumn()) {
QSharedPointer<Cell> data(new Cell(0, Cell::ArrayFormula, _format, this)); QSharedPointer<Cell> data(new Cell(0, Cell::ArrayFormula, _format, this));
data->d_ptr->formula = _formula; data->d_ptr->formula = _formula;
@ -708,7 +709,6 @@ int Worksheet::writeArrayFormula(const CellRange &range, const QString &formula,
} else { } else {
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(0, Cell::Numeric, _format, this)); d->cellTable[row][column] = QSharedPointer<Cell>(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 \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); return writeArrayFormula(CellRange(range), formula, format);
} }
@ -726,7 +726,7 @@ int Worksheet::writeArrayFormula(const QString &range, const QString &formula, F
/*! /*!
\overload \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 //convert the "A1" notation to row/column notation
QPoint pos = xl_cell_to_rowcol(row_column); 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 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); Q_D(Worksheet);
if (d->checkDimensions(row, column)) if (d->checkDimensions(row, column))
return -1; return -1;
format = format ? format : d->cellFormat(row, column); Format fmt = format.isValid() ? format : d->cellFormat(row, column);
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(QVariant(), Cell::Blank, format, this)); d->workbook->styles()->addFormat(fmt);
d->workbook->styles()->addFormat(format);
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(QVariant(), Cell::Blank, fmt, this));
return 0; return 0;
} }
/*! /*!
\overload \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 //convert the "A1" notation to row/column notation
QPoint pos = xl_cell_to_rowcol(row_column); 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 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); Q_D(Worksheet);
if (d->checkDimensions(row, column)) if (d->checkDimensions(row, column))
return -1; return -1;
format = format ? format : d->cellFormat(row, column); Format fmt = format.isValid() ? format : d->cellFormat(row, column);
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Boolean, format, this)); d->workbook->styles()->addFormat(fmt);
d->workbook->styles()->addFormat(format); d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Boolean, fmt, this));
return 0; return 0;
} }
/*! /*!
\overload \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 //convert the "A1" notation to row/column notation
QPoint pos = xl_cell_to_rowcol(row_column); 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 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); Q_D(Worksheet);
if (d->checkDimensions(row, column)) if (d->checkDimensions(row, column))
return -1; return -1;
if (!format) { Format fmt = format;
format = d->workbook->createFormat(); if (!fmt.isValid())
format->setNumberFormat(d->workbook->defaultDateFormat()); fmt.setNumberFormat(d->workbook->defaultDateFormat());
} d->workbook->styles()->addFormat(fmt);
double value = datetimeToNumber(dt, d->workbook->isDate1904()); double value = datetimeToNumber(dt, d->workbook->isDate1904());
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Numeric, format, this)); d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Numeric, fmt, this));
d->workbook->styles()->addFormat(format);
return 0; return 0;
} }
@ -817,7 +817,7 @@ int Worksheet::writeDateTime(int row, int column, const QDateTime &dt, Format *f
/*! /*!
\overload \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 //convert the "A1" notation to row/column notation
QPoint pos = xl_cell_to_rowcol(row_column); 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 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); Q_D(Worksheet);
if (d->checkDimensions(row, column)) 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); urlString = url.toString(QUrl::RemoveFragment);
} }
Format fmt = format.isValid() ? format : d->cellFormat(row, column);
//Given a default style for hyperlink //Given a default style for hyperlink
if (!format) { if (!fmt.isValid()) {
format = d->workbook->createFormat(); fmt.setFontColor(Qt::blue);
format->setFontColor(Qt::blue); fmt.setFontUnderline(Format::FontUnderlineSingle);
format->setFontUnderline(Format::FontUnderlineSingle);
} }
d->workbook->styles()->addFormat(fmt);
//Write the hyperlink string as normal string. //Write the hyperlink string as normal string.
d->sharedStrings()->addSharedString(displayString); d->sharedStrings()->addSharedString(displayString);
format = format ? format : d->cellFormat(row, column); d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(displayString, Cell::String, fmt, this));
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(displayString, Cell::String, format, this));
d->workbook->styles()->addFormat(format);
//Store the hyperlink data in a separate table //Store the hyperlink data in a separate table
d->urlTable[row][column] = new XlsxUrlData(XlsxUrlData::External, urlString, locationString, tip); 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. \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); Q_D(Worksheet);
if (range.rowCount() < 2 && range.columnCount() < 2) 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())) if (d->checkDimensions(range.firstRow(), range.firstColumn()))
return -1; return -1;
if (format.isValid())
d->workbook->styles()->addFormat(format);
for (int row = range.firstRow(); row <= range.lastRow(); ++row) { for (int row = range.firstRow(); row <= range.lastRow(); ++row) {
for (int col = range.firstColumn(); col <= range.lastColumn(); ++col) { for (int col = range.firstColumn(); col <= range.lastColumn(); ++col) {
if (row == range.firstRow() && col == range.firstColumn()) { if (row == range.firstRow() && col == range.firstColumn()) {
Cell *cell = cellAt(row, col); Cell *cell = cellAt(row, col);
if (cell) { if (cell) {
if (format) if (format.isValid())
cell->d_ptr->format = format; cell->d_ptr->format = format;
} else { } else {
writeBlank(row, col, format); 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); d->merges.append(range);
return 0; 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. \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(':')); QStringList cells = range.split(QLatin1Char(':'));
if (cells.size() != 2) if (cells.size() != 2)
@ -1060,8 +1059,8 @@ void Worksheet::saveToXmlFile(QIODevice *device)
writer.writeAttribute(QStringLiteral("max"), QString::number(col_info->lastColumn)); writer.writeAttribute(QStringLiteral("max"), QString::number(col_info->lastColumn));
if (col_info->width) if (col_info->width)
writer.writeAttribute(QStringLiteral("width"), QString::number(col_info->width, 'g', 15)); writer.writeAttribute(QStringLiteral("width"), QString::number(col_info->width, 'g', 15));
if (col_info->format) if (!col_info->format.isEmpty())
writer.writeAttribute(QStringLiteral("style"), QString::number(col_info->format->xfIndex())); writer.writeAttribute(QStringLiteral("style"), QString::number(col_info->format.xfIndex()));
if (col_info->hidden) if (col_info->hidden)
writer.writeAttribute(QStringLiteral("hidden"), QStringLiteral("1")); writer.writeAttribute(QStringLiteral("hidden"), QStringLiteral("1"));
if (col_info->width) if (col_info->width)
@ -1112,8 +1111,8 @@ void WorksheetPrivate::writeSheetData(XmlStreamWriter &writer)
if (rowsInfo.contains(row_num)) { if (rowsInfo.contains(row_num)) {
QSharedPointer<XlsxRowInfo> rowInfo = rowsInfo[row_num]; QSharedPointer<XlsxRowInfo> rowInfo = rowsInfo[row_num];
if (rowInfo->format) { if (!rowInfo->format.isEmpty()) {
writer.writeAttribute(QStringLiteral("s"), QString::number(rowInfo->format->xfIndex())); writer.writeAttribute(QStringLiteral("s"), QString::number(rowInfo->format.xfIndex()));
writer.writeAttribute(QStringLiteral("customFormat"), QStringLiteral("1")); writer.writeAttribute(QStringLiteral("customFormat"), QStringLiteral("1"));
} }
if (rowInfo->height != 15 && rowInfo->height != 0) { 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); writer.writeAttribute(QStringLiteral("r"), cell_pos);
//Style used by the cell, row or col //Style used by the cell, row or col
if (cell->format()) if (!cell->format().isEmpty())
writer.writeAttribute(QStringLiteral("s"), QString::number(cell->format()->xfIndex())); writer.writeAttribute(QStringLiteral("s"), QString::number(cell->format().xfIndex()));
else if (rowsInfo.contains(row) && rowsInfo[row]->format) else if (rowsInfo.contains(row) && !rowsInfo[row]->format.isEmpty())
writer.writeAttribute(QStringLiteral("s"), QString::number(rowsInfo[row]->format->xfIndex())); writer.writeAttribute(QStringLiteral("s"), QString::number(rowsInfo[row]->format.xfIndex()));
else if (colsInfoHelper.contains(col) && colsInfoHelper[col]->format) else if (colsInfoHelper.contains(col) && !colsInfoHelper[col]->format.isEmpty())
writer.writeAttribute(QStringLiteral("s"), QString::number(colsInfoHelper[col]->format->xfIndex())); writer.writeAttribute(QStringLiteral("s"), QString::number(colsInfoHelper[col]->format.xfIndex()));
if (cell->dataType() == Cell::String) { if (cell->dataType() == Cell::String) {
int sst_idx; 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 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. 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); Q_D(Worksheet);
int min_col = d->dimension.firstColumn() < 0 ? 0 : d->dimension.firstColumn(); 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 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. 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); Q_D(Worksheet);
bool ignore_row = true; bool ignore_row = true;
bool ignore_col = (format || (width && hidden)) ? false : true; bool ignore_col = (format.isValid() || (width && hidden)) ? false : true;
if (colFirst > colLast) if (colFirst > colLast)
return false; 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 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", ... 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 col1 = xl_col_name_to_value(colFirst);
int col2 = xl_col_name_to_value(colLast); int col2 = xl_col_name_to_value(colLast);
@ -1882,11 +1881,11 @@ void WorksheetPrivate::readSheetData(XmlStreamReader &reader)
QPoint pos = xl_cell_to_rowcol(r); QPoint pos = xl_cell_to_rowcol(r);
//get format //get format
Format *format = 0; Format format;
if (attributes.hasAttribute(QLatin1String("s"))) { if (attributes.hasAttribute(QLatin1String("s"))) {
int idx = attributes.value(QLatin1String("s")).toString().toInt(); int idx = attributes.value(QLatin1String("s")).toString().toInt();
format = workbook->styles()->xfFormat(idx); format = workbook->styles()->xfFormat(idx);
if (!format) if (!format.isValid())
qDebug()<<QStringLiteral("<c s=\"%1\">Invalid style index: ").arg(idx)<<idx; qDebug()<<QStringLiteral("<c s=\"%1\">Invalid style index: ").arg(idx)<<idx;
} }

50
src/xlsx/xlsxworksheet.h

@ -53,28 +53,28 @@ class Q_XLSX_EXPORT Worksheet
{ {
Q_DECLARE_PRIVATE(Worksheet) Q_DECLARE_PRIVATE(Worksheet)
public: public:
int write(const QString &row_column, const QVariant &value, Format *format=0); int write(const QString &row_column, const QVariant &value, const Format &format=Format());
int write(int row, int column, const QVariant &value, Format *format=0); int write(int row, int column, const QVariant &value, const Format &format=Format());
QVariant read(const QString &row_column) const; QVariant read(const QString &row_column) const;
QVariant read(int row, int column) const; QVariant read(int row, int column) const;
int writeString(const QString &row_column, const QString &value, Format *format=0); int writeString(const QString &row_column, const QString &value, const Format &format=Format());
int writeString(int row, int column, const QString &value, Format *format=0); int writeString(int row, int column, const QString &value, const Format &format=Format());
int writeInlineString(const QString &row_column, const QString &value, Format *format=0); int writeInlineString(const QString &row_column, const QString &value, const Format &format=Format());
int writeInlineString(int row, int column, const QString &value, Format *format=0); int writeInlineString(int row, int column, const QString &value, const Format &format=Format());
int writeNumeric(const QString &row_column, double value, Format *format=0); int writeNumeric(const QString &row_column, double value, const Format &format=Format());
int writeNumeric(int row, int column, double value, Format *format=0); int writeNumeric(int row, int column, double value, const Format &format=Format());
int writeFormula(const QString &row_column, const QString &formula, Format *format=0, double result=0); int writeFormula(const QString &row_column, const QString &formula, const Format &format=Format(), double result=0);
int writeFormula(int row, int column, const QString &formula, Format *format=0, double result=0); int writeFormula(int row, int column, const QString &formula, const Format &format=Format(), double result=0);
int writeArrayFormula(const QString &range, const QString &formula, Format *format=0); int writeArrayFormula(const QString &range, const QString &formula, const Format &format=Format());
int writeArrayFormula(const CellRange &range, const QString &formula, Format *format=0); int writeArrayFormula(const CellRange &range, const QString &formula, const Format &format=Format());
int writeBlank(const QString &row_column, Format *format=0); int writeBlank(const QString &row_column, const Format &format=Format());
int writeBlank(int row, int column, Format *format=0); int writeBlank(int row, int column, const Format &format=Format());
int writeBool(const QString &row_column, bool value, Format *format=0); int writeBool(const QString &row_column, bool value, const Format &format=Format());
int writeBool(int row, int column, bool value, Format *format=0); int writeBool(int row, int column, bool value, const Format &format=Format());
int writeDateTime(const QString &row_column, const QDateTime& dt, Format *format=0); int writeDateTime(const QString &row_column, const QDateTime& dt, const Format &format=Format());
int writeDateTime(int row, int column, const QDateTime& dt, Format *format=0); int writeDateTime(int row, int column, const QDateTime& dt, const Format &format=Format());
int writeHyperlink(const QString &row_column, const QUrl &url, Format *format=0, const QString &display=QString(), const QString &tip=QString()); int writeHyperlink(const QString &row_column, const QUrl &url, const Format &format=Format(), const QString &display=QString(), const QString &tip=QString());
int writeHyperlink(int row, int column, const QUrl &url, Format *format=0, const QString &display=QString(), const QString &tip=QString()); int writeHyperlink(int row, int column, const QUrl &url, const Format &format=Format(), const QString &display=QString(), const QString &tip=QString());
bool addDataValidation(const DataValidation &validation); bool addDataValidation(const DataValidation &validation);
@ -83,14 +83,14 @@ public:
int insertImage(int row, int column, const QImage &image, const QPointF &offset=QPointF(), double xScale=1, double yScale=1); int insertImage(int row, int column, const QImage &image, const QPointF &offset=QPointF(), double xScale=1, double yScale=1);
int mergeCells(const QString &range, Format *format=0); int mergeCells(const QString &range, const Format &format=Format());
int mergeCells(const CellRange &range, Format *format=0); int mergeCells(const CellRange &range, const Format &format=Format());
int unmergeCells(const QString &range); int unmergeCells(const QString &range);
int unmergeCells(const CellRange &range); int unmergeCells(const CellRange &range);
bool setRow(int row, double height, 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, Format* format=0, 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, Format* format=0, 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 groupRows(int rowFirst, int rowLast, bool collapsed = true);
bool groupColumns(int colFirst, int colLast, bool collapsed = true); bool groupColumns(int colFirst, int colLast, bool collapsed = true);
bool groupColumns(const QString &colFirst, const QString &colLast, bool collapsed = true); bool groupColumns(const QString &colFirst, const QString &colLast, bool collapsed = true);

10
src/xlsx/xlsxworksheet_p.h

@ -122,7 +122,7 @@ struct XlsxObjectPositionData
struct XlsxRowInfo struct XlsxRowInfo
{ {
XlsxRowInfo(double height=0, Format *format=0, bool hidden=false) : XlsxRowInfo(double height=0, const Format &format=Format(), bool hidden=false) :
height(height), format(format), hidden(hidden), outlineLevel(0) height(height), format(format), hidden(hidden), outlineLevel(0)
, collapsed(false) , collapsed(false)
{ {
@ -130,7 +130,7 @@ struct XlsxRowInfo
} }
double height; double height;
Format *format; Format format;
bool hidden; bool hidden;
int outlineLevel; int outlineLevel;
bool collapsed; bool collapsed;
@ -138,7 +138,7 @@ struct XlsxRowInfo
struct XlsxColumnInfo struct XlsxColumnInfo
{ {
XlsxColumnInfo(int firstColumn=0, int lastColumn=1, double width=0, Format *format=0, bool hidden=false) : XlsxColumnInfo(int firstColumn=0, int lastColumn=1, double width=0, const Format &format=Format(), bool hidden=false) :
firstColumn(firstColumn), lastColumn(lastColumn), width(width), format(format), hidden(hidden) firstColumn(firstColumn), lastColumn(lastColumn), width(width), format(format), hidden(hidden)
, outlineLevel(0), collapsed(false) , outlineLevel(0), collapsed(false)
{ {
@ -147,7 +147,7 @@ struct XlsxColumnInfo
int firstColumn; int firstColumn;
int lastColumn; int lastColumn;
double width; double width;
Format *format; Format format;
bool hidden; bool hidden;
int outlineLevel; int outlineLevel;
bool collapsed; bool collapsed;
@ -160,7 +160,7 @@ public:
WorksheetPrivate(Worksheet *p); WorksheetPrivate(Worksheet *p);
~WorksheetPrivate(); ~WorksheetPrivate();
int checkDimensions(int row, int col, bool ignore_row=false, bool ignore_col=false); int checkDimensions(int row, int col, bool ignore_row=false, bool ignore_col=false);
Format *cellFormat(int row, int col) const; Format cellFormat(int row, int col) const;
QString generateDimensionString(); QString generateDimensionString();
void calculateSpans(); void calculateSpans();
void splitColsInfo(int colFirst, int colLast); void splitColsInfo(int colFirst, int colLast);

80
tests/auto/document/tst_documenttest.cpp

@ -45,10 +45,10 @@ void DocumentTest::testReadWriteString()
{ {
Document xlsx1; Document xlsx1;
xlsx1.write("A1", "Hello Qt!"); xlsx1.write("A1", "Hello Qt!");
Format *format = xlsx1.createFormat(); Format format;
format->setFontColor(Qt::blue); format.setFontColor(Qt::blue);
format->setBorderStyle(Format::BorderDashDotDot); format.setBorderStyle(Format::BorderDashDotDot);
format->setFillPattern(Format::PatternSolid); format.setFillPattern(Format::PatternSolid);
xlsx1.write("A2", "Hello Qt again!", format); xlsx1.write("A2", "Hello Qt again!", format);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs("test.xlsx");
@ -57,8 +57,8 @@ void DocumentTest::testReadWriteString()
QCOMPARE(xlsx2.cellAt("A1")->value().toString(), QString("Hello Qt!")); QCOMPARE(xlsx2.cellAt("A1")->value().toString(), QString("Hello Qt!"));
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::String); QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::String);
QCOMPARE(xlsx2.cellAt("A2")->value().toString(), QString("Hello Qt again!")); QCOMPARE(xlsx2.cellAt("A2")->value().toString(), QString("Hello Qt again!"));
QVERIFY(xlsx2.cellAt("A2")->format()!=0); QVERIFY(xlsx2.cellAt("A2")->format().isValid());
QCOMPARE(*xlsx2.cellAt("A2")->format(), *format); QCOMPARE(xlsx2.cellAt("A2")->format(), format);
QFile::remove("test.xlsx"); QFile::remove("test.xlsx");
} }
@ -67,11 +67,11 @@ void DocumentTest::testReadWriteNumeric()
{ {
Document xlsx1; Document xlsx1;
xlsx1.write("A1", 123); xlsx1.write("A1", 123);
Format *format = xlsx1.createFormat(); Format format;
format->setFontColor(Qt::blue); format.setFontColor(Qt::blue);
format->setBorderStyle(Format::BorderDashDotDot); format.setBorderStyle(Format::BorderDashDotDot);
format->setFillPattern(Format::PatternSolid); format.setFillPattern(Format::PatternSolid);
format->setNumberFormatIndex(10); format.setNumberFormatIndex(10);
xlsx1.write("A2", 12345, format); xlsx1.write("A2", 12345, format);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs("test.xlsx");
@ -80,8 +80,8 @@ void DocumentTest::testReadWriteNumeric()
QCOMPARE(xlsx2.cellAt("A1")->value().toDouble(), 123.0); QCOMPARE(xlsx2.cellAt("A1")->value().toDouble(), 123.0);
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric); QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric);
QCOMPARE(xlsx2.cellAt("A2")->value().toDouble(), 12345.0); QCOMPARE(xlsx2.cellAt("A2")->value().toDouble(), 12345.0);
QVERIFY(xlsx2.cellAt("A2")->format()!=0); QVERIFY(xlsx2.cellAt("A2")->format().isValid());
QCOMPARE(*xlsx2.cellAt("A2")->format(), *format); QCOMPARE(xlsx2.cellAt("A2")->format(), format);
QFile::remove("test.xlsx"); QFile::remove("test.xlsx");
} }
@ -90,10 +90,10 @@ void DocumentTest::testReadWriteBool()
{ {
Document xlsx1; Document xlsx1;
xlsx1.write("A1", true); xlsx1.write("A1", true);
Format *format = xlsx1.createFormat(); Format format;
format->setFontColor(Qt::blue); format.setFontColor(Qt::blue);
format->setBorderStyle(Format::BorderDashDotDot); format.setBorderStyle(Format::BorderDashDotDot);
format->setFillPattern(Format::PatternSolid); format.setFillPattern(Format::PatternSolid);
xlsx1.write("A2", false, format); xlsx1.write("A2", false, format);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs("test.xlsx");
@ -102,8 +102,8 @@ void DocumentTest::testReadWriteBool()
QCOMPARE(xlsx2.cellAt("A1")->value().toBool(), true); QCOMPARE(xlsx2.cellAt("A1")->value().toBool(), true);
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Boolean); QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Boolean);
QCOMPARE(xlsx2.cellAt("A2")->value().toBool(), false); QCOMPARE(xlsx2.cellAt("A2")->value().toBool(), false);
QVERIFY(xlsx2.cellAt("A2")->format()!=0); QVERIFY(xlsx2.cellAt("A2")->format().isValid());
QCOMPARE(*xlsx2.cellAt("A2")->format(), *format); QCOMPARE(xlsx2.cellAt("A2")->format(), format);
QFile::remove("test.xlsx"); QFile::remove("test.xlsx");
} }
@ -112,10 +112,10 @@ void DocumentTest::testReadWriteBlank()
{ {
Document xlsx1; Document xlsx1;
xlsx1.write("A1", QVariant()); xlsx1.write("A1", QVariant());
Format *format = xlsx1.createFormat(); Format format;
format->setFontColor(Qt::blue); format.setFontColor(Qt::blue);
format->setBorderStyle(Format::BorderDashDotDot); format.setBorderStyle(Format::BorderDashDotDot);
format->setFillPattern(Format::PatternSolid); format.setFillPattern(Format::PatternSolid);
xlsx1.write("A2", QVariant(), format); xlsx1.write("A2", QVariant(), format);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs("test.xlsx");
@ -126,8 +126,8 @@ void DocumentTest::testReadWriteBlank()
QVERIFY(xlsx2.cellAt("A2")); QVERIFY(xlsx2.cellAt("A2"));
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Blank); QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Blank);
QVERIFY(!xlsx2.cellAt("A2")->value().isValid()); QVERIFY(!xlsx2.cellAt("A2")->value().isValid());
QVERIFY(xlsx2.cellAt("A2")->format()!=0); QVERIFY(xlsx2.cellAt("A2")->format().isValid());
QCOMPARE(*xlsx2.cellAt("A2")->format(), *format); QCOMPARE(xlsx2.cellAt("A2")->format(), format);
QFile::remove("test.xlsx"); QFile::remove("test.xlsx");
} }
@ -136,10 +136,10 @@ void DocumentTest::testReadWriteFormula()
{ {
Document xlsx1; Document xlsx1;
xlsx1.write("A1", "=11+22"); xlsx1.write("A1", "=11+22");
Format *format = xlsx1.createFormat(); Format format;
format->setFontColor(Qt::blue); format.setFontColor(Qt::blue);
format->setBorderStyle(Format::BorderDashDotDot); format.setBorderStyle(Format::BorderDashDotDot);
format->setFillPattern(Format::PatternSolid); format.setFillPattern(Format::PatternSolid);
xlsx1.write("A2", "=22+33", format); xlsx1.write("A2", "=22+33", format);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs("test.xlsx");
@ -151,8 +151,8 @@ void DocumentTest::testReadWriteFormula()
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Formula); QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Formula);
// QCOMPARE(xlsx2.cellAt("A2")->value().toDouble(), 0.0); // QCOMPARE(xlsx2.cellAt("A2")->value().toDouble(), 0.0);
QCOMPARE(xlsx2.cellAt("A2")->formula(), QStringLiteral("22+33")); QCOMPARE(xlsx2.cellAt("A2")->formula(), QStringLiteral("22+33"));
QVERIFY(xlsx2.cellAt("A2")->format()!=0); QVERIFY(xlsx2.cellAt("A2")->format().isValid());
QCOMPARE(*xlsx2.cellAt("A2")->format(), *format); QCOMPARE(xlsx2.cellAt("A2")->format(), format);
QFile::remove("test.xlsx"); QFile::remove("test.xlsx");
} }
@ -164,14 +164,14 @@ void DocumentTest::testReadWriteDateTime()
xlsx1.write("A1", dt); xlsx1.write("A1", dt);
Format *format = xlsx1.createFormat(); Format format;
format->setFontColor(Qt::blue); format.setFontColor(Qt::blue);
format->setBorderStyle(Format::BorderDashDotDot); format.setBorderStyle(Format::BorderDashDotDot);
format->setFillPattern(Format::PatternSolid); format.setFillPattern(Format::PatternSolid);
xlsx1.write("A2", dt, format); xlsx1.write("A2", dt, format);
Format *format3 = xlsx1.createFormat(); Format format3;
format3->setNumberFormat("dd/mm/yyyy"); format3.setNumberFormat("dd/mm/yyyy");
xlsx1.write("A3", dt, format3); xlsx1.write("A3", dt, format3);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs("test.xlsx");
@ -187,11 +187,11 @@ void DocumentTest::testReadWriteDateTime()
// QCOMPARE(xlsx2.cellAt("A2")->dateTime(), dt); // QCOMPARE(xlsx2.cellAt("A2")->dateTime(), dt);
QCOMPARE(xlsx2.cellAt("A3")->dataType(), Cell::Numeric); QCOMPARE(xlsx2.cellAt("A3")->dataType(), Cell::Numeric);
QVERIFY(xlsx2.cellAt("A3")->format()!=0); QVERIFY(xlsx2.cellAt("A3")->format().isValid());
qDebug()<<xlsx2.cellAt("A3")->format()->numberFormat(); qDebug()<<xlsx2.cellAt("A3")->format().numberFormat();
QCOMPARE(xlsx2.cellAt("A3")->isDateTime(), true); QCOMPARE(xlsx2.cellAt("A3")->isDateTime(), true);
QCOMPARE(xlsx2.cellAt("A3")->dateTime(), dt); QCOMPARE(xlsx2.cellAt("A3")->dateTime(), dt);
QCOMPARE(xlsx2.cellAt("A3")->format()->numberFormat(), QString("dd/mm/yyyy")); QCOMPARE(xlsx2.cellAt("A3")->format().numberFormat(), QString("dd/mm/yyyy"));
QFile::remove("test.xlsx"); QFile::remove("test.xlsx");

34
tests/auto/styles/tst_stylestest.cpp

@ -43,8 +43,8 @@ void StylesTest::testAddFormat()
QXlsx::Styles styles; QXlsx::Styles styles;
for (int i=0; i<10; ++i) { for (int i=0; i<10; ++i) {
QXlsx::Format *format = styles.createFormat(); QXlsx::Format format;
format->setFontBold(true); format.setFontBold(true);
styles.addFormat(format); styles.addFormat(format);
} }
@ -56,25 +56,25 @@ void StylesTest::testAddFormat2()
{ {
QXlsx::Styles styles; QXlsx::Styles styles;
QXlsx::Format *format = styles.createFormat(); QXlsx::Format format;
format->setNumberFormat("h:mm:ss AM/PM"); //builtin 19 format.setNumberFormat("h:mm:ss AM/PM"); //builtin 19
styles.addFormat(format); styles.addFormat(format);
QCOMPARE(format->numberFormatIndex(), 19); QCOMPARE(format.numberFormatIndex(), 19);
QXlsx::Format *format2 = styles.createFormat(); QXlsx::Format format2;
format2->setNumberFormat("aaaaa h:mm:ss AM/PM"); //custom format2.setNumberFormat("aaaaa h:mm:ss AM/PM"); //custom
styles.addFormat(format2); styles.addFormat(format2);
QCOMPARE(format2->numberFormatIndex(), 176); QCOMPARE(format2.numberFormatIndex(), 176);
} }
// For a solid fill, Excel reverses the role of foreground and background colours // For a solid fill, Excel reverses the role of foreground and background colours
void StylesTest::testSolidFillBackgroundColor() void StylesTest::testSolidFillBackgroundColor()
{ {
QXlsx::Styles styles; QXlsx::Styles styles;
QXlsx::Format *format = styles.createFormat(); QXlsx::Format format;
format->setPatternBackgroundColor(QColor(Qt::red)); format.setPatternBackgroundColor(QColor(Qt::red));
styles.addFormat(format); styles.addFormat(format);
QByteArray xmlData = styles.saveToXmlData(); QByteArray xmlData = styles.saveToXmlData();
@ -85,8 +85,8 @@ void StylesTest::testSolidFillBackgroundColor()
void StylesTest::testWriteBorders() void StylesTest::testWriteBorders()
{ {
QXlsx::Styles styles; QXlsx::Styles styles;
QXlsx::Format *format = styles.createFormat(); QXlsx::Format format;
format->setRightBorderStyle(QXlsx::Format::BorderThin); format.setRightBorderStyle(QXlsx::Format::BorderThin);
styles.addFormat(format); styles.addFormat(format);
QByteArray xmlData = styles.saveToXmlData(); QByteArray xmlData = styles.saveToXmlData();
@ -108,9 +108,9 @@ void StylesTest::testReadFonts()
styles.readFonts(reader); styles.readFonts(reader);
QCOMPARE(styles.m_fontsList.size(), 3); QCOMPARE(styles.m_fontsList.size(), 3);
QXlsx::Format *font0 = styles.m_fontsList[0]; QXlsx::Format font0 = styles.m_fontsList[0];
QCOMPARE(font0->fontSize(), 11); QCOMPARE(font0.fontSize(), 11);
QCOMPARE(font0->fontName(), QString("Calibri")); QCOMPARE(font0.fontName(), QString("Calibri"));
} }
void StylesTest::testReadFills() void StylesTest::testReadFills()
@ -127,8 +127,8 @@ void StylesTest::testReadFills()
styles.readFills(reader); styles.readFills(reader);
QCOMPARE(styles.m_fillsList.size(), 4); QCOMPARE(styles.m_fillsList.size(), 4);
QCOMPARE(styles.m_fillsList[3]->fillPattern(), QXlsx::Format::PatternSolid); QCOMPARE(styles.m_fillsList[3].fillPattern(), QXlsx::Format::PatternSolid);
QCOMPARE(styles.m_fillsList[3]->patternBackgroundColor(), QColor(Qt::gray));//for solid pattern, bg vs. fg color! QCOMPARE(styles.m_fillsList[3].patternBackgroundColor(), QColor(Qt::gray));//for solid pattern, bg vs. fg color!
} }
void StylesTest::testReadBorders() void StylesTest::testReadBorders()

2
tests/auto/worksheet/tst_worksheet.cpp

@ -106,7 +106,7 @@ void WorksheetTest::testWriteCells()
sheet.writeInlineString(3, 1, "Hello inline"); //A3 sheet.writeInlineString(3, 1, "Hello inline"); //A3
sheet.write("A4", true); sheet.write("A4", true);
sheet.write("A5", "=44+33"); sheet.write("A5", "=44+33");
sheet.writeFormula(5, 2, "44+33", 0, 77); sheet.writeFormula(5, 2, "44+33", QXlsx::Format(), 77);
QByteArray xmldata = sheet.saveToXmlData(); QByteArray xmldata = sheet.saveToXmlData();

Loading…
Cancel
Save