aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/Template/TableTemplate.hpp
blob: 6454ccd580d3f7d029671440d3eb3f4022bfbe6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#pragma once

#include "Utils/Vector.hpp"
#include "Utils/VectorHash.hpp"
#include "cplt_fwd.hpp"

#include <tsl/robin_map.h>
#include <string>
#include <vector>

class TableCell
{
public:
	enum TextAlignment
	{
		/// For horizontal alignment, this means align left. For vertical alignment, this means align top.
		AlignAxisMin,
		/// Align middle of the text to the middle of the axis.
		AlignCenter,
		/// For horizontal alignment, this means align right. For vertical alignment, this means align bottom.
		AlignAxisMax,
	};

	enum CellType
	{
		ConstantCell,
		ParametricCell,
	};

public:
	std::string Content;
	Vec2i Location;
	/// Location of the primary (top left) cell, if this cell is a part of a merged group.
	/// Otherwise, either component of this field shall be -1.
	Vec2i PrimaryCellLocation;
	int SpanX;
	int SpanY;
	TextAlignment HorizontalAlignment = AlignAxisMin;
	TextAlignment VerticalAlignment = AlignAxisMin;
	CellType Type;

public:
	bool IsPrimaryCell() const;
	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:
	enum ExpansionDirection
	{
		ExpandUp,
		ExpandDown,
		ExpandLeft,
		ExpandRight,
	};

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:
	std::vector<TableCell> mCells;
	std::vector<TableArrayParameterGroup> mArrayGroups;
	int mTableWidth;
	int mTableHeight;

public:
	int GetTableWidth() const;
	int GetTableHeight() const;
	void Resize(int newWidth, int newHeight);

	const TableCell& GetCell(Vec2i pos) const;
	TableCell& GetCell(Vec2i pos);

	enum MergeCellsResult
	{
		MCR_CellAlreadyMerged,
		MCR_Success,
	};
	MergeCellsResult MergeCells(Vec2i topLeft, Vec2i bottomRight);

	enum BreakCellsResult
	{
		BCR_CellNotMerged,
		BCR_Success,
	};
	BreakCellsResult BreakCells(Vec2i topLeft);

	lxw_workbook* InstanciateToExcelWorkbook(const TableInstanciationParameters& params) const;
	lxw_worksheet* InstanciateToExcelWorksheet(lxw_workbook* workbook, const TableInstanciationParameters& params) const;
};