2019-03-03 13:59:34 +01:00
|
|
|
from TextDesign import TextDesign
|
2019-03-05 22:10:33 +01:00
|
|
|
from TextWraper import wrap_text_with_font
|
2019-04-05 11:37:51 +02:00
|
|
|
from Assets import defaultfontsize, colors
|
2019-05-06 11:05:10 +02:00
|
|
|
from DesignEntity import DesignEntity
|
2019-03-26 22:30:37 +01:00
|
|
|
|
2019-03-03 13:59:34 +01:00
|
|
|
|
2019-03-10 19:10:05 +01:00
|
|
|
default_props = {
|
2019-04-05 11:37:51 +02:00
|
|
|
"color" : colors["fg"],
|
|
|
|
"background_color" : colors["bg"]
|
2019-03-10 19:10:05 +01:00
|
|
|
}
|
|
|
|
|
2019-05-01 07:17:40 +02:00
|
|
|
class TableDesign (TextDesign):
|
|
|
|
"""Gets a matrix with text or designs that is than
|
2019-03-03 13:59:34 +01:00
|
|
|
displayed in a table without borders."""
|
2019-05-06 11:05:10 +02:00
|
|
|
def __init__ (self, size, matrix, max_col_size = None, max_row_size = None, font = None, fontsize = defaultfontsize, column_horizontal_alignments = [], mask = True, line_spacing = 0, col_spacing = 0, truncate_rows = True, truncate_cols = True, wrap = False, truncate_text=True, truncate_suffix="...", cell_properties=None, background_color = colors["bg"]):
|
2019-05-01 07:17:40 +02:00
|
|
|
super(TableDesign, self).__init__(size, font=font, fontsize=fontsize, mask=mask)
|
2019-03-24 21:09:10 +01:00
|
|
|
self.__init_image__(background_color)
|
2019-05-06 11:05:10 +02:00
|
|
|
self.matrix = matrix
|
2019-03-03 13:59:34 +01:00
|
|
|
self.max_col_size = max_col_size
|
2019-03-05 22:10:33 +01:00
|
|
|
self.max_row_size = max_row_size
|
2019-03-03 14:53:54 +01:00
|
|
|
self.line_spacing = line_spacing
|
|
|
|
self.col_spacing = col_spacing
|
2019-03-03 16:44:11 +01:00
|
|
|
self.truncate_rows = truncate_rows
|
|
|
|
self.truncate_cols = truncate_cols
|
|
|
|
self.max_row = None
|
|
|
|
self.max_col = None
|
2019-03-03 18:13:25 +01:00
|
|
|
self.column_horizontal_alignments = column_horizontal_alignments
|
2019-03-05 22:10:33 +01:00
|
|
|
self.wrap = wrap
|
2019-03-10 18:39:17 +01:00
|
|
|
self.truncate_text = truncate_text
|
|
|
|
self.truncate_suffix = truncate_suffix
|
2019-03-10 19:10:05 +01:00
|
|
|
self.cell_properties = cell_properties
|
2019-03-03 13:59:34 +01:00
|
|
|
|
2019-03-03 16:44:11 +01:00
|
|
|
def __finish_image__ (self):
|
2019-03-09 22:03:00 +01:00
|
|
|
if len(self.matrix) is 0:
|
|
|
|
return
|
2019-03-03 13:59:34 +01:00
|
|
|
self.__reform_col_size__()
|
2019-03-05 22:10:33 +01:00
|
|
|
self.__reform_row_size__()
|
2019-03-03 13:59:34 +01:00
|
|
|
self.cell_sizes = self.__get_cell_sizes__()
|
2019-03-03 16:44:11 +01:00
|
|
|
self.max_col, self.max_row = self.__get_truncated_counts__()
|
2019-03-03 13:59:34 +01:00
|
|
|
self.__print_table__(self.matrix)
|
|
|
|
|
2019-03-03 16:44:11 +01:00
|
|
|
def __reform_col_size__ (self):
|
2019-03-03 13:59:34 +01:00
|
|
|
if self.max_col_size is not None:
|
|
|
|
return
|
|
|
|
|
|
|
|
col_sizes = []
|
|
|
|
for c in range(len(self.matrix[0])): #amout of columns
|
|
|
|
for r in range(len(self.matrix)):
|
2019-05-06 11:05:10 +02:00
|
|
|
row_col_size = self.__get_cell_size__(r,c)[0]
|
2019-03-03 13:59:34 +01:00
|
|
|
if len(col_sizes) - 1 < c:
|
|
|
|
col_sizes.append(row_col_size)
|
|
|
|
elif row_col_size > col_sizes[c]:
|
|
|
|
col_sizes[c] = row_col_size
|
2019-03-05 22:10:33 +01:00
|
|
|
|
|
|
|
for index, size in enumerate(col_sizes):
|
2019-04-07 11:16:40 +02:00
|
|
|
preceding_size = sum(col_sizes[:index]) + index * self.col_spacing
|
2019-03-05 22:10:33 +01:00
|
|
|
if preceding_size + size > self.size[0]:
|
|
|
|
col_sizes[index] = self.size[0] - preceding_size
|
|
|
|
break
|
|
|
|
|
|
|
|
self.max_col_size = col_sizes
|
|
|
|
|
|
|
|
def __reform_row_size__ (self):
|
|
|
|
if self.max_row_size is not None:
|
|
|
|
return
|
|
|
|
|
|
|
|
row_sizes = []
|
|
|
|
for r in range(len(self.matrix)):
|
|
|
|
for c in range(len(self.matrix[0])): #amout of columns
|
2019-05-06 11:05:10 +02:00
|
|
|
col_row_size = self.__get_cell_size__(r,c)[1]
|
2019-03-05 22:10:33 +01:00
|
|
|
if len(row_sizes) - 1 < r:
|
|
|
|
row_sizes.append(col_row_size)
|
|
|
|
elif col_row_size > row_sizes[r]:
|
|
|
|
row_sizes[r] = col_row_size
|
2019-03-03 18:22:41 +01:00
|
|
|
|
2019-03-05 22:10:33 +01:00
|
|
|
self.max_row_size = row_sizes
|
2019-03-03 13:59:34 +01:00
|
|
|
|
2019-05-06 11:05:10 +02:00
|
|
|
def __get_cell_size__(self, r, c):
|
|
|
|
content = self.matrix[r][c]
|
|
|
|
size = (0, 0)
|
|
|
|
|
|
|
|
if content == None:
|
|
|
|
return size
|
|
|
|
elif type(content) == str:
|
|
|
|
font = self.__get_font__()
|
|
|
|
width = font.getsize(self.matrix[r][c])[0] #get width of text in that row/col
|
|
|
|
if self.wrap and self.max_col_size != None:
|
|
|
|
content = wrap_text_with_font(content, self.max_col_size[c], font)
|
|
|
|
height = font.getsize_multiline(content)[1] #get height of text in that col/row
|
|
|
|
size = (width, height)
|
|
|
|
else: #DesignEntity
|
|
|
|
size = content.size
|
|
|
|
|
|
|
|
return size
|
|
|
|
|
2019-03-03 16:44:11 +01:00
|
|
|
def __get_truncated_counts__ (self):
|
|
|
|
max_col = 0
|
|
|
|
if self.truncate_cols:
|
2019-03-09 19:51:31 +01:00
|
|
|
while max_col < len(self.matrix[0]) and self.__get_cell_pos__(0, max_col + 1)[0] - self.col_spacing <= self.size[0]:
|
2019-03-03 16:44:11 +01:00
|
|
|
max_col += 1
|
|
|
|
else:
|
|
|
|
max_col = len(self.matrix[0])
|
|
|
|
|
|
|
|
max_row = 0
|
|
|
|
if self.truncate_rows:
|
2019-03-09 19:51:31 +01:00
|
|
|
while max_row < len(self.matrix) and self.__get_cell_pos__(max_row + 1,0)[1] - self.line_spacing <= self.size[1]:
|
2019-03-03 16:44:11 +01:00
|
|
|
max_row += 1
|
|
|
|
else:
|
|
|
|
max_row = len(self.matrix)
|
|
|
|
|
|
|
|
return (max_col, max_row)
|
|
|
|
|
|
|
|
def __print_table__ (self, matrix):
|
|
|
|
for r in range(self.max_row):
|
|
|
|
for c in range(self.max_col):
|
2019-05-06 11:05:10 +02:00
|
|
|
self.__draw_cell__(r,c)
|
2019-03-03 13:59:34 +01:00
|
|
|
|
2019-03-03 18:13:25 +01:00
|
|
|
def __draw_text__ (self, pos, size, row, col):
|
2019-03-10 19:10:05 +01:00
|
|
|
color = self.__get_cell_prop__(row, col, "color")
|
|
|
|
bg_color = self.__get_cell_prop__(row, col, "background_color")
|
|
|
|
|
|
|
|
design = TextDesign(size, text=self.matrix[row][col], font=self.font_family, color=color, background_color=bg_color, fontsize=self.font_size, horizontalalignment=self.__get_col_hori_alignment__(col), wrap=self.wrap, truncate=self.truncate_text, truncate_suffix=self.truncate_suffix)
|
2019-03-03 13:59:34 +01:00
|
|
|
design.pos = pos
|
|
|
|
self.draw_design(design)
|
2019-05-06 11:05:10 +02:00
|
|
|
|
|
|
|
def __draw_design__ (self, pos, size, row, col):
|
|
|
|
bg_color = self.__get_cell_prop__(row, col, "background_color")
|
|
|
|
|
|
|
|
source_design = self.matrix[row][col]
|
|
|
|
source_design.mask = False
|
|
|
|
|
|
|
|
framed_design = DesignEntity(size, mask = False)
|
|
|
|
framed_design.__init_image__(color=bg_color)
|
|
|
|
framed_design.draw_design(source_design)
|
|
|
|
|
|
|
|
framed_design.pos = pos
|
|
|
|
self.draw_design(framed_design)
|
|
|
|
|
|
|
|
def __draw_cell__ (self, row, col):
|
|
|
|
size = self.cell_sizes[row][col]
|
|
|
|
pos = self.__get_cell_pos__(row,col)
|
|
|
|
|
|
|
|
if self.matrix[row][col] == None:
|
|
|
|
return
|
|
|
|
elif type(self.matrix[row][col]) == str:
|
|
|
|
self.__draw_text__(pos, size, row, col)
|
|
|
|
else:
|
|
|
|
self.__draw_design__(pos, size, row, col)
|
2019-03-03 13:59:34 +01:00
|
|
|
|
2019-03-03 16:44:11 +01:00
|
|
|
def __get_cell_pos__ (self, row, col):
|
2019-03-03 13:59:34 +01:00
|
|
|
xpos, ypos = (0, 0)
|
|
|
|
for c in range(col):
|
|
|
|
xpos += self.cell_sizes[row][c][0]
|
2019-03-03 14:53:54 +01:00
|
|
|
xpos += self.col_spacing
|
2019-03-03 13:59:34 +01:00
|
|
|
for r in range(row):
|
|
|
|
ypos += self.cell_sizes[r][col][1]
|
2019-03-03 14:53:54 +01:00
|
|
|
ypos += self.line_spacing
|
2019-03-03 13:59:34 +01:00
|
|
|
return (xpos, ypos)
|
|
|
|
|
2019-03-03 16:44:11 +01:00
|
|
|
def __get_cell_sizes__ (self):
|
2019-03-03 13:59:34 +01:00
|
|
|
size_matrix = []
|
|
|
|
for r in range(len(self.matrix)):
|
|
|
|
size_matrix.append([])
|
|
|
|
for c in range(len(self.matrix[0])):
|
2019-03-05 22:10:33 +01:00
|
|
|
size = (self.max_col_size[c], int(self.max_row_size[r]))
|
2019-03-03 13:59:34 +01:00
|
|
|
size_matrix[r].append(size)
|
2019-03-03 18:13:25 +01:00
|
|
|
return size_matrix
|
|
|
|
|
|
|
|
def __get_col_hori_alignment__ (self, c):
|
|
|
|
if len(self.column_horizontal_alignments) <= c:
|
|
|
|
return "left"
|
2019-03-10 19:10:05 +01:00
|
|
|
return self.column_horizontal_alignments[c]
|
|
|
|
|
|
|
|
def __get_cell_prop__(self, r, c, prop):
|
|
|
|
if self.cell_properties is None:
|
|
|
|
return default_props[prop]
|
|
|
|
try:
|
|
|
|
return self.cell_properties[r][c][prop]
|
|
|
|
except:
|
|
|
|
return default_props[prop]
|