Empirical
_TableCell.h
Go to the documentation of this file.
1 
12 #ifndef EMP_WEB_TABLE_CELL_H
13 #define EMP_WEB_TABLE_CELL_H
14 
15 namespace emp {
16 namespace web {
17 
19  class TableCell : public TableWidget {
20  public:
21  TableCell(size_t r, size_t c, const std::string & in_id="") : TableWidget(r,c,in_id) { ; }
22  TableCell(const TableWidget & in) : TableWidget(in) { ; }
23  TableCell(const Widget & in) : TableWidget(in) { ; }
24  TableCell(internal::TableInfo * in_info, size_t _row=0, size_t _col=0)
25  : TableWidget(in_info, _row, _col) { ; }
26 
28  void DoCSS(const std::string & setting, const std::string & value) override {
29  Info()->rows[cur_row].data[cur_col].extras.style.Set(setting, value);
30  if (IsActive()) Info()->ReplaceHTML(); // @CAO only should replace cell's CSS
31  }
32 
34  void DoAttr(const std::string & setting, const std::string & value) override {
35  Info()->rows[cur_row].data[cur_col].extras.attr.Set(setting, value);
36  if (IsActive()) Info()->ReplaceHTML(); // @CAO only should replace cell's CSS
37  }
38 
40  void DoListen(const std::string & event_name, size_t fun_id) override {
41  Info()->rows[cur_row].data[cur_col].extras.listen.Set(event_name, fun_id);
42  if (IsActive()) Info()->ReplaceHTML(); // @CAO only should replace cell's CSS
43  }
44 
45  TableCell & Clear() { Info()->ClearCell(cur_row, cur_col); return *this; }
46  TableCell & ClearStyle() { Info()->rows[cur_row].data[cur_col].extras.style.Clear(); return *this; }
47  TableCell & ClearAttr() { Info()->rows[cur_row].data[cur_col].extras.attr.Clear(); return *this; }
48  TableCell & ClearListen() { Info()->rows[cur_row].data[cur_col].extras.listen.Clear(); return *this; }
49  TableCell & ClearExtras() { Info()->rows[cur_row].data[cur_col].extras.Clear(); return *this; }
51  TableCell & ClearCells() { Info()->ClearCell(cur_row, cur_col); return *this; }
52 
54  std::string GetCSS(const std::string & setting) override {
55  return Info()->rows[cur_row].data[cur_col].extras.GetStyle(setting);
56  }
57 
58  TableCell & SetHeader(bool _h=true) {
59  Info()->rows[cur_row].data[cur_col].header = _h;
60  if (IsActive()) Info()->ReplaceHTML(); // @CAO only should replace cell's CSS
61  return *this;
62  }
63 
65  TableCell & SetRowSpan(size_t new_span) {
66  emp_assert((cur_row + new_span <= GetNumRows()) && "Row span too wide for table!");
67 
68  auto & datum = Info()->rows[cur_row].data[cur_col];
69  const size_t old_span = datum.rowspan;
70  const size_t col_span = datum.colspan;
71  datum.rowspan = new_span;
72 
73  // For each col, make sure NEW rows are masked!
74  for (size_t row = cur_row + old_span; row < cur_row + new_span; row++) {
75  for (size_t col = cur_col; col < cur_col + col_span; col++) {
76  Info()->rows[row].data[col].masked = true;
77  }
78  }
79 
80  // For each row, make sure former columns are unmasked!
81  for (size_t row = cur_row + new_span; row < cur_row + old_span; row++) {
82  for (size_t col = cur_col; col < cur_col + col_span; col++) {
83  Info()->rows[row].data[col].masked = false;
84  }
85  }
86 
87  // Redraw the entire table to fix row span information.
88  if (IsActive()) Info()->ReplaceHTML();
89 
90  return *this;
91  }
92 
94  TableCell & SetColSpan(size_t new_span) {
95  emp_assert((cur_col + new_span <= GetNumCols()) && "Col span too wide for table!",
96  cur_col, new_span, GetNumCols(), GetID());
97 
98  auto & datum = Info()->rows[cur_row].data[cur_col];
99  const size_t old_span = datum.colspan;
100  const size_t row_span = datum.rowspan;
101  datum.colspan = new_span;
102 
103  // For each row, make sure new columns are masked!
104  for (size_t row = cur_row; row < cur_row + row_span; row++) {
105  for (size_t col = cur_col + old_span; col < cur_col + new_span; col++) {
106  Info()->rows[row].data[col].masked = true;
107  }
108  }
109 
110  // For each row, make sure former columns are unmasked!
111  for (size_t row = cur_row; row < cur_row + row_span; row++) {
112  for (size_t col = cur_col + new_span; col < cur_col + old_span; col++) {
113  Info()->rows[row].data[col].masked = false;
114  }
115  }
116 
117  // Redraw the entire table to fix col span information.
118  if (IsActive()) Info()->ReplaceHTML();
119 
120  return *this;
121  }
122 
124  TableCell & SetSpan(size_t row_span, size_t col_span) {
125  // @CAO Can do this more efficiently, but probably not worth it.
126  SetRowSpan(row_span);
127  SetColSpan(col_span);
128  return *this;
129  }
130 
131  };
132 }
133 }
134 
135 #endif
bool IsActive() const
Test if the activity state of this widget is currently ACTIVE.
Definition: Widget.h:395
void DoAttr(const std::string &setting, const std::string &value) override
Udpate the attributes for this cell (override default Table)
Definition: _TableCell.h:34
TableCell & ClearListen()
Definition: _TableCell.h:48
size_t cur_col
Definition: Table.h:517
TableCell & SetSpan(size_t row_span, size_t col_span)
Update both row and column span for this cell.
Definition: _TableCell.h:124
TableCell & SetColSpan(size_t new_span)
Adjust the column span of the current cell.
Definition: _TableCell.h:94
An object that focuses on a single cell in a specified table.
Definition: _TableCell.h:19
Definition: Table.h:513
TableCell & SetRowSpan(size_t new_span)
Adjust the row span of the current cell.
Definition: _TableCell.h:65
const std::string & GetID() const
What is the HTML string ID for this Widget?
Definition: Widget.h:401
TableCell & ClearChildren()
Definition: _TableCell.h:50
emp::vector< TableRowInfo > rows
Definition: Table.h:113
void DoListen(const std::string &event_name, size_t fun_id) override
Update a listener for this cell (override default Table)
Definition: _TableCell.h:40
TableCell & ClearExtras()
Definition: _TableCell.h:49
TableCell & ClearAttr()
Definition: _TableCell.h:47
TableCell & SetHeader(bool _h=true)
Definition: _TableCell.h:58
TableCell & ClearStyle()
Definition: _TableCell.h:46
TableCell(size_t r, size_t c, const std::string &in_id="")
Definition: _TableCell.h:21
TableCell(const Widget &in)
Definition: _TableCell.h:23
size_t GetNumCols() const
Definition: Table.h:561
std::string GetCSS(const std::string &setting) override
Get the current CSS value for the specified setting of this Cell.
Definition: _TableCell.h:54
TableCell(internal::TableInfo *in_info, size_t _row=0, size_t _col=0)
Definition: _TableCell.h:24
internal::TableInfo * Info()
Get a properly cast version of info.
Definition: Table.h:522
If we are in emscripten, make sure to include the header.
Definition: array.h:37
Widget is effectively a smart pointer to a WidgetInfo object, plus some basic accessors.
Definition: Widget.h:78
#define emp_assert(...)
Definition: assert.h:199
Definition: Table.h:107
size_t cur_row
Definition: Table.h:516
void DoCSS(const std::string &setting, const std::string &value) override
Update the CSS for this cell. (override default Table)
Definition: _TableCell.h:28
void ClearCell(size_t row_id, size_t col_id)
Definition: Table.h:352
void ClearCellChildren(size_t row_id, size_t col_id)
Definition: Table.h:322
void ReplaceHTML() override
Definition: Table.h:462
size_t GetNumRows() const
Definition: Table.h:562
TableCell & Clear()
Definition: _TableCell.h:45
TableCell(const TableWidget &in)
Definition: _TableCell.h:22
TableCell & ClearCells()
Definition: _TableCell.h:51