Added truncating of rows and columns that are not fully visible
This commit is contained in:
parent
7043911cb8
commit
ad07a03283
1 changed files with 33 additions and 11 deletions
|
@ -1,22 +1,27 @@
|
||||||
from TextDesign import TextDesign
|
from TextDesign import TextDesign
|
||||||
|
|
||||||
class TableTextDesign(TextDesign):
|
class TableTextDesign (TextDesign):
|
||||||
"""Gets a matrix with text that is than
|
"""Gets a matrix with text that is than
|
||||||
displayed in a table without borders."""
|
displayed in a table without borders."""
|
||||||
def __init__(self, size, text_matrix, max_col_size=None, font = None, fontsize = 12, horizontalalignment = "left", verticalalignment = "top", mask = True, line_spacing = 0, col_spacing = 0):
|
def __init__ (self, size, text_matrix, max_col_size = None, font = None, fontsize = 12, horizontalalignment = "left", verticalalignment = "top", mask = True, line_spacing = 0, col_spacing = 0, truncate_rows = True, truncate_cols = True):
|
||||||
super(TableTextDesign, self).__init__(size, font=font, fontsize=fontsize, horizontalalignment=horizontalalignment, verticalalignment=verticalalignment, mask=mask)
|
super(TableTextDesign, self).__init__(size, font=font, fontsize=fontsize, horizontalalignment=horizontalalignment, verticalalignment=verticalalignment, mask=mask)
|
||||||
self.matrix = text_matrix
|
self.matrix = text_matrix
|
||||||
self.max_col_size = max_col_size
|
self.max_col_size = max_col_size
|
||||||
self.line_spacing = line_spacing
|
self.line_spacing = line_spacing
|
||||||
self.col_spacing = col_spacing
|
self.col_spacing = col_spacing
|
||||||
|
self.truncate_rows = truncate_rows
|
||||||
|
self.truncate_cols = truncate_cols
|
||||||
|
self.max_row = None
|
||||||
|
self.max_col = None
|
||||||
|
|
||||||
def __finish_image__(self):
|
def __finish_image__ (self):
|
||||||
self.__reform_col_size__()
|
self.__reform_col_size__()
|
||||||
self.cell_sizes = self.__get_cell_sizes__()
|
self.cell_sizes = self.__get_cell_sizes__()
|
||||||
|
self.max_col, self.max_row = self.__get_truncated_counts__()
|
||||||
self.__print_table__(self.matrix)
|
self.__print_table__(self.matrix)
|
||||||
|
|
||||||
|
|
||||||
def __reform_col_size__(self):
|
def __reform_col_size__ (self):
|
||||||
if self.max_col_size is not None:
|
if self.max_col_size is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -32,19 +37,36 @@ class TableTextDesign(TextDesign):
|
||||||
|
|
||||||
self.max_col_size = col_sizes
|
self.max_col_size = col_sizes
|
||||||
|
|
||||||
def __print_table__(self, matrix):
|
def __get_truncated_counts__ (self):
|
||||||
for r, row in enumerate(matrix):
|
max_col = 0
|
||||||
for c, cell in enumerate(row):
|
if self.truncate_cols:
|
||||||
|
while max_col < len(self.matrix[0]) and self.__get_cell_pos__(0, max_col + 1)[0] <= self.size[0]:
|
||||||
|
max_col += 1
|
||||||
|
else:
|
||||||
|
max_col = len(self.matrix[0])
|
||||||
|
|
||||||
|
max_row = 0
|
||||||
|
if self.truncate_rows:
|
||||||
|
while max_row < len(self.matrix) and self.__get_cell_pos__(max_row + 1,0)[1] <= self.size[1]:
|
||||||
|
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):
|
||||||
size = self.cell_sizes[r][c]
|
size = self.cell_sizes[r][c]
|
||||||
pos = self.__get_cell_pos__(r,c)
|
pos = self.__get_cell_pos__(r,c)
|
||||||
self.__draw_text__(pos, size, self.matrix[r][c])
|
self.__draw_text__(pos, size, self.matrix[r][c])
|
||||||
|
|
||||||
def __draw_text__(self, pos, size, text):
|
def __draw_text__ (self, pos, size, text):
|
||||||
design = TextDesign(size, text=text, font=self.font_family, fontsize=self.font_size, horizontalalignment=self.horizontal_alignment, verticalalignment=self.vertical_alignment)
|
design = TextDesign(size, text=text, font=self.font_family, fontsize=self.font_size, horizontalalignment=self.horizontal_alignment, verticalalignment=self.vertical_alignment)
|
||||||
design.pos = pos
|
design.pos = pos
|
||||||
self.draw_design(design)
|
self.draw_design(design)
|
||||||
|
|
||||||
def __get_cell_pos__(self, row, col):
|
def __get_cell_pos__ (self, row, col):
|
||||||
xpos, ypos = (0, 0)
|
xpos, ypos = (0, 0)
|
||||||
for c in range(col):
|
for c in range(col):
|
||||||
xpos += self.cell_sizes[row][c][0]
|
xpos += self.cell_sizes[row][c][0]
|
||||||
|
@ -54,11 +76,11 @@ class TableTextDesign(TextDesign):
|
||||||
ypos += self.line_spacing
|
ypos += self.line_spacing
|
||||||
return (xpos, ypos)
|
return (xpos, ypos)
|
||||||
|
|
||||||
def __get_cell_sizes__(self):
|
def __get_cell_sizes__ (self):
|
||||||
size_matrix = []
|
size_matrix = []
|
||||||
for r in range(len(self.matrix)):
|
for r in range(len(self.matrix)):
|
||||||
size_matrix.append([])
|
size_matrix.append([])
|
||||||
for c in range(len(self.matrix[0])):
|
for c in range(len(self.matrix[0])):
|
||||||
size = (self.max_col_size[c], self.font_size)
|
size = (self.max_col_size[c], int(self.font_size * 1.1))
|
||||||
size_matrix[r].append(size)
|
size_matrix[r].append(size)
|
||||||
return size_matrix
|
return size_matrix
|
Loading…
Reference in a new issue