Basic pageSetup persistence

-- NOTE: The struct page_setup doesn't currently reflect the attributes of the pageSetup element
This commit is contained in:
Crzyrndm 2018-06-24 12:34:56 +12:00
parent 7820ac548f
commit 93323f334d
7 changed files with 88 additions and 34 deletions

View File

@ -25,6 +25,7 @@
#pragma once #pragma once
#include <xlnt/xlnt_config.hpp> #include <xlnt/xlnt_config.hpp>
#include <xlnt/utils/optional.hpp>
namespace xlnt { namespace xlnt {
@ -33,6 +34,7 @@ namespace xlnt {
/// </summary> /// </summary>
enum class XLNT_API orientation enum class XLNT_API orientation
{ {
default,
portrait, portrait,
landscape landscape
}; };
@ -117,16 +119,6 @@ public:
/// </summary> /// </summary>
void paper_size(xlnt::paper_size paper_size); void paper_size(xlnt::paper_size paper_size);
/// <summary>
/// Returns the orientation of the worksheet using this page setup.
/// </summary>
xlnt::orientation orientation() const;
/// <summary>
/// Sets the orientation of the page.
/// </summary>
void orientation(xlnt::orientation orientation);
/// <summary> /// <summary>
/// Returns true if this worksheet should be scaled to fit on a single page during printing. /// Returns true if this worksheet should be scaled to fit on a single page during printing.
/// </summary> /// </summary>
@ -189,6 +181,19 @@ public:
/// </summary> /// </summary>
double scale() const; double scale() const;
/// <summary>
/// The orientation
/// </summary>
xlnt::optional<xlnt::orientation> orientation_;
/// <summary>
/// The horizontal dpi
/// </summary>
xlnt::optional<std::size_t> horizontal_dpi_;
/// <summary>
/// The vertical dpi
/// </summary>
xlnt::optional<std::size_t> vertical_dpi_;
private: private:
/// <summary> /// <summary>
/// The break /// The break
@ -205,11 +210,6 @@ private:
/// </summary> /// </summary>
xlnt::paper_size paper_size_; xlnt::paper_size paper_size_;
/// <summary>
/// The orientation
/// </summary>
xlnt::orientation orientation_;
/// <summary> /// <summary>
/// Whether or not to fit to page /// Whether or not to fit to page
/// </summary> /// </summary>
@ -241,4 +241,4 @@ private:
double scale_; double scale_;
}; };
} // namespace xlnt } // namespace xlnt

View File

@ -343,5 +343,19 @@ std::string to_string(pane_state state)
default_case("frozen"); 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 detail
} // namespace xlnt } // namespace xlnt

View File

@ -37,6 +37,7 @@
#include <xlnt/utils/exceptions.hpp> #include <xlnt/utils/exceptions.hpp>
#include <xlnt/utils/variant.hpp> #include <xlnt/utils/variant.hpp>
#include <xlnt/worksheet/pane.hpp> #include <xlnt/worksheet/pane.hpp>
#include <xlnt/worksheet/page_setup.hpp>
#include <xlnt/workbook/metadata_property.hpp> #include <xlnt/workbook/metadata_property.hpp>
namespace xlnt { namespace xlnt {
@ -76,6 +77,8 @@ std::string to_string(target_mode mode);
std::string to_string(pane_state state); std::string to_string(pane_state state);
std::string to_string(orientation state);
template<typename T> template<typename T>
static T from_string(const std::string &string_value); 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); 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 detail
} // namespace xlnt } // namespace xlnt
@ -582,6 +594,20 @@ struct value_traits<xlnt::extended_property>
} }
}; };
template <>
struct value_traits<xlnt::orientation>
{
static xlnt::orientation parse(std::string string, const parser &)
{
return xlnt::detail::from_string<xlnt::orientation>(string);
}
static std::string serialize(xlnt::orientation orientation, const serializer &)
{
return xlnt::detail::to_string(orientation);
}
};
} // namespace xml } // namespace xml

View File

@ -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 else if (current_worksheet_element == qn("spreadsheetml", "printOptions")) // CT_PrintOptions 0-1
{ {
skip_remaining_content(current_worksheet_element); skip_remaining_content(current_worksheet_element);
} }
else if (current_worksheet_element == qn("spreadsheetml", "pageMargins")) // CT_PageMargins 0-1 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 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>("orientation"));
}
if (parser().attribute_present("horizontalDpi"))
{
setup.horizontal_dpi_.set(parser().attribute<std::size_t>("horizontalDpi"));
}
if (parser().attribute_present("verticalDpi"))
{
setup.vertical_dpi_.set(parser().attribute<std::size_t>("verticalDpi"));
}
ws.page_setup(setup);
skip_remaining_content(current_worksheet_element); skip_remaining_content(current_worksheet_element);
} }
else if (current_worksheet_element == qn("spreadsheetml", "headerFooter")) // CT_HeaderFooter 0-1 else if (current_worksheet_element == qn("spreadsheetml", "headerFooter")) // CT_HeaderFooter 0-1

View File

@ -2768,11 +2768,21 @@ void xlsx_producer::write_worksheet(const relationship &rel)
if (ws.has_page_setup()) if (ws.has_page_setup())
{ {
write_start_element(xmlns, "pageSetup"); write_start_element(xmlns, "pageSetup");
write_attribute( if (ws.page_setup().orientation_.is_set())
"orientation", ws.page_setup().orientation() == xlnt::orientation::landscape ? "landscape" : "portrait"); {
write_attribute("paperSize", static_cast<std::size_t>(ws.page_setup().paper_size())); 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<std::size_t>(ws.page_setup().paper_size()));
write_attribute("fitToHeight", write_bool(ws.page_setup().fit_to_height())); 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"); write_end_element(xmlns, "pageSetup");
} }

View File

@ -29,7 +29,6 @@ page_setup::page_setup()
: break_(xlnt::page_break::none), : break_(xlnt::page_break::none),
sheet_state_(xlnt::sheet_state::visible), sheet_state_(xlnt::sheet_state::visible),
paper_size_(xlnt::paper_size::letter), paper_size_(xlnt::paper_size::letter),
orientation_(xlnt::orientation::portrait),
fit_to_page_(false), fit_to_page_(false),
fit_to_height_(false), fit_to_height_(false),
fit_to_width_(false), fit_to_width_(false),
@ -69,16 +68,6 @@ void page_setup::paper_size(xlnt::paper_size paper_size)
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 bool page_setup::fit_to_page() const
{ {
return fit_to_page_; return fit_to_page_;

View File

@ -44,9 +44,9 @@ public:
ps.paper_size(xlnt::paper_size::executive); ps.paper_size(xlnt::paper_size::executive);
xlnt_assert_equals(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); xlnt_assert(!ps.orientation_.is_set());
ps.orientation(xlnt::orientation::landscape); ps.orientation_.set(xlnt::orientation::landscape);
xlnt_assert_equals(ps.orientation(), xlnt::orientation::landscape); xlnt_assert_equals(ps.orientation_.get(), xlnt::orientation::landscape);
xlnt_assert(!ps.fit_to_page()); xlnt_assert(!ps.fit_to_page());
ps.fit_to_page(true); ps.fit_to_page(true);