Skip to content

Commit

Permalink
redo sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Dec 10, 2023
1 parent e0cfb10 commit 37e1698
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 279 deletions.
35 changes: 13 additions & 22 deletions src/odr/document_element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,11 @@ TableDimensions Sheet::content(std::optional<TableDimensions> range) const {
}

SheetColumn Sheet::column(std::uint32_t column) const {
return exists_() ? SheetColumn(m_document, m_element, column,
m_element->column(m_document, column))
: SheetColumn();
return exists_() ? SheetColumn(m_document, m_element, column) : SheetColumn();
}

SheetRow Sheet::row(std::uint32_t row) const {
return exists_() ? SheetRow(m_document, m_element, row,
m_element->row(m_document, row))
: SheetRow();
return exists_() ? SheetRow(m_document, m_element, row) : SheetRow();
}

SheetCell Sheet::cell(std::uint32_t column, std::uint32_t row) const {
Expand All @@ -207,22 +203,20 @@ ElementRange Sheet::shapes() const {
}

SheetColumn::SheetColumn(const internal::abstract::Document *document,
internal::abstract::Sheet *sheet, std::uint32_t column,
internal::abstract::SheetColumn *element)
: TypedElement(document, element), m_sheet{sheet}, m_column{column} {}
internal::abstract::Sheet *sheet, std::uint32_t column)
: TypedElement(document, sheet), m_column{column} {}

TableColumnStyle SheetColumn::style() const {
return exists_() ? m_element->style(m_document, m_sheet, m_column)
return exists_() ? m_element->column_style(m_document, m_column)
: TableColumnStyle();
}

SheetRow::SheetRow(const internal::abstract::Document *document,
internal::abstract::Sheet *sheet, std::uint32_t row,
internal::abstract::SheetRow *element)
: TypedElement(document, element), m_sheet{sheet}, m_row{row} {}
internal::abstract::Sheet *sheet, std::uint32_t row)
: TypedElement(document, sheet), m_row{row} {}

TableRowStyle SheetRow::style() const {
return m_element->style(m_document, m_sheet, m_row);
return exists_() ? m_element->row_style(m_document, m_row) : TableRowStyle();
}

SheetCell::SheetCell(const internal::abstract::Document *document,
Expand All @@ -232,23 +226,20 @@ SheetCell::SheetCell(const internal::abstract::Document *document,
m_row{row} {}

bool SheetCell::is_covered() const {
return exists_() ? m_element->is_covered(m_document, m_sheet, m_column, m_row)
: false;
return exists_() ? m_element->is_covered(m_document) : false;
}

TableDimensions SheetCell::span() const {
return exists_() ? m_element->span(m_document, m_sheet, m_column, m_row)
: TableDimensions();
return exists_() ? m_element->span(m_document) : TableDimensions(1, 1);
}

ValueType SheetCell::value_type() const {
return exists_() ? m_element->value_type(m_document, m_sheet, m_column, m_row)
: ValueType::unknown;
return exists_() ? m_element->value_type(m_document) : ValueType::unknown;
}

TableCellStyle SheetCell::style() const {
return exists_() ? m_element->style(m_document, m_sheet, m_column, m_row)
: TableCellStyle();
return m_sheet != nullptr ? m_sheet->cell_style(m_document, m_column, m_row)
: TableCellStyle();
}

std::string Page::name() const {
Expand Down
14 changes: 5 additions & 9 deletions src/odr/document_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,31 +278,27 @@ class Sheet final : public TypedElement<internal::abstract::Sheet> {
[[nodiscard]] ElementRange shapes() const;
};

class SheetColumn final : public TypedElement<internal::abstract::SheetColumn> {
class SheetColumn final : public TypedElement<internal::abstract::Sheet> {
public:
SheetColumn() = default;
SheetColumn(const internal::abstract::Document *document,
internal::abstract::Sheet *sheet, std::uint32_t column,
internal::abstract::SheetColumn *element);
internal::abstract::Sheet *sheet, std::uint32_t column);

[[nodiscard]] TableColumnStyle style() const;

private:
internal::abstract::Sheet *m_sheet{};
std::uint32_t m_column{};
};

class SheetRow final : public TypedElement<internal::abstract::SheetRow> {
class SheetRow final : public TypedElement<internal::abstract::Sheet> {
public:
SheetRow() = default;
SheetRow(const internal::abstract::Document *document,
internal::abstract::Sheet *sheet, std::uint32_t row,
internal::abstract::SheetRow *element);
internal::abstract::Sheet *sheet, std::uint32_t row);

[[nodiscard]] TableRowStyle style() const;

private:
internal::abstract::Sheet *m_sheet{};
std::uint32_t m_row{};
};

Expand All @@ -320,7 +316,7 @@ class SheetCell final : public TypedElement<internal::abstract::SheetCell> {
[[nodiscard]] TableCellStyle style() const;

private:
internal::abstract::Sheet *m_sheet{};
internal::abstract::Sheet *m_sheet;
std::uint32_t m_column{};
std::uint32_t m_row{};
};
Expand Down
49 changes: 11 additions & 38 deletions src/odr/internal/abstract/sheet_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <odr/internal/abstract/document_element.hpp>

namespace odr::internal::abstract {
class SheetColumn;
class SheetRow;
class SheetCell;

class Sheet : public virtual Element {
Expand All @@ -20,36 +18,19 @@ class Sheet : public virtual Element {
[[nodiscard]] virtual TableDimensions
content(const Document *, std::optional<TableDimensions> range) const = 0;

[[nodiscard]] virtual SheetColumn *column(const Document *,
std::uint32_t column) const = 0;
[[nodiscard]] virtual SheetRow *row(const Document *,
std::uint32_t row) const = 0;
[[nodiscard]] virtual SheetCell *cell(const Document *, std::uint32_t column,
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 {
public:
[[nodiscard]] ElementType type(const Document *) const override {
return ElementType::table_column;
}

[[nodiscard]] virtual TableColumnStyle style(const Document *, Sheet *,
std::uint32_t column) const = 0;
};

class SheetRow : public virtual Element {
public:
[[nodiscard]] ElementType type(const Document *) const override {
return ElementType::table_row;
}

[[nodiscard]] virtual TableRowStyle style(const Document *, Sheet *,
std::uint32_t row) const = 0;
[[nodiscard]] virtual TableColumnStyle
column_style(const Document *, std::uint32_t column) const = 0;
[[nodiscard]] virtual TableRowStyle row_style(const Document *,
std::uint32_t row) const = 0;
[[nodiscard]] virtual TableCellStyle cell_style(const Document *,
std::uint32_t column,
std::uint32_t row) const = 0;
};

class SheetCell : public virtual Element {
Expand All @@ -58,19 +39,11 @@ class SheetCell : public virtual Element {
return ElementType::table_cell;
}

[[nodiscard]] virtual bool is_covered(const Document *, Sheet *,
std::uint32_t column,
std::uint32_t row) const = 0;
[[nodiscard]] virtual TableDimensions span(const Document *, Sheet *,
std::uint32_t column,
std::uint32_t row) const = 0;
[[nodiscard]] virtual ValueType value_type(const Document *, Sheet *,
std::uint32_t column,
std::uint32_t row) const = 0;
[[nodiscard]] virtual bool is_covered(const Document *) const = 0;
[[nodiscard]] virtual TableDimensions span(const Document *) const = 0;
[[nodiscard]] virtual ValueType value_type(const Document *) const = 0;

[[nodiscard]] virtual TableCellStyle style(const Document *, Sheet *,
std::uint32_t column,
std::uint32_t row) const = 0;
[[nodiscard]] virtual TableCellStyle style(const Document *) const = 0;
};

} // namespace odr::internal::abstract
Expand Down
23 changes: 16 additions & 7 deletions src/odr/internal/html/document_element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <odr/internal/html/document_style.hpp>
#include <odr/internal/html/image_file.hpp>

#include "odr/internal/common/table_cursor.hpp"
#include <iostream>

namespace odr::internal {
Expand Down Expand Up @@ -99,6 +100,8 @@ void html::translate_sheet(Element element, std::ostream &out,
end_column = std::min(end_column, config.spreadsheet_limit->columns);
end_row = std::min(end_row, config.spreadsheet_limit->rows);
}
end_column = std::max(1u, end_column);
end_row = std::max(1u, end_row);

out << "<col>";

Expand Down Expand Up @@ -127,7 +130,9 @@ void html::translate_sheet(Element element, std::ostream &out,
out << "</tr>";
}

for (std::uint32_t row_index = 0; row_index < end_row; ++row_index) {
common::TableCursor cursor;
for (std::uint32_t row_index = cursor.row(); row_index < end_row;
row_index = cursor.row()) {
auto table_row = sheet.row(row_index);
auto table_row_style = table_row.style();

Expand All @@ -144,8 +149,8 @@ void html::translate_sheet(Element element, std::ostream &out,
out << common::TablePosition::to_row_string(row_index);
out << "</td>";

for (std::uint32_t column_index = 0; column_index < end_column;
++column_index) {
for (std::uint32_t column_index = cursor.column();
column_index < end_column; column_index = cursor.column()) {
auto cell = sheet.cell(column_index, row_index);

if (cell.is_covered()) {
Expand All @@ -160,27 +165,31 @@ void html::translate_sheet(Element element, std::ostream &out,
auto cell_elements = cell.children();

out << "<td";
if (cell_span.rows > 1) {
out << " rowspan=\"" << cell_span.rows << "\"";
}
if (cell_span.columns > 1) {
out << " colspan=\"" << cell_span.columns << "\"";
}
if (cell_span.rows > 1) {
out << " rowspan=\"" << cell_span.rows << "\"";
}
out << optional_style_attribute(translate_table_cell_style(cell_style));
if (cell_value_type == ValueType::float_number) {
out << " class=\"odr-value-type-float\"";
}
out << ">";
if ((row_index == 0) && (column_index == 0)) {
if ((column_index == 0) && (row_index == 0)) {
for (auto shape : sheet.shapes()) {
translate_element(shape, out, config);
}
}
translate_children(cell_elements, out, config);
out << "</td>";

cursor.add_cell(cell_span.columns, cell_span.rows);
}

out << "</tr>";

cursor.add_row();
}

out << "</table>";
Expand Down
7 changes: 4 additions & 3 deletions src/odr/internal/odf/odf_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ class TableRow;

Element *parse_tree(Document &document, pugi::xml_node node);

template <typename element_t>
template <typename element_t, typename... args_t>
std::tuple<element_t *, pugi::xml_node>
parse_element_tree(Document &document, pugi::xml_node node) {
parse_element_tree(Document &document, pugi::xml_node node, args_t &&...args) {
if (!node) {
return std::make_tuple(nullptr, pugi::xml_node());
}

auto element_unique = std::make_unique<element_t>(node);
auto element_unique =
std::make_unique<element_t>(node, std::forward<args_t>(args)...);
auto element = element_unique.get();
document.register_element_(std::move(element_unique));

Expand Down
Loading

0 comments on commit 37e1698

Please sign in to comment.