aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/Template
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-05-30 23:00:41 -0700
committerrtk0c <[email protected]>2021-05-30 23:00:41 -0700
commitc90f78df080a9891930ee346b0ad87498ba5b697 (patch)
treec4f4c475086337e25cbb985625423591c47310e1 /core/src/Model/Template
parent088da97531935a61870ecada10f06b9b9a8255d1 (diff)
Initial work on templates UI
Diffstat (limited to 'core/src/Model/Template')
-rw-r--r--core/src/Model/Template/TableTemplate.cpp63
-rw-r--r--core/src/Model/Template/TableTemplate.hpp12
-rw-r--r--core/src/Model/Template/Template.hpp45
-rw-r--r--core/src/Model/Template/Template_Main.cpp5
-rw-r--r--core/src/Model/Template/Template_RTTI.cpp21
-rw-r--r--core/src/Model/Template/fwd.hpp4
6 files changed, 131 insertions, 19 deletions
diff --git a/core/src/Model/Template/TableTemplate.cpp b/core/src/Model/Template/TableTemplate.cpp
index 258bb37..0ad1cca 100644
--- a/core/src/Model/Template/TableTemplate.cpp
+++ b/core/src/Model/Template/TableTemplate.cpp
@@ -5,6 +5,7 @@
bool TableCell::IsDataHoldingCell() const
{
+ return IsPrimaryCell() || !IsMergedCell();
}
bool TableCell::IsPrimaryCell() const
@@ -58,12 +59,12 @@ const TableTemplate& TableInstanciationParameters::GetTable() const
int TableTemplate::GetTableWidth() const
{
- return mTableWidth;
+ return mColumnWidths.size();
}
int TableTemplate::GetTableHeight() const
{
- return mTableHeight;
+ return mRowHeights.size();
}
void TableTemplate::Resize(int newWidth, int newHeight)
@@ -73,10 +74,13 @@ void TableTemplate::Resize(int newWidth, int newHeight)
std::vector<TableCell> cells;
cells.reserve(newWidth * newHeight);
- int yEnd = std::min(mTableHeight, newHeight);
- int xEnd = std::min(mTableWidth, newWidth);
+ int tableWidth = GetTableWidth();
+ int tableHeight = GetTableHeight();
+
+ int yEnd = std::min(tableHeight, newHeight);
+ int xEnd = std::min(tableWidth, newWidth);
for (int y = 0; y < yEnd; ++y) {
- if (y >= mTableHeight) {
+ if (y >= tableHeight) {
for (int x = 0; x < xEnd; ++x) {
cells.push_back(TableCell{});
}
@@ -84,7 +88,7 @@ void TableTemplate::Resize(int newWidth, int newHeight)
}
for (int x = 0; x < xEnd; ++x) {
- if (x >= mTableWidth) {
+ if (x >= tableWidth) {
cells.push_back(TableCell{});
} else {
auto& cell = GetCell({ x, y });
@@ -96,9 +100,30 @@ void TableTemplate::Resize(int newWidth, int newHeight)
mCells = std::move(cells);
}
+int TableTemplate::GetRowHeight(int row) const
+{
+ return mRowHeights[row];
+}
+
+void TableTemplate::SetRowHeight(int row, int height)
+{
+ mRowHeights[row] = height;
+}
+
+int TableTemplate::GetColumnWidth(int column) const
+{
+ return mColumnWidths[column];
+}
+
+void TableTemplate::SetColumnWidth(int column, int width)
+{
+ mColumnWidths[column] = width;
+}
+
const TableCell& TableTemplate::GetCell(Vec2i pos) const
{
- return mCells[pos.y * mTableWidth + pos.x];
+ int tableWidth = GetTableWidth();
+ return mCells[pos.y * tableWidth + pos.x];
}
TableCell& TableTemplate::GetCell(Vec2i pos)
@@ -194,7 +219,7 @@ lxw_worksheet* TableTemplate::InstanciateToExcelWorksheet(lxw_workbook* workbook
// Not enough space to fit in this array group, update (or insert) the appropriate amount of generated rows
int row = i;
int count = param.size();
- generatedRanges.insert(row, count);
+ generatedRanges.try_emplace(row, count);
}
auto GetOffset = [&](int y) -> int {
@@ -217,27 +242,33 @@ lxw_worksheet* TableTemplate::InstanciateToExcelWorksheet(lxw_workbook* workbook
}
};
- // Write/instanciate all array groups
+ // Write/instantiate all array groups
for (size_t i = 0; i < mArrayGroups.size(); ++i) {
auto& groupInfo = mArrayGroups[i];
auto& groupParams = params.ArrayGroups[i];
int rowCellCount = groupInfo.GetCount();
- int row = groupInfo.Row + GetOffset(groupInfo.Row);
+ int rowCount = groupParams.size();
+ int baseRowIdx = groupInfo.Row + GetOffset(groupInfo.Row);
// For each row that would be generated
- for (auto& rowCells : groupParams) {
+ for (int rowIdx = 0; rowIdx < rowCount; ++rowIdx) {
+ auto& row = groupParams[rowIdx];
+
// For each cell in the row
- for (int i = 0; i < rowCellCount; ++i) {
- // TODO spport merged cells in array groups
- worksheet_write_string(worksheet, row + i, col, rowCells[i].c_str(), nullptr);
+ for (int rowCellIdx = 0; rowCellIdx < rowCellCount; ++rowCellIdx) {
+ // TODO support merged cells in array groups
+ worksheet_write_string(worksheet, baseRowIdx + rowIdx, rowCellIdx, row[rowCellIdx].c_str(), nullptr);
}
}
}
+ int tableWidth = GetTableWidth();
+ int tableHeight = GetTableHeight();
+
// Write all regular and singular parameter cells
- for (int y = 0; y < mTableHeight; ++y) {
- for (int x = 0; x < mTableWidth; ++x) {
+ for (int y = 0; y < tableHeight; ++y) {
+ for (int x = 0; x < tableWidth; ++x) {
auto& cell = GetCell({ x, y });
if (!cell.IsDataHoldingCell()) {
diff --git a/core/src/Model/Template/TableTemplate.hpp b/core/src/Model/Template/TableTemplate.hpp
index be043a9..688192a 100644
--- a/core/src/Model/Template/TableTemplate.hpp
+++ b/core/src/Model/Template/TableTemplate.hpp
@@ -1,5 +1,6 @@
#pragma once
+#include "Model/Template/Template.hpp"
#include "Utils/Vector.hpp"
#include "Utils/VectorHash.hpp"
#include "cplt_fwd.hpp"
@@ -127,19 +128,24 @@ public:
/// parametric rows/columns, and grids are also supported.
///
/// This current supports exporting to xlsx files.
-class TableTemplate
+class TableTemplate : public Template
{
private:
std::vector<TableCell> mCells;
std::vector<TableCellArrayGroup> mArrayGroups;
- int mTableWidth;
- int mTableHeight;
+ std::vector<int> mRowHeights;
+ std::vector<int> mColumnWidths;
public:
int GetTableWidth() const;
int GetTableHeight() const;
void Resize(int newWidth, int newHeight);
+ int GetRowHeight(int row) const;
+ void SetRowHeight(int row, int height);
+ int GetColumnWidth(int column) const;
+ void SetColumnWidth(int column, int width);
+
const TableCell& GetCell(Vec2i pos) const;
TableCell& GetCell(Vec2i pos);
diff --git a/core/src/Model/Template/Template.hpp b/core/src/Model/Template/Template.hpp
new file mode 100644
index 0000000..0901a1b
--- /dev/null
+++ b/core/src/Model/Template/Template.hpp
@@ -0,0 +1,45 @@
+#pragma once
+
+#include "cplt_fwd.hpp"
+
+#include <filesystem>
+#include <iosfwd>
+#include <memory>
+#include <string>
+
+class Template
+{
+public:
+ enum Kind
+ {
+ KD_Table,
+
+ InvalidKind,
+ KindCount = InvalidKind,
+ };
+
+public:
+ static const char* FormatKind(Kind kind);
+ static std::unique_ptr<Template> CreateByKind(Kind kind);
+
+ virtual ~Template() = default;
+
+ enum class ReadResult
+ {
+ RR_Success,
+ RR_InvalidFormat,
+ };
+ ReadResult ReadFrom(std::istream& stream) = 0;
+ void WriteTo(std::ostream& stream) const = 0;
+};
+
+class TemplateInfo
+{
+public:
+ std::filesystem::path Path;
+ std::string Name;
+ std::string PathStringCache = Path.string();
+ Template::Kind Kind;
+
+ std::unique_ptr<Template> LoadFromDisk() const;
+};
diff --git a/core/src/Model/Template/Template_Main.cpp b/core/src/Model/Template/Template_Main.cpp
new file mode 100644
index 0000000..eeb6871
--- /dev/null
+++ b/core/src/Model/Template/Template_Main.cpp
@@ -0,0 +1,5 @@
+#include "Template.hpp"
+
+std::unique_ptr<Template> TemplateInfo::LoadFromDisk() const
+{
+}
diff --git a/core/src/Model/Template/Template_RTTI.cpp b/core/src/Model/Template/Template_RTTI.cpp
new file mode 100644
index 0000000..042aaec
--- /dev/null
+++ b/core/src/Model/Template/Template_RTTI.cpp
@@ -0,0 +1,21 @@
+#include "Template.hpp"
+
+#include "Model/Template/TableTemplate.hpp"
+
+inline const char* Template::FormatKind(Kind kind)
+{
+ switch (kind) {
+ case KD_Table: return "Table template";
+
+ case InvalidKind: return "<invalid kind>";
+ }
+}
+
+inline std::unique_ptr<Template> Template::CreateByKind(Kind kind)
+{
+ switch (kind) {
+ case KD_Table: return std::make_unique<TableTemplate>();
+
+ case InvalidKind: return nullptr;
+ }
+}
diff --git a/core/src/Model/Template/fwd.hpp b/core/src/Model/Template/fwd.hpp
index 2f41f22..6bc7349 100644
--- a/core/src/Model/Template/fwd.hpp
+++ b/core/src/Model/Template/fwd.hpp
@@ -5,3 +5,7 @@ class TableCell;
class TableCellArrayGroup;
class TableInstanciationParameters;
class TableTemplate;
+
+// Template.hpp
+class Template;
+class TemplateInfo;