Mercurial > clickerconvert
comparison src/xlsx/xlsxdatavalidation.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 | |
26 #include "xlsxdatavalidation.h" | |
27 #include "xlsxdatavalidation_p.h" | |
28 #include "xlsxworksheet.h" | |
29 #include "xlsxcellrange.h" | |
30 | |
31 #include <QXmlStreamReader> | |
32 #include <QXmlStreamWriter> | |
33 | |
34 QT_BEGIN_NAMESPACE_XLSX | |
35 | |
36 DataValidationPrivate::DataValidationPrivate() | |
37 :validationType(DataValidation::None), validationOperator(DataValidation::Between) | |
38 , errorStyle(DataValidation::Stop), allowBlank(false), isPromptMessageVisible(true) | |
39 , isErrorMessageVisible(true) | |
40 { | |
41 | |
42 } | |
43 | |
44 DataValidationPrivate::DataValidationPrivate(DataValidation::ValidationType type, DataValidation::ValidationOperator op, const QString &formula1, const QString &formula2, bool allowBlank) | |
45 :validationType(type), validationOperator(op) | |
46 , errorStyle(DataValidation::Stop), allowBlank(allowBlank), isPromptMessageVisible(true) | |
47 , isErrorMessageVisible(true), formula1(formula1), formula2(formula2) | |
48 { | |
49 | |
50 } | |
51 | |
52 DataValidationPrivate::DataValidationPrivate(const DataValidationPrivate &other) | |
53 :QSharedData(other) | |
54 , validationType(DataValidation::None), validationOperator(DataValidation::Between) | |
55 , errorStyle(DataValidation::Stop), allowBlank(false), isPromptMessageVisible(true) | |
56 , isErrorMessageVisible(true) | |
57 { | |
58 | |
59 } | |
60 | |
61 DataValidationPrivate::~DataValidationPrivate() | |
62 { | |
63 | |
64 } | |
65 | |
66 /*! | |
67 * \class DataValidation | |
68 * \brief Data validation for single cell or a range | |
69 * \inmodule QtXlsx | |
70 * | |
71 * The data validation can be applied to a single cell or a range of cells. | |
72 */ | |
73 | |
74 /*! | |
75 * \enum DataValidation::ValidationType | |
76 * | |
77 * The enum type defines the type of data that you wish to validate. | |
78 * | |
79 * \value None the type of data is unrestricted. This is the same as not applying a data validation. | |
80 * \value Whole restricts the cell to integer values. Means "Whole number"? | |
81 * \value Decimal restricts the cell to decimal values. | |
82 * \value List restricts the cell to a set of user specified values. | |
83 * \value Date restricts the cell to date values. | |
84 * \value Time restricts the cell to time values. | |
85 * \value TextLength restricts the cell data based on an integer string length. | |
86 * \value Custom restricts the cell based on an external Excel formula that returns a true/false value. | |
87 */ | |
88 | |
89 /*! | |
90 * \enum DataValidation::ValidationOperator | |
91 * | |
92 * The enum type defines the criteria by which the data in the | |
93 * cell is validated | |
94 * | |
95 * \value Between | |
96 * \value NotBetween | |
97 * \value Equal | |
98 * \value NotEqual | |
99 * \value LessThan | |
100 * \value LessThanOrEqual | |
101 * \value GreaterThan | |
102 * \value GreaterThanOrEqual | |
103 */ | |
104 | |
105 /*! | |
106 * \enum DataValidation::ErrorStyle | |
107 * | |
108 * The enum type defines the type of error dialog that | |
109 * is displayed. | |
110 * | |
111 * \value Stop | |
112 * \value Warning | |
113 * \value Information | |
114 */ | |
115 | |
116 /*! | |
117 * Construct a data validation object with the given \a type, \a op, \a formula1 | |
118 * \a formula2, and \a allowBlank. | |
119 */ | |
120 DataValidation::DataValidation(ValidationType type, ValidationOperator op, const QString &formula1, const QString &formula2, bool allowBlank) | |
121 :d(new DataValidationPrivate(type, op, formula1, formula2, allowBlank)) | |
122 { | |
123 | |
124 } | |
125 | |
126 /*! | |
127 Construct a data validation object | |
128 */ | |
129 DataValidation::DataValidation() | |
130 :d(new DataValidationPrivate()) | |
131 { | |
132 | |
133 } | |
134 | |
135 /*! | |
136 Constructs a copy of \a other. | |
137 */ | |
138 DataValidation::DataValidation(const DataValidation &other) | |
139 :d(other.d) | |
140 { | |
141 | |
142 } | |
143 | |
144 /*! | |
145 Assigns \a other to this validation and returns a reference to this validation. | |
146 */ | |
147 DataValidation &DataValidation::operator=(const DataValidation &other) | |
148 { | |
149 this->d = other.d; | |
150 return *this; | |
151 } | |
152 | |
153 | |
154 /*! | |
155 * Destroy the object. | |
156 */ | |
157 DataValidation::~DataValidation() | |
158 { | |
159 } | |
160 | |
161 /*! | |
162 Returns the validation type. | |
163 */ | |
164 DataValidation::ValidationType DataValidation::validationType() const | |
165 { | |
166 return d->validationType; | |
167 } | |
168 | |
169 /*! | |
170 Returns the validation operator. | |
171 */ | |
172 DataValidation::ValidationOperator DataValidation::validationOperator() const | |
173 { | |
174 return d->validationOperator; | |
175 } | |
176 | |
177 /*! | |
178 Returns the validation error style. | |
179 */ | |
180 DataValidation::ErrorStyle DataValidation::errorStyle() const | |
181 { | |
182 return d->errorStyle; | |
183 } | |
184 | |
185 /*! | |
186 Returns the formula1. | |
187 */ | |
188 QString DataValidation::formula1() const | |
189 { | |
190 return d->formula1; | |
191 } | |
192 | |
193 /*! | |
194 Returns the formula2. | |
195 */ | |
196 QString DataValidation::formula2() const | |
197 { | |
198 return d->formula2; | |
199 } | |
200 | |
201 /*! | |
202 Returns whether blank is allowed. | |
203 */ | |
204 bool DataValidation::allowBlank() const | |
205 { | |
206 return d->allowBlank; | |
207 } | |
208 | |
209 /*! | |
210 Returns the error message. | |
211 */ | |
212 QString DataValidation::errorMessage() const | |
213 { | |
214 return d->errorMessage; | |
215 } | |
216 | |
217 /*! | |
218 Returns the error message title. | |
219 */ | |
220 QString DataValidation::errorMessageTitle() const | |
221 { | |
222 return d->errorMessageTitle; | |
223 } | |
224 | |
225 /*! | |
226 Returns the prompt message. | |
227 */ | |
228 QString DataValidation::promptMessage() const | |
229 { | |
230 return d->promptMessage; | |
231 } | |
232 | |
233 /*! | |
234 Returns the prompt message title. | |
235 */ | |
236 QString DataValidation::promptMessageTitle() const | |
237 { | |
238 return d->promptMessageTitle; | |
239 } | |
240 | |
241 /*! | |
242 Returns the whether prompt message is shown. | |
243 */ | |
244 bool DataValidation::isPromptMessageVisible() const | |
245 { | |
246 return d->isPromptMessageVisible; | |
247 } | |
248 | |
249 /*! | |
250 Returns the whether error message is shown. | |
251 */ | |
252 bool DataValidation::isErrorMessageVisible() const | |
253 { | |
254 return d->isErrorMessageVisible; | |
255 } | |
256 | |
257 /*! | |
258 Returns the ranges on which the validation will be applied. | |
259 */ | |
260 QList<CellRange> DataValidation::ranges() const | |
261 { | |
262 return d->ranges; | |
263 } | |
264 | |
265 /*! | |
266 Sets the validation type to \a type. | |
267 */ | |
268 void DataValidation::setValidationType(DataValidation::ValidationType type) | |
269 { | |
270 d->validationType = type; | |
271 } | |
272 | |
273 /*! | |
274 Sets the validation operator to \a op. | |
275 */ | |
276 void DataValidation::setValidationOperator(DataValidation::ValidationOperator op) | |
277 { | |
278 d->validationOperator = op; | |
279 } | |
280 | |
281 /*! | |
282 Sets the error style to \a es. | |
283 */ | |
284 void DataValidation::setErrorStyle(DataValidation::ErrorStyle es) | |
285 { | |
286 d->errorStyle = es; | |
287 } | |
288 | |
289 /*! | |
290 Sets the formula1 to \a formula. | |
291 */ | |
292 void DataValidation::setFormula1(const QString &formula) | |
293 { | |
294 if (formula.startsWith(QLatin1Char('='))) | |
295 d->formula1 = formula.mid(1); | |
296 else | |
297 d->formula1 = formula; | |
298 } | |
299 | |
300 /*! | |
301 Sets the formulas to \a formula. | |
302 */ | |
303 void DataValidation::setFormula2(const QString &formula) | |
304 { | |
305 if (formula.startsWith(QLatin1Char('='))) | |
306 d->formula2 = formula.mid(1); | |
307 else | |
308 d->formula2 = formula; | |
309 } | |
310 | |
311 /*! | |
312 Sets the error message to \a error with title \a title. | |
313 */ | |
314 void DataValidation::setErrorMessage(const QString &error, const QString &title) | |
315 { | |
316 d->errorMessage = error; | |
317 d->errorMessageTitle = title; | |
318 } | |
319 | |
320 /*! | |
321 Sets the prompt message to \a prompt with title \a title. | |
322 */ | |
323 void DataValidation::setPromptMessage(const QString &prompt, const QString &title) | |
324 { | |
325 d->promptMessage = prompt; | |
326 d->promptMessageTitle = title; | |
327 } | |
328 | |
329 /*! | |
330 Enable/disabe blank allow based on \a enable. | |
331 */ | |
332 void DataValidation::setAllowBlank(bool enable) | |
333 { | |
334 d->allowBlank = enable; | |
335 } | |
336 | |
337 /*! | |
338 Enable/disabe prompt message visible based on \a visible. | |
339 */ | |
340 void DataValidation::setPromptMessageVisible(bool visible) | |
341 { | |
342 d->isPromptMessageVisible = visible; | |
343 } | |
344 | |
345 /*! | |
346 Enable/disabe error message visible based on \a visible. | |
347 */ | |
348 void DataValidation::setErrorMessageVisible(bool visible) | |
349 { | |
350 d->isErrorMessageVisible = visible; | |
351 } | |
352 | |
353 /*! | |
354 Add the \a cell on which the DataValidation will apply to. | |
355 */ | |
356 void DataValidation::addCell(const CellReference &cell) | |
357 { | |
358 d->ranges.append(CellRange(cell, cell)); | |
359 } | |
360 | |
361 /*! | |
362 \overload | |
363 Add the cell(\a row, \a col) on which the DataValidation will apply to. | |
364 */ | |
365 void DataValidation::addCell(int row, int col) | |
366 { | |
367 d->ranges.append(CellRange(row, col, row, col)); | |
368 } | |
369 | |
370 /*! | |
371 \overload | |
372 Add the range(\a firstRow, \a firstCol, \a lastRow, \a lastCol) on | |
373 which the DataValidation will apply to. | |
374 */ | |
375 void DataValidation::addRange(int firstRow, int firstCol, int lastRow, int lastCol) | |
376 { | |
377 d->ranges.append(CellRange(firstRow, firstCol, lastRow, lastCol)); | |
378 } | |
379 | |
380 /*! | |
381 Add the \a range on which the DataValidation will apply to. | |
382 */ | |
383 void DataValidation::addRange(const CellRange &range) | |
384 { | |
385 d->ranges.append(range); | |
386 } | |
387 | |
388 /*! | |
389 * \internal | |
390 */ | |
391 bool DataValidation::saveToXml(QXmlStreamWriter &writer) const | |
392 { | |
393 static QMap<DataValidation::ValidationType, QString> typeMap; | |
394 static QMap<DataValidation::ValidationOperator, QString> opMap; | |
395 static QMap<DataValidation::ErrorStyle, QString> esMap; | |
396 if (typeMap.isEmpty()) { | |
397 typeMap.insert(DataValidation::None, QStringLiteral("none")); | |
398 typeMap.insert(DataValidation::Whole, QStringLiteral("whole")); | |
399 typeMap.insert(DataValidation::Decimal, QStringLiteral("decimal")); | |
400 typeMap.insert(DataValidation::List, QStringLiteral("list")); | |
401 typeMap.insert(DataValidation::Date, QStringLiteral("date")); | |
402 typeMap.insert(DataValidation::Time, QStringLiteral("time")); | |
403 typeMap.insert(DataValidation::TextLength, QStringLiteral("textLength")); | |
404 typeMap.insert(DataValidation::Custom, QStringLiteral("custom")); | |
405 | |
406 opMap.insert(DataValidation::Between, QStringLiteral("between")); | |
407 opMap.insert(DataValidation::NotBetween, QStringLiteral("notBetween")); | |
408 opMap.insert(DataValidation::Equal, QStringLiteral("equal")); | |
409 opMap.insert(DataValidation::NotEqual, QStringLiteral("notEqual")); | |
410 opMap.insert(DataValidation::LessThan, QStringLiteral("lessThan")); | |
411 opMap.insert(DataValidation::LessThanOrEqual, QStringLiteral("lessThanOrEqual")); | |
412 opMap.insert(DataValidation::GreaterThan, QStringLiteral("greaterThan")); | |
413 opMap.insert(DataValidation::GreaterThanOrEqual, QStringLiteral("greaterThanOrEqual")); | |
414 | |
415 esMap.insert(DataValidation::Stop, QStringLiteral("stop")); | |
416 esMap.insert(DataValidation::Warning, QStringLiteral("warning")); | |
417 esMap.insert(DataValidation::Information, QStringLiteral("information")); | |
418 } | |
419 | |
420 writer.writeStartElement(QStringLiteral("dataValidation")); | |
421 if (validationType() != DataValidation::None) | |
422 writer.writeAttribute(QStringLiteral("type"), typeMap[validationType()]); | |
423 if (errorStyle() != DataValidation::Stop) | |
424 writer.writeAttribute(QStringLiteral("errorStyle"), esMap[errorStyle()]); | |
425 if (validationOperator() != DataValidation::Between) | |
426 writer.writeAttribute(QStringLiteral("operator"), opMap[validationOperator()]); | |
427 if (allowBlank()) | |
428 writer.writeAttribute(QStringLiteral("allowBlank"), QStringLiteral("1")); | |
429 // if (dropDownVisible()) | |
430 // writer.writeAttribute(QStringLiteral("showDropDown"), QStringLiteral("1")); | |
431 if (isPromptMessageVisible()) | |
432 writer.writeAttribute(QStringLiteral("showInputMessage"), QStringLiteral("1")); | |
433 if (isErrorMessageVisible()) | |
434 writer.writeAttribute(QStringLiteral("showErrorMessage"), QStringLiteral("1")); | |
435 if (!errorMessageTitle().isEmpty()) | |
436 writer.writeAttribute(QStringLiteral("errorTitle"), errorMessageTitle()); | |
437 if (!errorMessage().isEmpty()) | |
438 writer.writeAttribute(QStringLiteral("error"), errorMessage()); | |
439 if (!promptMessageTitle().isEmpty()) | |
440 writer.writeAttribute(QStringLiteral("promptTitle"), promptMessageTitle()); | |
441 if (!promptMessage().isEmpty()) | |
442 writer.writeAttribute(QStringLiteral("prompt"), promptMessage()); | |
443 | |
444 QStringList sqref; | |
445 foreach (CellRange range, ranges()) | |
446 sqref.append(range.toString()); | |
447 writer.writeAttribute(QStringLiteral("sqref"), sqref.join(QLatin1Char(' '))); | |
448 | |
449 if (!formula1().isEmpty()) | |
450 writer.writeTextElement(QStringLiteral("formula1"), formula1()); | |
451 if (!formula2().isEmpty()) | |
452 writer.writeTextElement(QStringLiteral("formula2"), formula2()); | |
453 | |
454 writer.writeEndElement(); //dataValidation | |
455 | |
456 return true; | |
457 } | |
458 | |
459 /*! | |
460 * \internal | |
461 */ | |
462 DataValidation DataValidation::loadFromXml(QXmlStreamReader &reader) | |
463 { | |
464 Q_ASSERT(reader.name() == QLatin1String("dataValidation")); | |
465 | |
466 static QMap<QString, DataValidation::ValidationType> typeMap; | |
467 static QMap<QString, DataValidation::ValidationOperator> opMap; | |
468 static QMap<QString, DataValidation::ErrorStyle> esMap; | |
469 if (typeMap.isEmpty()) { | |
470 typeMap.insert(QStringLiteral("none"), DataValidation::None); | |
471 typeMap.insert(QStringLiteral("whole"), DataValidation::Whole); | |
472 typeMap.insert(QStringLiteral("decimal"), DataValidation::Decimal); | |
473 typeMap.insert(QStringLiteral("list"), DataValidation::List); | |
474 typeMap.insert(QStringLiteral("date"), DataValidation::Date); | |
475 typeMap.insert(QStringLiteral("time"), DataValidation::Time); | |
476 typeMap.insert(QStringLiteral("textLength"), DataValidation::TextLength); | |
477 typeMap.insert(QStringLiteral("custom"), DataValidation::Custom); | |
478 | |
479 opMap.insert(QStringLiteral("between"), DataValidation::Between); | |
480 opMap.insert(QStringLiteral("notBetween"), DataValidation::NotBetween); | |
481 opMap.insert(QStringLiteral("equal"), DataValidation::Equal); | |
482 opMap.insert(QStringLiteral("notEqual"), DataValidation::NotEqual); | |
483 opMap.insert(QStringLiteral("lessThan"), DataValidation::LessThan); | |
484 opMap.insert(QStringLiteral("lessThanOrEqual"), DataValidation::LessThanOrEqual); | |
485 opMap.insert(QStringLiteral("greaterThan"), DataValidation::GreaterThan); | |
486 opMap.insert(QStringLiteral("greaterThanOrEqual"), DataValidation::GreaterThanOrEqual); | |
487 | |
488 esMap.insert(QStringLiteral("stop"), DataValidation::Stop); | |
489 esMap.insert(QStringLiteral("warning"), DataValidation::Warning); | |
490 esMap.insert(QStringLiteral("information"), DataValidation::Information); | |
491 } | |
492 | |
493 DataValidation validation; | |
494 QXmlStreamAttributes attrs = reader.attributes(); | |
495 | |
496 QString sqref = attrs.value(QLatin1String("sqref")).toString(); | |
497 foreach (QString range, sqref.split(QLatin1Char(' '))) | |
498 validation.addRange(range); | |
499 | |
500 if (attrs.hasAttribute(QLatin1String("type"))) { | |
501 QString t = attrs.value(QLatin1String("type")).toString(); | |
502 validation.setValidationType(typeMap.contains(t) ? typeMap[t] : DataValidation::None); | |
503 } | |
504 if (attrs.hasAttribute(QLatin1String("errorStyle"))) { | |
505 QString es = attrs.value(QLatin1String("errorStyle")).toString(); | |
506 validation.setErrorStyle(esMap.contains(es) ? esMap[es] : DataValidation::Stop); | |
507 } | |
508 if (attrs.hasAttribute(QLatin1String("operator"))) { | |
509 QString op = attrs.value(QLatin1String("operator")).toString(); | |
510 validation.setValidationOperator(opMap.contains(op) ? opMap[op] : DataValidation::Between); | |
511 } | |
512 if (attrs.hasAttribute(QLatin1String("allowBlank"))) { | |
513 validation.setAllowBlank(true); | |
514 } else { | |
515 validation.setAllowBlank(false); | |
516 } | |
517 if (attrs.hasAttribute(QLatin1String("showInputMessage"))) { | |
518 validation.setPromptMessageVisible(true); | |
519 } else { | |
520 validation.setPromptMessageVisible(false); | |
521 } | |
522 if (attrs.hasAttribute(QLatin1String("showErrorMessage"))) { | |
523 validation.setErrorMessageVisible(true); | |
524 } else { | |
525 validation.setErrorMessageVisible(false); | |
526 } | |
527 | |
528 QString et = attrs.value(QLatin1String("errorTitle")).toString(); | |
529 QString e = attrs.value(QLatin1String("error")).toString(); | |
530 if (!e.isEmpty() || !et.isEmpty()) | |
531 validation.setErrorMessage(e, et); | |
532 | |
533 QString pt = attrs.value(QLatin1String("promptTitle")).toString(); | |
534 QString p = attrs.value(QLatin1String("prompt")).toString(); | |
535 if (!p.isEmpty() || !pt.isEmpty()) | |
536 validation.setPromptMessage(p, pt); | |
537 | |
538 //find the end | |
539 while(!(reader.name() == QLatin1String("dataValidation") && reader.tokenType() == QXmlStreamReader::EndElement)) { | |
540 reader.readNextStartElement(); | |
541 if (reader.tokenType() == QXmlStreamReader::StartElement) { | |
542 if (reader.name() == QLatin1String("formula1")) { | |
543 validation.setFormula1(reader.readElementText()); | |
544 } else if (reader.name() == QLatin1String("formula2")) { | |
545 validation.setFormula2(reader.readElementText()); | |
546 } | |
547 } | |
548 } | |
549 return validation; | |
550 } | |
551 | |
552 QT_END_NAMESPACE_XLSX |