-
Notifications
You must be signed in to change notification settings - Fork 4
/
griddata.cpp
107 lines (90 loc) · 2.76 KB
/
griddata.cpp
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
#include "griddata.h"
#include "controller.h"
#include "misc.h"
// GridCol definitions
int GridCol::getNumberRows(const bool include_header) const {
return values.size() + (include_header? 2 : 0);
}
SomeText GridCol::getElem(const int row, const bool include_header) const {
if (include_header) {
if (row == 0){
return SomeText(header);
}
else if (row == 1){
return SomeText(unit);
}
}
return SomeText(values[row - (include_header? 2 : 0)]);
}
void GridCol::pushBack(const std::string val){
values.push_back(val);
}
// GridData definitions
size_t GridData::getNumberRows(bool include_header) const {return columns[0].getNumberRows(include_header);}
size_t GridData::getNumberCols() const {return columns.size();}
unsigned char GridData::getFormat(const size_t col) const {
return columns[col].format;
}
bool GridData::hideCol(const int col) const{
return columns[col].hide_col;
}
void GridData::print() const {
constexpr int width = 20;
for (size_t row = 0; row < getNumberRows(true); ++row){
for (const GridCol& col : columns) {
if (col.hide_col){continue;}
SomeText cell = col.getElem(row,true);
cell.replaceNewlines();
if (cell.str.empty()) {
Ctrl::getInstance()->notifyUser(wfield(width, cell.wstr));
}
else{
Ctrl::getInstance()->notifyUser(field(width, cell.str));
}
}
Ctrl::getInstance()->notifyUser("\n");
}
}
std::string GridData::getHeader(const size_t col) const {
return columns[col].header;
}
std::vector<std::string> GridData::getHeaders() const {
std::vector<std::string> headers;
for (size_t i = 0; i < getNumberCols(); ++i){
headers.push_back(getHeader(i));
}
return headers;
}
std::string GridData::getSubheader(const size_t col) const {
return columns[col].subheader;
}
std::vector<std::string> GridData::getSubheaders() const {
std::vector<std::string> subheaders;
for (size_t i = 0; i < getNumberCols(); ++i){
subheaders.push_back(getSubheader(i));
}
return subheaders;
}
std::wstring GridData::getUnit(const size_t col) const {
return columns[col].unit;
}
std::string GridData::getValue(const size_t row, const size_t col) const {
return columns[col].getElem(row, false).str;
}
std::vector<std::string> GridData::getRow(const size_t row) const {
std::vector<std::string> row_values;
for (size_t i = 0; i < getNumberCols(); ++i){
row_values.push_back(getValue(row, i));
}
return row_values;
}
void GridData::storeValues(const std::vector<std::string> vals){
for (size_t col = 0; col < vals.size(); ++col){
columns[col].pushBack(vals[col]);
}
}
// SomeText definitions
void SomeText::replaceNewlines(){
std::replace( str.begin(), str.end(), '\n', ' ');
std::replace( wstr.begin(), wstr.end(), '\n', ' ');
}