From 93323f334db96ccf600d8a0210a38f8cc47199ac Mon Sep 17 00:00:00 2001 From: Crzyrndm Date: Sun, 24 Jun 2018 12:34:56 +1200 Subject: [PATCH] Basic pageSetup persistence -- NOTE: The struct page_setup doesn't currently reflect the attributes of the pageSetup element --- include/xlnt/worksheet/page_setup.hpp | 32 +++++++++---------- .../serialization/custom_value_traits.cpp | 14 ++++++++ .../serialization/custom_value_traits.hpp | 26 +++++++++++++++ source/detail/serialization/xlsx_consumer.cpp | 15 +++++++++ source/detail/serialization/xlsx_producer.cpp | 18 ++++++++--- source/worksheet/page_setup.cpp | 11 ------- tests/worksheet/page_setup_test_suite.hpp | 6 ++-- 7 files changed, 88 insertions(+), 34 deletions(-) diff --git a/include/xlnt/worksheet/page_setup.hpp b/include/xlnt/worksheet/page_setup.hpp index 4c6d7fc6..fd92a183 100644 --- a/include/xlnt/worksheet/page_setup.hpp +++ b/include/xlnt/worksheet/page_setup.hpp @@ -25,6 +25,7 @@ #pragma once #include +#include namespace xlnt { @@ -33,6 +34,7 @@ namespace xlnt { /// enum class XLNT_API orientation { + default, portrait, landscape }; @@ -117,16 +119,6 @@ public: /// void paper_size(xlnt::paper_size paper_size); - /// - /// Returns the orientation of the worksheet using this page setup. - /// - xlnt::orientation orientation() const; - - /// - /// Sets the orientation of the page. - /// - void orientation(xlnt::orientation orientation); - /// /// Returns true if this worksheet should be scaled to fit on a single page during printing. /// @@ -189,6 +181,19 @@ public: /// double scale() const; + /// + /// The orientation + /// + xlnt::optional orientation_; + /// + /// The horizontal dpi + /// + xlnt::optional horizontal_dpi_; + /// + /// The vertical dpi + /// + xlnt::optional vertical_dpi_; + private: /// /// The break @@ -205,11 +210,6 @@ private: /// xlnt::paper_size paper_size_; - /// - /// The orientation - /// - xlnt::orientation orientation_; - /// /// Whether or not to fit to page /// @@ -241,4 +241,4 @@ private: double scale_; }; -} // namespace xlnt +} // namespace xlnt \ No newline at end of file diff --git a/source/detail/serialization/custom_value_traits.cpp b/source/detail/serialization/custom_value_traits.cpp index 036967e5..6002f98b 100644 --- a/source/detail/serialization/custom_value_traits.cpp +++ b/source/detail/serialization/custom_value_traits.cpp @@ -343,5 +343,19 @@ std::string to_string(pane_state state) default_case("frozen"); } +std::string to_string(orientation orientation) +{ + switch (orientation) + { + case orientation::default: + return "default"; + case orientation::landscape: + return "landscape"; + case orientation::portrait: + return "portrait"; + } + default_case("default"); +} + } // namespace detail } // namespace xlnt diff --git a/source/detail/serialization/custom_value_traits.hpp b/source/detail/serialization/custom_value_traits.hpp index 9f33d08d..bdcc344f 100644 --- a/source/detail/serialization/custom_value_traits.hpp +++ b/source/detail/serialization/custom_value_traits.hpp @@ -37,6 +37,7 @@ #include #include #include +#include #include namespace xlnt { @@ -76,6 +77,8 @@ std::string to_string(target_mode mode); std::string to_string(pane_state state); +std::string to_string(orientation state); + template static T from_string(const std::string &string_value); @@ -394,6 +397,15 @@ pane_corner from_string(const std::string &string) default_case(pane_corner::bottom_left); } +template <> +orientation from_string(const std::string &string) +{ + if (string == "default") return orientation::default; + else if (string == "landscape") return orientation::landscape; + else if (string == "portrait") return orientation::portrait; + default_case(orientation::default); +} + } // namespace detail } // namespace xlnt @@ -582,6 +594,20 @@ struct value_traits } }; +template <> +struct value_traits +{ + static xlnt::orientation parse(std::string string, const parser &) + { + return xlnt::detail::from_string(string); + } + + static std::string serialize(xlnt::orientation orientation, const serializer &) + { + return xlnt::detail::to_string(orientation); + } +}; + } // namespace xml diff --git a/source/detail/serialization/xlsx_consumer.cpp b/source/detail/serialization/xlsx_consumer.cpp index 0cd81c05..d575fcf2 100644 --- a/source/detail/serialization/xlsx_consumer.cpp +++ b/source/detail/serialization/xlsx_consumer.cpp @@ -926,6 +926,7 @@ worksheet xlsx_consumer::read_worksheet_end(const std::string &rel_id) } else if (current_worksheet_element == qn("spreadsheetml", "printOptions")) // CT_PrintOptions 0-1 { + skip_remaining_content(current_worksheet_element); } else if (current_worksheet_element == qn("spreadsheetml", "pageMargins")) // CT_PageMargins 0-1 @@ -943,6 +944,20 @@ worksheet xlsx_consumer::read_worksheet_end(const std::string &rel_id) } else if (current_worksheet_element == qn("spreadsheetml", "pageSetup")) // CT_PageSetup 0-1 { + page_setup setup; + if (parser().attribute_present("orientation")) + { + setup.orientation_.set(parser().attribute("orientation")); + } + if (parser().attribute_present("horizontalDpi")) + { + setup.horizontal_dpi_.set(parser().attribute("horizontalDpi")); + } + if (parser().attribute_present("verticalDpi")) + { + setup.vertical_dpi_.set(parser().attribute("verticalDpi")); + } + ws.page_setup(setup); skip_remaining_content(current_worksheet_element); } else if (current_worksheet_element == qn("spreadsheetml", "headerFooter")) // CT_HeaderFooter 0-1 diff --git a/source/detail/serialization/xlsx_producer.cpp b/source/detail/serialization/xlsx_producer.cpp index e9c172bf..b11f4cea 100644 --- a/source/detail/serialization/xlsx_producer.cpp +++ b/source/detail/serialization/xlsx_producer.cpp @@ -2768,11 +2768,21 @@ void xlsx_producer::write_worksheet(const relationship &rel) if (ws.has_page_setup()) { write_start_element(xmlns, "pageSetup"); - write_attribute( - "orientation", ws.page_setup().orientation() == xlnt::orientation::landscape ? "landscape" : "portrait"); - write_attribute("paperSize", static_cast(ws.page_setup().paper_size())); + if (ws.page_setup().orientation_.is_set()) + { + write_attribute("orientation", ws.page_setup().orientation_.get()); + } + if (ws.page_setup().horizontal_dpi_.is_set()) + { + write_attribute("horizontalDpi", ws.page_setup().horizontal_dpi_.get()); + } + if (ws.page_setup().vertical_dpi_.is_set()) + { + write_attribute("verticalDpi", ws.page_setup().vertical_dpi_.get()); + } + /*write_attribute("paperSize", static_cast(ws.page_setup().paper_size())); write_attribute("fitToHeight", write_bool(ws.page_setup().fit_to_height())); - write_attribute("fitToWidth", write_bool(ws.page_setup().fit_to_width())); + write_attribute("fitToWidth", write_bool(ws.page_setup().fit_to_width()));*/ write_end_element(xmlns, "pageSetup"); } diff --git a/source/worksheet/page_setup.cpp b/source/worksheet/page_setup.cpp index e3ae812f..07361369 100644 --- a/source/worksheet/page_setup.cpp +++ b/source/worksheet/page_setup.cpp @@ -29,7 +29,6 @@ page_setup::page_setup() : break_(xlnt::page_break::none), sheet_state_(xlnt::sheet_state::visible), paper_size_(xlnt::paper_size::letter), - orientation_(xlnt::orientation::portrait), fit_to_page_(false), fit_to_height_(false), fit_to_width_(false), @@ -69,16 +68,6 @@ void page_setup::paper_size(xlnt::paper_size paper_size) paper_size_ = paper_size; } -orientation page_setup::orientation() const -{ - return orientation_; -} - -void page_setup::orientation(xlnt::orientation orientation) -{ - orientation_ = orientation; -} - bool page_setup::fit_to_page() const { return fit_to_page_; diff --git a/tests/worksheet/page_setup_test_suite.hpp b/tests/worksheet/page_setup_test_suite.hpp index 1a82a1db..6ca59458 100644 --- a/tests/worksheet/page_setup_test_suite.hpp +++ b/tests/worksheet/page_setup_test_suite.hpp @@ -44,9 +44,9 @@ public: ps.paper_size(xlnt::paper_size::executive); xlnt_assert_equals(ps.paper_size(), xlnt::paper_size::executive); - xlnt_assert_equals(ps.orientation(), xlnt::orientation::portrait); - ps.orientation(xlnt::orientation::landscape); - xlnt_assert_equals(ps.orientation(), xlnt::orientation::landscape); + xlnt_assert(!ps.orientation_.is_set()); + ps.orientation_.set(xlnt::orientation::landscape); + xlnt_assert_equals(ps.orientation_.get(), xlnt::orientation::landscape); xlnt_assert(!ps.fit_to_page()); ps.fit_to_page(true);