From f957f4094a8d98ad0de294c3e6325ce9a860994e Mon Sep 17 00:00:00 2001 From: rtk0c Date: Wed, 26 May 2021 15:31:12 -0700 Subject: More work on TableTemplate --- Size.hpp | 61 ++++++++++++++++++++++++ core/src/Model/GlobalStates.cpp | 4 +- core/src/Model/Template/TableTemplate.cpp | 31 ++++++++++++ core/src/Model/Template/TableTemplate.hpp | 53 ++++++++++++++++++++- core/src/Model/Template/fwd.hpp | 4 +- core/src/Model/Workflow/Values/BasicValues.cpp | 5 ++ core/src/Utils/Size.hpp | 65 ++++++++++++++++++++++++++ core/src/Utils/Vector.hpp | 30 ++++++++++++ 8 files changed, 248 insertions(+), 5 deletions(-) create mode 100755 Size.hpp create mode 100755 core/src/Utils/Size.hpp diff --git a/Size.hpp b/Size.hpp new file mode 100755 index 0000000..f737f9c --- /dev/null +++ b/Size.hpp @@ -0,0 +1,61 @@ +#pragma once + +#include + +template +class Size2 { +public: + T width; + T height; + +public: + Size2() + : width{ 0 }, height{ 0 } { + } + + Size2(T width, T height) + : width{ width }, height{ height } { + } + + Size2(glm::vec<2, T> vec) + : width{ vec.x }, height{ vec.y } { + } + + operator glm::vec<2, T>() const { + return { width, height }; + } + + glm::vec<2, T> AsVec() const { + return { width, height }; + } + + friend bool operator==(const Size2&, const Size2&) = default; + + template + Size2 Cast() const { + return { + static_cast(width), + static_cast(height), + }; + } +}; + +template +Size2 operator+(Size2 a, Size2 b) { + return { a.width + b.width, a.height + b.height }; +} + +template +Size2 operator-(Size2 a, Size2 b) { + return { a.width - b.width, a.height - b.height }; +} + +template +auto operator*(Size2 a, N mult) -> Size2 { + return { a.width * mult, a.height * mult }; +} + +template +auto operator/(Size2 a, N mult) -> Size2 { + return { a.width / mult, a.height / mult }; +} diff --git a/core/src/Model/GlobalStates.cpp b/core/src/Model/GlobalStates.cpp index a9b6806..2230367 100644 --- a/core/src/Model/GlobalStates.cpp +++ b/core/src/Model/GlobalStates.cpp @@ -28,7 +28,7 @@ void GlobalStates::Init(std::filesystem::path userDataDir) fs::create_directories(globalDataPath); // Reading recent projects - [&]() { + { std::ifstream ifs(globalDataPath / "recents.json"); if (!ifs) return; @@ -50,7 +50,7 @@ void GlobalStates::Init(std::filesystem::path userDataDir) }); } } - }(); + } } void GlobalStates::Shutdown() diff --git a/core/src/Model/Template/TableTemplate.cpp b/core/src/Model/Template/TableTemplate.cpp index caf0e21..0684c77 100644 --- a/core/src/Model/Template/TableTemplate.cpp +++ b/core/src/Model/Template/TableTemplate.cpp @@ -1,5 +1,7 @@ #include "TableTemplate.hpp" +#include + bool TableCell::IsPrimaryCell() const { return PrimaryCellLocation == Location; @@ -117,3 +119,32 @@ TableTemplate::BreakCellsResult TableTemplate::BreakCells(Vec2i topLeft) return BCR_Success; } + +lxw_workbook* TableTemplate::InstanciateToExcelWorkbook(const TableInstanciationParameters& params) const +{ + auto workbook = workbook_new("Table.xlsx"); + InstanciateToExcelWorksheet(workbook, params); + return workbook; +} + +lxw_worksheet* TableTemplate::InstanciateToExcelWorksheet(lxw_workbook* workbook, const TableInstanciationParameters& params) const +{ + auto worksheet = workbook_add_worksheet(workbook, "Cplt Export"); + + for (int y = 0; y < mTableHeight; ++y) { + for (int x = 0; x < mTableWidth; ++x) { + auto& cell = GetCell({ x, y }); + switch (cell.Type) { + case TableCell::ConstantCell: { + worksheet_write_string(worksheet, y, x, cell.Content.c_str(), nullptr); + } break; + + case TableCell::ParametricCell: { + // TODO + } break; + } + } + } + + return worksheet; +} diff --git a/core/src/Model/Template/TableTemplate.hpp b/core/src/Model/Template/TableTemplate.hpp index d704b1f..6454ccd 100644 --- a/core/src/Model/Template/TableTemplate.hpp +++ b/core/src/Model/Template/TableTemplate.hpp @@ -1,8 +1,10 @@ -#pragma once +#pragma once #include "Utils/Vector.hpp" #include "Utils/VectorHash.hpp" +#include "cplt_fwd.hpp" +#include #include #include @@ -42,6 +44,32 @@ public: bool IsMergedCell() const; }; +/// Parameter group information for a grouped array of cells, either horizontal or vertical. +/// These cells can have "expansion behavior", i.e. when instanciating, a list of cell values would be provided by +/// the caller: +/// \code +/// [["foo", "bar", "foobar"], +/// ["a", "b", c"], +/// ["1", "2", "3"], +/// ["x", "y", "z"]] +/// // ... may be more +/// \endcode +/// +/// For a array parameter group that vertically expands, this would create 4 rows of data in the place of the +/// original parameter group. If more than one array parameter groups are on the same row, they would share space +/// between each other : +/// +/// \code +/// | 2 elements was fed to it +/// | | 1 element was fed to it +/// V V +/// {~~~~~~~~~~~~~~~~}{~~~~~~~~~~~~~~} +/// +------+---------+---------------+ +/// | Foo | Example | Another group | +/// +------+---------+---------------+ +/// | Cool | Example | | +/// +------+---------+---------------+ +/// \endcode class TableArrayParameterGroup { public: @@ -57,6 +85,26 @@ public: ExpansionDirection ExpandDirection; }; +// Forward declaration of libxlsxwriter structs +struct lxw_workbook; +struct lxw_worksheet; + +/// An object containing the necessary information to instanciate a table template. +/// \see TableTemplate +class TableInstanciationParameters +{ +private: + TableTemplate* mTemplate; + tsl::robin_map ParametricCells; + +public: + // TODO +}; + +/// A table template, where individual cells can be filled by workflows instanciating this template. Merged cells, +/// parametric rows/columns, and grids are also supported. +/// +/// This current supports exporting to xlsx files. class TableTemplate { private: @@ -86,4 +134,7 @@ public: BCR_Success, }; BreakCellsResult BreakCells(Vec2i topLeft); + + lxw_workbook* InstanciateToExcelWorkbook(const TableInstanciationParameters& params) const; + lxw_worksheet* InstanciateToExcelWorksheet(lxw_workbook* workbook, const TableInstanciationParameters& params) const; }; diff --git a/core/src/Model/Template/fwd.hpp b/core/src/Model/Template/fwd.hpp index 2bf6814..d62552f 100644 --- a/core/src/Model/Template/fwd.hpp +++ b/core/src/Model/Template/fwd.hpp @@ -2,6 +2,6 @@ // TableTemplate.hpp class TableCell; -class ConstantTableCell; -class ParametricTableCell; +class TableArrayParameterGroup; +class TableInstanciationParameters; class TableTemplate; diff --git a/core/src/Model/Workflow/Values/BasicValues.cpp b/core/src/Model/Workflow/Values/BasicValues.cpp index 93ee366..a7cf635 100644 --- a/core/src/Model/Workflow/Values/BasicValues.cpp +++ b/core/src/Model/Workflow/Values/BasicValues.cpp @@ -20,12 +20,17 @@ static std::string NumberToString(T value) constexpr auto kSize = std::numeric_limits::max_digits10; char buf[kSize]; +#if PLATFORM_WIN32 auto res = std::to_chars(buf, buf + kSize, value); if (res.ec == std::errc()) { return std::string(buf, res.ptr); } else { return ""; } +#else + // TODO libstdc++ doesn't have floating point charconv yet + return std::to_string(value); +#endif } std::string NumericValue::GetTruncatedString() const diff --git a/core/src/Utils/Size.hpp b/core/src/Utils/Size.hpp new file mode 100755 index 0000000..98be41a --- /dev/null +++ b/core/src/Utils/Size.hpp @@ -0,0 +1,65 @@ +#pragma once + +#include "Utils/Vector.hpp" + +template +class Size2 { +public: + T width; + T height; + +public: + Size2() + : width{ 0 }, height{ 0 } { + } + + Size2(T width, T height) + : width{ width }, height{ height } { + } + + Size2(Vec2 vec) + : width{ vec.x }, height{ vec.y } + { + } + + operator Vec2() const + { + return { width, height }; + } + + Vec2 AsVec() const + { + return { width, height }; + } + + friend bool operator==(const Size2&, const Size2&) = default; + + template + Size2 Cast() const + { + return { + static_cast(width), + static_cast(height), + }; + } +}; + +template +Size2 operator+(Size2 a, Size2 b) { + return { a.width + b.width, a.height + b.height }; +} + +template +Size2 operator-(Size2 a, Size2 b) { + return { a.width - b.width, a.height - b.height }; +} + +template +auto operator*(Size2 a, N mult) -> Size2 { + return { a.width * mult, a.height * mult }; +} + +template +auto operator/(Size2 a, N mult) -> Size2 { + return { a.width / mult, a.height / mult }; +} diff --git a/core/src/Utils/Vector.hpp b/core/src/Utils/Vector.hpp index f49965e..7e71b73 100644 --- a/core/src/Utils/Vector.hpp +++ b/core/src/Utils/Vector.hpp @@ -6,6 +6,15 @@ struct Vec2 T x = 0; T y = 0; + template + Vec2 Cast() const + { + return { + static_cast(x), + static_cast(y), + }; + } + friend constexpr bool operator==(const Vec2& a, const Vec2& b) = default; friend constexpr Vec2 operator+(const Vec2& a, const Vec2& b) { return { a.x + b.x, a.y + b.y }; } @@ -29,6 +38,16 @@ struct Vec3 T y = 0; T z = 0; + template + Vec3 Cast() const + { + return { + static_cast(x), + static_cast(y), + static_cast(z), + }; + } + friend constexpr bool operator==(const Vec3& a, const Vec3& b) = default; friend constexpr Vec3 operator+(const Vec3& a, const Vec3& b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; } @@ -53,6 +72,17 @@ struct Vec4 T z = 0; T w = 0; + template + Vec4 Cast() const + { + return { + static_cast(x), + static_cast(y), + static_cast(z), + static_cast(w), + }; + } + friend constexpr bool operator==(const Vec4& a, const Vec4& b) = default; friend constexpr Vec4 operator+(const Vec4& a, const Vec4& b) { return { a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w }; } -- cgit v1.2.3-70-g09d2