xlsxformat.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  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 "xlsxformat.h"
  26. #include "xlsxformat_p.h"
  27. #include "xlsxcolor_p.h"
  28. #include "xlsxnumformatparser_p.h"
  29. #include <QDataStream>
  30. #include <QDebug>
  31. QT_BEGIN_NAMESPACE_XLSX
  32. FormatPrivate::FormatPrivate()
  33. : dirty(true)
  34. , font_dirty(true), font_index_valid(false), font_index(0)
  35. , fill_dirty(true), fill_index_valid(false), fill_index(0)
  36. , border_dirty(true), border_index_valid(false), border_index(0)
  37. , xf_index(-1), xf_indexValid(false)
  38. , is_dxf_fomat(false), dxf_index(-1), dxf_indexValid(false)
  39. , theme(0)
  40. {
  41. }
  42. FormatPrivate::FormatPrivate(const FormatPrivate &other)
  43. : QSharedData(other)
  44. , dirty(other.dirty), formatKey(other.formatKey)
  45. , font_dirty(other.font_dirty), font_index_valid(other.font_index_valid), font_key(other.font_key), font_index(other.font_index)
  46. , fill_dirty(other.fill_dirty), fill_index_valid(other.fill_index_valid), fill_key(other.fill_key), fill_index(other.fill_index)
  47. , border_dirty(other.border_dirty), border_index_valid(other.border_index_valid), border_key(other.border_key), border_index(other.border_index)
  48. , xf_index(other.xf_index), xf_indexValid(other.xf_indexValid)
  49. , is_dxf_fomat(other.is_dxf_fomat), dxf_index(other.dxf_index), dxf_indexValid(other.dxf_indexValid)
  50. , theme(other.theme)
  51. , properties(other.properties)
  52. {
  53. }
  54. FormatPrivate::~FormatPrivate()
  55. {
  56. }
  57. /*!
  58. * \class Format
  59. * \inmodule QtXlsx
  60. * \brief Providing the methods and properties that are available for formatting cells in Excel.
  61. */
  62. /*!
  63. * \enum Format::FontScript
  64. *
  65. * The enum type defines the type of font script.
  66. *
  67. * \value FontScriptNormal normal
  68. * \value FontScriptSuper super script
  69. * \value FontScriptSub sub script
  70. */
  71. /*!
  72. * \enum Format::FontUnderline
  73. *
  74. * The enum type defines the type of font underline.
  75. *
  76. * \value FontUnderlineNone
  77. * \value FontUnderlineSingle
  78. * \value FontUnderlineDouble
  79. * \value FontUnderlineSingleAccounting
  80. * \value FontUnderlineDoubleAccounting
  81. */
  82. /*!
  83. * \enum Format::HorizontalAlignment
  84. *
  85. * The enum type defines the type of horizontal alignment.
  86. *
  87. * \value AlignHGeneral
  88. * \value AlignLeft
  89. * \value AlignHCenter
  90. * \value AlignRight
  91. * \value AlignHFill
  92. * \value AlignHJustify
  93. * \value AlignHMerge
  94. * \value AlignHDistributed
  95. */
  96. /*!
  97. * \enum Format::VerticalAlignment
  98. *
  99. * The enum type defines the type of vertical alignment.
  100. *
  101. * \value AlignTop,
  102. * \value AlignVCenter,
  103. * \value AlignBottom,
  104. * \value AlignVJustify,
  105. * \value AlignVDistributed
  106. */
  107. /*!
  108. * \enum Format::BorderStyle
  109. *
  110. * The enum type defines the type of font underline.
  111. *
  112. * \value BorderNone
  113. * \value BorderThin
  114. * \value BorderMedium
  115. * \value BorderDashed
  116. * \value BorderDotted
  117. * \value BorderThick
  118. * \value BorderDouble
  119. * \value BorderHair
  120. * \value BorderMediumDashed
  121. * \value BorderDashDot
  122. * \value BorderMediumDashDot
  123. * \value BorderDashDotDot
  124. * \value BorderMediumDashDotDot
  125. * \value BorderSlantDashDot
  126. */
  127. /*!
  128. * \enum Format::DiagonalBorderType
  129. *
  130. * The enum type defines the type of diagonal border.
  131. *
  132. * \value DiagonalBorderNone
  133. * \value DiagonalBorderDown
  134. * \value DiagonalBorderUp
  135. * \value DiagnoalBorderBoth
  136. */
  137. /*!
  138. * \enum Format::FillPattern
  139. *
  140. * The enum type defines the type of fill.
  141. *
  142. * \value PatternNone
  143. * \value PatternSolid
  144. * \value PatternMediumGray
  145. * \value PatternDarkGray
  146. * \value PatternLightGray
  147. * \value PatternDarkHorizontal
  148. * \value PatternDarkVertical
  149. * \value PatternDarkDown
  150. * \value PatternDarkUp
  151. * \value PatternDarkGrid
  152. * \value PatternDarkTrellis
  153. * \value PatternLightHorizontal
  154. * \value PatternLightVertical
  155. * \value PatternLightDown
  156. * \value PatternLightUp
  157. * \value PatternLightTrellis
  158. * \value PatternGray125
  159. * \value PatternGray0625
  160. * \value PatternLightGrid
  161. */
  162. /*!
  163. * Creates a new invalid format.
  164. */
  165. Format::Format()
  166. {
  167. //The d pointer is initialized with a null pointer
  168. }
  169. /*!
  170. Creates a new format with the same attributes as the \a other format.
  171. */
  172. Format::Format(const Format &other)
  173. :d(other.d)
  174. {
  175. }
  176. /*!
  177. Assigns the \a other format to this format, and returns a
  178. reference to this format.
  179. */
  180. Format &Format::operator =(const Format &other)
  181. {
  182. d = other.d;
  183. return *this;
  184. }
  185. /*!
  186. * Destroys this format.
  187. */
  188. Format::~Format()
  189. {
  190. }
  191. /*!
  192. * Returns the number format identifier.
  193. */
  194. int Format::numberFormatIndex() const
  195. {
  196. return intProperty(FormatPrivate::P_NumFmt_Id, 0);
  197. }
  198. /*!
  199. * Set the number format identifier. The \a format
  200. * must be a valid built-in number format identifier
  201. * or the identifier of a custom number format.
  202. */
  203. void Format::setNumberFormatIndex(int format)
  204. {
  205. setProperty(FormatPrivate::P_NumFmt_Id, format);
  206. clearProperty(FormatPrivate::P_NumFmt_FormatCode);
  207. }
  208. /*!
  209. * Returns the number format string.
  210. * \note for built-in number formats, this may
  211. * return an empty string.
  212. */
  213. QString Format::numberFormat() const
  214. {
  215. return stringProperty(FormatPrivate::P_NumFmt_FormatCode);
  216. }
  217. /*!
  218. * Set number \a format.
  219. * http://office.microsoft.com/en-001/excel-help/create-a-custom-number-format-HP010342372.aspx
  220. */
  221. void Format::setNumberFormat(const QString &format)
  222. {
  223. if (format.isEmpty())
  224. return;
  225. setProperty(FormatPrivate::P_NumFmt_FormatCode, format);
  226. clearProperty(FormatPrivate::P_NumFmt_Id); //numFmt id must be re-generated.
  227. }
  228. /*!
  229. * Returns whether the number format is probably a dateTime or not
  230. */
  231. bool Format::isDateTimeFormat() const
  232. {
  233. if (hasProperty(FormatPrivate::P_NumFmt_FormatCode)) {
  234. //Custom numFmt, so
  235. //Gauss from the number string
  236. return NumFormatParser::isDateTime(numberFormat());
  237. } else if (hasProperty(FormatPrivate::P_NumFmt_Id)){
  238. //Non-custom numFmt
  239. int idx = numberFormatIndex();
  240. //Is built-in date time number id?
  241. if ((idx >= 14 && idx <= 22) || (idx >= 45 && idx <= 47))
  242. return true;
  243. if ((idx >= 27 && idx <= 36) || (idx >= 50 && idx <= 58)) //Used in CHS\CHT\JPN\KOR
  244. return true;
  245. }
  246. return false;
  247. }
  248. /*!
  249. \internal
  250. Set a custom num \a format with the given \a id.
  251. */
  252. void Format::setNumberFormat(int id, const QString &format)
  253. {
  254. setProperty(FormatPrivate::P_NumFmt_Id, id);
  255. setProperty(FormatPrivate::P_NumFmt_FormatCode, format);
  256. }
  257. /*!
  258. \internal
  259. Called by styles to fix the numFmt
  260. */
  261. void Format::fixNumberFormat(int id, const QString &format)
  262. {
  263. setProperty(FormatPrivate::P_NumFmt_Id, id, 0, false);
  264. setProperty(FormatPrivate::P_NumFmt_FormatCode, format, QString(), false);
  265. }
  266. /*!
  267. \internal
  268. Return true if the format has number format.
  269. */
  270. bool Format::hasNumFmtData() const
  271. {
  272. if (!d)
  273. return false;
  274. if (hasProperty(FormatPrivate::P_NumFmt_Id)
  275. || hasProperty(FormatPrivate::P_NumFmt_FormatCode)) {
  276. return true;
  277. }
  278. return false;
  279. }
  280. /*!
  281. * Return the size of the font in points.
  282. */
  283. int Format::fontSize() const
  284. {
  285. return intProperty(FormatPrivate::P_Font_Size);
  286. }
  287. /*!
  288. * Set the \a size of the font in points.
  289. */
  290. void Format::setFontSize(int size)
  291. {
  292. setProperty(FormatPrivate::P_Font_Size, size, 0);
  293. }
  294. /*!
  295. * Return whether the font is italic.
  296. */
  297. bool Format::fontItalic() const
  298. {
  299. return boolProperty(FormatPrivate::P_Font_Italic);
  300. }
  301. /*!
  302. * Turn on/off the italic font based on \a italic.
  303. */
  304. void Format::setFontItalic(bool italic)
  305. {
  306. setProperty(FormatPrivate::P_Font_Italic, italic, false);
  307. }
  308. /*!
  309. * Return whether the font is strikeout.
  310. */
  311. bool Format::fontStrikeOut() const
  312. {
  313. return boolProperty(FormatPrivate::P_Font_StrikeOut);
  314. }
  315. /*!
  316. * Turn on/off the strikeOut font based on \a strikeOut.
  317. */
  318. void Format::setFontStrikeOut(bool strikeOut)
  319. {
  320. setProperty(FormatPrivate::P_Font_StrikeOut, strikeOut, false);
  321. }
  322. /*!
  323. * Return the color of the font.
  324. */
  325. QColor Format::fontColor() const
  326. {
  327. if (hasProperty(FormatPrivate::P_Font_Color))
  328. return colorProperty(FormatPrivate::P_Font_Color);
  329. return QColor();
  330. }
  331. /*!
  332. * Set the \a color of the font.
  333. */
  334. void Format::setFontColor(const QColor &color)
  335. {
  336. setProperty(FormatPrivate::P_Font_Color, XlsxColor(color), XlsxColor());
  337. }
  338. /*!
  339. * Return whether the font is bold.
  340. */
  341. bool Format::fontBold() const
  342. {
  343. return boolProperty(FormatPrivate::P_Font_Bold);
  344. }
  345. /*!
  346. * Turn on/off the bold font based on the given \a bold.
  347. */
  348. void Format::setFontBold(bool bold)
  349. {
  350. setProperty(FormatPrivate::P_Font_Bold, bold, false);
  351. }
  352. /*!
  353. * Return the script style of the font.
  354. */
  355. Format::FontScript Format::fontScript() const
  356. {
  357. return static_cast<Format::FontScript>(intProperty(FormatPrivate::P_Font_Script));
  358. }
  359. /*!
  360. * Set the script style of the font to \a script.
  361. */
  362. void Format::setFontScript(FontScript script)
  363. {
  364. setProperty(FormatPrivate::P_Font_Script, script, FontScriptNormal);
  365. }
  366. /*!
  367. * Return the underline style of the font.
  368. */
  369. Format::FontUnderline Format::fontUnderline() const
  370. {
  371. return static_cast<Format::FontUnderline>(intProperty(FormatPrivate::P_Font_Underline));
  372. }
  373. /*!
  374. * Set the underline style of the font to \a underline.
  375. */
  376. void Format::setFontUnderline(FontUnderline underline)
  377. {
  378. setProperty(FormatPrivate::P_Font_Underline, underline, FontUnderlineNone);
  379. }
  380. /*!
  381. * Return whether the font is outline.
  382. */
  383. bool Format::fontOutline() const
  384. {
  385. return boolProperty(FormatPrivate::P_Font_Outline);
  386. }
  387. /*!
  388. * Turn on/off the outline font based on \a outline.
  389. */
  390. void Format::setFontOutline(bool outline)
  391. {
  392. setProperty(FormatPrivate::P_Font_Outline, outline, false);
  393. }
  394. /*!
  395. * Return the name of the font.
  396. */
  397. QString Format::fontName() const
  398. {
  399. return stringProperty(FormatPrivate::P_Font_Name, QStringLiteral("Calibri"));
  400. }
  401. /*!
  402. * Set the name of the font to \a name.
  403. */
  404. void Format::setFontName(const QString &name)
  405. {
  406. setProperty(FormatPrivate::P_Font_Name, name, QStringLiteral("Calibri"));
  407. }
  408. /*!
  409. * Returns a QFont object based on font data contained in the format.
  410. */
  411. QFont Format::font() const
  412. {
  413. QFont font;
  414. font.setFamily(fontName());
  415. if (fontSize() > 0)
  416. font.setPointSize(fontSize());
  417. font.setBold(fontBold());
  418. font.setItalic(fontItalic());
  419. font.setUnderline(fontUnderline()!=FontUnderlineNone);
  420. font.setStrikeOut(fontStrikeOut());
  421. return font;
  422. }
  423. /*!
  424. * Set the format properties from the given \a font.
  425. */
  426. void Format::setFont(const QFont &font)
  427. {
  428. setFontName(font.family());
  429. if (font.pointSize() > 0)
  430. setFontSize(font.pointSize());
  431. setFontBold(font.bold());
  432. setFontItalic(font.italic());
  433. setFontUnderline(font.underline() ? FontUnderlineSingle : FontUnderlineNone);
  434. setFontStrikeOut(font.strikeOut());
  435. }
  436. /*!
  437. * \internal
  438. * When the format has font data, when need to assign a valid index for it.
  439. * The index value is depend on the order <fonts > in styles.xml
  440. */
  441. bool Format::fontIndexValid() const
  442. {
  443. if (!hasFontData())
  444. return false;
  445. return d->font_index_valid;
  446. }
  447. /*!
  448. * \internal
  449. */
  450. int Format::fontIndex() const
  451. {
  452. if (fontIndexValid())
  453. return d->font_index;
  454. return 0;
  455. }
  456. /*!
  457. * \internal
  458. */
  459. void Format::setFontIndex(int index)
  460. {
  461. d->font_index = index;
  462. d->font_index_valid = true;
  463. }
  464. /*!
  465. * \internal
  466. */
  467. QByteArray Format::fontKey() const
  468. {
  469. if (isEmpty())
  470. return QByteArray();
  471. if (d->font_dirty) {
  472. QByteArray key;
  473. QDataStream stream(&key, QIODevice::WriteOnly);
  474. for (int i=FormatPrivate::P_Font_STARTID; i<FormatPrivate::P_Font_ENDID; ++i) {
  475. if (d->properties.contains(i))
  476. stream << i << d->properties[i];
  477. };
  478. const_cast<Format*>(this)->d->font_key = key;
  479. const_cast<Format*>(this)->d->font_dirty = false;
  480. }
  481. return d->font_key;
  482. }
  483. /*!
  484. \internal
  485. Return true if the format has font format, otherwise return false.
  486. */
  487. bool Format::hasFontData() const
  488. {
  489. if (!d)
  490. return false;
  491. for (int i=FormatPrivate::P_Font_STARTID; i<FormatPrivate::P_Font_ENDID; ++i) {
  492. if (hasProperty(i))
  493. return true;
  494. }
  495. return false;
  496. }
  497. /*!
  498. * Return the horizontal alignment.
  499. */
  500. Format::HorizontalAlignment Format::horizontalAlignment() const
  501. {
  502. return static_cast<Format::HorizontalAlignment>(intProperty(FormatPrivate::P_Alignment_AlignH, AlignHGeneral));
  503. }
  504. /*!
  505. * Set the horizontal alignment with the given \a align.
  506. */
  507. void Format::setHorizontalAlignment(HorizontalAlignment align)
  508. {
  509. if (hasProperty(FormatPrivate::P_Alignment_Indent)
  510. &&(align != AlignHGeneral && align != AlignLeft && align != AlignRight && align != AlignHDistributed)) {
  511. clearProperty(FormatPrivate::P_Alignment_Indent);
  512. }
  513. if (hasProperty(FormatPrivate::P_Alignment_ShinkToFit)
  514. && (align == AlignHFill || align == AlignHJustify || align == AlignHDistributed)) {
  515. clearProperty(FormatPrivate::P_Alignment_ShinkToFit);
  516. }
  517. setProperty(FormatPrivate::P_Alignment_AlignH, align, AlignHGeneral);
  518. }
  519. /*!
  520. * Return the vertical alignment.
  521. */
  522. Format::VerticalAlignment Format::verticalAlignment() const
  523. {
  524. return static_cast<Format::VerticalAlignment>(intProperty(FormatPrivate::P_Alignment_AlignV, AlignBottom));
  525. }
  526. /*!
  527. * Set the vertical alignment with the given \a align.
  528. */
  529. void Format::setVerticalAlignment(VerticalAlignment align)
  530. {
  531. setProperty(FormatPrivate::P_Alignment_AlignV, align, AlignBottom);
  532. }
  533. /*!
  534. * Return whether the cell text is wrapped.
  535. */
  536. bool Format::textWrap() const
  537. {
  538. return boolProperty(FormatPrivate::P_Alignment_Wrap);
  539. }
  540. /*!
  541. * Enable the text wrap if \a wrap is true.
  542. */
  543. void Format::setTextWarp(bool wrap)
  544. {
  545. if (wrap && hasProperty(FormatPrivate::P_Alignment_ShinkToFit))
  546. clearProperty(FormatPrivate::P_Alignment_ShinkToFit);
  547. setProperty(FormatPrivate::P_Alignment_Wrap, wrap, false);
  548. }
  549. /*!
  550. * Return the text rotation.
  551. */
  552. int Format::rotation() const
  553. {
  554. return intProperty(FormatPrivate::P_Alignment_Rotation);
  555. }
  556. /*!
  557. * Set the text roation with the given \a rotation. Must be in the range [0, 180] or 255.
  558. */
  559. void Format::setRotation(int rotation)
  560. {
  561. setProperty(FormatPrivate::P_Alignment_Rotation, rotation, 0);
  562. }
  563. /*!
  564. * Return the text indentation level.
  565. */
  566. int Format::indent() const
  567. {
  568. return intProperty(FormatPrivate::P_Alignment_Indent);
  569. }
  570. /*!
  571. * Set the text indentation level with the given \a indent. Must be less than or equal to 15.
  572. */
  573. void Format::setIndent(int indent)
  574. {
  575. if (indent && hasProperty(FormatPrivate::P_Alignment_AlignH)) {
  576. HorizontalAlignment hl = horizontalAlignment();
  577. if (hl != AlignHGeneral && hl != AlignLeft && hl!= AlignRight && hl!= AlignHJustify) {
  578. setHorizontalAlignment(AlignLeft);
  579. }
  580. }
  581. setProperty(FormatPrivate::P_Alignment_Indent, indent, 0);
  582. }
  583. /*!
  584. * Return whether the cell is shrink to fit.
  585. */
  586. bool Format::shrinkToFit() const
  587. {
  588. return boolProperty(FormatPrivate::P_Alignment_ShinkToFit);
  589. }
  590. /*!
  591. * Turn on/off shrink to fit base on \a shink.
  592. */
  593. void Format::setShrinkToFit(bool shink)
  594. {
  595. if (shink && hasProperty(FormatPrivate::P_Alignment_Wrap))
  596. clearProperty(FormatPrivate::P_Alignment_Wrap);
  597. if (shink && hasProperty(FormatPrivate::P_Alignment_AlignH)) {
  598. HorizontalAlignment hl = horizontalAlignment();
  599. if (hl == AlignHFill || hl == AlignHJustify || hl == AlignHDistributed)
  600. setHorizontalAlignment(AlignLeft);
  601. }
  602. setProperty(FormatPrivate::P_Alignment_ShinkToFit, shink, false);
  603. }
  604. /*!
  605. * \internal
  606. */
  607. bool Format::hasAlignmentData() const
  608. {
  609. if (!d)
  610. return false;
  611. for (int i=FormatPrivate::P_Alignment_STARTID; i<FormatPrivate::P_Alignment_ENDID; ++i) {
  612. if (hasProperty(i))
  613. return true;
  614. }
  615. return false;
  616. }
  617. /*!
  618. * Set the border style with the given \a style.
  619. */
  620. void Format::setBorderStyle(BorderStyle style)
  621. {
  622. setLeftBorderStyle(style);
  623. setRightBorderStyle(style);
  624. setBottomBorderStyle(style);
  625. setTopBorderStyle(style);
  626. }
  627. /*!
  628. * Sets the border color with the given \a color.
  629. */
  630. void Format::setBorderColor(const QColor &color)
  631. {
  632. setLeftBorderColor(color);
  633. setRightBorderColor(color);
  634. setTopBorderColor(color);
  635. setBottomBorderColor(color);
  636. }
  637. /*!
  638. * Returns the left border style
  639. */
  640. Format::BorderStyle Format::leftBorderStyle() const
  641. {
  642. return static_cast<BorderStyle>(intProperty(FormatPrivate::P_Border_LeftStyle));
  643. }
  644. /*!
  645. * Sets the left border style to \a style
  646. */
  647. void Format::setLeftBorderStyle(BorderStyle style)
  648. {
  649. setProperty(FormatPrivate::P_Border_LeftStyle, style, BorderNone);
  650. }
  651. /*!
  652. * Returns the left border color
  653. */
  654. QColor Format::leftBorderColor() const
  655. {
  656. return colorProperty(FormatPrivate::P_Border_LeftColor);
  657. }
  658. /*!
  659. Sets the left border color to the given \a color
  660. */
  661. void Format::setLeftBorderColor(const QColor &color)
  662. {
  663. setProperty(FormatPrivate::P_Border_LeftColor, XlsxColor(color), XlsxColor());
  664. }
  665. /*!
  666. Returns the right border style.
  667. */
  668. Format::BorderStyle Format::rightBorderStyle() const
  669. {
  670. return static_cast<BorderStyle>(intProperty(FormatPrivate::P_Border_RightStyle));
  671. }
  672. /*!
  673. Sets the right border style to the given \a style.
  674. */
  675. void Format::setRightBorderStyle(BorderStyle style)
  676. {
  677. setProperty(FormatPrivate::P_Border_RightStyle, style, BorderNone);
  678. }
  679. /*!
  680. Returns the right border color.
  681. */
  682. QColor Format::rightBorderColor() const
  683. {
  684. return colorProperty(FormatPrivate::P_Border_RightColor);
  685. }
  686. /*!
  687. Sets the right border color to the given \a color
  688. */
  689. void Format::setRightBorderColor(const QColor &color)
  690. {
  691. setProperty(FormatPrivate::P_Border_RightColor, XlsxColor(color), XlsxColor());
  692. }
  693. /*!
  694. Returns the top border style.
  695. */
  696. Format::BorderStyle Format::topBorderStyle() const
  697. {
  698. return static_cast<BorderStyle>(intProperty(FormatPrivate::P_Border_TopStyle));
  699. }
  700. /*!
  701. Sets the top border style to the given \a style.
  702. */
  703. void Format::setTopBorderStyle(BorderStyle style)
  704. {
  705. setProperty(FormatPrivate::P_Border_TopStyle, style, BorderNone);
  706. }
  707. /*!
  708. Returns the top border color.
  709. */
  710. QColor Format::topBorderColor() const
  711. {
  712. return colorProperty(FormatPrivate::P_Border_TopColor);
  713. }
  714. /*!
  715. Sets the top border color to the given \a color.
  716. */
  717. void Format::setTopBorderColor(const QColor &color)
  718. {
  719. setProperty(FormatPrivate::P_Border_TopColor, XlsxColor(color), XlsxColor());
  720. }
  721. /*!
  722. Returns the bottom border style.
  723. */
  724. Format::BorderStyle Format::bottomBorderStyle() const
  725. {
  726. return static_cast<BorderStyle>(intProperty(FormatPrivate::P_Border_BottomStyle));
  727. }
  728. /*!
  729. Sets the bottom border style to the given \a style.
  730. */
  731. void Format::setBottomBorderStyle(BorderStyle style)
  732. {
  733. setProperty(FormatPrivate::P_Border_BottomStyle, style, BorderNone);
  734. }
  735. /*!
  736. Returns the bottom border color.
  737. */
  738. QColor Format::bottomBorderColor() const
  739. {
  740. return colorProperty(FormatPrivate::P_Border_BottomColor);
  741. }
  742. /*!
  743. Sets the bottom border color to the given \a color.
  744. */
  745. void Format::setBottomBorderColor(const QColor &color)
  746. {
  747. setProperty(FormatPrivate::P_Border_BottomColor, XlsxColor(color), XlsxColor());
  748. }
  749. /*!
  750. Return the diagonla border style.
  751. */
  752. Format::BorderStyle Format::diagonalBorderStyle() const
  753. {
  754. return static_cast<BorderStyle>(intProperty(FormatPrivate::P_Border_DiagonalStyle));
  755. }
  756. /*!
  757. Sets the diagonal border style to the given \a style.
  758. */
  759. void Format::setDiagonalBorderStyle(BorderStyle style)
  760. {
  761. setProperty(FormatPrivate::P_Border_DiagonalStyle, style, BorderNone);
  762. }
  763. /*!
  764. Returns the diagonal border type.
  765. */
  766. Format::DiagonalBorderType Format::diagonalBorderType() const
  767. {
  768. return static_cast<DiagonalBorderType>(intProperty(FormatPrivate::P_Border_DiagonalType));
  769. }
  770. /*!
  771. Sets the diagonal border type to the given \a style
  772. */
  773. void Format::setDiagonalBorderType(DiagonalBorderType style)
  774. {
  775. setProperty(FormatPrivate::P_Border_DiagonalType, style, DiagonalBorderNone);
  776. }
  777. /*!
  778. Returns the diagonal border color.
  779. */
  780. QColor Format::diagonalBorderColor() const
  781. {
  782. return colorProperty(FormatPrivate::P_Border_DiagonalColor);
  783. }
  784. /*!
  785. Sets the diagonal border color to the given \a color
  786. */
  787. void Format::setDiagonalBorderColor(const QColor &color)
  788. {
  789. setProperty(FormatPrivate::P_Border_DiagonalColor, XlsxColor(color), XlsxColor());
  790. }
  791. /*!
  792. \internal
  793. Returns whether this format has been set valid border index.
  794. */
  795. bool Format::borderIndexValid() const
  796. {
  797. if (!hasBorderData())
  798. return false;
  799. return d->border_index_valid;
  800. }
  801. /*!
  802. \internal
  803. Returns the border index.
  804. */
  805. int Format::borderIndex() const
  806. {
  807. if (borderIndexValid())
  808. return d->border_index;
  809. return 0;
  810. }
  811. /*!
  812. * \internal
  813. */
  814. void Format::setBorderIndex(int index)
  815. {
  816. d->border_index = index;
  817. d->border_index_valid = true;
  818. }
  819. /*! \internal
  820. */
  821. QByteArray Format::borderKey() const
  822. {
  823. if (isEmpty())
  824. return QByteArray();
  825. if (d->border_dirty) {
  826. QByteArray key;
  827. QDataStream stream(&key, QIODevice::WriteOnly);
  828. for (int i=FormatPrivate::P_Border_STARTID; i<FormatPrivate::P_Border_ENDID; ++i) {
  829. if (d->properties.contains(i))
  830. stream << i << d->properties[i];
  831. };
  832. const_cast<Format*>(this)->d->border_key = key;
  833. const_cast<Format*>(this)->d->border_dirty = false;
  834. }
  835. return d->border_key;
  836. }
  837. /*!
  838. \internal
  839. Return true if the format has border format, otherwise return false.
  840. */
  841. bool Format::hasBorderData() const
  842. {
  843. if (!d)
  844. return false;
  845. for (int i=FormatPrivate::P_Border_STARTID; i<FormatPrivate::P_Border_ENDID; ++i) {
  846. if (hasProperty(i))
  847. return true;
  848. }
  849. return false;
  850. }
  851. /*!
  852. Return the fill pattern.
  853. */
  854. Format::FillPattern Format::fillPattern() const
  855. {
  856. return static_cast<FillPattern>(intProperty(FormatPrivate::P_Fill_Pattern, PatternNone));
  857. }
  858. /*!
  859. Sets the fill pattern to the given \a pattern.
  860. */
  861. void Format::setFillPattern(FillPattern pattern)
  862. {
  863. setProperty(FormatPrivate::P_Fill_Pattern, pattern, PatternNone);
  864. }
  865. /*!
  866. Returns the foreground color of the pattern.
  867. */
  868. QColor Format::patternForegroundColor() const
  869. {
  870. return colorProperty(FormatPrivate::P_Fill_FgColor);
  871. }
  872. /*!
  873. Sets the foreground color of the pattern with the given \a color.
  874. */
  875. void Format::setPatternForegroundColor(const QColor &color)
  876. {
  877. if (color.isValid() && !hasProperty(FormatPrivate::P_Fill_Pattern))
  878. setFillPattern(PatternSolid);
  879. setProperty(FormatPrivate::P_Fill_FgColor, XlsxColor(color), XlsxColor());
  880. }
  881. /*!
  882. Returns the background color of the pattern.
  883. */
  884. QColor Format::patternBackgroundColor() const
  885. {
  886. return colorProperty(FormatPrivate::P_Fill_BgColor);
  887. }
  888. /*!
  889. Sets the background color of the pattern with the given \a color.
  890. */
  891. void Format::setPatternBackgroundColor(const QColor &color)
  892. {
  893. if (color.isValid() && !hasProperty(FormatPrivate::P_Fill_Pattern))
  894. setFillPattern(PatternSolid);
  895. setProperty(FormatPrivate::P_Fill_BgColor, XlsxColor(color), XlsxColor());
  896. }
  897. /*!
  898. * \internal
  899. */
  900. bool Format::fillIndexValid() const
  901. {
  902. if (!hasFillData())
  903. return false;
  904. return d->fill_index_valid;
  905. }
  906. /*!
  907. * \internal
  908. */
  909. int Format::fillIndex() const
  910. {
  911. if (fillIndexValid())
  912. return d->fill_index;
  913. return 0;
  914. }
  915. /*!
  916. * \internal
  917. */
  918. void Format::setFillIndex(int index)
  919. {
  920. d->fill_index = index;
  921. d->fill_index_valid = true;
  922. }
  923. /*!
  924. * \internal
  925. */
  926. QByteArray Format::fillKey() const
  927. {
  928. if (isEmpty())
  929. return QByteArray();
  930. if (d->fill_dirty) {
  931. QByteArray key;
  932. QDataStream stream(&key, QIODevice::WriteOnly);
  933. for (int i=FormatPrivate::P_Fill_STARTID; i<FormatPrivate::P_Fill_ENDID; ++i) {
  934. if (d->properties.contains(i))
  935. stream << i << d->properties[i];
  936. };
  937. const_cast<Format*>(this)->d->fill_key = key;
  938. const_cast<Format*>(this)->d->fill_dirty = false;
  939. }
  940. return d->fill_key;
  941. }
  942. /*!
  943. \internal
  944. Return true if the format has fill format, otherwise return false.
  945. */
  946. bool Format::hasFillData() const
  947. {
  948. if (!d)
  949. return false;
  950. for (int i=FormatPrivate::P_Fill_STARTID; i<FormatPrivate::P_Fill_ENDID; ++i) {
  951. if (hasProperty(i))
  952. return true;
  953. }
  954. return false;
  955. }
  956. /*!
  957. Returns whether the hidden protection property is set to true.
  958. */
  959. bool Format::hidden() const
  960. {
  961. return boolProperty(FormatPrivate::P_Protection_Hidden);
  962. }
  963. /*!
  964. Sets the hidden protection property with the given \a hidden.
  965. */
  966. void Format::setHidden(bool hidden)
  967. {
  968. setProperty(FormatPrivate::P_Protection_Hidden, hidden);
  969. }
  970. /*!
  971. Returns whether the locked protection property is set to true.
  972. */
  973. bool Format::locked() const
  974. {
  975. return boolProperty(FormatPrivate::P_Protection_Locked);
  976. }
  977. /*!
  978. Sets the locked protection property with the given \a locked.
  979. */
  980. void Format::setLocked(bool locked)
  981. {
  982. setProperty(FormatPrivate::P_Protection_Locked, locked);
  983. }
  984. /*!
  985. \internal
  986. Return true if the format has protection data, otherwise return false.
  987. */
  988. bool Format::hasProtectionData() const
  989. {
  990. if (!d)
  991. return false;
  992. //追逐 不知道有没有bug
  993. if (hasProperty(FormatPrivate::P_Protection_Hidden
  994. | FormatPrivate::P_Protection_Locked)) {
  995. return true;
  996. }
  997. return false;
  998. }
  999. /*!
  1000. Merges the current format with the properties described by format \a modifier.
  1001. */
  1002. void Format::mergeFormat(const Format &modifier)
  1003. {
  1004. if (!modifier.isValid())
  1005. return;
  1006. if (!isValid()) {
  1007. d = modifier.d;
  1008. return;
  1009. }
  1010. QMapIterator<int, QVariant> it(modifier.d->properties);
  1011. while(it.hasNext()) {
  1012. it.next();
  1013. setProperty(it.key(), it.value());
  1014. }
  1015. }
  1016. /*!
  1017. Returns true if the format is valid; otherwise returns false.
  1018. */
  1019. bool Format::isValid() const
  1020. {
  1021. if (d)
  1022. return true;
  1023. return false;
  1024. }
  1025. /*!
  1026. Returns true if the format is empty; otherwise returns false.
  1027. */
  1028. bool Format::isEmpty() const
  1029. {
  1030. if (!d)
  1031. return true;
  1032. return d->properties.isEmpty();
  1033. }
  1034. /*!
  1035. * \internal
  1036. */
  1037. QByteArray Format::formatKey() const
  1038. {
  1039. if (isEmpty())
  1040. return QByteArray();
  1041. if (d->dirty) {
  1042. QByteArray key;
  1043. QDataStream stream(&key, QIODevice::WriteOnly);
  1044. QMapIterator<int, QVariant> i(d->properties);
  1045. while (i.hasNext()) {
  1046. i.next();
  1047. stream<<i.key()<<i.value();
  1048. }
  1049. d->formatKey = key;
  1050. d->dirty = false;
  1051. }
  1052. return d->formatKey;
  1053. }
  1054. /*!
  1055. * \internal
  1056. * Called by QXlsx::Styles or some unittests.
  1057. */
  1058. void Format::setXfIndex(int index)
  1059. {
  1060. if (!d)
  1061. d = new FormatPrivate;
  1062. d->xf_index = index;
  1063. d->xf_indexValid = true;
  1064. }
  1065. /*!
  1066. * \internal
  1067. */
  1068. int Format::xfIndex() const
  1069. {
  1070. if (!d)
  1071. return -1;
  1072. return d->xf_index;
  1073. }
  1074. /*!
  1075. * \internal
  1076. */
  1077. bool Format::xfIndexValid() const
  1078. {
  1079. if (!d)
  1080. return false;
  1081. return d->xf_indexValid;
  1082. }
  1083. /*!
  1084. * \internal
  1085. * Called by QXlsx::Styles or some unittests.
  1086. */
  1087. void Format::setDxfIndex(int index)
  1088. {
  1089. if (!d)
  1090. d = new FormatPrivate;
  1091. d->dxf_index = index;
  1092. d->dxf_indexValid = true;
  1093. }
  1094. /*!
  1095. * \internal
  1096. * Returns the index in the styles dxfs.
  1097. */
  1098. int Format::dxfIndex() const
  1099. {
  1100. if (!d)
  1101. return -1;
  1102. return d->dxf_index;
  1103. }
  1104. /*!
  1105. * \internal
  1106. * Returns whether the dxf index is valid or not.
  1107. */
  1108. bool Format::dxfIndexValid() const
  1109. {
  1110. if (!d)
  1111. return false;
  1112. return d->dxf_indexValid;
  1113. }
  1114. /*!
  1115. Returns ture if the \a format is equal to this format.
  1116. */
  1117. bool Format::operator ==(const Format &format) const
  1118. {
  1119. return this->formatKey() == format.formatKey();
  1120. }
  1121. /*!
  1122. Returns ture if the \a format is not equal to this format.
  1123. */
  1124. bool Format::operator !=(const Format &format) const
  1125. {
  1126. return this->formatKey() != format.formatKey();
  1127. }
  1128. int Format::theme() const
  1129. {
  1130. return d->theme;
  1131. }
  1132. /*!
  1133. * \internal
  1134. */
  1135. QVariant Format::property(int propertyId, const QVariant &defaultValue) const
  1136. {
  1137. if (d && d->properties.contains(propertyId))
  1138. return d->properties[propertyId];
  1139. return defaultValue;
  1140. }
  1141. /*!
  1142. * \internal
  1143. */
  1144. void Format::setProperty(int propertyId, const QVariant &value, const QVariant &clearValue, bool detach)
  1145. {
  1146. if (!d)
  1147. d = new FormatPrivate;
  1148. if (value != clearValue) {
  1149. if (d->properties.contains(propertyId) && d->properties[propertyId] == value)
  1150. return;
  1151. if (detach)
  1152. d.detach();
  1153. d->properties[propertyId] = value;
  1154. } else {
  1155. if (!d->properties.contains(propertyId))
  1156. return;
  1157. if (detach)
  1158. d.detach();
  1159. d->properties.remove(propertyId);
  1160. }
  1161. d->dirty = true;
  1162. d->xf_indexValid = false;
  1163. d->dxf_indexValid = false;
  1164. if (propertyId >= FormatPrivate::P_Font_STARTID && propertyId < FormatPrivate::P_Font_ENDID) {
  1165. d->font_dirty = true;
  1166. d->font_index_valid = false;
  1167. } else if (propertyId >= FormatPrivate::P_Border_STARTID && propertyId < FormatPrivate::P_Border_ENDID) {
  1168. d->border_dirty = true;
  1169. d->border_index_valid = false;
  1170. } else if (propertyId >= FormatPrivate::P_Fill_STARTID && propertyId < FormatPrivate::P_Fill_ENDID) {
  1171. d->fill_dirty = true;
  1172. d->fill_index_valid = false;
  1173. }
  1174. }
  1175. /*!
  1176. * \internal
  1177. */
  1178. void Format::clearProperty(int propertyId)
  1179. {
  1180. setProperty(propertyId, QVariant());
  1181. }
  1182. /*!
  1183. * \internal
  1184. */
  1185. bool Format::hasProperty(int propertyId) const
  1186. {
  1187. if (!d)
  1188. return false;
  1189. return d->properties.contains(propertyId);
  1190. }
  1191. /*!
  1192. * \internal
  1193. */
  1194. bool Format::boolProperty(int propertyId, bool defaultValue) const
  1195. {
  1196. if (!hasProperty(propertyId))
  1197. return defaultValue;
  1198. const QVariant prop = d->properties[propertyId];
  1199. if (prop.userType() != QMetaType::Bool)
  1200. return defaultValue;
  1201. return prop.toBool();
  1202. }
  1203. /*!
  1204. * \internal
  1205. */
  1206. int Format::intProperty(int propertyId, int defaultValue) const
  1207. {
  1208. if (!hasProperty(propertyId))
  1209. return defaultValue;
  1210. const QVariant prop = d->properties[propertyId];
  1211. if (prop.userType() != QMetaType::Int)
  1212. return defaultValue;
  1213. return prop.toInt();
  1214. }
  1215. /*!
  1216. * \internal
  1217. */
  1218. double Format::doubleProperty(int propertyId, double defaultValue) const
  1219. {
  1220. if (!hasProperty(propertyId))
  1221. return defaultValue;
  1222. const QVariant prop = d->properties[propertyId];
  1223. if (prop.userType() != QMetaType::Double && prop.userType() != QMetaType::Float)
  1224. return defaultValue;
  1225. return prop.toDouble();
  1226. }
  1227. /*!
  1228. * \internal
  1229. */
  1230. QString Format::stringProperty(int propertyId, const QString &defaultValue) const
  1231. {
  1232. if (!hasProperty(propertyId))
  1233. return defaultValue;
  1234. const QVariant prop = d->properties[propertyId];
  1235. if (prop.userType() != QMetaType::QString)
  1236. return defaultValue;
  1237. return prop.toString();
  1238. }
  1239. /*!
  1240. * \internal
  1241. */
  1242. QColor Format::colorProperty(int propertyId, const QColor &defaultValue) const
  1243. {
  1244. if (!hasProperty(propertyId))
  1245. return defaultValue;
  1246. const QVariant prop = d->properties[propertyId];
  1247. if (prop.userType() != qMetaTypeId<XlsxColor>())
  1248. return defaultValue;
  1249. return qvariant_cast<XlsxColor>(prop).rgbColor();
  1250. }
  1251. #ifndef QT_NO_DEBUG_STREAM
  1252. QDebug operator<<(QDebug dbg, const Format &f)
  1253. {
  1254. dbg.nospace() << "QXlsx::Format(" << f.d->properties << ")";
  1255. return dbg.space();
  1256. }
  1257. #endif
  1258. QT_END_NAMESPACE_XLSX