comparison src/xlsx/xlsxcellformula.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 "xlsxcellformula.h"
26 #include "xlsxcellformula_p.h"
27 #include "xlsxutility_p.h"
28
29 #include <QXmlStreamReader>
30 #include <QXmlStreamWriter>
31
32 QT_BEGIN_NAMESPACE_XLSX
33
34 CellFormulaPrivate::CellFormulaPrivate(const QString &formula_, const CellRange &ref_, CellFormula::FormulaType type_)
35 :formula(formula_), type(type_), reference(ref_), ca(false), si(0)
36 {
37 //Remove the formula '=' sign if exists
38 if (formula.startsWith(QLatin1String("=")))
39 formula.remove(0,1);
40 else if (formula.startsWith(QLatin1String("{=")) && formula.endsWith(QLatin1String("}")))
41 formula = formula.mid(2, formula.length()-3);
42 }
43
44 CellFormulaPrivate::CellFormulaPrivate(const CellFormulaPrivate &other)
45 : QSharedData(other)
46 , formula(other.formula), type(other.type), reference(other.reference)
47 , ca(other.ca), si(other.si)
48 {
49
50 }
51
52 CellFormulaPrivate::~CellFormulaPrivate()
53 {
54
55 }
56
57 /*!
58 \class CellFormula
59 \inmodule QtXlsx
60 \brief The CellFormula class provides a API that is used to handle the cell formula.
61
62 */
63
64 /*!
65 \enum CellFormula::FormulaType
66 \value NormalType
67 \value ArrayType
68 \value DataTableType
69 \value SharedType
70 */
71
72 /*!
73 * Creates a new formula.
74 */
75 CellFormula::CellFormula()
76 {
77 //The d pointer is initialized with a null pointer
78 }
79
80 /*!
81 * Creates a new formula with the given \a formula and \a type.
82 */
83 CellFormula::CellFormula(const char *formula, FormulaType type)
84 :d(new CellFormulaPrivate(QString::fromLatin1(formula), CellRange(), type))
85 {
86
87 }
88
89 /*!
90 * Creates a new formula with the given \a formula and \a type.
91 */
92 CellFormula::CellFormula(const QString &formula, FormulaType type)
93 :d(new CellFormulaPrivate(formula, CellRange(), type))
94 {
95
96 }
97
98 /*!
99 * Creates a new formula with the given \a formula, \a ref and \a type.
100 */
101 CellFormula::CellFormula(const QString &formula, const CellRange &ref, FormulaType type)
102 :d(new CellFormulaPrivate(formula, ref, type))
103 {
104
105 }
106
107 /*!
108 Creates a new formula with the same attributes as the \a other formula.
109 */
110 CellFormula::CellFormula(const CellFormula &other)
111 :d(other.d)
112 {
113 }
114
115 /*!
116 Assigns the \a other formula to this formula, and returns a
117 reference to this formula.
118 */
119 CellFormula &CellFormula::operator =(const CellFormula &other)
120 {
121 d = other.d;
122 return *this;
123 }
124
125 /*!
126 * Destroys this formula.
127 */
128 CellFormula::~CellFormula()
129 {
130
131 }
132
133 /*!
134 * Returns the type of the formula.
135 */
136 CellFormula::FormulaType CellFormula::formulaType() const
137 {
138 return d ? d->type : NormalType;
139 }
140
141 /*!
142 * Returns the contents of the formula.
143 */
144 QString CellFormula::formulaText() const
145 {
146 return d ? d->formula : QString();
147 }
148
149 /*!
150 * Returns the reference cells of the formula. For normal formula,
151 * this will return an invalid CellRange object.
152 */
153 CellRange CellFormula::reference() const
154 {
155 return d ? d->reference : CellRange();
156 }
157
158 /*!
159 * Returns whether the formula is valid.
160 */
161 bool CellFormula::isValid() const
162 {
163 return d;
164 }
165
166 /*!
167 * Returns the shared index for shared formula.
168 */
169 int CellFormula::sharedIndex() const
170 {
171 return d && d->type == SharedType ? d->si : -1;
172 }
173
174 /*!
175 * \internal
176 */
177 bool CellFormula::saveToXml(QXmlStreamWriter &writer) const
178 {
179 writer.writeStartElement(QStringLiteral("f"));
180 QString t;
181 switch (d->type) {
182 case CellFormula::ArrayType:
183 t = QStringLiteral("array");
184 break;
185 case CellFormula::SharedType:
186 t = QStringLiteral("shared");
187 break;
188 default:
189 break;
190 }
191 if (!t.isEmpty())
192 writer.writeAttribute(QStringLiteral("t"), t);
193 if (d->reference.isValid())
194 writer.writeAttribute(QStringLiteral("ref"), d->reference.toString());
195 if (d->ca)
196 writer.writeAttribute(QStringLiteral("ca"), QStringLiteral("1"));
197 if (d->type == CellFormula::SharedType)
198 writer.writeAttribute(QStringLiteral("si"), QString::number(d->si));
199
200 if (!d->formula.isEmpty())
201 writer.writeCharacters(d->formula);
202
203 writer.writeEndElement(); //f
204
205 return true;
206 }
207
208 /*!
209 * \internal
210 */
211 bool CellFormula::loadFromXml(QXmlStreamReader &reader)
212 {
213 Q_ASSERT(reader.name() == QLatin1String("f"));
214 if (!d)
215 d = new CellFormulaPrivate(QString(), CellRange(), NormalType);
216
217 QXmlStreamAttributes attributes = reader.attributes();
218 QString typeString = attributes.value(QLatin1String("t")).toString();
219 if (typeString == QLatin1String("array"))
220 d->type = ArrayType;
221 else if (typeString == QLatin1String("shared"))
222 d->type = SharedType;
223 else
224 d->type = NormalType;
225
226 if (attributes.hasAttribute(QLatin1String("ref"))) {
227 QString refString = attributes.value(QLatin1String("ref")).toString();
228 d->reference = CellRange(refString);
229 }
230
231 QString ca = attributes.value(QLatin1String("si")).toString();
232 d->ca = parseXsdBoolean(ca, false);
233
234 if (attributes.hasAttribute(QLatin1String("si")))
235 d->si = attributes.value(QLatin1String("si")).toString().toInt();
236
237 d->formula = reader.readElementText();
238 return true;
239 }
240
241 /*!
242 * \internal
243 */
244 bool CellFormula::operator ==(const CellFormula &formula) const
245 {
246 return d->formula == formula.d->formula && d->type == formula.d->type
247 && d->si ==formula.d->si;
248 }
249
250 /*!
251 * \internal
252 */
253 bool CellFormula::operator !=(const CellFormula &formula) const
254 {
255 return d->formula != formula.d->formula || d->type != formula.d->type
256 || d->si !=formula.d->si;
257 }
258
259 QT_END_NAMESPACE_XLSX
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)