comparison src/xlsx/xlsxchartsheet.cpp @ 1:93d3106bb9a4

Add qt xlsx library
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 22 Mar 2016 10:38:08 +0100
parents
children
comparison
equal deleted inserted replaced
0:49cd5cc0b072 1:93d3106bb9a4
1 /****************************************************************************
2 ** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me>
3 ** All right reserved.
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining
6 ** a copy of this software and associated documentation files (the
7 ** "Software"), to deal in the Software without restriction, including
8 ** without limitation the rights to use, copy, modify, merge, publish,
9 ** distribute, sublicense, and/or sell copies of the Software, and to
10 ** permit persons to whom the Software is furnished to do so, subject to
11 ** the following conditions:
12 **
13 ** The above copyright notice and this permission notice shall be
14 ** included in all copies or substantial portions of the Software.
15 **
16 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 **
24 ****************************************************************************/
25 #include "xlsxchartsheet.h"
26 #include "xlsxchartsheet_p.h"
27 #include "xlsxworkbook.h"
28 #include "xlsxutility_p.h"
29 #include "xlsxdrawing_p.h"
30 #include "xlsxdrawinganchor_p.h"
31 #include "xlsxchart.h"
32
33 #include <QXmlStreamReader>
34 #include <QXmlStreamWriter>
35 #include <QDir>
36
37 QT_BEGIN_NAMESPACE_XLSX
38
39 ChartsheetPrivate::ChartsheetPrivate(Chartsheet *p, Chartsheet::CreateFlag flag)
40 : AbstractSheetPrivate(p, flag), chart(0)
41 {
42
43 }
44
45 ChartsheetPrivate::~ChartsheetPrivate()
46 {
47 }
48
49 /*!
50 \class Chartsheet
51 \inmodule QtXlsx
52 \brief Represent one chartsheet in the workbook.
53 */
54
55 /*!
56 * \internal
57 */
58 Chartsheet::Chartsheet(const QString &name, int id, Workbook *workbook, CreateFlag flag)
59 :AbstractSheet(name, id, workbook, new ChartsheetPrivate(this, flag))
60 {
61 setSheetType(ST_ChartSheet);
62
63 if (flag == Chartsheet::F_NewFromScratch) {
64 d_func()->drawing = QSharedPointer<Drawing>(new Drawing(this, flag));
65
66 DrawingAbsoluteAnchor *anchor = new DrawingAbsoluteAnchor(drawing(), DrawingAnchor::Picture);
67
68 anchor->pos = QPoint(0, 0);
69 anchor->ext = QSize(9293679, 6068786);
70
71 QSharedPointer<Chart> chart = QSharedPointer<Chart>(new Chart(this, flag));
72 chart->setChartType(Chart::CT_Bar);
73 anchor->setObjectGraphicFrame(chart);
74
75 d_func()->chart = chart.data();
76 }
77 }
78
79 /*!
80 * \internal
81 *
82 * Make a copy of this sheet.
83 */
84
85 Chartsheet *Chartsheet::copy(const QString &distName, int distId) const
86 {
87 //:Todo
88 Q_UNUSED(distName)
89 Q_UNUSED(distId)
90 return 0;
91 }
92
93 /*!
94 * Destroys this workssheet.
95 */
96 Chartsheet::~Chartsheet()
97 {
98 }
99
100 /*!
101 * Returns the chart object of the sheet.
102 */
103 Chart *Chartsheet::chart()
104 {
105 Q_D(Chartsheet);
106
107 return d->chart;
108 }
109
110 void Chartsheet::saveToXmlFile(QIODevice *device) const
111 {
112 Q_D(const Chartsheet);
113 d->relationships->clear();
114
115 QXmlStreamWriter writer(device);
116
117 writer.writeStartDocument(QStringLiteral("1.0"), true);
118 writer.writeDefaultNamespace(QStringLiteral("http://schemas.openxmlformats.org/spreadsheetml/2006/main"));
119 writer.writeNamespace(QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), QStringLiteral("r"));
120 writer.writeStartElement(QStringLiteral("chartsheet"));
121
122 writer.writeStartElement(QStringLiteral("sheetViews"));
123 writer.writeEmptyElement(QStringLiteral("sheetView"));
124 writer.writeAttribute(QStringLiteral("workbookViewId"), QString::number(0));
125 writer.writeAttribute(QStringLiteral("zoomToFit"), QStringLiteral("1"));
126 writer.writeEndElement(); //sheetViews
127
128 int idx = d->workbook->drawings().indexOf(d->drawing.data());
129 d->relationships->addWorksheetRelationship(QStringLiteral("/drawing"), QStringLiteral("../drawings/drawing%1.xml").arg(idx+1));
130
131 writer.writeEmptyElement(QStringLiteral("drawing"));
132 writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(d->relationships->count()));
133
134 writer.writeEndElement();//chartsheet
135 writer.writeEndDocument();
136 }
137
138 bool Chartsheet::loadFromXmlFile(QIODevice *device)
139 {
140 Q_D(Chartsheet);
141
142 QXmlStreamReader reader(device);
143 while (!reader.atEnd()) {
144 reader.readNextStartElement();
145 if (reader.tokenType() == QXmlStreamReader::StartElement) {
146 if (reader.name() == QLatin1String("drawing")) {
147 QString rId = reader.attributes().value(QStringLiteral("r:id")).toString();
148 QString name = d->relationships->getRelationshipById(rId).target;
149 QString path = QDir::cleanPath(splitPath(filePath())[0] + QLatin1String("/") + name);
150 d->drawing = QSharedPointer<Drawing>(new Drawing(this, F_LoadFromExists));
151 d->drawing->setFilePath(path);
152 }
153 }
154 }
155
156 return true;
157 }
158
159 QT_END_NAMESPACE_XLSX
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)