aboutsummaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-05-26 15:31:12 -0700
committerrtk0c <[email protected]>2021-05-29 13:34:43 -0700
commitf957f4094a8d98ad0de294c3e6325ce9a860994e (patch)
treedb5caa6fbefa1699149e974448fd4dab6b2535b1 /core/src
parenta611b22650d1e40593db4fb1bce29d925e49e932 (diff)
More work on TableTemplate
Diffstat (limited to 'core/src')
-rw-r--r--core/src/Model/GlobalStates.cpp4
-rw-r--r--core/src/Model/Template/TableTemplate.cpp31
-rw-r--r--core/src/Model/Template/TableTemplate.hpp53
-rw-r--r--core/src/Model/Template/fwd.hpp4
-rw-r--r--core/src/Model/Workflow/Values/BasicValues.cpp5
-rwxr-xr-xcore/src/Utils/Size.hpp65
-rw-r--r--core/src/Utils/Vector.hpp30
7 files changed, 187 insertions, 5 deletions
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 <xlsxwriter.h>
+
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 <tsl/robin_map.h>
#include <string>
#include <vector>
@@ -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<Vec2i, std::string> 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<T>::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 "<err>";
}
+#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 T>
+class Size2 {
+public:
+ T width;
+ T height;
+
+public:
+ Size2()
+ : width{ 0 }, height{ 0 } {
+ }
+
+ Size2(T width, T height)
+ : width{ width }, height{ height } {
+ }
+
+ Size2(Vec2<T> vec)
+ : width{ vec.x }, height{ vec.y }
+ {
+ }
+
+ operator Vec2<T>() const
+ {
+ return { width, height };
+ }
+
+ Vec2<T> AsVec() const
+ {
+ return { width, height };
+ }
+
+ friend bool operator==(const Size2<T>&, const Size2<T>&) = default;
+
+ template <class TTarget>
+ Size2<TTarget> Cast() const
+ {
+ return {
+ static_cast<TTarget>(width),
+ static_cast<TTarget>(height),
+ };
+ }
+};
+
+template <class T>
+Size2<T> operator+(Size2<T> a, Size2<T> b) {
+ return { a.width + b.width, a.height + b.height };
+}
+
+template <class T>
+Size2<T> operator-(Size2<T> a, Size2<T> b) {
+ return { a.width - b.width, a.height - b.height };
+}
+
+template <class T, class N>
+auto operator*(Size2<T> a, N mult) -> Size2<decltype(a.width * mult)> {
+ return { a.width * mult, a.height * mult };
+}
+
+template <class T, class N>
+auto operator/(Size2<T> a, N mult) -> Size2<decltype(a.width / mult)> {
+ 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 <class TTarget>
+ Vec2<TTarget> Cast() const
+ {
+ return {
+ static_cast<TTarget>(x),
+ static_cast<TTarget>(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 <class TTarget>
+ Vec3<TTarget> Cast() const
+ {
+ return {
+ static_cast<TTarget>(x),
+ static_cast<TTarget>(y),
+ static_cast<TTarget>(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 <class TTarget>
+ Vec4<TTarget> Cast() const
+ {
+ return {
+ static_cast<TTarget>(x),
+ static_cast<TTarget>(y),
+ static_cast<TTarget>(z),
+ static_cast<TTarget>(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 }; }