From 6edd122a1f054ac6e8bc2a93a5813d98e0608cdf Mon Sep 17 00:00:00 2001 From: niki_main Date: Tue, 23 Jan 2018 14:52:53 +0200 Subject: [PATCH] reorganization of the code --- ete3/treeview/qt4_render.py | 142 ++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/ete3/treeview/qt4_render.py b/ete3/treeview/qt4_render.py index 10f30aed5..f7f31f9d0 100644 --- a/ete3/treeview/qt4_render.py +++ b/ete3/treeview/qt4_render.py @@ -83,6 +83,7 @@ def __init__(self, node): self.setBrush(QBrush(QColor(self.node.img_style["fgcolor"]))) self.setPen(QPen(QColor(self.node.img_style["fgcolor"]))) + class _RectItem(QGraphicsRectItem, _ActionDelegator): def __init__(self, node): self.node = node @@ -92,6 +93,7 @@ def __init__(self, node): self.setBrush(QBrush(QColor(self.node.img_style["fgcolor"]))) self.setPen(QPen(QColor(self.node.img_style["fgcolor"]))) + class _SphereItem(QGraphicsEllipseItem, _ActionDelegator): def __init__(self, node): self.node = node @@ -107,6 +109,7 @@ def __init__(self, node): self.setBrush(QBrush(gradient)) # self.setPen(Qt.NoPen) + class _EmptyItem(QGraphicsItem): def __init__(self, parent=None): QGraphicsItem.__init__(self) @@ -124,6 +127,7 @@ def boundingRect(self): def paint(self, *args, **kargs): return + class _TreeItem(QGraphicsRectItem): def __init__(self, parent=None): QGraphicsRectItem.__init__(self) @@ -131,6 +135,7 @@ def __init__(self, parent=None): self.n2i = {} self.n2f = {} + class _NodeItem(_EmptyItem): def __init__(self, node, parent): _EmptyItem.__init__(self, parent) @@ -140,18 +145,22 @@ def __init__(self, node, parent): self.fullRegion = QRectF() self.highlighted = False + class _NodeLineItem(QGraphicsLineItem, _ActionDelegator): def __init__(self, node, *args, **kargs): self.node = node QGraphicsLineItem.__init__(self, *args, **kargs) _ActionDelegator.__init__(self) + def paint(self, painter, option, widget): QGraphicsLineItem.paint(self, painter, option, widget) + class _LineItem(QGraphicsLineItem): def paint(self, painter, option, widget): QGraphicsLineItem.paint(self, painter, option, widget) + class _PointerItem(QGraphicsRectItem): def __init__(self, parent=None): QGraphicsRectItem.__init__(self,0,0,0,0, parent) @@ -164,14 +173,14 @@ def paint(self, p, option, widget): p.drawRect(self.rect()) return # Draw info text - font = QFont("Arial",13) - text = "%d selected." % len(self.get_selected_nodes()) + font = QFont("Arial", 13) + text = "%d selected." % len(self.get_selected_nodes()) textR = QFontMetrics(font).boundingRect(text) if self.rect().width() > textR.width() and \ - self.rect().height() > textR.height()/2.0 and 0: # OJO !!!! + self.rect().height() > textR.height()/2.0 and 0: # OJO !!!! p.setPen(QPen(self.color)) - p.setFont(QFont("Arial",13)) - p.drawText(self.rect().bottomLeft().x(),self.rect().bottomLeft().y(),text) + p.setFont(QFont("Arial", 13)) + p.drawText(self.rect().bottomLeft().x(), self.rect().bottomLeft().y(),text) def get_selected_nodes(self): selPath = QPainterPath() @@ -185,6 +194,7 @@ def setActive(self,bool): def isActive(self): return self._active + class _TreeScene(QGraphicsScene): def __init__(self): QGraphicsScene.__init__(self) @@ -208,12 +218,13 @@ def draw(self): tree_item.setParentItem(self.master_item) self.setSceneRect(tree_item.rect()) -#@tracktime + +# @tracktime def render(root_node, img, hide_root=False): - '''main render function. hide_root option is used when render + """main render function. hide_root option is used when render trees as Faces - ''' + """ mode = img.mode orientation = img.orientation @@ -222,10 +233,10 @@ def render(root_node, img, hide_root=False): layout_fn = img._layout_handler parent = _TreeItem() - n2i = parent.n2i # node to items - n2f = parent.n2f # node to faces + n2i = parent.n2i # node to items + n2f = parent.n2f # node to faces - parent.bg_layer = _EmptyItem(parent) + parent.bg_layer = _EmptyItem(parent) parent.tree_layer = _EmptyItem(parent) parent.float_layer = _EmptyItem(parent) parent.float_behind_layer = _EmptyItem(parent) @@ -262,13 +273,13 @@ def render(root_node, img, hide_root=False): if _leaf(n) and n.name and img.show_leaf_name: faces.add_face_to_node(na_face, n, 0, position="branch-right") - if _leaf(n):# or len(n.img_style["_faces"]["aligned"]): + if _leaf(n): # or len(n.img_style["_faces"]["aligned"]): virtual_leaves += 1 update_node_faces(n, n2f, img) rot_step = float(arc_span) / virtual_leaves - #rot_step = float(arc_span) / len([n for n in root_node.traverse() if _leaf(n)]) + # rot_step = float(arc_span) / len([n for n in root_node.traverse() if _leaf(n)]) # Calculate optimal branch length if img._scale is not None: @@ -286,7 +297,7 @@ def render(root_node, img, hide_root=False): update_branch_lengths(root_node, n2i, n2f, img) else: img._scale = crender.calculate_optimal_scale(root_node, n2i, rot_step, img) - #print "OPTIMAL circular scale", img._scale + # print "OPTIMAL circular scale", img._scale update_branch_lengths(root_node, n2i, n2f, img) init_items(root_node, parent, n2i, n2f, img, rot_step, hide_root) else: @@ -294,7 +305,7 @@ def render(root_node, img, hide_root=False): img._scale = img.scale init_items(root_node, parent, n2i, n2f, img, rot_step, hide_root) - #print "USING scale", img._scale + # print "USING scale", img._scale # Draw node content for node in root_node.traverse(is_leaf_fn=_leaf): if node is not root_node or not hide_root: @@ -330,8 +341,8 @@ def render(root_node, img, hide_root=False): if img.rotation: rect = parent.boundingRect() - x = rect.x() + (rect.width()/2.0) - y = rect.y() + (rect.height()/2.0) + x = rect.x() + (rect.width()/2.0) + y = rect.y() + (rect.height()/2.0) parent.setTransform(QTransform().translate(x, y).rotate(img.rotation).translate(-x, -y)) # Creates the main tree item that will act as frame for the whole image @@ -364,6 +375,7 @@ def render(root_node, img, hide_root=False): return frame, n2i, n2f + def adjust_faces_to_tranformations(img, mainRect, n2i, n2f, tree_layers): if img.mode == "c": rotate_inverted_faces(n2i, n2f, img) @@ -375,6 +387,7 @@ def adjust_faces_to_tranformations(img, mainRect, n2i, n2f, tree_layers): for pos, fb in six.iteritems(faceblock): fb.flip_hz() + def add_legend(img, mainRect, parent): if img.legend: legend = _FaceGroupItem(img.legend, None) @@ -398,6 +411,7 @@ def add_legend(img, mainRect, parent): legend.setPos(pos.x()-lg_w, pos.y()) mainRect.adjust(0, 0, dw, lg_h) + def add_title(img, mainRect, parent): if img.title: title = _FaceGroupItem(img.title, None) @@ -406,31 +420,9 @@ def add_title(img, mainRect, parent): lg_w, lg_h = title.get_size() dw = max(0, lg_w-mainRect.width()) title.setParentItem(parent) - mainRect.adjust(0, -lg_h, dw, 0) + mainRect.adjust(0, -(lg_h+20), dw, 0) title.setPos(mainRect.topLeft()) -def add_legend(img, mainRect, parent): - if img.legend: - legend = _FaceGroupItem(img.legend, None) - legend.setup_grid() - legend.render() - lg_w, lg_h = legend.get_size() - dw = max(0, lg_w-mainRect.width()) - legend.setParentItem(parent) - if img.legend_position == 1: - mainRect.adjust(0, -lg_h, dw, 0) - legend.setPos(mainRect.topLeft()) - elif img.legend_position == 2: - mainRect.adjust(0, -lg_h, dw, 0) - pos = mainRect.topRight() - legend.setPos(pos.x()-lg_w, pos.y()) - elif img.legend_position == 3: - legend.setPos(mainRect.bottomLeft()) - mainRect.adjust(0, 0, dw, lg_h) - elif img.legend_position == 4: - pos = mainRect.bottomRight() - legend.setPos(pos.x()-lg_w, pos.y()) - mainRect.adjust(0, 0, dw, lg_h) def add_scale(img, mainRect, parent): if img.show_scale: @@ -479,10 +471,11 @@ def add_scale(img, mainRect, parent): def rotate_inverted_faces(n2i, n2f, img): for node, faceblock in six.iteritems(n2f): item = n2i[node] - if item.rotation > 90 and item.rotation < 270: + if 90 < item.rotation < 270: for pos, fb in six.iteritems(faceblock): fb.rotate(181) + def render_backgrounds(img, mainRect, bg_layer, n2i, n2f): if img.mode == "c": @@ -540,6 +533,7 @@ def render_backgrounds(img, mainRect, bg_layer, n2i, n2f): bg.setParentItem(bg_layer) bg.setZValue(item.zValue()) + def set_node_size(node, n2i, n2f, img): scale = img._scale min_separation = img.min_leaf_separation @@ -551,7 +545,7 @@ def set_node_size(node, n2i, n2f, img): branch_length = item.branch_length = float(node.dist * scale) # Organize faces by groups - #faceblock = update_node_faces(node, n2f, img) + # faceblock = update_node_faces(node, n2f, img) faceblock = n2f[node] aligned_height = 0 if _leaf(node): @@ -578,8 +572,8 @@ def set_node_size(node, n2i, n2f, img): h1 = top_half_h + bottom_half_h h2 = max(faceblock["branch-right"].h, \ - aligned_height, \ - min_separation ) + aligned_height, \ + min_separation ) h = max(h1, h2) imbalance = abs(top_half_h - bottom_half_h) if imbalance > h2/2.0: @@ -597,7 +591,7 @@ def set_node_size(node, n2i, n2f, img): ) # This breaks ultrametric tree visualization - #w += node.img_style["vt_line_width"] + # w += node.img_style["vt_line_width"] # rightside faces region item.facesRegion.setRect(0, 0, faceblock["branch-right"].w, h) @@ -608,6 +602,7 @@ def set_node_size(node, n2i, n2f, img): # This is the node total region covered by the node item.fullRegion.setRect(0, 0, w, h) + def render_node_content(node, n2i, n2f, img): style = node.img_style item = n2i[node] @@ -621,7 +616,6 @@ def render_node_content(node, n2i, n2f, img): # Node points ball_size = style["size"] - vlw = style["vt_line_width"] if not _leaf(node) and len(node.children) > 1 else 0.0 # face_start_x = nodeR.width() - facesR.width() - vlw @@ -638,10 +632,10 @@ def render_node_content(node, n2i, n2f, img): node_ball.setPos(ball_start_x, center-(ball_size/2.0)) - #from qt4_gui import _BasicNodeActions - #node_ball.delegate = _BasicNodeActions() - #node_ball.setAcceptHoverEvents(True) - #node_ball.setCursor(Qt.PointingHandCursor) + # from qt4_gui import _BasicNodeActions + # node_ball.delegate = _BasicNodeActions() + # node_ball.setAcceptHoverEvents(True) + # node_ball.setCursor(Qt.PointingHandCursor) else: node_ball = None @@ -652,9 +646,9 @@ def render_node_content(node, n2i, n2f, img): pen.setColor(QColor(style["hz_line_color"])) pen.setWidth(style["hz_line_width"]) pen.setCapStyle(Qt.FlatCap) - #pen.setCapStyle(Qt.RoundCap) - #pen.setCapStyle(Qt.SquareCap) - #pen.setJoinStyle(Qt.RoundJoin) + # pen.setCapStyle(Qt.RoundCap) + # pen.setCapStyle(Qt.SquareCap) + # pen.setJoinStyle(Qt.RoundJoin) hz_line = _LineItem() hz_line = _NodeLineItem(node) hz_line.setPen(pen) @@ -727,35 +721,34 @@ def render_node_content(node, n2i, n2f, img): pen.setColor(QColor(style["vt_line_color"])) pen.setWidth(style["vt_line_width"]) pen.setCapStyle(Qt.FlatCap) - #pen.setCapStyle(Qt.RoundCap) - #pen.setCapStyle(Qt.SquareCap) + # pen.setCapStyle(Qt.RoundCap) + # pen.setCapStyle(Qt.SquareCap) vt_line.setPen(pen) item.vt_line = vt_line else: vt_line = None item.bg = QGraphicsItemGroup() - item.movable_items = [] #QGraphicsItemGroup() - item.static_items = [] #QGraphicsItemGroup() + item.movable_items = [] # QGraphicsItemGroup() + item.static_items = [] # QGraphicsItemGroup() # Items fow which coordinates are exported in the image map item.mapped_items = [node_ball, fblock_r, fblock_b, fblock_t] - for i in [vt_line, extra_line, hz_line]: if i: - #item.static_items.addToGroup(i) + # item.static_items.addToGroup(i) item.static_items.append(i) i.setParentItem(item.content) for i in [node_ball, fblock_r, fblock_b, fblock_t]: if i: - #item.movable_items.addToGroup(i) + # item.movable_items.addToGroup(i) item.movable_items.append(i) i.setParentItem(item.content) + # item.movable_items.setParentItem(item.content) + # item.static_items.setParentItem(item.content) - #item.movable_items.setParentItem(item.content) - #item.static_items.setParentItem(item.content) def set_pen_style(pen, line_style): if line_style == 0: @@ -765,8 +758,9 @@ def set_pen_style(pen, line_style): elif line_style == 2: pen.setStyle(Qt.DotLine) + def set_style(n, layout_func): - #if not isinstance(getattr(n, "img_style", None), NodeStyle): + # if not isinstance(getattr(n, "img_style", None), NodeStyle): # print "Style of", n.name ,"is None" # n.set_style() # n.img_style = NodeStyle() @@ -776,8 +770,9 @@ def set_style(n, layout_func): for func in layout_func: func(n) + def render_floatings(n2i, n2f, img, float_layer, float_behind_layer): - #floating_faces = [ [node, fb["float"]] for node, fb in n2f.iteritems() if "float" in fb] + # floating_faces = [ [node, fb["float"]] for node, fb in n2f.iteritems() if "float" in fb] for node, faces in six.iteritems(n2f): face_set = [ [float_layer, faces.get("float", None)], @@ -799,10 +794,10 @@ def render_floatings(n2i, n2f, img, float_layer, float_behind_layer): # Floatings are positioned over branches crender.rotate_and_displace(fb, item.rotation, fb.h, item.radius - item.nodeRegion.width() + xtra) # Floatings are positioned starting from the node circle - #crender.rotate_and_displace(fb, item.rotation, fb.h, item.radius - item.nodeRegion.width()) + # crender.rotate_and_displace(fb, item.rotation, fb.h, item.radius - item.nodeRegion.width()) elif img.mode == "r": - start = item.branch_length + xtra - fb.w #if fb.w < item.branch_length else 0.0 + start = item.branch_length + xtra - fb.w # if fb.w < item.branch_length else 0.0 fb.setPos(item.content.mapToScene(start, item.center - (fb.h/2.0))) z = item.zValue() @@ -813,6 +808,7 @@ def render_floatings(n2i, n2f, img, float_layer, float_behind_layer): fb.update_columns_size() fb.render() + def render_aligned_faces(img, mainRect, parent, n2i, n2f): # Prepares and renders aligned face headers. Used to later # place aligned faces @@ -913,6 +909,7 @@ def render_aligned_faces(img, mainRect, parent, n2i, n2f): mainRect.adjust(0, 0, extra_width, 0) return extra_width + def get_tree_img_map(n2i, x_scale=1, y_scale=1): MOTIF_ITEMS = set([faces.QGraphicsTriangleItem, faces.QGraphicsEllipseItem, @@ -922,9 +919,9 @@ def get_tree_img_map(n2i, x_scale=1, y_scale=1): node_list = [] face_list = [] node_areas = {} - #nid = 0 + # nid = 0 for n, main_item in six.iteritems(n2i): - #n.add_feature("_nid", str(nid)) + # n.add_feature("_nid", str(nid)) nid = n._nid rect = main_item.mapToScene(main_item.fullRegion).boundingRect() @@ -957,7 +954,7 @@ def get_tree_img_map(n2i, x_scale=1, y_scale=1): if isinstance(f, _TextFaceItem): face_list.append([x1, y1, x2, y2, nid, str(getattr(f, "face_label", f.text()))]) elif isinstance(f, faces.SeqMotifRectItem): - #face_list.append([x1, y1, x2, y2, nid, str(getattr(f, "face_label", None))]) + # face_list.append([x1, y1, x2, y2, nid, str(getattr(f, "face_label", None))]) for mf in f.childItems(): r = mf.boundingRect() rect = mf.mapToScene(r).boundingRect() @@ -976,7 +973,8 @@ def get_tree_img_map(n2i, x_scale=1, y_scale=1): return {"nodes": node_list, "faces": face_list, "node_areas": node_areas} -#@tracktime + +# @tracktime def init_items(root_node, parent, n2i, n2f, img, rot_step, hide_root): # ::: Precalculate values ::: visited = set() @@ -1091,7 +1089,7 @@ def init_node_dimensions(node, item, faceblock, img): # Calculate total node size total_w = sum([w0, w1, w2, w3, w4, item.xoff]) # do not count aligned faces - + if img.mode == "c": max_h = max(item.heights[:4] + [min_separation]) elif img.mode == "r": @@ -1108,6 +1106,7 @@ def init_node_dimensions(node, item, faceblock, img): item.nodeRegion.setRect(0, 0, total_w, max_h) item.fullRegion.setRect(0, 0, total_w, max_h) + def update_branch_lengths(tree, n2i, n2f, img): for node in tree.traverse("postorder", is_leaf_fn=_leaf): item = n2i[node] @@ -1128,6 +1127,7 @@ def update_branch_lengths(tree, n2i, n2f, img): n2i[ch].moveBy(w0, 0) item.fullRegion.setWidth(item.nodeRegion.width() + child_width) + def init_tree_style(t, ts): custom_ts = True if not ts: