table updates

This commit is contained in:
Gabor Kiss-Vamosi
2018-11-29 08:52:21 +01:00
parent c1140ec6a7
commit e7058e9135
2 changed files with 207 additions and 31 deletions

View File

@@ -27,6 +27,7 @@
static bool lv_table_design(lv_obj_t * table, const lv_area_t * mask, lv_design_mode_t mode); static bool lv_table_design(lv_obj_t * table, const lv_area_t * mask, lv_design_mode_t mode);
static lv_res_t lv_table_signal(lv_obj_t * table, lv_signal_t sign, void * param); static lv_res_t lv_table_signal(lv_obj_t * table, lv_signal_t sign, void * param);
static lv_coord_t get_row_height(lv_obj_t * table, uint16_t row_id); static lv_coord_t get_row_height(lv_obj_t * table, uint16_t row_id);
static void refr_size(lv_obj_t * table);
/********************** /**********************
* STATIC VARIABLES * STATIC VARIABLES
@@ -34,11 +35,6 @@ static lv_coord_t get_row_height(lv_obj_t * table, uint16_t row_id);
static lv_signal_func_t ancestor_signal; static lv_signal_func_t ancestor_signal;
static lv_design_func_t ancestor_scrl_design; static lv_design_func_t ancestor_scrl_design;
static const char * cell_data_example[] = {"r1", "rc11", "rc12",
"ro row 2", "rc21", "rc22",
"row3", "rc31", "rc32",
"row4", "rc41", "rc42"};
/********************** /**********************
* MACROS * MACROS
**********************/ **********************/
@@ -70,10 +66,10 @@ lv_obj_t * lv_table_create(lv_obj_t * par, const lv_obj_t * copy)
if(ancestor_scrl_design == NULL) ancestor_scrl_design = lv_obj_get_design_func(new_table); if(ancestor_scrl_design == NULL) ancestor_scrl_design = lv_obj_get_design_func(new_table);
/*Initialize the allocated 'ext' */ /*Initialize the allocated 'ext' */
ext->cell_data = cell_data_example; ext->cell_data = NULL;
ext->cell_style = &lv_style_pretty; ext->cell_style = &lv_style_pretty;
ext->col_cnt = 3; ext->col_cnt = 0;
ext->row_cnt = 4; ext->row_cnt = 0;
ext->col_w[0] = 50; ext->col_w[0] = 50;
ext->col_w[1] = 70; ext->col_w[1] = 70;
ext->col_w[2] = 80; ext->col_w[2] = 80;
@@ -112,9 +108,92 @@ lv_obj_t * lv_table_create(lv_obj_t * par, const lv_obj_t * copy)
* Setter functions * Setter functions
*====================*/ *====================*/
/* void lv_table_set_cell_value(lv_obj_t * table, uint16_t row, uint16_t col, const char * txt)
* New object specific "set" functions come here {
*/ lv_table_ext_t * ext = lv_obj_get_ext_attr(table);
if(row >= ext->row_cnt || col >= ext->col_cnt) {
LV_LOG_WARN("lv_table_set_cell_value: invalid row or column");
return;
}
uint32_t cell = row * ext->col_cnt + col;
ext->cell_data[cell] = lv_mem_realloc(ext->cell_data[cell], strlen(txt) + 2); /*+1: trailing '\0; +1: format byte*/
strcpy(ext->cell_data[cell] + 1, txt); /*Leave the format byte*/
refr_size(table);
}
void lv_table_set_cell_format(lv_obj_t * table, uint16_t row, uint16_t col, lv_table_cell_align_t align)
{
lv_table_ext_t * ext = lv_obj_get_ext_attr(table);
if(row >= ext->row_cnt || col >= ext->col_cnt) {
LV_LOG_WARN("lv_table_set_cell_format: invalid row or column");
return;
}
uint32_t cell = row * ext->col_cnt + col;
if(ext->cell_data[cell] == NULL) {
ext->cell_data[cell] = lv_mem_alloc(2); /*+1: trailing '\0; +1: format byte*/
ext->cell_data[1] = '\0';
}
lv_table_cell_format_t format;
format.format_byte = ext->cell_data[cell][0];
format.align = align;
ext->cell_data[cell][0] = format.format_byte;
}
void lv_table_set_cell_merge_right(lv_obj_t * table, uint16_t row, uint16_t col, bool en)
{
lv_table_ext_t * ext = lv_obj_get_ext_attr(table);
if(row >= ext->row_cnt || col >= ext->col_cnt) {
LV_LOG_WARN("lv_table_set_cell_merge_right: invalid row or column");
return;
}
uint32_t cell = row * ext->col_cnt + col;
if(ext->cell_data[cell] == NULL) {
ext->cell_data[cell] = lv_mem_alloc(2); /*+1: trailing '\0; +1: format byte*/
ext->cell_data[1] = '\0';
}
lv_table_cell_format_t format;
format.format_byte = ext->cell_data[cell][0];
format.right_merge = en ? 1 : 0;
ext->cell_data[cell][0] = format.format_byte;
}
void lv_table_set_row_cnt(lv_obj_t * table, uint16_t row_cnt)
{
lv_table_ext_t * ext = lv_obj_get_ext_attr(table);
ext->row_cnt = row_cnt;
if(ext->row_cnt > 0 && ext->col_cnt > 0) {
ext->cell_data = lv_mem_realloc(ext->cell_data, ext->row_cnt * ext->col_cnt * sizeof(char*));
}
else {
lv_mem_free(ext->cell_data);
ext->cell_data = NULL;
}
refr_size(table);
}
void lv_table_set_col_cnt(lv_obj_t * table, uint16_t col_cnt)
{
lv_table_ext_t * ext = lv_obj_get_ext_attr(table);
ext->col_cnt = col_cnt;
if(ext->row_cnt > 0 && ext->col_cnt > 0) {
ext->cell_data = lv_mem_realloc(ext->cell_data, ext->row_cnt * ext->col_cnt * sizeof(char*));
}
else {
lv_mem_free(ext->cell_data);
ext->cell_data = NULL;
}
refr_size(table);
}
/** /**
@@ -199,43 +278,76 @@ static bool lv_table_design(lv_obj_t * table, const lv_area_t * mask, lv_design_
ancestor_scrl_design(table, mask, mode); ancestor_scrl_design(table, mask, mode);
lv_table_ext_t * ext = lv_obj_get_ext_attr(table); lv_table_ext_t * ext = lv_obj_get_ext_attr(table);
lv_style_t * bg_style = lv_obj_get_style(table);
lv_coord_t h_row; lv_coord_t h_row;
lv_point_t txt_size; lv_point_t txt_size;
lv_area_t cell_area; lv_area_t cell_area;
lv_area_t txt_area; lv_area_t txt_area;
lv_txt_flag_t txt_flags;
uint16_t col; uint16_t col;
uint16_t row; uint16_t row;
uint16_t cell = 0; uint16_t cell = 0;
cell_area.y2 = table->coords.y1; cell_area.y2 = table->coords.y1 + bg_style->body.padding.ver;
for(row = 0; row < ext->row_cnt; row++) { for(row = 0; row < ext->row_cnt; row++) {
h_row = get_row_height(table, row); h_row = get_row_height(table, row);
cell_area.y1 = cell_area.y2; cell_area.y1 = cell_area.y2;
cell_area.y2 = cell_area.y1 + h_row; cell_area.y2 = cell_area.y1 + h_row;
cell_area.x2 = table->coords.x1 + bg_style->body.padding.hor;
uint16_t col_x = 0;
for(col = 0; col < ext->col_cnt; col++) { for(col = 0; col < ext->col_cnt; col++) {
cell_area.x1 = table->coords.x1 + col_x; cell_area.x1 = cell_area.x2;
cell_area.x2 = cell_area.x1 + ext->col_w[col]; cell_area.x2 = cell_area.x1 + ext->col_w[col];
txt_area.x1 = cell_area.x1 + ext->cell_style->body.padding.hor; uint16_t col_merge = 0;
txt_area.x2 = cell_area.x2 - ext->cell_style->body.padding.hor; for(col_merge = 0; col_merge + col < ext->col_cnt - 1; col_merge ++) {
txt_area.y1 = cell_area.y1 + ext->cell_style->body.padding.ver;
txt_area.y2 = cell_area.y2 - ext->cell_style->body.padding.ver;
lv_txt_get_size(&txt_size, ext->cell_data[cell], ext->cell_style->text.font, if(ext->cell_data[cell + col_merge] != NULL) {
ext->cell_style->text.letter_space, ext->cell_style->text.line_space, lv_area_get_width(&txt_area), LV_TXT_FLAG_NONE); lv_table_cell_format_t format;
format.format_byte = ext->cell_data[cell + col_merge][0];
if(format.right_merge) cell_area.x2 += ext->col_w[col + col_merge + 1];
else break;
} else {
break;
}
}
col_x += ext->col_w[col];
lv_draw_rect(&cell_area, mask, ext->cell_style, LV_OPA_COVER); lv_draw_rect(&cell_area, mask, ext->cell_style, LV_OPA_COVER);
lv_draw_label(&txt_area, mask, ext->cell_style, LV_OPA_COVER, ext->cell_data[cell], LV_TXT_FLAG_NONE, NULL);
cell++; if(ext->cell_data[cell]) {
txt_area.x1 = cell_area.x1 + ext->cell_style->body.padding.hor;
txt_area.x2 = cell_area.x2 - ext->cell_style->body.padding.hor;
txt_area.y1 = cell_area.y1 + ext->cell_style->body.padding.ver;
txt_area.y2 = cell_area.y2 - ext->cell_style->body.padding.ver;
lv_table_cell_format_t format;
format.format_byte = ext->cell_data[cell][0];
switch(format.align) {
case LV_TABLE_CELL_ALIGN_LEFT:
txt_flags = LV_TXT_FLAG_NONE;
break;
case LV_TABLE_CELL_ALIGN_RIGHT:
txt_flags = LV_TXT_FLAG_RIGHT;
break;
case LV_TABLE_CELL_ALIGN_CENTER:
txt_flags = LV_TXT_FLAG_CENTER;
break;
}
lv_txt_get_size(&txt_size, ext->cell_data[cell] + 1, ext->cell_style->text.font,
ext->cell_style->text.letter_space, ext->cell_style->text.line_space, lv_area_get_width(&txt_area), txt_flags);
lv_draw_label(&txt_area, mask, ext->cell_style, LV_OPA_COVER, ext->cell_data[cell] + 1, txt_flags, NULL);
}
cell += col_merge + 1;
col += col_merge;
} }
} }
@@ -280,6 +392,30 @@ static lv_res_t lv_table_signal(lv_obj_t * table, lv_signal_t sign, void * param
return res; return res;
} }
static void refr_size(lv_obj_t * table)
{
lv_coord_t h = 0;
lv_coord_t w = 0;
lv_table_ext_t * ext = lv_obj_get_ext_attr(table);
uint16_t i;
for(i= 0; i < ext->col_cnt; i++) {
w += ext->col_w[i];
}
for(i= 0; i < ext->row_cnt; i++) {
h += get_row_height(table, i);
}
lv_style_t * bg_style = lv_obj_get_style(table);
w += bg_style->body.padding.hor * 2;
h += bg_style->body.padding.ver * 2;
lv_obj_set_size(table, w, h);
lv_obj_invalidate(table);
}
static lv_coord_t get_row_height(lv_obj_t * table, uint16_t row_id) static lv_coord_t get_row_height(lv_obj_t * table, uint16_t row_id)
{ {
lv_table_ext_t * ext = lv_obj_get_ext_attr(table); lv_table_ext_t * ext = lv_obj_get_ext_attr(table);
@@ -289,18 +425,33 @@ static lv_coord_t get_row_height(lv_obj_t * table, uint16_t row_id)
uint16_t row_start = row_id * ext->col_cnt; uint16_t row_start = row_id * ext->col_cnt;
uint16_t cell; uint16_t cell;
uint16_t col; uint16_t col;
lv_coord_t h_max = 0; lv_coord_t h_max = lv_font_get_height(ext->cell_style->text.font);
for(cell = row_start, col = 0; cell < row_start + ext->col_cnt; cell++, col ++) { for(cell = row_start, col = 0; cell < row_start + ext->col_cnt; cell++, col ++) {
txt_w = ext->col_w[col] - 2 * ext->cell_style->body.padding.hor; if(ext->cell_data[cell] != NULL) {
lv_txt_get_size(&txt_size, ext->cell_data[cell], ext->cell_style->text.font,
ext->cell_style->text.letter_space, ext->cell_style->text.line_space, txt_w, LV_TXT_FLAG_NONE);
h_max = LV_MATH_MAX(txt_size.y, h_max); txt_w = ext->col_w[col];
uint16_t col_merge = 0;
for(col_merge = 0; col_merge + col < ext->col_cnt - 1; col_merge ++) {
if(ext->cell_data[cell + col_merge] != NULL) {
lv_table_cell_format_t format;
format.format_byte = ext->cell_data[cell + col_merge][0];
if(format.right_merge) txt_w += ext->col_w[col + col_merge + 1];
else break;
} else {
break;
}
}
txt_w -= 2 * ext->cell_style->body.padding.hor;
lv_txt_get_size(&txt_size, ext->cell_data[cell] + 1, ext->cell_style->text.font,
ext->cell_style->text.letter_space, ext->cell_style->text.line_space, txt_w, LV_TXT_FLAG_NONE);
h_max = LV_MATH_MAX(txt_size.y, h_max);
}
} }
printf("row:%d, h:%d\n", row_id, h_max);
return h_max + 2 * ext->cell_style->body.padding.ver; return h_max + 2 * ext->cell_style->body.padding.ver;
} }

View File

@@ -31,12 +31,27 @@ extern "C" {
/********************** /**********************
* TYPEDEFS * TYPEDEFS
**********************/ **********************/
typedef enum {
LV_TABLE_CELL_ALIGN_LEFT,
LV_TABLE_CELL_ALIGN_RIGHT,
LV_TABLE_CELL_ALIGN_CENTER,
}lv_table_cell_align_t;
typedef union {
struct {
uint8_t align:2;
uint8_t right_merge:1;
};
uint8_t format_byte;
}lv_table_cell_format_t;
/*Data of table*/ /*Data of table*/
typedef struct { typedef struct {
/*New data for this type */ /*New data for this type */
uint16_t col_cnt; uint16_t col_cnt;
uint16_t row_cnt; uint16_t row_cnt;
const char ** cell_data; char ** cell_data;
lv_style_t * cell_style; lv_style_t * cell_style;
lv_coord_t col_w[LV_TABLE_COL_MAX]; lv_coord_t col_w[LV_TABLE_COL_MAX];
} lv_table_ext_t; } lv_table_ext_t;
@@ -62,6 +77,16 @@ typedef uint8_t lv_table_style_t;
*/ */
lv_obj_t * lv_table_create(lv_obj_t * par, const lv_obj_t * copy); lv_obj_t * lv_table_create(lv_obj_t * par, const lv_obj_t * copy);
void lv_table_set_cell_value(lv_obj_t * table, uint16_t row, uint16_t col, const char * txt);
void lv_table_set_cell_format(lv_obj_t * table, uint16_t row, uint16_t col, lv_table_cell_align_t align);
void lv_table_set_cell_merge_right(lv_obj_t * table, uint16_t row, uint16_t col, bool en);
void lv_table_set_row_cnt(lv_obj_t * table, uint16_t row_cnt);
void lv_table_set_col_cnt(lv_obj_t * table, uint16_t col_cnt);
/*====================== /*======================
* Add/remove functions * Add/remove functions
*=====================*/ *=====================*/