xlsxdatavalidation.cpp 17 KB

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