1 /*
2  * This file is part of Office 2007 Filters for Calligra
3  *
4  * Copyright (C) 2010 Sebastian Sauer <sebsauer@kdab.com>
5  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6  *
7  * Contact: Suresh Chande suresh.chande@nokia.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * version 2.1 as published by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #ifndef XLSXXMLDRAWINGREADER_H
26 #define XLSXXMLDRAWINGREADER_H
27 
28 #include <MsooXmlTheme.h>
29 #include <MsooXmlCommonReader.h>
30 
31 #include <KoXmlWriter.h>
32 #include <KoBorder.h>     // Needed in DrawingMLMethods.h
33 
34 
35 class XlsxImport;
36 class XlsxXmlWorksheetReaderContext;
37 class XlsxXmlChartReaderContext;
38 class XlsxXmlEmbeddedPicture;
39 class Sheet;
40 
41 namespace MSOOXML
42 {
43     class MsooXmlDiagramReaderContext;
44 }
45 
46 
47 class XlsxShape {
48 };
49 
50 class XlsxDrawingObject {
51     public:
52         Sheet* m_sheet;
53         enum Type {
54             Unknown,
55             Chart,
56             Diagram,
57             Picture,
58             Shape
59         };
60         Type m_type;
61         union {
62             XlsxXmlChartReaderContext* m_chart;
63             MSOOXML::MsooXmlDiagramReaderContext* m_diagram;
64             XlsxXmlEmbeddedPicture* m_picture;
65             XlsxShape* m_shape;
66         };
67         enum AnchorType {
68             NoAnchor,
69             FromAnchor,
70             ToAnchor
71         };
72         struct Position {
73             int m_row, m_col, m_rowOff, m_colOff;
PositionPosition74             Position() : m_row(0), m_col(0), m_rowOff(0), m_colOff(0) {}
75         };
76         QMap<AnchorType, Position> m_positions;
XlsxDrawingObject(Sheet * sheet)77         explicit XlsxDrawingObject(Sheet* sheet) : m_sheet(sheet), m_type(Unknown), m_shapeBody(0) {}
~XlsxDrawingObject()78         ~XlsxDrawingObject() { delete m_shapeBody; }
setPicture(XlsxXmlEmbeddedPicture * picture)79         void setPicture(XlsxXmlEmbeddedPicture* picture) { m_type = Picture; m_picture = picture; }
setChart(XlsxXmlChartReaderContext * chart)80         void setChart(XlsxXmlChartReaderContext* chart) { m_type = Chart; m_chart = chart; }
setDiagram(MSOOXML::MsooXmlDiagramReaderContext * diagram)81         void setDiagram(MSOOXML::MsooXmlDiagramReaderContext* diagram) { m_type = Diagram; m_diagram = diagram; }
82         KoXmlWriter* setShape(XlsxShape* shape);
83         void save(KoXmlWriter* xmlWriter);
84 
85         KoXmlWriter * pictureWriter();
86 
87 
88         /**
89          * @return true if drawing object is anchored to cell
90          * Presence of FromAnchor position is tested
91          */
92         bool isAnchoredToCell() const;
93 
94         /**
95          * @return From anchor cell address
96          * @see toCellAddress()
97          */
98 
99         QString fromCellAddress() const;
100         /**
101          * @return End anchor cell address, in format like Sheetname.A5,
102          * empty string if no ToAnchor position is available
103          *
104          */
105         QString toCellAddress() const;
106 
107 private:
108         QRect positionRect() const;
109         /**
110         * Computes the cell address name in xlsx documents
111         *
112         * @return For sheetname "Sheet" and row 0, column 0 returns "Sheet.A1",
113         * for row 0, column 1 "Sheet.B1" etc.
114         * for row 1, column 0 "Sheet.A2" etc.
115         */
116         QString cellAddress(const QString &sheetname, int row, int column) const;
117 
118         KoXmlWriter* m_shapeBody;
119 };
120 
121 class XlsxXmlDrawingReaderContext : public MSOOXML::MsooXmlReaderContext
122 {
123 public:
124     XlsxXmlDrawingReaderContext(XlsxXmlWorksheetReaderContext* _worksheetReaderContext, Sheet* _sheet, const QString& _path, const QString& _file);
125     ~XlsxXmlDrawingReaderContext() override;
126 
127     XlsxImport* import;
128     QString path; // contains the path to the file which is being processed (i.e. 'xl/drawings')
129     QString file; // contains the name of the file which is being processed (i.e. 'drawing1.xml')
130     const MSOOXML::DrawingMLTheme* themes;
131 
132     XlsxXmlWorksheetReaderContext* worksheetReaderContext;
133     Sheet* sheet;
134     quint32 m_groupDepthCounter; // How deep we currently are
135 };
136 
137 class XlsxXmlDrawingReader : public MSOOXML::MsooXmlCommonReader
138 {
139 public:
140     explicit XlsxXmlDrawingReader(KoOdfWriters *writers);
141     ~XlsxXmlDrawingReader() override;
142     KoFilter::ConversionStatus read(MSOOXML::MsooXmlReaderContext* context = 0) override;
143 
144 protected:
145     KoFilter::ConversionStatus read_oneCellAnchor();
146     KoFilter::ConversionStatus read_twoCellAnchor();
147     KoFilter::ConversionStatus read_absoluteAnchor();
148     KoFilter::ConversionStatus read_anchor(const QString& reference);
149     KoFilter::ConversionStatus read_from();
150     KoFilter::ConversionStatus read_to();
151     KoFilter::ConversionStatus read_col();
152     KoFilter::ConversionStatus read_row();
153     KoFilter::ConversionStatus read_colOff();
154     KoFilter::ConversionStatus read_rowOff();
155     KoFilter::ConversionStatus read_graphicFrame();
156     KoFilter::ConversionStatus read_graphic2();
157     KoFilter::ConversionStatus read_graphicData2();
158     KoFilter::ConversionStatus read_chart2();
159     KoFilter::ConversionStatus read_diagram();
160 private:
161     XlsxXmlDrawingReaderContext *m_context;
162     XlsxDrawingObject *m_currentDrawingObject;
163     XlsxDrawingObject::AnchorType m_anchorType;
164     int m_chartNumber;
165 
166 #include <MsooXmlCommonReaderMethods.h>
167 #include <MsooXmlCommonReaderDrawingMLMethods.h>
168 // #include <MsooXmlDrawingReaderTableMethods.h>
169 
170     Q_DISABLE_COPY(XlsxXmlDrawingReader)
171 };
172 
173 // This class is used for storing information about embedded pictures.
174 // It's saving service is used in XlsxXmlDrawingReader.cpp, it's created in MsooXmlCommonReaderDrawingMLImpl.h
175 class XlsxXmlEmbeddedPicture
176 {
177 public:
178     XlsxXmlEmbeddedPicture();
179     ~XlsxXmlEmbeddedPicture();
180 
181     /**
182      * Use this pointer for KoXmlWriter for writing odf representing
183      * the image anchored/embedded to element like cell.
184      * The ownership of this pointer belong to XlsxXmlEmbeddedPicture class.
185      * */
186     KoXmlWriter * pictureWriter();
187 
188     /**
189      * Save the .xml part of the picture (the picture itself isn't stored here)
190      * @return true if saving was successful
191      * */
192     bool saveXml(KoXmlWriter *xmlWriter);
193 
194 private:
195     KoXmlWriter * m_pictureWriter;
196     QBuffer m_pictureBuffer;
197 };
198 
199 #endif
200