class InteractiveItem(QGraphicsRectItem): def __init__(self, *arg, **karg): QGraphicsRectItem.__init__(self, *arg, **karg) self.node = None self.label = None self.setCursor(QtCore.Qt.PointingHandCursor) self.setAcceptsHoverEvents(True) def hoverEnterEvent (self, e): # There are many ways of adding interactive elements. With the # following code, I show/hide a text item over my custom # DynamicItemFace if not self.label: self.label = QGraphicsRectItem() self.label.setParentItem(self) # This is to ensure that the label is rendered over the # rest of item children (default ZValue for items is 0) self.label.setZValue(1) self.label.setBrush(QBrush(QColor("white"))) self.label.text = QGraphicsSimpleTextItem() self.label.text.setParentItem(self.label) self.label.text.setText(self.node.name) self.label.setRect(self.label.text.boundingRect()) self.label.setVisible(True) def hoverLeaveEvent(self, e): if self.label: self.label.setVisible(False)
class InteractiveItem(QGraphicsRectItem): def __init__(self, *arg, **karg): QGraphicsRectItem.__init__(self, *arg, **karg) self.node = None self.label = None self.setCursor(QtCore.Qt.PointingHandCursor) self.setAcceptsHoverEvents(True) def hoverEnterEvent(self, e): # There are many ways of adding interactive elements. With the # following code, I show/hide a text item over my custom # DynamicItemFace if not self.label: self.label = QGraphicsRectItem() self.label.setParentItem(self) # This is to ensure that the label is rendered over the # rest of item children (default ZValue for items is 0) self.label.setZValue(1) self.label.setBrush(QBrush(QColor("white"))) self.label.text = QGraphicsSimpleTextItem() self.label.text.setParentItem(self.label) self.label.text.setText(self.node.name) self.label.setRect(self.label.text.boundingRect()) self.label.setVisible(True) def hoverLeaveEvent(self, e): if self.label: self.label.setVisible(False)
def ugly_name_face(node, *args, **kargs): """ This is my item generator. It must receive a node object, and returns a Qt4 graphics item that can be used as a node face. """ width = node.dist * 2.5 height = 12 masterItem = InteractiveItem(0, 0, width, height) masterItem.node = node masterItem.setPen(QPen(QtCore.Qt.NoPen)) color_rect = QGraphicsRectItem(masterItem.rect()) color_rect.setParentItem(masterItem) color_rect.setBrush(QBrush(QColor(100, 100, 200, 100))) color_rect.setPen(QPen(QtCore.Qt.NoPen)) masterItem.color_rect = color_rect return masterItem
def draw_colored_boxes(self, h): stick_colors = self.colors num_col_red = stick_colors.count("red") num_col_orange = stick_colors.count("orange") num_col_yellow = stick_colors.count("#EFDB00") num_col_gray = stick_colors.count("gray") colored_box_h = self.height + h - 5 if num_col_red: rect_red = QGraphicsRectItem(self.col_w * 0 - 1, self.coordY(self.ylim[1]) - 5, self.col_w * num_col_red, colored_box_h) qpen = QPen(QColor('red')) qpen.setWidth(1) qpen.setStyle( 5) # dash line : http://doc.qt.io/qt-4.8/qt.html#PenStyle-enum rect_red.setPen(qpen) rect_red.setBrush(QColor("#FFD1D1")) rect_red.setZValue(-1) rect_red.setParentItem(self.item) #rect_red.setOpacity(0.5) if num_col_orange: rect_orange = QGraphicsRectItem(self.col_w * num_col_red + 1, self.coordY(self.ylim[1]) - 5, self.col_w * num_col_orange - 2, colored_box_h) qpen = QPen(QColor('orange')) qpen.setWidth(1) qpen.setStyle( 5) # dash line : http://doc.qt.io/qt-4.8/qt.html#PenStyle-enum rect_orange.setPen(qpen) rect_orange.setBrush(QColor("#FFE3B0")) rect_orange.setZValue(-1) rect_orange.setParentItem(self.item) #rect_orange.setOpacity(0.5) if num_col_yellow: rect_yellow = QGraphicsRectItem( self.col_w * (num_col_orange + num_col_red) + 1, self.coordY(self.ylim[1]) - 5, self.col_w * num_col_yellow - 2, colored_box_h) qpen = QPen(QColor('#EFDB00')) qpen.setWidth(1) qpen.setStyle( 5) # dash line : http://doc.qt.io/qt-4.8/qt.html#PenStyle-enum rect_yellow.setPen(qpen) rect_yellow.setBrush(QColor("#FBFFA5")) rect_yellow.setZValue(-1) rect_yellow.setParentItem(self.item) #rect_yellow.setOpacity(0.5) if num_col_gray: rect_gray = QGraphicsRectItem( self.col_w * (num_col_orange + num_col_red + num_col_yellow) + 1, self.coordY(self.ylim[1]) - 5, self.col_w * num_col_gray, colored_box_h) qpen = QPen(QColor('gray')) qpen.setWidth(1) qpen.setStyle( 5) # dash line : http://doc.qt.io/qt-4.8/qt.html#PenStyle-enum rect_gray.setPen(qpen) rect_gray.setBrush(QColor("#E2E2E2")) rect_gray.setZValue(-1) rect_gray.setParentItem(self.item)