TinyMaterialDesign
Loading...
Searching...
No Matches
TableLayout.h
Go to the documentation of this file.
1#pragma once
2#include "TinyMaterialDesign/Core/Bounds.h"
3
4namespace tinymd {
5
6/**
7 * @brief Grid with explicit, independently-sized column widths and row heights.
8 *
9 * Not a widget - see GridLayout.h for the rationale. GridLayout assumes
10 * uniform cells and computes the column count for you; TableLayout is for
11 * dashboards mixing wide/narrow columns or short/tall rows, where you supply
12 * the sizes explicitly:
13 *
14 * int32_t colW[] = {120, 80, 80};
15 * int32_t rowH[] = {48, 96};
16 * TableLayout table(Bounds(16, 56, 280, 200), colW, 3, rowH, 2);
17 * cell.bounds = table.cellRect(row, col);
18 */
20 public:
21 TableLayout(Bounds container, const int32_t* columnWidths, int columnCount, const int32_t* rowHeights,
22 int rowCount, int32_t spacing = 8)
23 : container_(container),
24 columnWidths_(columnWidths),
25 columnCount_(columnCount),
26 rowHeights_(rowHeights),
27 rowCount_(rowCount),
28 spacing_(spacing) {}
29
30 /// Bounds for the cell at (`row`, `col`), 0-based.
31 Bounds cellRect(int row, int col) const {
32 int32_t x = container_.x;
33 for (int c = 0; c < col && c < columnCount_; ++c) x += columnWidths_[c] + spacing_;
34 int32_t y = container_.y;
35 for (int r = 0; r < row && r < rowCount_; ++r) y += rowHeights_[r] + spacing_;
36
37 const int32_t w = col < columnCount_ ? columnWidths_[col] : 0;
38 const int32_t h = row < rowCount_ ? rowHeights_[row] : 0;
39 return Bounds(x, y, w, h);
40 }
41
42 int columns() const { return columnCount_; }
43 int rows() const { return rowCount_; }
44
45 /// Total width needed for all columns.
46 int32_t totalWidth() const {
47 int32_t total = 0;
48 for (int c = 0; c < columnCount_; ++c) total += columnWidths_[c] + (c > 0 ? spacing_ : 0);
49 return total;
50 }
51
52 /// Total height needed for all rows.
53 int32_t totalHeight() const {
54 int32_t total = 0;
55 for (int r = 0; r < rowCount_; ++r) total += rowHeights_[r] + (r > 0 ? spacing_ : 0);
56 return total;
57 }
58
59 private:
60 Bounds container_;
61 const int32_t* columnWidths_;
62 int columnCount_;
63 const int32_t* rowHeights_;
64 int rowCount_;
65 int32_t spacing_;
66};
67
68} // namespace tinymd
Grid with explicit, independently-sized column widths and row heights.
Definition: TableLayout.h:19
int32_t totalWidth() const
Total width needed for all columns.
Definition: TableLayout.h:46
int columns() const
Definition: TableLayout.h:42
TableLayout(Bounds container, const int32_t *columnWidths, int columnCount, const int32_t *rowHeights, int rowCount, int32_t spacing=8)
Definition: TableLayout.h:21
Bounds cellRect(int row, int col) const
Bounds for the cell at (row, col), 0-based.
Definition: TableLayout.h:31
int32_t totalHeight() const
Total height needed for all rows.
Definition: TableLayout.h:53
int rows() const
Definition: TableLayout.h:43
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22