aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-06-19 22:41:05 -0700
committerrtk0c <[email protected]>2021-06-19 22:41:05 -0700
commit4378dcd83d2387534f3b10276365d1003832fe81 (patch)
tree6bc055a1bd81d5a27c9066f05e28e5115f5de944 /core/src/Model
parenteec8dfd8a21b8bb37f6acac1da84cde8fbf7ace7 (diff)
Add cell type conversion mechanism
Diffstat (limited to 'core/src/Model')
-rw-r--r--core/src/Model/Template/TableTemplate.cpp114
-rw-r--r--core/src/Model/Template/TableTemplate.hpp39
-rw-r--r--core/src/Model/Template/fwd.hpp2
3 files changed, 140 insertions, 15 deletions
diff --git a/core/src/Model/Template/TableTemplate.cpp b/core/src/Model/Template/TableTemplate.cpp
index be61606..88980ae 100644
--- a/core/src/Model/Template/TableTemplate.cpp
+++ b/core/src/Model/Template/TableTemplate.cpp
@@ -34,26 +34,30 @@ int TableCellArrayGroup::GetCount() const
return RightCell - LeftCell + 1;
}
-TableInstanciationParameters::TableInstanciationParameters(const TableTemplate& table)
+Vec2i TableCellArrayGroup::FindCell(std::string_view name)
+{
+}
+
+TableInstantiationParameters::TableInstantiationParameters(const TableTemplate& table)
: mTable{ &table }
{
}
-TableInstanciationParameters& TableInstanciationParameters::ResetTable(const TableTemplate& newTable)
+TableInstantiationParameters& TableInstantiationParameters::ResetTable(const TableTemplate& newTable)
{
mTable = &newTable;
return *this;
}
-TableInstanciationParameters TableInstanciationParameters::RebindTable(const TableTemplate& newTable) const
+TableInstantiationParameters TableInstantiationParameters::RebindTable(const TableTemplate& newTable) const
{
- TableInstanciationParameters result(newTable);
+ TableInstantiationParameters result(newTable);
result.SingularCells = this->SingularCells;
result.ArrayGroups = this->ArrayGroups;
return result;
}
-const TableTemplate& TableInstanciationParameters::GetTable() const
+const TableTemplate& TableInstantiationParameters::GetTable() const
{
return *mTable;
}
@@ -142,6 +146,102 @@ TableCell& TableTemplate::GetCell(Vec2i pos)
return const_cast<TableCell&>(const_cast<const TableTemplate*>(this)->GetCell(pos));
}
+void TableTemplate::SetCellType(Vec2i pos, TableCell::CellType type)
+{
+ auto& cell = GetCell(pos);
+ if (cell.Type == type) {
+ return;
+ }
+
+ switch (type) {
+ case TableCell::ConstantCell: {
+ // TODO
+ } break;
+
+ case TableCell::SingularParametricCell: {
+ cell.Type = type;
+ } break;
+
+ // Setting the cell to be a part of a group requires calling the overload that supplies the group
+ case TableCell::ArrayParametricCell: break;
+ }
+}
+
+int TableTemplate::GetArrayGroupCount() const
+{
+ return mArrayGroups.size();
+}
+
+const TableCellArrayGroup& TableTemplate::GetArrayGroup(int id) const
+{
+ return mArrayGroups[id];
+}
+
+TableCellArrayGroup& TableTemplate::GetArrayGroup(int id)
+{
+ return mArrayGroups[id];
+}
+
+TableCellArrayGroup& TableTemplate::AddArrayGroup(int row, int left, int right)
+{
+ assert(row >= 0 && row < GetTableHeight());
+ assert(left >= 0 && left < GetTableWidth());
+ assert(right >= 0 && right < GetTableWidth());
+
+ // TODO check for overlap
+
+ if (left > right) {
+ std::swap(left, right);
+ }
+
+ mArrayGroups.push_back(TableCellArrayGroup{
+ .Row = row,
+ .LeftCell = left,
+ .RightCell = right,
+ });
+ return mArrayGroups.back();
+}
+
+bool TableTemplate::ExtendArrayGroupLeft(int id, int n)
+{
+ assert(n > 0);
+
+ auto& ag = mArrayGroups[id];
+ ag.LeftCell -= n;
+
+ return false;
+}
+
+bool TableTemplate::ExtendArrayGroupRight(int id, int n)
+{
+ assert(n > 0);
+
+ auto& ag = mArrayGroups[id];
+ ag.RightCell += n;
+
+ return false;
+}
+
+TableCell* TableTemplate::FindCell(std::string_view name)
+{
+ auto iter = mName2Parameters.find(name);
+ if (iter != mName2Parameters.end()) {
+ return &mCells[iter.value()];
+ } else {
+ return nullptr;
+ }
+}
+
+TableCellArrayGroup* TableTemplate::FindArrayGroup(std::string_view name)
+{
+ auto iter = mName2ArrayGroups.find(name);
+ if (iter != mName2ArrayGroups.end()) {
+ return &mArrayGroups[iter.value()];
+ } else {
+ return nullptr;
+ }
+}
+
TableTemplate::MergeCellsResult TableTemplate::MergeCells(Vec2i topLeft, Vec2i bottomRight)
{
auto SortTwo = [](int& a, int& b) {
@@ -200,14 +300,14 @@ TableTemplate::BreakCellsResult TableTemplate::BreakCells(Vec2i topLeft)
return BCR_Success;
}
-lxw_workbook* TableTemplate::InstantiateToExcelWorkbook(const TableInstanciationParameters& params) const
+lxw_workbook* TableTemplate::InstantiateToExcelWorkbook(const TableInstantiationParameters& params) const
{
auto workbook = workbook_new("Table.xlsx");
InstantiateToExcelWorksheet(workbook, params);
return workbook;
}
-lxw_worksheet* TableTemplate::InstantiateToExcelWorksheet(lxw_workbook* workbook, const TableInstanciationParameters& params) const
+lxw_worksheet* TableTemplate::InstantiateToExcelWorksheet(lxw_workbook* workbook, const TableInstantiationParameters& params) const
{
auto worksheet = workbook_add_worksheet(workbook, "CpltExport.xlsx");
diff --git a/core/src/Model/Template/TableTemplate.hpp b/core/src/Model/Template/TableTemplate.hpp
index ed1a96f..754ebe1 100644
--- a/core/src/Model/Template/TableTemplate.hpp
+++ b/core/src/Model/Template/TableTemplate.hpp
@@ -5,8 +5,10 @@
#include "Utils/VectorHash.hpp"
#include "cplt_fwd.hpp"
+#include <tsl/array_map.h>
#include <tsl/robin_map.h>
#include <string>
+#include <string_view>
#include <vector>
class TableCell
@@ -80,11 +82,13 @@ public:
/// \endcode
///
/// \see TableCell
-/// \see TableInstanciationParameters
+/// \see TableInstantiationParameters
/// \see TableTemplate
class TableCellArrayGroup
{
public:
+ /// Parameter name mapped to cell location (index from LeftCell).
+ tsl::array_map<char, int> mName2Cell;
int Row;
/// Leftmost cell in this group
int LeftCell;
@@ -95,6 +99,9 @@ public:
Vec2i GetLeftCell() const;
Vec2i GetRightCell() const;
int GetCount() const;
+
+ /// Find the location of the cell within this array group that has the given name.
+ Vec2i FindCell(std::string_view name);
};
// Forward declaration of libxlsxwriter structs
@@ -103,7 +110,7 @@ struct lxw_worksheet;
/// An object containing the necessary information to instanciate a table template.
/// \see TableTemplate
-class TableInstanciationParameters
+class TableInstantiationParameters
{
private:
const TableTemplate* mTable;
@@ -116,10 +123,10 @@ public:
std::vector<ArrayGroupData> ArrayGroups;
public:
- TableInstanciationParameters(const TableTemplate& table);
+ TableInstantiationParameters(const TableTemplate& table);
- TableInstanciationParameters& ResetTable(const TableTemplate& newTable);
- TableInstanciationParameters RebindTable(const TableTemplate& newTable) const;
+ TableInstantiationParameters& ResetTable(const TableTemplate& newTable);
+ TableInstantiationParameters RebindTable(const TableTemplate& newTable) const;
const TableTemplate& GetTable() const;
};
@@ -131,6 +138,10 @@ public:
class TableTemplate : public Template
{
private:
+ /// Map from parameter name to index of the parameter cell (stored in mCells).
+ tsl::array_map<char, int> mName2Parameters;
+ /// Map from array group name to the index of the array group (stored in mArrayGroups).
+ tsl::array_map<char, int> mName2ArrayGroups;
std::vector<TableCell> mCells;
std::vector<TableCellArrayGroup> mArrayGroups;
std::vector<int> mRowHeights;
@@ -151,6 +162,20 @@ public:
const TableCell& GetCell(Vec2i pos) const;
TableCell& GetCell(Vec2i pos);
+ void SetCellType(Vec2i pos, TableCell::CellType type);
+
+ int GetArrayGroupCount() const;
+ const TableCellArrayGroup& GetArrayGroup(int id) const;
+ TableCellArrayGroup& GetArrayGroup(int id);
+ TableCellArrayGroup& AddArrayGroup(int row, int left, int right);
+ bool ExtendArrayGroupLeft(int id, int n);
+ bool ExtendArrayGroupRight(int id, int n);
+
+ /// Find a singular parameter cell by its name. This does not include cells within an array group.
+ TableCell* FindCell(std::string_view name);
+
+ /// Find an array group by its name.
+ TableCellArrayGroup* FindArrayGroup(std::string_view name);
enum MergeCellsResult
{
@@ -166,8 +191,8 @@ public:
};
BreakCellsResult BreakCells(Vec2i topLeft);
- lxw_workbook* InstantiateToExcelWorkbook(const TableInstanciationParameters& params) const;
- lxw_worksheet* InstantiateToExcelWorksheet(lxw_workbook* workbook, const TableInstanciationParameters& params) const;
+ lxw_workbook* InstantiateToExcelWorkbook(const TableInstantiationParameters& params) const;
+ lxw_worksheet* InstantiateToExcelWorksheet(lxw_workbook* workbook, const TableInstantiationParameters& params) const;
virtual ReadResult ReadFrom(std::istream& stream) override;
virtual void WriteTo(std::ostream& stream) const override;
diff --git a/core/src/Model/Template/fwd.hpp b/core/src/Model/Template/fwd.hpp
index b0acb28..b918755 100644
--- a/core/src/Model/Template/fwd.hpp
+++ b/core/src/Model/Template/fwd.hpp
@@ -3,7 +3,7 @@
// TableTemplate.hpp
class TableCell;
class TableCellArrayGroup;
-class TableInstanciationParameters;
+class TableInstantiationParameters;
class TableTemplate;
// Template.hpp