From 9b1392c9125021fa7df06524d9e7aa8a508b560e Mon Sep 17 00:00:00 2001 From: Brian Matherly Date: Tue, 17 Nov 2009 05:11:32 +0000 Subject: [PATCH] Clean up descendant tree - PEP8, naming, etc. svn: r13603 --- src/plugins/drawreport/DescendTree.py | 270 ++++++++++++++------------ 1 file changed, 148 insertions(+), 122 deletions(-) diff --git a/src/plugins/drawreport/DescendTree.py b/src/plugins/drawreport/DescendTree.py index 5b492f4dd..c93cf692c 100644 --- a/src/plugins/drawreport/DescendTree.py +++ b/src/plugins/drawreport/DescendTree.py @@ -26,7 +26,7 @@ #------------------------------------------------------------------------ # -# python modules +# GRAMPS modules # #------------------------------------------------------------------------ from BasicUtils import name_displayer @@ -38,14 +38,6 @@ from ReportBase import Report, MenuReportOptions, ReportUtils from SubstKeywords import SubstKeywords from TransUtils import sgettext as _ -#------------------------------------------------------------------------ -# -# GRAMPS modules -# -#------------------------------------------------------------------------ -pt2cm = ReportUtils.pt2cm -cm2pt = ReportUtils.cm2pt - #------------------------------------------------------------------------ # # Constants @@ -81,11 +73,11 @@ class GenChart(object): def get_xy(self, x, y): if y not in self.array: return 0 - return self.array[y].get(x,0) + return self.array[y].get(x, 0) def set_xy(self, x, y, value): - self.max_x = max(self.max_x,x) - self.max_y = max(self.max_y,y) + self.max_x = max(self.max_x, x) + self.max_y = max(self.max_y, y) if y not in self.array: self.array[y] = {} @@ -104,20 +96,20 @@ class GenChart(object): self.sparray[col_x, row_y] = value def dimensions(self): + """ + Returns the dimensions of the chart. + """ return (self.max_y+1, self.max_x+1) - def not_blank(self, line): - for i in line: - if i and isinstance(i, tuple): - return 1 - return 0 - #------------------------------------------------------------------------ # # DescendTree # #------------------------------------------------------------------------ class DescendTree(Report): + """ + Report class that generates a descendant tree. + """ def __init__(self, database, options_class): """ @@ -161,10 +153,18 @@ class DescendTree(Report): self.lines = 0 self.scale = 1 self.box_gap = 0.2 + self.box_pad_pts = 0 + self.offset = 0 + self.page_label_x_offset = 0 + self.page_label_y_offset = 0 + self.usable_height = 0 + self.usable_width = 0 + self.generations_per_page = 0 + self.delta = 0 self.genchart = GenChart(32) - self.apply_filter(center_person.get_handle(),0,0) + self.apply_filter(center_person.get_handle(), 0, 0) self.calc() @@ -190,12 +190,12 @@ class DescendTree(Report): pstyle = style_sheet.get_paragraph_style("DC2-Normal") font = pstyle.get_font() - em = self.doc.string_width(font,"m") + em = self.doc.string_width(font, "m") subst = SubstKeywords(self.database, person_handle) self.text[(col_x, row_y)] = subst.replace_and_clean(self.display) for line in self.text[(col_x, row_y)]: - this_box_width = self.doc.string_width(font, line) + 2*em + this_box_width = self.doc.string_width(font, line) + (2 * em) self.box_width = max(self.box_width, this_box_width) self.lines = max(self.lines, len(self.text[(col_x, row_y)])) @@ -205,7 +205,7 @@ class DescendTree(Report): of a line is found, or until we reach the maximum number of generations that we want to deal with""" - if col_x/2 >= self.max_generations: + if (col_x / 2) >= self.max_generations: return 0 person = self.database.get_person_from_handle(person_handle) @@ -219,13 +219,13 @@ class DescendTree(Report): if self.showspouse: spouse_handle = ReportUtils.find_spouse(person, family) - self.add_person(spouse_handle, col_x, row_y+working_col, + self.add_person(spouse_handle, col_x, row_y + working_col, _PERSON_SPOUSE) working_col += 1 for child_ref in family.get_child_ref_list(): - next_col += self.apply_filter(child_ref.ref, col_x+2, - row_y+next_col) + next_col += self.apply_filter(child_ref.ref, col_x + 2, + row_y + next_col) working_col = next_col = max(working_col, next_col) @@ -233,50 +233,55 @@ class DescendTree(Report): def add_lines(self): + """ + Add the lines that connect the boxes in the chart. + """ (maxy, maxx) = self.genchart.dimensions() - for y in range(0, maxy+1): - for x in range(0, maxx+1): + for y in range(0, maxy + 1): + for x in range(0, maxx + 1): # skip columns reserved for rows - no data here - if x%2: + if x % 2: continue # if we have a direct child to the right of a person # check to see if the child is a descendant of the person - if self.genchart.get_sp(x+2, y) == _PERSON_DIRECT: + if self.genchart.get_sp(x + 2, y) == _PERSON_DIRECT: if self.genchart.get_sp(x, y) == _PERSON_DIRECT: - self.genchart.set_xy(x+1, y , _LINE_HORIZONTAL) + self.genchart.set_xy(x + 1, y , _LINE_HORIZONTAL) continue elif self.genchart.get_sp(x, y) == _PERSON_SPOUSE and \ - self.genchart.get_sp(x, y-1) != _PERSON_DIRECT: - self.genchart.set_xy(x+1, y , _LINE_HORIZONTAL) + self.genchart.get_sp(x, y - 1) != _PERSON_DIRECT: + self.genchart.set_xy(x + 1, y , _LINE_HORIZONTAL) continue else: continue - self.genchart.set_xy(x+1, y, _LINE_ANGLE) + self.genchart.set_xy(x + 1, y, _LINE_ANGLE) # look through the entries ABOVE this one. All direct people # in the next column are descendants until we hit the first # direct person (marked with _LINE_HORIZONTAL) - last = y-1 + last = y - 1 while last > 0: - if self.genchart.get_xy(x+1, last) == 0: - self.genchart.set_xy(x+1, last, _LINE_VERTICAL) + if self.genchart.get_xy(x + 1, last) == 0: + self.genchart.set_xy(x + 1, last, _LINE_VERTICAL) else: break last -= 1 def write_report(self): - - (maxy,maxx) = self.genchart.dimensions() - if maxx <> 1: - maxx = (maxx-1)*2 + """ + Write the report to the document. + """ + (maxy, maxx) = self.genchart.dimensions() + if maxx != 1: + maxx = (maxx - 1) * 2 else: #no descendants maxx = 1 - maxh = int((self.uh-0.75)/(self.box_height*1.25)) + maxh = int((self.usable_height - 0.75) / (self.box_height * 1.25)) if self.force_fit: self.print_page(0, maxx, 0, maxy, 0, 0) @@ -287,11 +292,11 @@ class DescendTree(Report): startx = 0 colx = 0 while startx < maxx: - stopx = min(maxx, startx+self.generations_per_page*2) - stopy = min(maxy, starty+maxh) + stopx = min(maxx, startx + (self.generations_per_page * 2)) + stopy = min(maxy, starty + maxh) self.print_page(startx, stopx, starty, stopy, colx, coly) colx += 1 - startx += self.generations_per_page*2 + startx += self.generations_per_page * 2 coly += 1 starty += maxh @@ -309,7 +314,7 @@ class DescendTree(Report): if self.title and self.force_fit: pstyle = style_sheet.get_paragraph_style("DC2-Title") tfont = pstyle.get_font() - self.offset = pt2cm(1.25 * tfont.get_size()) + self.offset = ReportUtils.pt2cm(1.25 * tfont.get_size()) gstyle = style_sheet.get_draw_style("DC2-box") shadow_height = gstyle.get_shadow_space() @@ -317,49 +322,54 @@ class DescendTree(Report): # Make space for the page number labels at the bottom. p = style_sheet.get_paragraph_style("DC2-Normal") font = p.get_font() - lheight = pt2cm(1.2*font.get_size()) - lwidth = pt2cm(1.1*self.doc.string_width(font,"(00,00)")) - self.page_label_x_offset = self.doc.get_usable_width() - lwidth + lheight = ReportUtils.pt2cm(1.2 * font.get_size()) + lwidth = ReportUtils.pt2cm(1.1 * + self.doc.string_width(font, "(00,00)")) + self.page_label_x_offset = self.doc.get_usable_width() - lwidth self.page_label_y_offset = self.doc.get_usable_height() - lheight - self.offset = pt2cm(1.25 * font.get_size()) + self.offset = ReportUtils.pt2cm(1.25 * font.get_size()) shadow_height = 0 - self.uh = self.doc.get_usable_height() - self.offset - shadow_height - uw = self.doc.get_usable_width() - pt2cm(self.box_pad_pts) + self.usable_height = self.doc.get_usable_height() \ + - self.offset \ + - shadow_height + self.usable_width = self.doc.get_usable_width() \ + - ReportUtils.pt2cm(self.box_pad_pts) - calc_width = pt2cm(self.box_width + self.box_pad_pts) + self.box_gap - self.box_width = pt2cm(self.box_width) + calc_width = ReportUtils.pt2cm(self.box_width + self.box_pad_pts) \ + + self.box_gap + self.box_width = ReportUtils.pt2cm(self.box_width) pstyle = style_sheet.get_paragraph_style("DC2-Normal") font = pstyle.get_font() - self.box_height = self.lines*pt2cm(1.25*font.get_size()) + self.box_height = self.lines*ReportUtils.pt2cm(1.25 * font.get_size()) self.scale = 1 if self.force_fit: (maxy, maxx) = self.genchart.dimensions() - bw = (calc_width/(uw/(maxx+1))) - bh = (self.box_height*(1.25)+self.box_gap)/(self.uh/maxy) + bw = (calc_width / (self.usable_width / (maxx + 1))) + bh = ((self.box_height * 1.25) + self.box_gap) \ + / (self.usable_height / maxy) - self.scale = max(bw/2, bh) + self.scale = max(bw / 2, bh) self.box_width /= self.scale self.box_height /= self.scale self.box_pad_pts /= self.scale self.box_gap /= self.scale -# maxh = int((self.uh)/(self.box_height+self.box_gap)) - maxw = int(uw/calc_width) - - # build array of x indices + maxw = int(self.usable_width / calc_width) self.generations_per_page = maxw - self.delta = pt2cm(self.box_pad_pts) + self.box_width + self.box_gap + self.delta = ReportUtils.pt2cm(self.box_pad_pts) \ + + self.box_width \ + + self.box_gap if not self.force_fit: - calc_width = self.box_width + pt2cm(self.box_pad_pts) - remain = self.doc.get_usable_width() - \ - ((self.generations_per_page)*calc_width) - self.delta += remain/float(self.generations_per_page) + calc_width = self.box_width + ReportUtils.pt2cm(self.box_pad_pts) + remain = self.doc.get_usable_width() \ + - (self.generations_per_page * calc_width) + self.delta += remain / float(self.generations_per_page) def scale_styles(self): """ @@ -367,16 +377,17 @@ class DescendTree(Report): """ style_sheet = self.doc.get_style_sheet() - g = style_sheet.get_draw_style("DC2-box") - g.set_shadow(g.get_shadow(),g.get_shadow_space()/self.scale) - g.set_line_width(g.get_line_width()/self.scale) - style_sheet.add_draw_style("DC2-box",g) + box_style = style_sheet.get_draw_style("DC2-box") + box_style.set_shadow(box_style.get_shadow(), + box_style.get_shadow_space() / self.scale) + box_style.set_line_width(box_style.get_line_width() / self.scale) + style_sheet.add_draw_style("DC2-box", box_style) - p = style_sheet.get_paragraph_style("DC2-Normal") - font = p.get_font() - font.set_size(font.get_size()/self.scale) - p.set_font(font) - style_sheet.add_paragraph_style("DC2-Normal", p) + para_style = style_sheet.get_paragraph_style("DC2-Normal") + font = para_style.get_font() + font.set_size(font.get_size() / self.scale) + para_style.set_font(font) + style_sheet.add_paragraph_style("DC2-Normal", para_style) self.doc.set_style_sheet(style_sheet) @@ -388,13 +399,15 @@ class DescendTree(Report): if self.genchart.get_xy(x, y) != 0: blank = False break - if not blank: break - if blank: return + if not blank: + break + if blank: + return self.doc.start_page() if self.title and self.force_fit: self.doc.center_text('DC2-title', self.title, - self.doc.get_usable_width()/2,0) + self.doc.get_usable_width() / 2, 0) phys_y = 1 bh = self.box_height * 1.25 for y in range(starty, stopy): @@ -403,8 +416,8 @@ class DescendTree(Report): value = self.genchart.get_xy(x, y) if isinstance(value, basestring): text = '\n'.join(self.text[(x, y)]) - xbegin = phys_x*self.delta - yend = phys_y*bh+self.offset + xbegin = phys_x * self.delta + yend = (phys_y * bh) + self.offset self.doc.draw_box("DC2-box", text, xbegin, @@ -412,32 +425,45 @@ class DescendTree(Report): self.box_width, self.box_height) elif value == _LINE_HORIZONTAL: - xbegin = phys_x*self.delta - ystart = (phys_y*bh + self.box_height/2.0) + self.offset + xbegin = phys_x * self.delta + ystart = ((phys_y * bh) + self.box_height / 2.0) \ + + self.offset xstart = xbegin + self.box_width - xstop = (phys_x+1)*self.delta + xstop = (phys_x + 1) * self.delta self.doc.draw_line('DC2-line', xstart, ystart, xstop, ystart) elif value == _LINE_VERTICAL: - ystart = ((phys_y-1)*bh + self.box_height/2.0) + self.offset - ystop = (phys_y*bh + self.box_height/2.0) + self.offset - xlast = (phys_x*self.delta) + self.box_width + self.box_gap + ystart = ((phys_y - 1) * bh) \ + + (self.box_height / 2.0) \ + + self.offset + ystop = (phys_y * bh) \ + + (self.box_height / 2.0) \ + + self.offset + xlast = (phys_x * self.delta) \ + + self.box_width \ + + self.box_gap self.doc.draw_line('DC2-line', xlast, ystart, xlast, ystop) elif value == _LINE_ANGLE: - ystart = ((phys_y-1)*bh + self.box_height/2.0) + self.offset - ystop = (phys_y*bh + self.box_height/2.0) + self.offset - xlast = (phys_x*self.delta) + self.box_width + self.box_gap - xnext = (phys_x+1)*self.delta + ystart = ((phys_y - 1) * bh) \ + + (self.box_height / 2.0) \ + + self.offset + ystop = (phys_y * bh) \ + + (self.box_height / 2.0) \ + + self.offset + xlast = (phys_x * self.delta) \ + + self.box_width \ + + self.box_gap + xnext = (phys_x + 1) * self.delta self.doc.draw_line('DC2-line', xlast, ystart, xlast, ystop) self.doc.draw_line('DC2-line', xlast, ystop, xnext, ystop) - if x%2: - phys_x +=1 + if x % 2: + phys_x += 1 phys_y += 1 if not self.force_fit: self.doc.draw_text('DC2-box', - '(%d,%d)' % (colx+1, coly+1), + '(%d,%d)' % (colx + 1, coly + 1), self.page_label_x_offset, self.page_label_y_offset) self.doc.end_page() @@ -487,39 +513,39 @@ class DescendTreeOptions(MenuReportOptions): shows.set_help(_("Whether to show spouses in the tree.")) menu.add_option(category_name, "shows", shows) - def make_default_style(self,default_style): + def make_default_style(self, default_style): """Make the default output style for the Ancestor Tree.""" ## Paragraph Styles: - f = FontStyle() - f.set_size(9) - f.set_type_face(FONT_SANS_SERIF) - p = ParagraphStyle() - p.set_font(f) - p.set_description(_('The basic style used for the text display.')) - default_style.add_paragraph_style("DC2-Normal", p) + font = FontStyle() + font.set_size(9) + font.set_type_face(FONT_SANS_SERIF) + p_style = ParagraphStyle() + p_style.set_font(font) + p_style.set_description(_('The basic style used for the text display.')) + default_style.add_paragraph_style("DC2-Normal", p_style) - f = FontStyle() - f.set_size(16) - f.set_type_face(FONT_SANS_SERIF) - p = ParagraphStyle() - p.set_font(f) - p.set_alignment(PARA_ALIGN_CENTER) - p.set_description(_('The basic style used for the title display.')) - default_style.add_paragraph_style("DC2-Title", p) + font = FontStyle() + font.set_size(16) + font.set_type_face(FONT_SANS_SERIF) + p_style = ParagraphStyle() + p_style.set_font(font) + p_style.set_alignment(PARA_ALIGN_CENTER) + p_style.set_description(_('The basic style used for the title display.')) + default_style.add_paragraph_style("DC2-Title", p_style) ## Draw styles - g = GraphicsStyle() - g.set_paragraph_style("DC2-Normal") - g.set_shadow(1, 0.2) - g.set_fill_color((255, 255, 255)) - default_style.add_draw_style("DC2-box", g) + g_style = GraphicsStyle() + g_style.set_paragraph_style("DC2-Normal") + g_style.set_shadow(1, 0.2) + g_style.set_fill_color((255, 255, 255)) + default_style.add_draw_style("DC2-box", g_style) - g = GraphicsStyle() - g.set_paragraph_style("DC2-Title") - g.set_color((0, 0, 0)) - g.set_fill_color((255, 255, 255)) - g.set_line_width(0) - default_style.add_draw_style("DC2-title", g) + g_style = GraphicsStyle() + g_style.set_paragraph_style("DC2-Title") + g_style.set_color((0, 0, 0)) + g_style.set_fill_color((255, 255, 255)) + g_style.set_line_width(0) + default_style.add_draw_style("DC2-title", g_style) - g = GraphicsStyle() - default_style.add_draw_style("DC2-line", g) + g_style = GraphicsStyle() + default_style.add_draw_style("DC2-line", g_style)