andre@1: /**************************************************************************** andre@1: ** Copyright (c) 2013-2014 Debao Zhang andre@1: ** All right reserved. andre@1: ** andre@1: ** Permission is hereby granted, free of charge, to any person obtaining andre@1: ** a copy of this software and associated documentation files (the andre@1: ** "Software"), to deal in the Software without restriction, including andre@1: ** without limitation the rights to use, copy, modify, merge, publish, andre@1: ** distribute, sublicense, and/or sell copies of the Software, and to andre@1: ** permit persons to whom the Software is furnished to do so, subject to andre@1: ** the following conditions: andre@1: ** andre@1: ** The above copyright notice and this permission notice shall be andre@1: ** included in all copies or substantial portions of the Software. andre@1: ** andre@1: ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, andre@1: ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF andre@1: ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND andre@1: ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE andre@1: ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION andre@1: ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION andre@1: ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. andre@1: ** andre@1: ****************************************************************************/ andre@1: #include "xlsxchartsheet.h" andre@1: #include "xlsxchartsheet_p.h" andre@1: #include "xlsxworkbook.h" andre@1: #include "xlsxutility_p.h" andre@1: #include "xlsxdrawing_p.h" andre@1: #include "xlsxdrawinganchor_p.h" andre@1: #include "xlsxchart.h" andre@1: andre@1: #include andre@1: #include andre@1: #include andre@1: andre@1: QT_BEGIN_NAMESPACE_XLSX andre@1: andre@1: ChartsheetPrivate::ChartsheetPrivate(Chartsheet *p, Chartsheet::CreateFlag flag) andre@1: : AbstractSheetPrivate(p, flag), chart(0) andre@1: { andre@1: andre@1: } andre@1: andre@1: ChartsheetPrivate::~ChartsheetPrivate() andre@1: { andre@1: } andre@1: andre@1: /*! andre@1: \class Chartsheet andre@1: \inmodule QtXlsx andre@1: \brief Represent one chartsheet in the workbook. andre@1: */ andre@1: andre@1: /*! andre@1: * \internal andre@1: */ andre@1: Chartsheet::Chartsheet(const QString &name, int id, Workbook *workbook, CreateFlag flag) andre@1: :AbstractSheet(name, id, workbook, new ChartsheetPrivate(this, flag)) andre@1: { andre@1: setSheetType(ST_ChartSheet); andre@1: andre@1: if (flag == Chartsheet::F_NewFromScratch) { andre@1: d_func()->drawing = QSharedPointer(new Drawing(this, flag)); andre@1: andre@1: DrawingAbsoluteAnchor *anchor = new DrawingAbsoluteAnchor(drawing(), DrawingAnchor::Picture); andre@1: andre@1: anchor->pos = QPoint(0, 0); andre@1: anchor->ext = QSize(9293679, 6068786); andre@1: andre@1: QSharedPointer chart = QSharedPointer(new Chart(this, flag)); andre@1: chart->setChartType(Chart::CT_Bar); andre@1: anchor->setObjectGraphicFrame(chart); andre@1: andre@1: d_func()->chart = chart.data(); andre@1: } andre@1: } andre@1: andre@1: /*! andre@1: * \internal andre@1: * andre@1: * Make a copy of this sheet. andre@1: */ andre@1: andre@1: Chartsheet *Chartsheet::copy(const QString &distName, int distId) const andre@1: { andre@1: //:Todo andre@1: Q_UNUSED(distName) andre@1: Q_UNUSED(distId) andre@1: return 0; andre@1: } andre@1: andre@1: /*! andre@1: * Destroys this workssheet. andre@1: */ andre@1: Chartsheet::~Chartsheet() andre@1: { andre@1: } andre@1: andre@1: /*! andre@1: * Returns the chart object of the sheet. andre@1: */ andre@1: Chart *Chartsheet::chart() andre@1: { andre@1: Q_D(Chartsheet); andre@1: andre@1: return d->chart; andre@1: } andre@1: andre@1: void Chartsheet::saveToXmlFile(QIODevice *device) const andre@1: { andre@1: Q_D(const Chartsheet); andre@1: d->relationships->clear(); andre@1: andre@1: QXmlStreamWriter writer(device); andre@1: andre@1: writer.writeStartDocument(QStringLiteral("1.0"), true); andre@1: writer.writeDefaultNamespace(QStringLiteral("http://schemas.openxmlformats.org/spreadsheetml/2006/main")); andre@1: writer.writeNamespace(QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), QStringLiteral("r")); andre@1: writer.writeStartElement(QStringLiteral("chartsheet")); andre@1: andre@1: writer.writeStartElement(QStringLiteral("sheetViews")); andre@1: writer.writeEmptyElement(QStringLiteral("sheetView")); andre@1: writer.writeAttribute(QStringLiteral("workbookViewId"), QString::number(0)); andre@1: writer.writeAttribute(QStringLiteral("zoomToFit"), QStringLiteral("1")); andre@1: writer.writeEndElement(); //sheetViews andre@1: andre@1: int idx = d->workbook->drawings().indexOf(d->drawing.data()); andre@1: d->relationships->addWorksheetRelationship(QStringLiteral("/drawing"), QStringLiteral("../drawings/drawing%1.xml").arg(idx+1)); andre@1: andre@1: writer.writeEmptyElement(QStringLiteral("drawing")); andre@1: writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(d->relationships->count())); andre@1: andre@1: writer.writeEndElement();//chartsheet andre@1: writer.writeEndDocument(); andre@1: } andre@1: andre@1: bool Chartsheet::loadFromXmlFile(QIODevice *device) andre@1: { andre@1: Q_D(Chartsheet); andre@1: andre@1: QXmlStreamReader reader(device); andre@1: while (!reader.atEnd()) { andre@1: reader.readNextStartElement(); andre@1: if (reader.tokenType() == QXmlStreamReader::StartElement) { andre@1: if (reader.name() == QLatin1String("drawing")) { andre@1: QString rId = reader.attributes().value(QStringLiteral("r:id")).toString(); andre@1: QString name = d->relationships->getRelationshipById(rId).target; andre@1: QString path = QDir::cleanPath(splitPath(filePath())[0] + QLatin1String("/") + name); andre@1: d->drawing = QSharedPointer(new Drawing(this, F_LoadFromExists)); andre@1: d->drawing->setFilePath(path); andre@1: } andre@1: } andre@1: } andre@1: andre@1: return true; andre@1: } andre@1: andre@1: QT_END_NAMESPACE_XLSX