format.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. // Copyright (c) 2010-2015 openpyxl
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE
  21. //
  22. // @license: http://www.opensource.org/licenses/mit-license.php
  23. // @author: see AUTHORS file
  24. // detail imports must come first in this file.
  25. #include <detail/implementations/format_impl.hpp>
  26. #include <detail/implementations/stylesheet.hpp>
  27. #include <xlnt/styles/format.hpp>
  28. #include <xlnt/styles/style.hpp>
  29. namespace xlnt {
  30. format::format(detail::format_impl *d)
  31. : d_(d)
  32. {
  33. }
  34. void format::clear_style()
  35. {
  36. d_->style.clear();
  37. }
  38. format format::style(const xlnt::style &new_style)
  39. {
  40. d_ = d_->parent->find_or_create_with(d_, new_style.name());
  41. return format(d_);
  42. }
  43. format format::style(const std::string &new_style)
  44. {
  45. d_->style = new_style;
  46. return format(d_);
  47. }
  48. bool format::has_style() const
  49. {
  50. return d_->style.is_set();
  51. }
  52. style format::style()
  53. {
  54. if (!has_style())
  55. {
  56. throw invalid_attribute();
  57. }
  58. return d_->parent->style(d_->style.get());
  59. }
  60. const style format::style() const
  61. {
  62. if (!has_style())
  63. {
  64. throw invalid_attribute();
  65. }
  66. return d_->parent->style(d_->style.get());
  67. }
  68. xlnt::alignment format::alignment() const
  69. {
  70. return d_->parent->alignments.at(d_->alignment_id.get());
  71. }
  72. format format::alignment(const xlnt::alignment &new_alignment, optional<bool> applied)
  73. {
  74. d_ = d_->parent->find_or_create_with(d_, new_alignment, applied);
  75. return format(d_);
  76. }
  77. xlnt::border format::border() const
  78. {
  79. return d_->parent->borders.at(d_->border_id.get());
  80. }
  81. format format::border(const xlnt::border &new_border, optional<bool> applied)
  82. {
  83. d_ = d_->parent->find_or_create_with(d_, new_border, applied);
  84. return format(d_);
  85. }
  86. xlnt::fill format::fill() const
  87. {
  88. return d_->parent->fills.at(d_->fill_id.get());
  89. }
  90. format format::fill(const xlnt::fill &new_fill, optional<bool> applied)
  91. {
  92. d_ = d_->parent->find_or_create_with(d_, new_fill, applied);
  93. return format(d_);
  94. }
  95. xlnt::font format::font() const
  96. {
  97. return d_->parent->fonts.at(d_->font_id.get());
  98. }
  99. format format::font(const xlnt::font &new_font, optional<bool> applied)
  100. {
  101. d_ = d_->parent->find_or_create_with(d_, new_font, applied);
  102. return format(d_);
  103. }
  104. xlnt::number_format format::number_format() const
  105. {
  106. if (d_->number_format_id.is_set())
  107. {
  108. const auto number_format_id = d_->number_format_id.get();
  109. if (number_format::is_builtin_format(number_format_id))
  110. {
  111. return number_format::from_builtin_id(number_format_id);
  112. }
  113. const auto it = std::find_if(d_->parent->number_formats.begin(),
  114. d_->parent->number_formats.end(),
  115. [number_format_id](const xlnt::number_format &nf)
  116. {
  117. return nf.id() == number_format_id;
  118. });
  119. if (it != d_->parent->number_formats.end())
  120. {
  121. return *it;
  122. }
  123. }
  124. return xlnt::number_format();
  125. }
  126. format format::number_format(const xlnt::number_format &new_number_format, optional<bool> applied)
  127. {
  128. auto copy = new_number_format;
  129. if (!copy.has_id())
  130. {
  131. copy.id(d_->parent->next_custom_number_format_id());
  132. d_->parent->number_formats.push_back(copy);
  133. }
  134. d_ = d_->parent->find_or_create_with(d_, copy, applied);
  135. return format(d_);
  136. }
  137. xlnt::protection format::protection() const
  138. {
  139. return d_->parent->protections.at(d_->protection_id.get());
  140. }
  141. format format::protection(const xlnt::protection &new_protection, optional<bool> applied)
  142. {
  143. d_ = d_->parent->find_or_create_with(d_, new_protection, applied);
  144. return format(d_);
  145. }
  146. bool format::alignment_applied() const
  147. {
  148. return d_->alignment_applied.is_set()
  149. ? d_->alignment_applied.get()
  150. : d_->alignment_id.is_set();
  151. }
  152. bool format::border_applied() const
  153. {
  154. return d_->border_applied.is_set()
  155. ? d_->border_applied.get()
  156. : d_->border_id.is_set();
  157. }
  158. bool format::fill_applied() const
  159. {
  160. return d_->fill_applied.is_set()
  161. ? d_->fill_applied.get()
  162. : d_->fill_id.is_set();
  163. }
  164. bool format::font_applied() const
  165. {
  166. return d_->font_applied.is_set()
  167. ? d_->font_applied.get()
  168. : d_->font_id.is_set();
  169. }
  170. bool format::number_format_applied() const
  171. {
  172. return d_->number_format_applied.is_set()
  173. ? d_->number_format_applied.get()
  174. : d_->number_format_id.is_set();
  175. }
  176. bool format::protection_applied() const
  177. {
  178. return d_->protection_applied.is_set()
  179. ? d_->protection_applied.get()
  180. : d_->protection_id.is_set();
  181. }
  182. bool format::pivot_button() const
  183. {
  184. return d_->pivot_button_;
  185. }
  186. void format::pivot_button(bool show)
  187. {
  188. d_->pivot_button_ = show;
  189. }
  190. bool format::quote_prefix() const
  191. {
  192. return d_->quote_prefix_;
  193. }
  194. void format::quote_prefix(bool quote)
  195. {
  196. d_->quote_prefix_ = quote;
  197. }
  198. } // namespace xlnt