Table columns can now have a different width

This commit is contained in:
Bertrand Songis
2020-03-03 10:33:51 +01:00
parent 7c3cbddbb3
commit 5f8295eb40
2 changed files with 36 additions and 23 deletions
+6 -6
View File
@@ -25,9 +25,9 @@ void Table::Header::paint(BitmapBuffer * dc)
coord_t x = 10; coord_t x = 10;
if (!header.values.empty()) { if (!header.values.empty()) {
dc->clear(TABLE_HEADER_BGCOLOR); dc->clear(TABLE_HEADER_BGCOLOR);
for (auto &value: header.values) { for (int i = 0; i < header.values.size(); i++) {
dc->drawText(x, (TABLE_LINE_HEIGHT - getFontHeight(TABLE_HEADER_FONT)) / 2, value.c_str(), TABLE_HEADER_FONT); dc->drawText(x, (TABLE_LINE_HEIGHT - getFontHeight(TABLE_HEADER_FONT)) / 2, header.values[i].c_str(), TABLE_HEADER_FONT);
x += 200; x += columnsWidth[i];
} }
} }
} }
@@ -40,9 +40,9 @@ void Table::Body::paint(BitmapBuffer * dc)
for (auto & line: lines) { for (auto & line: lines) {
dc->drawSolidFilledRect(0, y, width(), TABLE_LINE_HEIGHT - 2, index == selection ? FOCUS_BGCOLOR : TABLE_BGCOLOR); dc->drawSolidFilledRect(0, y, width(), TABLE_LINE_HEIGHT - 2, index == selection ? FOCUS_BGCOLOR : TABLE_BGCOLOR);
x = 10; x = 10;
for (auto &value: line.values) { for (int i = 0; i < line.values.size(); i++) {
dc->drawText(x, y + (TABLE_LINE_HEIGHT - getFontHeight(TABLE_BODY_FONT)) / 2 + 3, value.c_str(), TABLE_BODY_FONT | (index == selection ? FOCUS_COLOR : DEFAULT_COLOR)); dc->drawText(x, y + (TABLE_LINE_HEIGHT - getFontHeight(TABLE_BODY_FONT)) / 2 + 3, line.values[i].c_str(), TABLE_BODY_FONT | (index == selection ? FOCUS_COLOR : DEFAULT_COLOR));
x += 200; x += columnsWidth[i];
} }
y += TABLE_LINE_HEIGHT; y += TABLE_LINE_HEIGHT;
index += 1; index += 1;
+30 -17
View File
@@ -27,14 +27,20 @@
class Table: public Window { class Table: public Window {
class Line { class Line {
public: public:
std::list<std::string> values; explicit Line(uint8_t columnsCount):
values(columnsCount)
{
}
std::vector<std::string> values;
std::function<void()> onPress; std::function<void()> onPress;
}; };
class Header: public Window { class Header: public Window {
public: public:
Header(Window * parent, const rect_t & rect): Header(Table * parent, const rect_t & rect, uint8_t columnsCount):
Window(parent, rect, OPAQUE) Window(parent, rect, OPAQUE),
header(columnsCount),
columnsWidth(parent->columnsWidth)
{ {
} }
@@ -47,14 +53,16 @@ class Table: public Window {
protected: protected:
Line header; Line header;
std::vector<coord_t> & columnsWidth;
}; };
class Body: public Window { class Body: public Window {
friend class Table; friend class Table;
public: public:
Body(Window * parent, const rect_t & rect): Body(Table * parent, const rect_t & rect):
Window(parent, rect, OPAQUE) Window(parent, rect, OPAQUE),
columnsWidth(parent->columnsWidth)
{ {
} }
@@ -85,6 +93,7 @@ class Table: public Window {
#endif #endif
protected: protected:
std::vector<coord_t> & columnsWidth;
std::vector<Line> lines; std::vector<Line> lines;
int selection = -1; int selection = -1;
}; };
@@ -92,10 +101,11 @@ class Table: public Window {
public: public:
Table(Window * parent, const rect_t & rect, uint8_t columnsCount): Table(Window * parent, const rect_t & rect, uint8_t columnsCount):
Window(parent, rect), Window(parent, rect),
header(this, {0, 0, width(), 0}), header(this, {0, 0, width(), 0}, columnsCount),
body(this, {0, 0, width(), height()}) body(this, {0, 0, width(), height()}),
columnsCount(columnsCount),
columnsWidth(columnsCount, width() / columnsCount)
{ {
setColumnsCount(columnsCount);
} }
~Table() override ~Table() override
@@ -104,9 +114,11 @@ class Table: public Window {
body.detach(); body.detach();
} }
void setColumnsCount(uint8_t value) void setColumnsWidth(const coord_t width[])
{ {
count = value; for (uint8_t i = 0; i < columnsCount; i++) {
columnsWidth[i] = width[i];
}
} }
int getSelection() const int getSelection() const
@@ -134,18 +146,18 @@ class Table: public Window {
header.setHeight(TABLE_HEADER_HEIGHT); header.setHeight(TABLE_HEADER_HEIGHT);
body.setTop(TABLE_HEADER_HEIGHT); body.setTop(TABLE_HEADER_HEIGHT);
body.setHeight(height() - TABLE_HEADER_HEIGHT); body.setHeight(height() - TABLE_HEADER_HEIGHT);
Line line; Line line(columnsCount);
for (uint8_t i = 0; i < count; i++) { for (uint8_t i = 0; i < columnsCount; i++) {
line.values.push_back(values[i]); line.values[i] = values[i];
} }
header.setLine(line); header.setLine(line);
} }
void addLine(const char * const values[], std::function<void()> onPress = nullptr) void addLine(const char * const values[], std::function<void()> onPress = nullptr)
{ {
Line line; Line line(columnsCount);
for (uint8_t i = 0; i < count; i++) { for (uint8_t i = 0; i < columnsCount; i++) {
line.values.push_back(values[i]); line.values[i] = values[i];
} }
line.onPress = onPress; line.onPress = onPress;
body.addLine(line); body.addLine(line);
@@ -158,7 +170,8 @@ class Table: public Window {
} }
protected: protected:
uint8_t count = 1; uint8_t columnsCount;
std::vector<coord_t> columnsWidth;
Header header; Header header;
Body body; Body body;
}; };