xlsxdrawing.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "xlsxdrawing_p.h"
  26. #include "xlsxdrawinganchor_p.h"
  27. #include "xlsxabstractsheet.h"
  28. #include <QXmlStreamWriter>
  29. #include <QXmlStreamReader>
  30. #include <QBuffer>
  31. namespace QXlsx {
  32. Drawing::Drawing(AbstractSheet *sheet, CreateFlag flag)
  33. :AbstractOOXmlFile(flag), sheet(sheet)
  34. {
  35. workbook = sheet->workbook();
  36. }
  37. Drawing::~Drawing()
  38. {
  39. qDeleteAll(anchors);
  40. }
  41. void Drawing::saveToXmlFile(QIODevice *device) const
  42. {
  43. relationships()->clear();
  44. QXmlStreamWriter writer(device);
  45. writer.writeStartDocument(QStringLiteral("1.0"), true);
  46. writer.writeStartElement(QStringLiteral("xdr:wsDr"));
  47. writer.writeAttribute(QStringLiteral("xmlns:xdr"), QStringLiteral("http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"));
  48. writer.writeAttribute(QStringLiteral("xmlns:a"), QStringLiteral("http://schemas.openxmlformats.org/drawingml/2006/main"));
  49. foreach (DrawingAnchor *anchor, anchors)
  50. anchor->saveToXml(writer);
  51. writer.writeEndElement();//xdr:wsDr
  52. writer.writeEndDocument();
  53. }
  54. bool Drawing::loadFromXmlFile(QIODevice *device)
  55. {
  56. QXmlStreamReader reader(device);
  57. while (!reader.atEnd()) {
  58. reader.readNextStartElement();
  59. if (reader.tokenType() == QXmlStreamReader::StartElement) {
  60. if (reader.name() == QLatin1String("absoluteAnchor")) {
  61. DrawingAbsoluteAnchor * anchor = new DrawingAbsoluteAnchor(this);
  62. anchor->loadFromXml(reader);
  63. } else if (reader.name() == QLatin1String("oneCellAnchor")) {
  64. DrawingOneCellAnchor * anchor = new DrawingOneCellAnchor(this);
  65. anchor->loadFromXml(reader);
  66. } else if (reader.name() == QLatin1String("twoCellAnchor")) {
  67. DrawingTwoCellAnchor * anchor = new DrawingTwoCellAnchor(this);
  68. anchor->loadFromXml(reader);
  69. }
  70. }
  71. }
  72. return true;
  73. }
  74. } // namespace QXlsx