OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
EntityResultTableData.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "DataBase.h"
5#include "OldTreeIcon.h"
6
7#include <exception>
8#include <bsoncxx/builder/basic/array.hpp>
9
11
13
14template <class T>
15EntityResultTableData<T>::EntityResultTableData(ot::UID ID, EntityBase *parent, EntityObserver *obs, ModelState *ms, ClassFactoryHandler* factory, const std::string &owner) :
16 EntityBase(ID, parent, obs, ms, factory, owner)
17{
18 className = className + "_" + ot::TemplateTypeName<T>::getTypeName();
19}
20
21template <class T>
22EntityResultTableData<T>::~EntityResultTableData()
23{
24}
25
26template <class T>
27bool EntityResultTableData<T>::getEntityBox(double &xmin, double &xmax, double &ymin, double &ymax, double &zmin, double &zmax)
28{
29 return false;
30}
31
32template <class T>
33void EntityResultTableData<T>::StoreToDataBase(void)
34{
35 EntityBase::StoreToDataBase();
36}
37
38template <class T>
39void EntityResultTableData<T>::AddStorageData(bsoncxx::builder::basic::document &storage)
40{
41 // We store the parent class information first
42 EntityBase::AddStorageData(storage);
43
44 auto headerArray = bsoncxx::builder::basic::array();
45
46 for (auto &text : header)
47 {
48 headerArray.append(text);
49 }
50
51 auto dataArray = bsoncxx::builder::basic::array();
52
53 for (unsigned int iRow = 0; iRow < getNumberOfRows(); iRow++)
54 {
55 for (unsigned int iCol = 0; iCol < getNumberOfColumns(); iCol++)
56 {
57 dataArray.append(getValue(iRow, iCol));
58 }
59 }
60
61 // Now add the actual text data
62
63 storage.append(
64 bsoncxx::builder::basic::kvp("NumberColums", (int) getNumberOfColumns()),
65 bsoncxx::builder::basic::kvp("NumberRows", (int) getNumberOfRows()),
66 bsoncxx::builder::basic::kvp("Header", headerArray),
67 bsoncxx::builder::basic::kvp("Data", dataArray)
68 );
69}
70
71template <class T>
72void EntityResultTableData<T>::readSpecificDataFromDataBase(bsoncxx::document::view &doc_view, std::map<ot::UID, EntityBase *> &entityMap)
73{
74 // We read the parent class information first
75 EntityBase::readSpecificDataFromDataBase(doc_view, entityMap);
76
77 // Now read the text data
78 header.clear();
79 data.clear();
80
81 // Now load the data from the storage
82 unsigned int numberCols = doc_view["NumberColums"].get_int32();
83 unsigned int numberRows = doc_view["NumberRows"].get_int32();
84
85 setNumberOfColumns(numberCols);
86 setNumberOfRows(numberRows);
87
88 auto headerArray = doc_view["Header"].get_array().value;
89 auto dataArray = doc_view["Data"].get_array().value;
90
91 unsigned int iCol = 0;
92 for (const auto& text : headerArray)
93 {
94 setHeaderText(iCol, text.get_utf8().value.data());
95 iCol++;
96 }
97
98 unsigned int iRow = 0;
99 iCol = 0;
100
101 for (const auto& data : dataArray)
102 {
103
104 setValue(iRow, iCol, BsonArrayTypeGetterWrapper<T>::getValue(data));
105
106 iCol++;
107 if (iCol == numberCols)
108 {
109 iRow++;
110 iCol = 0;
111 }
112 }
113
114 resetModified();
115}
116
117template <class T>
118void EntityResultTableData<T>::removeChild(EntityBase *child)
119{
120 EntityBase::removeChild(child);
121}
122
123template <class T>
124void EntityResultTableData<T>::setNumberOfColumns(uint64_t cols)
125{
126 header.resize(cols, "");
127
128 for (unsigned int iRow = 0; iRow < getNumberOfRows(); iRow++)
129 {
130 data[iRow].resize(cols);
131 }
132}
133
134template <class T>
135void EntityResultTableData<T>::setNumberOfRows(uint64_t rows)
136{
137 uint64_t oldRows = getNumberOfRows();
138
139 data.resize(rows);
140
141 if (rows > oldRows)
142 {
143 for (uint64_t iRow = oldRows; iRow < rows; iRow++)
144 {
145 data[iRow].resize(getNumberOfColumns());
146 }
147 }
148}
149
150template <class T>
151void EntityResultTableData<T>::setHeaderText(unsigned int col, const std::string &text)
152{
153 assert(col < getNumberOfColumns());
154 header[col] = text;
155}
156
157template <class T>
158void EntityResultTableData<T>::setValue(unsigned int row, unsigned int col, T value)
159{
160 assert(row < getNumberOfRows());
161 assert(col < getNumberOfColumns());
162
163 data[row][col] = value;
164}
165template<class T>
166void EntityResultTableData<T>::setRow(unsigned int row, std::vector<T>& value)
167{
168 if (row >= getNumberOfRows())
169 {
170 throw std::runtime_error("Row index out of range.");
171 }
172 if (value.size() > getNumberOfColumns())
173 {
174 throw std::runtime_error("Vector has too many entries for the set number of columns.");
175 }
176
177 uint64_t i = 0;
178 for (i; i < value.size(); i++)
179 {
180 data[row][i] = value[i];
181 }
182 while (i < getNumberOfColumns())
183 {
184 data[row][i] = "";
185 i++;
186 }
187}
188
189template<class T>
190void EntityResultTableData<T>::setColumn(unsigned int column, std::vector<T>& value)
191{
192 if (column >= getNumberOfColumns())
193 {
194 throw std::runtime_error("Column index out of range.");
195 }
196 if (value.size() >= getNumberOfRows())
197 {
198 throw std::runtime_error("Vector has too many entries for the set number of rows.");
199 }
200
201 for (int i = 0; i < value.size(); i++)
202 {
203 data[i][column] = value[i];
204 }
205
206}
207
208template <class T>
209uint64_t EntityResultTableData<T>::getNumberOfRows(void)
210{
211 return (data.size());
212}
213
214template <class T>
215uint64_t EntityResultTableData<T>::getNumberOfColumns(void)
216{
217 return (header.size());
218}
219
220template <class T>
221std::string EntityResultTableData<T>::getHeaderText(unsigned int col)
222{
223 assert(col < getNumberOfColumns());
224 return header[col];
225}
226
227template <class T>
228T EntityResultTableData<T>::getValue(unsigned int row, unsigned int col)
229{
230 assert(row < getNumberOfRows());
231 assert(col < getNumberOfColumns());
232
233 return data[row][col];
234}
235
bsoncxx::types::value value
Definition DocumentManager.h:16
Template for getting a type name. Some type names are not system independent, hence the need to creat...
Definition BsonArrayTypeGetterWrapper.h:9
static std::string getTypeName()
Definition TemplateTypeName.hpp:9
UICORE_API_EXPORT QString text(UID _checkBoxUID)
Definition uiAPI.cpp:387
int ID
The ID datatype used for items.
Definition globalDataTypes.h:68
unsigned long UID
Unique identifier (32 bit unsigned integer)
Definition CoreTypes.h:27