From eb6d4a4ef7629a7102a0061158b1a71c96d628f6 Mon Sep 17 00:00:00 2001 From: andiwand Date: Mon, 4 Dec 2023 13:51:00 +0100 Subject: [PATCH] tmp --- src/odr/internal/abstract/sheet_element.hpp | 14 +- src/odr/internal/odf/odf_element.hpp | 2 +- src/odr/internal/odf/odf_parser.cpp | 6 +- src/odr/internal/odf/odf_parser.hpp | 14 +- src/odr/internal/odf/odf_spreadsheet.cpp | 168 ++++++++---------- src/odr/internal/odf/odf_spreadsheet.hpp | 34 ++-- .../spreadsheet/ooxml_spreadsheet_element.cpp | 20 +-- .../spreadsheet/ooxml_spreadsheet_element.hpp | 19 +- 8 files changed, 130 insertions(+), 147 deletions(-) diff --git a/src/odr/internal/abstract/sheet_element.hpp b/src/odr/internal/abstract/sheet_element.hpp index 3185f41a..d464700b 100644 --- a/src/odr/internal/abstract/sheet_element.hpp +++ b/src/odr/internal/abstract/sheet_element.hpp @@ -28,6 +28,8 @@ class Sheet : public virtual Element { std::uint32_t row) const = 0; [[nodiscard]] virtual Element *first_shape(const Document *) const = 0; + + [[nodiscard]] virtual TableStyle style(const Document *) const = 0; }; class SheetColumn : public virtual Element { @@ -36,7 +38,7 @@ class SheetColumn : public virtual Element { return ElementType::table_column; } - [[nodiscard]] virtual TableColumnStyle style(const Document *, const Sheet *, + [[nodiscard]] virtual TableColumnStyle style(const Document *, Sheet *, std::uint32_t column) const = 0; }; @@ -46,7 +48,7 @@ class SheetRow : public virtual Element { return ElementType::table_row; } - [[nodiscard]] virtual TableRowStyle style(const Document *, const Sheet *, + [[nodiscard]] virtual TableRowStyle style(const Document *, Sheet *, std::uint32_t row) const = 0; }; @@ -56,17 +58,17 @@ class SheetCell : public virtual Element { return ElementType::table_cell; } - [[nodiscard]] virtual bool is_covered(const Document *, const Sheet *, + [[nodiscard]] virtual bool is_covered(const Document *, Sheet *, std::uint32_t column, std::uint32_t row) const = 0; - [[nodiscard]] virtual TableDimensions span(const Document *, const Sheet *, + [[nodiscard]] virtual TableDimensions span(const Document *, Sheet *, std::uint32_t column, std::uint32_t row) const = 0; - [[nodiscard]] virtual ValueType value_type(const Document *, const Sheet *, + [[nodiscard]] virtual ValueType value_type(const Document *, Sheet *, std::uint32_t column, std::uint32_t row) const = 0; - [[nodiscard]] virtual TableCellStyle style(const Document *, const Sheet *, + [[nodiscard]] virtual TableCellStyle style(const Document *, Sheet *, std::uint32_t column, std::uint32_t row) const = 0; }; diff --git a/src/odr/internal/odf/odf_element.hpp b/src/odr/internal/odf/odf_element.hpp index f7f35c8c..777faa65 100644 --- a/src/odr/internal/odf/odf_element.hpp +++ b/src/odr/internal/odf/odf_element.hpp @@ -24,9 +24,9 @@ class Element : public virtual common::Element { bool is_editable(const abstract::Document *document) const override; -protected: pugi::xml_node m_node; +protected: virtual const char *style_name_(const abstract::Document *) const; static const Document *document_(const abstract::Document *); diff --git a/src/odr/internal/odf/odf_parser.cpp b/src/odr/internal/odf/odf_parser.cpp index 1947f5c9..9a397213 100644 --- a/src/odr/internal/odf/odf_parser.cpp +++ b/src/odr/internal/odf/odf_parser.cpp @@ -78,7 +78,7 @@ void odf::parse_element_children(Document &document, DrawingRoot *element, } template <> -std::tuple +std::tuple odf::parse_element_tree(Document &document, pugi::xml_node first) { if (!first) { return std::make_tuple(nullptr, pugi::xml_node()); @@ -96,7 +96,7 @@ odf::parse_element_tree(Document &document, pugi::xml_node first) { } template <> -std::tuple +std::tuple odf::parse_element_tree(Document &document, pugi::xml_node node) { if (!node) { return std::make_tuple(nullptr, pugi::xml_node()); @@ -128,7 +128,7 @@ odf::parse_element_tree(Document &document, pugi::xml_node node) { } template <> -std::tuple +std::tuple odf::parse_element_tree(Document &document, pugi::xml_node node) { if (!node) { diff --git a/src/odr/internal/odf/odf_parser.hpp b/src/odr/internal/odf/odf_parser.hpp index b84ff821..94ae6e8e 100644 --- a/src/odr/internal/odf/odf_parser.hpp +++ b/src/odr/internal/odf/odf_parser.hpp @@ -20,14 +20,14 @@ class TableRow; Element *parse_tree(Document &document, pugi::xml_node node); -template -std::tuple parse_element_tree(Document &document, - pugi::xml_node node) { +template +std::tuple +parse_element_tree(Document &document, pugi::xml_node node) { if (!node) { return std::make_tuple(nullptr, pugi::xml_node()); } - auto element_unique = std::make_unique(node); + auto element_unique = std::make_unique(node); auto element = element_unique.get(); document.register_element_(std::move(element_unique)); @@ -36,13 +36,13 @@ std::tuple parse_element_tree(Document &document, return std::make_tuple(element, node.next_sibling()); } template <> -std::tuple +std::tuple parse_element_tree(Document &document, pugi::xml_node first); template <> -std::tuple +std::tuple parse_element_tree
(Document &document, pugi::xml_node node); template <> -std::tuple +std::tuple parse_element_tree(Document &document, pugi::xml_node node); std::tuple diff --git a/src/odr/internal/odf/odf_spreadsheet.cpp b/src/odr/internal/odf/odf_spreadsheet.cpp index ca31ed31..01038481 100644 --- a/src/odr/internal/odf/odf_spreadsheet.cpp +++ b/src/odr/internal/odf/odf_spreadsheet.cpp @@ -8,42 +8,50 @@ namespace odr::internal::odf { -namespace { - -class SheetColumn final : public Element, public abstract::TableColumn { +class SheetColumn final : public Element, public abstract::SheetColumn { public: using Element::Element; - [[nodiscard]] TableColumnStyle - style(const abstract::Document *document) const final { + [[nodiscard]] TableColumnStyle style(const abstract::Document *document, + abstract::Sheet *, + std::uint32_t /*column*/) const final { return partial_style(document).table_column_style; } }; -class SheetRow final : public Element, public abstract::TableRow { +class SheetRow final : public Element, public abstract::SheetRow { public: using Element::Element; - [[nodiscard]] TableRowStyle - style(const abstract::Document *document) const final { + [[nodiscard]] TableRowStyle style(const abstract::Document *document, + abstract::Sheet *, + std::uint32_t /*column*/) const final { return partial_style(document).table_row_style; } }; -class SheetCell final : public Element, public abstract::TableCell { +class SheetCell final : public Element, public abstract::SheetCell { public: using Element::Element; - [[nodiscard]] bool is_covered(const abstract::Document *) const final { + [[nodiscard]] bool is_covered(const abstract::Document *, abstract::Sheet *, + std::uint32_t /*column*/, + std::uint32_t /*row*/) const final { return std::strcmp(m_node.name(), "table:covered-table-cell") == 0; } - [[nodiscard]] TableDimensions span(const abstract::Document *) const final { + [[nodiscard]] TableDimensions span(const abstract::Document *, + abstract::Sheet *, + std::uint32_t /*column*/, + std::uint32_t /*row*/) const final { return {m_node.attribute("table:number-rows-spanned").as_uint(1), m_node.attribute("table:number-columns-spanned").as_uint(1)}; } - [[nodiscard]] ValueType value_type(const abstract::Document *) const final { + [[nodiscard]] ValueType value_type(const abstract::Document *, + abstract::Sheet *, + std::uint32_t /*column*/, + std::uint32_t /*row*/) const final { auto value_type = m_node.attribute("office:value-type").value(); if (std::strcmp("float", value_type) == 0) { return ValueType::float_number; @@ -51,14 +59,25 @@ class SheetCell final : public Element, public abstract::TableCell { return ValueType::string; } - [[nodiscard]] TableCellStyle - style(const abstract::Document *document) const final { - return partial_style(document).table_cell_style; + [[nodiscard]] TableCellStyle style(const abstract::Document *document, + abstract::Sheet *sheet, + std::uint32_t column, + std::uint32_t /*row*/) const final { + auto style_name = style_name_(document); + if (style_name == nullptr) { + style_name = dynamic_cast(sheet->column(document, column)) + ->m_node.attribute("table:default-cell-style-name") + .value(); + } + if (style_name == nullptr) { + return {}; + } + if (auto style = style_(document)->style(style_name)) { + return style->resolved().table_cell_style; + } } }; -} // namespace - std::string Sheet::name(const abstract::Document *) const { return m_node.attribute("table:name").value(); } @@ -100,87 +119,16 @@ Sheet::content(const abstract::Document *, return result; } -abstract::Element *Sheet::first_cell_element(const abstract::Document *document, - std::uint32_t column, - std::uint32_t row) const { - auto cell = cell_(document, column, row); - if (cell == nullptr) { - return nullptr; - } - return cell->first_child(document); -} - -abstract::Element *Sheet::first_shape(const abstract::Document *) const { - return m_first_shape; -} - -TableStyle Sheet::style(const abstract::Document *document) const { - return partial_style(document).table_style; -} - -TableColumnStyle Sheet::column_style(const abstract::Document *, - std::uint32_t /*column*/) const { - return TableColumnStyle(); // TODO -} - -TableRowStyle Sheet::row_style(const abstract::Document *, - std::uint32_t /*row*/) const { - return TableRowStyle(); // TODO -} - -TableCellStyle Sheet::cell_style(const abstract::Document *, - std::uint32_t /*column*/, - std::uint32_t /*row*/) const { - return TableCellStyle(); // TODO -} - -bool Sheet::is_covered(const abstract::Document *, std::uint32_t /*column*/, - std::uint32_t /*row*/) const { - return false; // TODO -} - -TableDimensions Sheet::span(const abstract::Document *, - std::uint32_t /*column*/, - std::uint32_t /*row*/) const { - return TableDimensions(); // TODO -} - -ValueType Sheet::value_type(const abstract::Document *, - std::uint32_t /*column*/, - std::uint32_t /*row*/) const { - return ValueType::unknown; // TODO -} - -void Sheet::init_column_(std::uint32_t column, std::uint32_t repeated, - Element *element) { - m_columns[column + repeated] = element; -} - -void Sheet::init_row_(std::uint32_t row, std::uint32_t repeated, - Element *element) { - m_rows[row + repeated].element = element; -} - -void Sheet::init_cell_(std::uint32_t column, std::uint32_t row, - std::uint32_t columns_repeated, - std::uint32_t rows_repeated, Element *element) { - m_rows[row + rows_repeated].cells[column + columns_repeated] = element; -} - -void Sheet::init_dimensions_(TableDimensions dimensions) { - m_dimensions = dimensions; -} - -abstract::Element *Sheet::column_(const abstract::Document *, - std::uint32_t column) const { +abstract::SheetColumn *Sheet::column(const abstract::Document *, + std::uint32_t column) const { if (auto it = util::map::lookup_greater_than(m_columns, column); it != std::end(m_columns)) { - return it->second; + return dynamic_cast(it->second); } return nullptr; } -abstract::Element *Sheet::row_(const abstract::Document *, +abstract::SheetRow *Sheet::row(const abstract::Document *, std::uint32_t row) const { if (auto it = util::map::lookup_greater_than(m_rows, row); it != std::end(m_rows)) { @@ -189,8 +137,9 @@ abstract::Element *Sheet::row_(const abstract::Document *, return nullptr; } -abstract::Element *Sheet::cell_(const abstract::Document *, - std::uint32_t column, std::uint32_t row) const { +abstract::SheetCell *Sheet::cell(const abstract::Document *, + std::uint32_t column, + std::uint32_t row) const { if (auto row_it = util::map::lookup_greater_than(m_rows, row); row_it != std::end(m_rows)) { auto &cells = row_it->second.cells; @@ -203,12 +152,41 @@ abstract::Element *Sheet::cell_(const abstract::Document *, return nullptr; } +abstract::Element *Sheet::first_shape(const abstract::Document *) const { + return m_first_shape; +} + +TableStyle Sheet::style(const abstract::Document *document) const { + return partial_style(document).table_style; +} + +void Sheet::init_column_(std::uint32_t column, std::uint32_t repeated, + SheetColumn *element) { + m_columns[column + repeated] = dynamic_cast(element); +} + +void Sheet::init_row_(std::uint32_t row, std::uint32_t repeated, + SheetRow *element) { + m_rows[row + repeated].element = dynamic_cast(element); +} + +void Sheet::init_cell_(std::uint32_t column, std::uint32_t row, + std::uint32_t columns_repeated, + std::uint32_t rows_repeated, SheetCell *element) { + m_rows[row + rows_repeated].cells[column + columns_repeated] = + dynamic_cast(element); +} + +void Sheet::init_dimensions_(TableDimensions dimensions) { + m_dimensions = dimensions; +} + } // namespace odr::internal::odf namespace odr::internal { template <> -std::tuple +std::tuple odf::parse_element_tree(Document &document, pugi::xml_node node) { if (!node) { return std::make_tuple(nullptr, pugi::xml_node()); diff --git a/src/odr/internal/odf/odf_spreadsheet.hpp b/src/odr/internal/odf/odf_spreadsheet.hpp index c905f068..92c1ec37 100644 --- a/src/odr/internal/odf/odf_spreadsheet.hpp +++ b/src/odr/internal/odf/odf_spreadsheet.hpp @@ -18,6 +18,9 @@ class xml_node; namespace odr::internal::odf { class Document; +class SheetColumn; +class SheetRow; +class SheetCell; class SpreadsheetRoot final : public Root { public: @@ -37,41 +40,42 @@ class Sheet final : public Element, public abstract::Sheet { content(const abstract::Document *, const std::optional range) const final; + abstract::SheetColumn *column(const abstract::Document *, + std::uint32_t column) const final; + abstract::SheetRow *row(const abstract::Document *, + std::uint32_t row) const final; + abstract::SheetCell *cell(const abstract::Document *, std::uint32_t column, + std::uint32_t row) const final; + [[nodiscard]] abstract::Element * first_shape(const abstract::Document *) const final; + [[nodiscard]] TableStyle style(const abstract::Document *) const final; + void init_column_(std::uint32_t column, std::uint32_t repeated, - Element *element); - void init_row_(std::uint32_t row, std::uint32_t repeated, Element *element); + SheetColumn *element); + void init_row_(std::uint32_t row, std::uint32_t repeated, SheetRow *element); void init_cell_(std::uint32_t column, std::uint32_t row, std::uint32_t columns_repeated, std::uint32_t rows_repeated, - Element *element); + SheetCell *element); void init_dimensions_(TableDimensions dimensions); - [[nodiscard]] abstract::Element *column_(const abstract::Document *, - std::uint32_t column) const; - [[nodiscard]] abstract::Element *row_(const abstract::Document *, - std::uint32_t row) const; - [[nodiscard]] abstract::Element *cell_(const abstract::Document *, - std::uint32_t column, - std::uint32_t row) const; - private: struct Row { - Element *element{nullptr}; - std::map cells; + abstract::SheetRow *element{nullptr}; + std::map cells; }; TableDimensions m_dimensions; - std::map m_columns; + std::map m_columns; std::map m_rows; Element *m_first_shape{nullptr}; }; template <> -std::tuple +std::tuple parse_element_tree(Document &document, pugi::xml_node node); } // namespace odr::internal::odf diff --git a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_element.cpp b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_element.cpp index e072d500..6ceb474a 100644 --- a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_element.cpp +++ b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_element.cpp @@ -106,6 +106,10 @@ abstract::Element *Sheet::first_shape(const abstract::Document *) const { return nullptr; // TODO } +TableStyle Sheet::style(const abstract::Document *) const { + return TableStyle(); // TODO +} + pugi::xml_node Sheet::sheet_node_(const abstract::Document *document) const { return sheet_(document, m_node.attribute("r:id").value()); } @@ -115,7 +119,7 @@ pugi::xml_node Sheet::drawing_node_(const abstract::Document *document) const { } TableColumnStyle SheetColumn::style(const abstract::Document *, - const abstract::Sheet *, + abstract::Sheet *, std::uint32_t /*column*/) const { TableColumnStyle result; if (auto width = m_node.attribute("width")) { @@ -134,8 +138,7 @@ SheetColumn::max_(const abstract::Document *) const { return m_node.attribute("max").as_uint() - 1; } -TableRowStyle SheetRow::style(const abstract::Document *, - const abstract::Sheet *, +TableRowStyle SheetRow::style(const abstract::Document *, abstract::Sheet *, std::uint32_t /*row*/) const { TableRowStyle result; if (auto height = m_node.attribute("ht")) { @@ -144,14 +147,13 @@ TableRowStyle SheetRow::style(const abstract::Document *, return result; } -bool SheetCell::is_covered(const abstract::Document *, const abstract::Sheet *, +bool SheetCell::is_covered(const abstract::Document *, abstract::Sheet *, std::uint32_t /*column*/, std::uint32_t /*row*/) const { return false; // TODO } -ValueType SheetCell::value_type(const abstract::Document *, - const abstract::Sheet *, +ValueType SheetCell::value_type(const abstract::Document *, abstract::Sheet *, std::uint32_t /*column*/, std::uint32_t /*row*/) const { return ValueType::string; @@ -165,16 +167,14 @@ SheetCell::partial_style(const abstract::Document *document) const { return {}; } -TableDimensions SheetCell::span(const abstract::Document *document, - const abstract::Sheet *, +TableDimensions SheetCell::span(const abstract::Document *, abstract::Sheet *, std::uint32_t /*column*/, std::uint32_t /*row*/) const { return {1, 1}; } TableCellStyle SheetCell::style(const abstract::Document *document, - const abstract::Sheet *, - std::uint32_t /*column*/, + abstract::Sheet *, std::uint32_t /*column*/, std::uint32_t /*row*/) const { return partial_style(document).table_cell_style; } diff --git a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_element.hpp b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_element.hpp index ae78034f..be747d31 100644 --- a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_element.hpp +++ b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_element.hpp @@ -77,6 +77,8 @@ class Sheet final : public Element, public abstract::Sheet { [[nodiscard]] abstract::Element * first_shape(const abstract::Document *) const final; + [[nodiscard]] TableStyle style(const abstract::Document *) const final; + private: pugi::xml_node sheet_node_(const abstract::Document *) const; pugi::xml_node drawing_node_(const abstract::Document *) const; @@ -87,7 +89,7 @@ class SheetColumn final : public Element, public abstract::SheetColumn { using Element::Element; [[nodiscard]] TableColumnStyle style(const abstract::Document *, - const abstract::Sheet *, + abstract::Sheet *, std::uint32_t column) const final; private: @@ -100,7 +102,7 @@ class SheetRow final : public Element, public abstract::SheetRow { using Element::Element; [[nodiscard]] TableRowStyle style(const abstract::Document *, - const abstract::Sheet *, + abstract::Sheet *, std::uint32_t row) const final; }; @@ -108,21 +110,18 @@ class SheetCell final : public Element, public abstract::SheetCell { public: using Element::Element; - [[nodiscard]] bool is_covered(const abstract::Document *, - const abstract::Sheet *, std::uint32_t column, + [[nodiscard]] bool is_covered(const abstract::Document *, abstract::Sheet *, + std::uint32_t column, std::uint32_t row) const final; [[nodiscard]] TableDimensions span(const abstract::Document *, - const abstract::Sheet *, - std::uint32_t column, + abstract::Sheet *, std::uint32_t column, std::uint32_t row) const final; [[nodiscard]] ValueType value_type(const abstract::Document *, - const abstract::Sheet *, - std::uint32_t column, + abstract::Sheet *, std::uint32_t column, std::uint32_t row) const final; [[nodiscard]] TableCellStyle style(const abstract::Document *, - const abstract::Sheet *, - std::uint32_t column, + abstract::Sheet *, std::uint32_t column, std::uint32_t row) const final; [[nodiscard]] common::ResolvedStyle