From fdce38520a5c58453b1d8a5038e84697672063cf Mon Sep 17 00:00:00 2001 From: Jaskaran Singh Date: Sun, 5 Jun 2016 00:35:43 +0530 Subject: [PATCH 1/6] Add option to hide cell while printing to existing protection options --- include/orcus/spreadsheet/import_interface.hpp | 1 + include/orcus/spreadsheet/styles.hpp | 2 ++ src/spreadsheet/styles.cpp | 7 ++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/orcus/spreadsheet/import_interface.hpp b/include/orcus/spreadsheet/import_interface.hpp index cc83e5a51..f4559d3e1 100644 --- a/include/orcus/spreadsheet/import_interface.hpp +++ b/include/orcus/spreadsheet/import_interface.hpp @@ -116,6 +116,7 @@ public: // cell protection virtual void set_cell_hidden(bool b) = 0; virtual void set_cell_locked(bool b) = 0; + virtual void set_cell_print_content(bool b) = 0; virtual size_t commit_cell_protection() = 0; // number format diff --git a/include/orcus/spreadsheet/styles.hpp b/include/orcus/spreadsheet/styles.hpp index 3cf019e73..2aca59e28 100644 --- a/include/orcus/spreadsheet/styles.hpp +++ b/include/orcus/spreadsheet/styles.hpp @@ -84,6 +84,7 @@ struct ORCUS_SPM_DLLPUBLIC protection_t { bool locked; bool hidden; + bool print_content; protection_t(); void reset(); @@ -165,6 +166,7 @@ public: virtual void set_cell_hidden(bool b); virtual void set_cell_locked(bool b); + virtual void set_cell_print_content(bool b); virtual size_t commit_cell_protection(); virtual void set_number_format_count(size_t n); diff --git a/src/spreadsheet/styles.cpp b/src/spreadsheet/styles.cpp index 9e62c0c6c..609d2d700 100644 --- a/src/spreadsheet/styles.cpp +++ b/src/spreadsheet/styles.cpp @@ -68,7 +68,7 @@ void border_t::reset() } protection_t::protection_t() : - locked(false), hidden(false) + locked(false), hidden(false), print_content(false) { } @@ -282,6 +282,11 @@ void import_styles::set_cell_locked(bool b) m_cur_protection.locked = b; } +void import_styles::set_cell_print_content(bool b) +{ + m_cur_protection.print_content = b; +} + size_t import_styles::commit_cell_protection() { m_protections.push_back(m_cur_protection); -- GitLab From e1173954171171f9bc6dd85e3bba085bd5145197 Mon Sep 17 00:00:00 2001 From: Jaskaran Singh Date: Sun, 5 Jun 2016 09:16:27 +0530 Subject: [PATCH 2/6] Add option to hide formula cell to existing protection options --- include/orcus/spreadsheet/import_interface.hpp | 1 + include/orcus/spreadsheet/styles.hpp | 2 ++ src/spreadsheet/styles.cpp | 7 ++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/orcus/spreadsheet/import_interface.hpp b/include/orcus/spreadsheet/import_interface.hpp index f4559d3e1..59b619dfc 100644 --- a/include/orcus/spreadsheet/import_interface.hpp +++ b/include/orcus/spreadsheet/import_interface.hpp @@ -117,6 +117,7 @@ public: virtual void set_cell_hidden(bool b) = 0; virtual void set_cell_locked(bool b) = 0; virtual void set_cell_print_content(bool b) = 0; + virtual void set_cell_formula_hidden(bool b) = 0; virtual size_t commit_cell_protection() = 0; // number format diff --git a/include/orcus/spreadsheet/styles.hpp b/include/orcus/spreadsheet/styles.hpp index 2aca59e28..2cd60ecee 100644 --- a/include/orcus/spreadsheet/styles.hpp +++ b/include/orcus/spreadsheet/styles.hpp @@ -85,6 +85,7 @@ struct ORCUS_SPM_DLLPUBLIC protection_t bool locked; bool hidden; bool print_content; + bool formula_hidden; protection_t(); void reset(); @@ -167,6 +168,7 @@ public: virtual void set_cell_hidden(bool b); virtual void set_cell_locked(bool b); virtual void set_cell_print_content(bool b); + virtual void set_cell_formula_hidden(bool b); virtual size_t commit_cell_protection(); virtual void set_number_format_count(size_t n); diff --git a/src/spreadsheet/styles.cpp b/src/spreadsheet/styles.cpp index 609d2d700..8dcac4085 100644 --- a/src/spreadsheet/styles.cpp +++ b/src/spreadsheet/styles.cpp @@ -68,7 +68,7 @@ void border_t::reset() } protection_t::protection_t() : - locked(false), hidden(false), print_content(false) + locked(false), hidden(false), print_content(false), formula_hidden(false) { } @@ -287,6 +287,11 @@ void import_styles::set_cell_print_content(bool b) m_cur_protection.print_content = b; } +void import_styles::set_cell_formula_hidden(bool b) +{ + m_cur_protection.formula_hidden = b; +} + size_t import_styles::commit_cell_protection() { m_protections.push_back(m_cur_protection); -- GitLab From 0f4e00dcf8b43bdf6e36e8167688ecc0384f958d Mon Sep 17 00:00:00 2001 From: Jaskaran Singh Date: Sun, 5 Jun 2016 22:54:45 +0530 Subject: [PATCH 3/6] Add entry for cell protection in a few necessary places --- include/orcus/spreadsheet/styles.hpp | 1 + src/liborcus/odf_styles.hpp | 3 ++- src/spreadsheet/styles.cpp | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/orcus/spreadsheet/styles.hpp b/include/orcus/spreadsheet/styles.hpp index 2cd60ecee..7ab745284 100644 --- a/include/orcus/spreadsheet/styles.hpp +++ b/include/orcus/spreadsheet/styles.hpp @@ -119,6 +119,7 @@ struct ORCUS_SPM_DLLPUBLIC cell_format_t bool apply_fill:1; bool apply_border:1; bool apply_alignment:1; + bool apply_protection:1; cell_format_t(); void reset(); diff --git a/src/liborcus/odf_styles.hpp b/src/liborcus/odf_styles.hpp index 9e6bf7a88..62a392b69 100644 --- a/src/liborcus/odf_styles.hpp +++ b/src/liborcus/odf_styles.hpp @@ -48,11 +48,12 @@ struct odf_style size_t font; size_t fill; size_t border; + size_t protection; size_t xf; bool automatic_style; - cell() : font(0), fill(0), border(0), + cell() : font(0), fill(0), border(0), protection(0), xf(0), automatic_style(false) {} }; diff --git a/src/spreadsheet/styles.cpp b/src/spreadsheet/styles.cpp index 8dcac4085..608df44e5 100644 --- a/src/spreadsheet/styles.cpp +++ b/src/spreadsheet/styles.cpp @@ -102,7 +102,8 @@ cell_format_t::cell_format_t() : apply_font(false), apply_fill(false), apply_border(false), - apply_alignment(false) + apply_alignment(false), + apply_protection(false) { } -- GitLab From 9b3565332085b89721d511e1c404fb7ef41dd6aa Mon Sep 17 00:00:00 2001 From: Jaskaran Singh Date: Sun, 5 Jun 2016 22:59:03 +0530 Subject: [PATCH 4/6] Add code to read cell protection attributes --- src/liborcus/odf_styles_context.cpp | 51 ++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/liborcus/odf_styles_context.cpp b/src/liborcus/odf_styles_context.cpp index b62edfc15..b266ca5db 100644 --- a/src/liborcus/odf_styles_context.cpp +++ b/src/liborcus/odf_styles_context.cpp @@ -169,7 +169,12 @@ public: m_background_red(0), m_background_green(0), m_background_blue(0), - m_background_color(false) + m_background_color(false), + m_locked(false), + m_hidden(false), + m_formula_hidden(false), + m_print_content(false), + m_cell_protection(false) {} private: @@ -179,6 +184,11 @@ private: spreadsheet::color_elem_t m_background_blue; bool m_background_color; + bool m_locked; + bool m_hidden; + bool m_formula_hidden; + bool m_print_content; + bool m_cell_protection; border_map_type m_border_style_dir_pair; @@ -249,6 +259,39 @@ public: ; } } + + else if(attr.ns == NS_odf_style) + { + switch(attr.name) + { + case XML_print_content: + { + m_cell_protection = true; + m_print_content = attr.value == "true"; + } + break; + case XML_cell_protect: + { + m_cell_protection = true; + if (attr.value == "protected") + m_locked = true; + else if (attr.value == "hidden-and-protected") + { + m_locked = true; + m_hidden = true; + } + else if (attr.value == "formula-hidden") + m_formula_hidden = true; + else if (attr.value == "protected formula-hidden" || attr.value == "formula-hidden protected") + { + m_formula_hidden = true; + m_locked = true; + } + } + default: + ; + } + } } bool has_background_color() const { return m_background_color; } @@ -263,6 +306,12 @@ public: bool has_border() const { return !m_border_style_dir_pair.empty(); } + bool has_protection() const { return m_cell_protection; } + bool is_locked() const { return m_locked; } + bool is_hidden() const { return m_hidden; } + bool is_formula_hidden() const { return m_formula_hidden; } + bool is_print_content() const { return m_print_content; } + const border_map_type& get_border_attrib() const { return m_border_style_dir_pair; -- GitLab From 90121fd55613937dfc566740f91cccbe98f66196 Mon Sep 17 00:00:00 2001 From: Jaskaran Singh Date: Sun, 5 Jun 2016 23:03:13 +0530 Subject: [PATCH 5/6] Add code to commit cell protection attributes --- src/liborcus/odf_styles_context.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/liborcus/odf_styles_context.cpp b/src/liborcus/odf_styles_context.cpp index b266ca5db..75548d4ec 100644 --- a/src/liborcus/odf_styles_context.cpp +++ b/src/liborcus/odf_styles_context.cpp @@ -515,6 +515,15 @@ void styles_context::start_element(xmlns_id_t ns, xml_token_t name, const std::v } size_t border_id = mp_styles->commit_border(); + + if (func.has_protection()) + { + mp_styles->set_cell_hidden(func.is_hidden()); + mp_styles->set_cell_locked(func.is_locked()); + mp_styles->set_cell_print_content(func.is_print_content()); + mp_styles->set_cell_formula_hidden(func.is_formula_hidden()); + } + size_t cell_protection_id = mp_styles->commit_cell_protection(); switch (m_current_style->family) { case style_family_table_cell: @@ -522,6 +531,7 @@ void styles_context::start_element(xmlns_id_t ns, xml_token_t name, const std::v odf_style::cell* data = m_current_style->cell_data; data->fill = fill_id; data->border = border_id; + data->protection = cell_protection_id; } break; default: @@ -554,6 +564,7 @@ bool styles_context::end_element(xmlns_id_t ns, xml_token_t name) mp_styles->set_xf_font(cell.font); mp_styles->set_xf_fill(cell.fill); mp_styles->set_xf_border(cell.border); + mp_styles->set_xf_protection(cell.protection); size_t xf_id = 0; if (cell.automatic_style) xf_id = mp_styles->commit_cell_xf(); -- GitLab From 74d75eed965adf8e5fe9efdccf171974c17a704c Mon Sep 17 00:00:00 2001 From: Jaskaran Singh Date: Sun, 5 Jun 2016 23:04:45 +0530 Subject: [PATCH 6/6] Add test for odf cell protection --- src/liborcus/odf_styles_context_test.cpp | 44 +++++++++++++++++++++++- test/ods/styles/cell-styles.xml | 9 +++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/liborcus/odf_styles_context_test.cpp b/src/liborcus/odf_styles_context_test.cpp index 838bf2d55..c6410ec89 100644 --- a/src/liborcus/odf_styles_context_test.cpp +++ b/src/liborcus/odf_styles_context_test.cpp @@ -58,7 +58,7 @@ int main() /* Test for Border Styles ===================================================== */ - assert(styles.get_border_count() == 5); + assert(styles.get_border_count() == 8); /* Test that border style applies to all the sides when not specified */ style = find_cell_style_by_name("Name1", &styles); @@ -112,5 +112,47 @@ int main() assert(cell_border->diagonal_tl_br.border_color.green == 0x00); assert(cell_border->diagonal_tl_br.border_width.value == 0.74); +/* Test for Cell Protection + ======================================================== +*/ + /* Test that Cell is only protected and not hidden , Print Content is true */ + style = find_cell_style_by_name("Name5", &styles); + xf = style->xf; + cell_format = styles.get_cell_style_format(xf); + size_t protection = cell_format->protection; + assert(cell_format); + + const orcus::spreadsheet::protection_t* cell_protection = styles.get_protection(protection); + assert(cell_protection->locked == true); + assert(cell_protection->hidden == true); + assert(cell_protection->print_content == true); + assert(cell_protection->formula_hidden == false); + + /* Test that Cell is protected and formula is hidden , Print Content is false */ + style = find_cell_style_by_name("Name6", &styles); + xf = style->xf; + cell_format = styles.get_cell_style_format(xf); + protection = cell_format->protection; + assert(cell_format); + + cell_protection = styles.get_protection(protection); + assert(cell_protection->locked == true); + assert(cell_protection->hidden == false); + assert(cell_protection->print_content == false); + assert(cell_protection->formula_hidden == true); + + /* Test that Cell is not protected by any way, Print Content is false */ + style = find_cell_style_by_name("Name7", &styles); + xf = style->xf; + cell_format = styles.get_cell_style_format(xf); + protection = cell_format->protection; + assert(cell_format); + + cell_protection = styles.get_protection(protection); + assert(cell_protection->locked == false); + assert(cell_protection->hidden == false); + assert(cell_protection->print_content == true); + assert(cell_protection->formula_hidden == false); + return 0; } diff --git a/test/ods/styles/cell-styles.xml b/test/ods/styles/cell-styles.xml index e9b944d1a..272271991 100644 --- a/test/ods/styles/cell-styles.xml +++ b/test/ods/styles/cell-styles.xml @@ -12,4 +12,13 @@ + + + + + + + + + -- GitLab