diff --git a/src/xlsx/qtxlsx.pri b/src/xlsx/qtxlsx.pri index e14561d..32adbad 100755 --- a/src/xlsx/qtxlsx.pri +++ b/src/xlsx/qtxlsx.pri @@ -18,6 +18,8 @@ HEADERS += $$PWD/xlsxdocpropscore_p.h \ $$PWD/xlsxabstractsheet_p.h \ $$PWD/xlsxworksheet.h \ $$PWD/xlsxworksheet_p.h \ + $$PWD/xlsxchartsheet.h \ + $$PWD/xlsxchartsheet_p.h \ $$PWD/xlsxzipwriter_p.h \ $$PWD/xlsxworkbook_p.h \ $$PWD/xlsxformat_p.h \ @@ -57,6 +59,7 @@ SOURCES += $$PWD/xlsxdocpropscore.cpp \ $$PWD/xlsxworkbook.cpp \ $$PWD/xlsxabstractsheet.cpp \ $$PWD/xlsxworksheet.cpp \ + $$PWD/xlsxchartsheet.cpp \ $$PWD/xlsxzipwriter.cpp \ $$PWD/xlsxdrawing.cpp \ $$PWD/xlsxzipreader.cpp \ diff --git a/src/xlsx/xlsxchart.cpp b/src/xlsx/xlsxchart.cpp index f317340..774f583 100644 --- a/src/xlsx/xlsxchart.cpp +++ b/src/xlsx/xlsxchart.cpp @@ -77,7 +77,7 @@ ChartPrivate::~ChartPrivate() /*! * \internal */ -Chart::Chart(Worksheet *parent) +Chart::Chart(AbstractSheet *parent) :AbstractOOXmlFile(new ChartPrivate(this)) { d_func()->sheet = parent; diff --git a/src/xlsx/xlsxchart.h b/src/xlsx/xlsxchart.h index 103334f..6db7c1b 100644 --- a/src/xlsx/xlsxchart.h +++ b/src/xlsx/xlsxchart.h @@ -35,6 +35,7 @@ class QXmlStreamWriter; QT_BEGIN_NAMESPACE_XLSX +class AbstractSheet; class Worksheet; class ChartPrivate; class CellRange; @@ -74,10 +75,12 @@ public: bool loadFromXmlFile(QIODevice *device); private: + friend class AbstractSheet; friend class Worksheet; + friend class Chartsheet; friend class DrawingAnchor; - Chart(Worksheet *parent); + Chart(AbstractSheet *parent); }; QT_END_NAMESPACE_XLSX diff --git a/src/xlsx/xlsxchart_p.h b/src/xlsx/xlsxchart_p.h index f098d26..c5ce01b 100644 --- a/src/xlsx/xlsxchart_p.h +++ b/src/xlsx/xlsxchart_p.h @@ -116,7 +116,7 @@ public: QList > seriesList; QList > axisList; - Worksheet *sheet; + AbstractSheet *sheet; }; } // namespace QXlsx diff --git a/src/xlsx/xlsxchartsheet.cpp b/src/xlsx/xlsxchartsheet.cpp new file mode 100644 index 0000000..30db0ef --- /dev/null +++ b/src/xlsx/xlsxchartsheet.cpp @@ -0,0 +1,142 @@ +/**************************************************************************** +** Copyright (c) 2013-2014 Debao Zhang +** All right reserved. +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +****************************************************************************/ +#include "xlsxchartsheet.h" +#include "xlsxchartsheet_p.h" +#include "xlsxworkbook.h" +#include "xlsxutility_p.h" +#include "xlsxdrawing_p.h" +#include "xlsxdrawinganchor_p.h" + +#include +#include +#include + +QT_BEGIN_NAMESPACE_XLSX + +ChartsheetPrivate::ChartsheetPrivate(Chartsheet *p) + : AbstractSheetPrivate(p) +{ +} + +ChartsheetPrivate::~ChartsheetPrivate() +{ +} + +/*! + \class Chartsheet + \inmodule QtXlsx + \brief Represent one chartsheet in the workbook. +*/ + +/*! + * \internal + */ +Chartsheet::Chartsheet(const QString &name, int id, Workbook *workbook) + :AbstractSheet(name, id, workbook, new ChartsheetPrivate(this)) +{ + setSheetType(ST_ChartSheet); +} + +/*! + * \internal + * + * Make a copy of this sheet. + */ + +Chartsheet *Chartsheet::copy(const QString &distName, int distId) const +{ + //:Todo + Q_UNUSED(distName) + Q_UNUSED(distId) + return 0; +} + +/*! + * Destroys this workssheet. + */ +Chartsheet::~Chartsheet() +{ +} + +/*! + * Returns the chart object of the sheet. + */ +Chart *Chartsheet::chart() +{ + Q_D(Chartsheet); + + return d->chart; +} + +void Chartsheet::saveToXmlFile(QIODevice *device) const +{ + Q_D(const Chartsheet); + d->relationships->clear(); + + QXmlStreamWriter writer(device); + + writer.writeStartDocument(QStringLiteral("1.0"), true); + writer.writeDefaultNamespace(QStringLiteral("http://schemas.openxmlformats.org/spreadsheetml/2006/main")); + writer.writeNamespace(QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), QStringLiteral("r")); + writer.writeStartElement(QStringLiteral("chartsheet")); + + writer.writeStartElement(QStringLiteral("sheetViews")); + writer.writeEmptyElement(QStringLiteral("sheetView")); + writer.writeAttribute(QStringLiteral("workbookViewId"), QString::number(0)); + writer.writeAttribute(QStringLiteral("zoomToFit"), QStringLiteral("1")); + writer.writeEndElement(); //sheetViews + + int idx = d->workbook->drawings().indexOf(d->drawing.data()); + d->relationships->addWorksheetRelationship(QStringLiteral("/drawing"), QStringLiteral("../drawings/drawing%1.xml").arg(idx+1)); + + writer.writeEmptyElement(QStringLiteral("drawing")); + writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(d->relationships->count())); + + writer.writeEndElement();//chartsheet + writer.writeEndDocument(); +} + +bool Chartsheet::loadFromXmlFile(QIODevice *device) +{ + Q_D(Chartsheet); + + QXmlStreamReader reader(device); + while (!reader.atEnd()) { + reader.readNextStartElement(); + if (reader.tokenType() == QXmlStreamReader::StartElement) { + if (reader.name() == QLatin1String("drawing")) { + QString rId = reader.attributes().value(QStringLiteral("r:id")).toString(); + QString name = d->relationships->getRelationshipById(rId).target; + QString path = QDir::cleanPath(splitPath(filePath())[0] + QLatin1String("/") + name); + d->drawing = QSharedPointer(new Drawing(this)); + d->drawing->setFilePath(path); + } + } + } + + return true; +} + +QT_END_NAMESPACE_XLSX diff --git a/src/xlsx/xlsxchartsheet.h b/src/xlsx/xlsxchartsheet.h new file mode 100644 index 0000000..2400a27 --- /dev/null +++ b/src/xlsx/xlsxchartsheet.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** Copyright (c) 2013-2014 Debao Zhang +** All right reserved. +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +****************************************************************************/ +#ifndef XLSXCHARTSHEET_H +#define XLSXCHARTSHEET_H + +#include "xlsxabstractsheet.h" +#include +#include + +QT_BEGIN_NAMESPACE_XLSX +class Workbook; +class DocumentPrivate; +class ChartsheetPrivate; +class Chart; +class Q_XLSX_EXPORT Chartsheet : public AbstractSheet +{ + Q_DECLARE_PRIVATE(Chartsheet) +public: + + ~Chartsheet(); + Chart *chart(); + +private: + friend class DocumentPrivate; + friend class Workbook; + Chartsheet(const QString &sheetName, int sheetId, Workbook *book); + Chartsheet *copy(const QString &distName, int distId) const; + + void saveToXmlFile(QIODevice *device) const; + bool loadFromXmlFile(QIODevice *device); +}; + +QT_END_NAMESPACE_XLSX +#endif // XLSXCHARTSHEET_H diff --git a/src/xlsx/xlsxchartsheet_p.h b/src/xlsx/xlsxchartsheet_p.h new file mode 100644 index 0000000..709091b --- /dev/null +++ b/src/xlsx/xlsxchartsheet_p.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** Copyright (c) 2013-2014 Debao Zhang +** All right reserved. +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +****************************************************************************/ +#ifndef XLSXCHARTSHEET_P_H +#define XLSXCHARTSHEET_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt Xlsx API. It exists for the convenience +// of the Qt Xlsx. This header file may change from +// version to version without notice, or even be removed. +// +// We mean it. +// + +#include "xlsxglobal.h" +#include "xlsxchartsheet.h" +#include "xlsxabstractsheet_p.h" + +namespace QXlsx { + +class XLSX_AUTOTEST_EXPORT ChartsheetPrivate : public AbstractSheetPrivate +{ + Q_DECLARE_PUBLIC(Chartsheet) +public: + ChartsheetPrivate(Chartsheet *p); + ~ChartsheetPrivate(); + + Chart *chart; +}; + +} +#endif // XLSXCHARTSHEET_P_H diff --git a/src/xlsx/xlsxdrawing.cpp b/src/xlsx/xlsxdrawing.cpp index 9aaac96..504b1b9 100644 --- a/src/xlsx/xlsxdrawing.cpp +++ b/src/xlsx/xlsxdrawing.cpp @@ -25,7 +25,7 @@ #include "xlsxdrawing_p.h" #include "xlsxdrawinganchor_p.h" -#include "xlsxworksheet.h" +#include "xlsxabstractsheet.h" #include #include @@ -33,10 +33,10 @@ namespace QXlsx { -Drawing::Drawing(Worksheet *worksheet) - :worksheet(worksheet) +Drawing::Drawing(AbstractSheet *sheet) + :sheet(sheet) { - workbook = worksheet->workbook(); + workbook = sheet->workbook(); } Drawing::~Drawing() diff --git a/src/xlsx/xlsxdrawing_p.h b/src/xlsx/xlsxdrawing_p.h index 9e7d6a2..21bcb4c 100644 --- a/src/xlsx/xlsxdrawing_p.h +++ b/src/xlsx/xlsxdrawing_p.h @@ -51,18 +51,18 @@ namespace QXlsx { class DrawingAnchor; class Workbook; -class Worksheet; +class AbstractSheet; class MediaFile; class Drawing : public AbstractOOXmlFile { public: - Drawing(Worksheet *workbook); + Drawing(AbstractSheet *sheet); ~Drawing(); void saveToXmlFile(QIODevice *device) const; bool loadFromXmlFile(QIODevice *device); - Worksheet *worksheet; + AbstractSheet *sheet; Workbook *workbook; QList anchors; }; diff --git a/src/xlsx/xlsxdrawinganchor.cpp b/src/xlsx/xlsxdrawinganchor.cpp index 0378f33..601a740 100644 --- a/src/xlsx/xlsxdrawinganchor.cpp +++ b/src/xlsx/xlsxdrawinganchor.cpp @@ -206,7 +206,7 @@ void DrawingAnchor::loadXmlObjectGraphicFrame(QXmlStreamReader &reader) } } if (!exist) { - m_chartFile = QSharedPointer (new Chart(m_drawing->worksheet)); + m_chartFile = QSharedPointer (new Chart(m_drawing->sheet)); m_chartFile->setFilePath(path); m_drawing->workbook->addChartFile(m_chartFile); } diff --git a/src/xlsx/xlsxworkbook.h b/src/xlsx/xlsxworkbook.h index 427994b..97fbdcd 100755 --- a/src/xlsx/xlsxworkbook.h +++ b/src/xlsx/xlsxworkbook.h @@ -45,6 +45,8 @@ class Relationships; class DocumentPrivate; class MediaFile; class Chart; +class Chartsheet; +class Worksheet; class WorkbookPrivate; class Q_XLSX_EXPORT Workbook : public AbstractOOXmlFile @@ -85,6 +87,7 @@ public: private: friend class Worksheet; + friend class Chartsheet; friend class WorksheetPrivate; friend class Document; friend class DocumentPrivate;