xlsxchartsheet.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // xlsxchartsheet.cpp
  2. #include <QtGlobal>
  3. #include <QXmlStreamReader>
  4. #include <QXmlStreamWriter>
  5. #include <QDir>
  6. #include "xlsxchartsheet.h"
  7. #include "xlsxchartsheet_p.h"
  8. #include "xlsxworkbook.h"
  9. #include "xlsxutility_p.h"
  10. #include "xlsxdrawing_p.h"
  11. #include "xlsxdrawinganchor_p.h"
  12. #include "xlsxchart.h"
  13. QT_BEGIN_NAMESPACE_XLSX
  14. ChartsheetPrivate::ChartsheetPrivate(Chartsheet *p, Chartsheet::CreateFlag flag)
  15. : AbstractSheetPrivate(p, flag), chart(0)
  16. {
  17. }
  18. ChartsheetPrivate::~ChartsheetPrivate()
  19. {
  20. }
  21. /*!
  22. \class Chartsheet
  23. \inmodule QtXlsx
  24. \brief Represent one chartsheet in the workbook.
  25. */
  26. /*!
  27. * \internal
  28. */
  29. Chartsheet::Chartsheet(const QString &name, int id, Workbook *workbook, CreateFlag flag)
  30. : AbstractSheet( name, id, workbook, new ChartsheetPrivate(this, flag) )
  31. {
  32. setSheetType(ST_ChartSheet);
  33. if (flag == Chartsheet::F_NewFromScratch)
  34. {
  35. d_func()->drawing = QSharedPointer<Drawing>(new Drawing(this, flag));
  36. DrawingAbsoluteAnchor *anchor = new DrawingAbsoluteAnchor(drawing(), DrawingAnchor::Picture);
  37. anchor->pos = QPoint(0, 0);
  38. anchor->ext = QSize(9293679, 6068786);
  39. QSharedPointer<Chart> chart = QSharedPointer<Chart>(new Chart(this, flag));
  40. chart->setChartType(Chart::CT_BarChart);
  41. anchor->setObjectGraphicFrame(chart);
  42. d_func()->chart = chart.data();
  43. }
  44. }
  45. /*!
  46. * \internal
  47. *
  48. * Make a copy of this sheet.
  49. */
  50. Chartsheet *Chartsheet::copy(const QString &distName, int distId) const
  51. {
  52. //:Todo
  53. Q_UNUSED(distName)
  54. Q_UNUSED(distId)
  55. return 0;
  56. }
  57. /*!
  58. * Destroys this workssheet.
  59. */
  60. Chartsheet::~Chartsheet()
  61. {
  62. }
  63. /*!
  64. * Returns the chart object of the sheet.
  65. */
  66. Chart *Chartsheet::chart()
  67. {
  68. Q_D(Chartsheet);
  69. return d->chart;
  70. }
  71. void Chartsheet::saveToXmlFile(QIODevice *device) const
  72. {
  73. Q_D(const Chartsheet);
  74. d->relationships->clear();
  75. QXmlStreamWriter writer(device);
  76. writer.writeStartDocument(QStringLiteral("1.0"), true);
  77. writer.writeDefaultNamespace(QStringLiteral("http://schemas.openxmlformats.org/spreadsheetml/2006/main"));
  78. writer.writeNamespace(QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), QStringLiteral("r"));
  79. writer.writeStartElement(QStringLiteral("chartsheet"));
  80. writer.writeStartElement(QStringLiteral("sheetViews"));
  81. writer.writeEmptyElement(QStringLiteral("sheetView"));
  82. writer.writeAttribute(QStringLiteral("workbookViewId"), QString::number(0));
  83. writer.writeAttribute(QStringLiteral("zoomToFit"), QStringLiteral("1"));
  84. writer.writeEndElement(); //sheetViews
  85. int idx = d->workbook->drawings().indexOf(d->drawing.data());
  86. d->relationships->addWorksheetRelationship(QStringLiteral("/drawing"), QStringLiteral("../drawings/drawing%1.xml").arg(idx+1));
  87. writer.writeEmptyElement(QStringLiteral("drawing"));
  88. writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(d->relationships->count()));
  89. writer.writeEndElement();//chartsheet
  90. writer.writeEndDocument();
  91. }
  92. bool Chartsheet::loadFromXmlFile(QIODevice *device)
  93. {
  94. Q_D(Chartsheet);
  95. QXmlStreamReader reader(device);
  96. while (!reader.atEnd()) {
  97. reader.readNextStartElement();
  98. if (reader.tokenType() == QXmlStreamReader::StartElement) {
  99. if (reader.name() == QLatin1String("drawing")) {
  100. QString rId = reader.attributes().value(QStringLiteral("r:id")).toString();
  101. QString name = d->relationships->getRelationshipById(rId).target;
  102. QString str = *( splitPath(filePath()).begin() );
  103. str = str + QLatin1String("/") ;
  104. str = str + name;
  105. QString path = QDir::cleanPath( str );
  106. d->drawing = QSharedPointer<Drawing>(new Drawing(this, F_LoadFromExists));
  107. d->drawing->setFilePath(path);
  108. }
  109. }
  110. }
  111. return true;
  112. }
  113. QT_END_NAMESPACE_XLSX