From 85e1b4a333d143612cac697701e7c72241cbcdb5 Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Fri, 10 Jun 2016 13:40:50 -0400 Subject: [PATCH] refactor styles... again --- include/xlnt/cell/cell.hpp | 22 +- .../xlnt/serialization/excel_serializer.hpp | 3 + .../xlnt/serialization/style_serializer.hpp | 211 +--- .../serialization/worksheet_serializer.hpp | 4 +- .../{formattable.hpp => base_format.hpp} | 8 +- include/xlnt/styles/fill.hpp | 24 +- include/xlnt/styles/font.hpp | 2 + include/xlnt/styles/format.hpp | 17 +- include/xlnt/styles/style.hpp | 4 +- include/xlnt/workbook/workbook.hpp | 11 +- include/xlnt/worksheet/worksheet.hpp | 2 + source/cell/cell.cpp | 61 +- source/detail/cell_impl.hpp | 3 + source/detail/stylesheet.hpp | 172 +++ source/detail/workbook_impl.hpp | 9 +- source/serialization/excel_serializer.cpp | 20 +- source/serialization/style_serializer.cpp | 988 ++++++++---------- .../serialization/tests/test_style_writer.hpp | 12 +- source/serialization/worksheet_serializer.cpp | 5 +- source/styles/common_style.cpp | 72 +- source/styles/fill.cpp | 12 + source/styles/font.cpp | 18 + source/styles/format.cpp | 41 +- source/styles/number_format.cpp | 2 +- source/styles/style.cpp | 8 +- source/styles/tests/test_stylesheet.hpp | 23 +- source/workbook/workbook.cpp | 106 +- source/worksheet/worksheet.cpp | 6 + 28 files changed, 912 insertions(+), 954 deletions(-) rename include/xlnt/styles/{formattable.hpp => base_format.hpp} (95%) create mode 100644 source/detail/stylesheet.hpp diff --git a/include/xlnt/cell/cell.hpp b/include/xlnt/cell/cell.hpp index 0fa9c9a3..e2d77254 100644 --- a/include/xlnt/cell/cell.hpp +++ b/include/xlnt/cell/cell.hpp @@ -38,7 +38,6 @@ enum class calendar; class alignment; class border; class cell_reference; -class cell_style; class comment; class fill; class font; @@ -46,6 +45,7 @@ class format; class number_format; class protection; class relationship; +class style; class workbook; class worksheet; @@ -205,6 +205,26 @@ public: const format &get_format() const; void set_format(const format &new_format); + + void clear_format(); + + // style + + /// + /// Return true if this cell has had a format applied to it. + /// + bool has_style() const; + + /// + /// Return a reference to the format applied to this cell. + /// + const style &get_style() const; + + void set_style(const style &new_style); + + void set_style(const std::string &style_name); + + void clear_style(); /// /// Return the number format of this cell. diff --git a/include/xlnt/serialization/excel_serializer.hpp b/include/xlnt/serialization/excel_serializer.hpp index 96ca27bf..0b13d3e0 100644 --- a/include/xlnt/serialization/excel_serializer.hpp +++ b/include/xlnt/serialization/excel_serializer.hpp @@ -30,6 +30,7 @@ #include #include +#include namespace xlnt { @@ -93,6 +94,8 @@ public: /// binary data to stream. /// bool save_stream_workbook(std::ostream &stream, bool as_template = false); + + xlnt::detail::stylesheet &get_stylesheet(); private: /// diff --git a/include/xlnt/serialization/style_serializer.hpp b/include/xlnt/serialization/style_serializer.hpp index d99c6353..580b77cf 100644 --- a/include/xlnt/serialization/style_serializer.hpp +++ b/include/xlnt/serialization/style_serializer.hpp @@ -28,8 +28,6 @@ #include #include -#include -#include namespace xlnt { @@ -40,7 +38,7 @@ class conditional_format; class fill; class font; class format; -class formattable; +class base_format; class style; class number_format; class protection; @@ -49,8 +47,10 @@ class style; class xml_document; class xml_node; +namespace detail { struct stylesheet; } + /// -/// Reads and writes xl/styles.xml from/to an associated workbook. +/// Reads and writes xl/styles.xml from/to an associated stylesheet. /// class XLNT_CLASS style_serializer { @@ -58,11 +58,7 @@ public: /// /// Construct a style_serializer which can write styles.xml based on wb or populate wb /// with styles from an existing styles.xml. - style_serializer(workbook &wb); - - // - // Primary methods - // + style_serializer(detail::stylesheet &s); /// /// Load all styles from xml_document into workbook given in constructor. @@ -73,204 +69,13 @@ public: /// Populate parameter xml with an XML tree representing the styles contained in the workbook /// given in the constructor. /// - xml_document write_stylesheet(); + bool write_stylesheet(xml_document &xml); - // TODO: These need to be public for unit tests. Could also make test class a friend? - // private: - - // - // Static element readers (i.e. readers that don't use internal state) - // - - /// - /// Read and return an alignment from alignment_node. - /// - static alignment read_alignment(const xml_node &alignment_node); - - /// - /// Read and return a border side from side_node. - /// - static side read_side(const xml_node &side_node); - - /// - /// Read and return a border from border_node. - /// - static border read_border(const xml_node &border_node); - - /// - /// Read and return a fill from fill_node. - /// - static fill read_fill(const xml_node &fill_node); - - /// - /// Read and return a font from font_node. - /// - static font read_font(const xml_node &font_node); - - /// - /// Read and return a number format from number_format_node. - /// - static number_format read_number_format(const xml_node &number_format_node); - - /// - /// Read and return a protection from protection_node. - /// - static protection read_protection(const xml_node &protection_node); - - /// - /// Read and return all indexed colors from indexed_colors_node. - /// - static std::vector read_indexed_colors(const xml_node &indexed_colors_node); - - /// - /// Read and return a color from color_node. - /// - static color read_color(const xml_node &color_node); - - /// - /// Read and return a conditional format, dxf, from conditional_format_node. - /// - static conditional_format read_conditional_format(const xml_node &conditional_formats_node); - - /// - /// Read a single format from the given node. In styles.xml, this is an "xf" element. - /// A format has an optional border id, fill id, font id, and number format id where - /// each id is an index in the corresponding list of borders, etc. A style also has - /// optional alignment and protection children. A style also defines whether each of - /// these is "applied". For example, a style with a defined font id, font=# but with - /// "applyFont=0" will not use the font in formatting. - /// - format read_format(const xml_node &format_node); - - /// - /// Read a single named style from the given named_style_node. In styles.xml, this is a - /// (confusingly named) "cellStyle" element. This node defines the name, whether it is - /// built-in and an xfId which is the index of an element in cellStyleXfs. style_format_node - /// should be the XML node of the element at the index of xfId. - /// - style read_style(const xml_node &style_node, const xml_node &style_format_node); - - // - // Non-static element readers (i.e. readers that modify internal state) - // - - bool read_number_formats(const xml_node &number_formats_node); - - /// - /// Read all borders from borders_node and add them to workbook. - /// Return true on success. - /// - bool read_borders(const xml_node &borders_node); - - /// - /// Read all fills from fills_node and add them to workbook. - /// Return true on success. - /// - bool read_fills(const xml_node &fills_node); - - /// - /// Read all fonts from fonts_node and add them to workbook. - /// Return true on success. - /// - bool read_fonts(const xml_node &fonts_node); - - /// - /// Read all colors from colors_node and add them to workbook. - /// Return true on success. - /// - bool read_colors(const xml_node &colors_node); - - /// - /// Read all cell styles from cell_styles_node and add them to workbook. - /// Return true on success. - /// - bool read_formats(const xml_node &formats_node); - - /// - /// Read all named styles from named_style_node and cell_styles_node and add them to workbook. - /// Return true on success. - /// - bool read_styles(const xml_node &styles_node, const xml_node &style_formats_node); - - /// - /// Read all borders from number_formats_node and add them to workbook. - /// Return true on success. - /// - bool read_stylesheet(const xml_node &number_formats_node); - - // - // Non-static element writers (i.e. writers that modify internal workbook) - // - - /// - /// Build an xml tree representing all borders in workbook into borders_node. - /// Returns true on success. - /// - bool write_borders(xml_node &borders_node) const; - - /// - /// Build an xml tree representing all conditional formats in workbook into conditional_formats_node. - /// Returns true on success. - /// - bool write_conditional_formats(xml_node &conditional_formats_node) const; - - /// - /// Build an xml tree representing all fills in workbook into fills_node. - /// Returns true on success. - /// - bool write_fills(xml_node &fills_node) const; - - /// - /// Build an xml tree representing all fonts in workbook into fonts_node. - /// Returns true on success. - /// - bool write_fonts(xml_node &fonts_node) const; - - /// - /// Build an xml tree representing all number formats in workbook into number_formats_node. - /// Returns true on success. - /// - bool write_number_formats(xml_node &number_formats_node) const; - - bool write_formattable(const formattable &xf, xml_node xf_node) const; - - /// - /// Build an xml tree representing the given style into style_node. - /// Returns true on success. - /// - bool write_formats(xml_node &styles_node) const; - - bool write_styles(xml_node &cell_styles_node, xml_node &cell_style_formats_node) const; - - bool write_colors(xml_node &colors_node) const; - - bool write_ext_list(xml_node &ext_list_node) const; - - const std::vector &get_borders() const; - - const std::vector &get_fills() const; - - const std::vector &get_fonts() const; - - const std::vector &get_number_formats() const; - - const std::vector &get_colors() const; - private: - void initialize_vectors(); - /// - /// Set in the constructor, this workbook is used as the source or target for all writing or reading, respectively. + /// Set in the constructor, this stylesheet is used as the source or target for all writing or reading, respectively. /// - workbook &workbook_; - - std::vector colors_; - std::vector borders_; - std::vector fills_; - std::vector fonts_; - std::vector number_formats_; - std::vector formats_; - std::vector