def scientific_name_face(node, *args, **kwargs): scientific_name_text = QGraphicsTextItem() #underscore = node.name.replace("_", " ") words = node.name.split("_") text = [] if len(words) < 2: # some sort of acronym or bin name, leave it alone text = words elif len(words) > 2: if len(words) >= 5: text.extend( ['<b>' + words[0] + ' <i> ' + words[1], words[2] + ' </i> ']) text.extend(words[3:] + ['</b>']) elif len(words) == 3: text.extend([ ' <span style="color:grey"><i> ' + words[0], words[1] + words[2] + ' </i></span>' ]) else: # assume that everything after the # second word is strain name # which should not get italicized text.extend([ ' <span style="color:grey"><i> ' + words[0], words[1] + ' </i></span>' ]) text.extend(words[2:]) else: text.extend([ ' <span style="color:grey"><i> ' + words[0], words[1] + ' </i></span> ' ]) scientific_name_text.setHtml(' '.join(text)) # below is a bit of a hack - I've found that the height of the bounding # box gives a bit too much padding around the name, so I just minus 10 # from the height and recenter it. Don't know whether this is a generally # applicable number to use #myFont = QFont() masterItem = QGraphicsRectItem( 0, 0, scientific_name_text.boundingRect().width(), scientific_name_text.boundingRect().height() - 10) scientific_name_text.setParentItem(masterItem) center = masterItem.boundingRect().center() scientific_name_text.setPos( masterItem.boundingRect().x(), center.y() - scientific_name_text.boundingRect().height() / 2) # I don't want a border around the masterItem masterItem.setPen(QPen(QtCore.Qt.NoPen)) return masterItem
def scientific_name_face(node, *args, **kwargs): scientific_name_text = QGraphicsTextItem() words = node.visual_label.split() text = [] if hasattr(node, 'bg_col'): container_div = '<div style="background-color:{};">'.format(node.bgcolor) text.append(container_div) if len(words) < 2: # some sort of acronym or bin name, leave it alone text.extend(words) elif len(words) > 2: if words[0] == 'Candidatus': # for candidatus names, only the Candidatus part is italicised # name shortening it for brevity text.append('<i>Ca.</i>') text.extend(words[1:]) elif re.match('^[A-Z]+$', words[0]): # If the first word is in all caps then it is an abreviation # so we don't want to italicize that at all text.extend(words) else: # assume that everything after the second word is strain name # which should not get italicised text.extend(['<i>'+words[0],words[1]+'</i>']) text.extend(words[2:]) else: text.extend(['<i>'+words[0],words[1]+'</i>']) if hasattr(node, 'bg_col'): text.append('</div>') scientific_name_text.setHtml(' '.join(text)) #print(scientific_name_text.boundingRect().width(), scientific_name_text.boundingRect().height()) # below is a bit of a hack - I've found that the height of the bounding # box gives a bit too much padding around the name, so I just minus 10 # from the height and recenter it. Don't know whether this is a generally # applicable number to use masterItem = QGraphicsRectItem(0, 0, scientific_name_text.boundingRect().width(), scientific_name_text.boundingRect().height() - 10) scientific_name_text.setParentItem(masterItem) center = masterItem.boundingRect().center() scientific_name_text.setPos(masterItem.boundingRect().x(), center.y() - scientific_name_text.boundingRect().height()/2) # I dont want a border around the masterItem masterItem.setPen(QPen(QtCore.Qt.NoPen)) return masterItem
def trinomial_face(binom, spp, ftype="Verdana", fsize=10, fgcolor="black", fstyle="normal", bold=False): """ Stolen from: https://connorskennerton.wordpress.com/2016/11/16/python-ete3-formatting-organism-names-the-way-i-want/ """ font = QFont(ftype, fsize) font.setBold(bold) if fstyle == "italic": font.setStyle(QFont.StyleItalic) elif fstyle == "oblique": font.setStyle(QFont.StyleOblique) text = QGraphicsTextItem() text.setFont(font) if spp is None: text.setHtml("<i>{}</i>".format(binom)) else: text.setHtml("<i>{}</i> {}".format(binom, spp)) rect = QGraphicsRectItem(0, 0, text.boundingRect().width(), text.boundingRect().height() - 10) text.setParentItem(rect) center = rect.boundingRect().center() text.setPos(rect.boundingRect().x(), center.y() - text.boundingRect().height() / 2) # no border rect.setPen(QPen(QtCore.Qt.NoPen)) return ete3.faces.StaticItemFace(rect)
def polygon_name_face(node, *args, **kwargs): """create a wedge shaped face in the style of ARB Args: width (int): size in pixels for the width of the wedge height (int): size in pixels for the height of the wedge width_percent (float): change the angle of the point of the wedge. This must be a number between 0 and 1 Returns: QGraphicsRectItem: The Qt graphics item of the polygon """ n_leaves = len(node.get_leaves()) closest_leaf_dist = node.get_closest_leaf()[1] farthest_leaf_dist = node.get_farthest_leaf()[1] base_height = 30 width = 60 height = math.log(n_leaves, 2) + base_height width_percent = closest_leaf_dist / farthest_leaf_dist #print(width, height, width_percent) points = [ (0.0, 0.0), # top left point (width, 0.0), # top right point (width * width_percent, height), # bottom right point (0.0, height), # bottom left point (0.0, 0.0) # back to the beginning ] shape = QPolygonF() for i in points: shape << QtCore.QPointF(*i) ## Creates a main master Item that will contain all other elements ## Items can be standard QGraphicsItem masterItem = QGraphicsRectItem(0, 0, width, height) # Keep a link within the item to access node info masterItem.node = node # I dont want a border around the masterItem masterItem.setPen(QPen(QtCore.Qt.NoPen)) polygon = QGraphicsPolygonItem(shape, masterItem) # Make the wedge grey in color polygon.setBrush(QBrush(QColor( '#D3D3D3'))) # Print the name of the node # Center text according to masterItem size center = masterItem.boundingRect().center() text = QGraphicsSimpleTextItem(node.name) text.setParentItem(polygon) tw = text.boundingRect().width() th = text.boundingRect().height() text.setPos(center.x() + tw/2, center.y() - th/2) # this is a hack to prevent the name being printed twice # we set the node name to blank after we write it with the QGraphicsSimpleTextItem # it must be set to a blank string for it not to be printed later node.name = '' # print the number of collapsed leaves in the polygon leaves_count_text = QGraphicsSimpleTextItem('('+str(n_leaves)+')') leaves_count_text.setParentItem(polygon) leaves_count_text.setFont(QFont('Veranda', 6)) leaves_count_text.setPos(masterItem.boundingRect().x() + 5, center.y() - leaves_count_text.boundingRect().height()/2) polygon.setPos(0, masterItem.boundingRect().y()/1.5) return masterItem
class GradientLegendItem(QGraphicsObject, GraphicsWidgetAnchor): gradient_width = 20 def __init__(self, title, palette, values, parent): QGraphicsObject.__init__(self, parent) GraphicsWidgetAnchor.__init__(self) self.parent = self.legend = parent self.palette = palette self.values = values self.title = QGraphicsTextItem('%s:' % title, self) f = self.title.font() f.setBold(True) self.title.setFont(f) self.title_item = QGraphicsRectItem(self.title.boundingRect(), self) self.title_item.setPen(QPen(Qt.NoPen)) self.title_item.stackBefore(self.title) self.label_items = [QGraphicsTextItem(text, self) for text in values] for i in self.label_items: i.setTextWidth(50) self.rect = QRectF() self.gradient_item = QGraphicsRectItem(self) self.gradient = QLinearGradient() self.gradient.setStops([(v * 0.1, self.palette[v * 0.1]) for v in range(11)]) self.orientation = Qt.Horizontal self.set_orientation(Qt.Vertical) self.setFlag(QGraphicsItem.ItemIgnoresTransformations, True) self.setFlag(QGraphicsItem.ItemIsMovable, True) def set_orientation(self, orientation): return if self.orientation == orientation: return self.orientation = orientation if self.orientation == Qt.Vertical: height = max(item.boundingRect().height() for item in self.label_items) total_height = height * max(5, len(self.label_items)) interval = (total_height - self.label_items[-1].boundingRect().height() ) / (len(self.label_items) - 1) self.gradient_item.setRect(10, 0, self.gradient_width, total_height) self.gradient.setStart(10, 0) self.gradient.setFinalStop(10, total_height) self.gradient_item.setBrush(QBrush(self.gradient)) self.gradient_item.setPen(QPen(Qt.NoPen)) y = -20 # hja, no; dela --> pri boundingRect() zato pristejem +20 x = 0 move_item_xy(self.title, x, y, False) y = 10 x = 30 for item in self.label_items: move_item_xy(item, x, y, False) # self.parent.graph.animate_plot) y += interval self.rect = QRectF(10, 0, self.gradient_width + max(item.boundingRect().width() for item in self.label_items), self.label_items[0].boundingRect().height() * max(5, len(self.label_items))) else: # za horizontalno orientacijo nisem dodajal title-a width = 50 height = max(item.boundingRect().height() for item in self.label_items) total_width = width * max(5, len(self.label_items)) interval = (total_width - self.label_items[-1].boundingRect().width() ) / (len(self.label_items) - 1) self.gradient_item.setRect(0, 0, total_width, self.gradient_width) self.gradient.setStart(0, 0) self.gradient.setFinalStop(total_width, 0) self.gradient_item.setBrush(QBrush(self.gradient)) self.gradient_item.setPen(QPen(Qt.NoPen)) x = 0 y = 30 for item in self.label_items: move_item_xy(item, x, y, False) # self.parent.graph.animate_plot) x += interval self.rect = QRectF(0, 0, total_width, self.gradient_width + height) # noinspection PyPep8Naming def boundingRect(self): width = max(self.rect.width(), self.title_item.boundingRect().width()) height = self.rect.height() + self.title_item.boundingRect().height() return QRectF(self.rect.left(), self.rect.top(), width, height) def paint(self, painter, option, widget): pass