Beispiel #1
0
 def _add_low_label(self):
     font = self.font()
     font.setPixelSize(9)
     item = QGraphicsSimpleTextItem("Low")
     item.setFont(font)
     item.setX(self.BAR_WIDTH + self.__offset)
     item.setY(self.__bar_height - item.boundingRect().height())
     self.__group.addToGroup(item)
Beispiel #2
0
 def _add_high_label(self):
     font = self.font()
     font.setPixelSize(9)
     item = QGraphicsSimpleTextItem("High")
     item.setFont(font)
     item.setX(self.BAR_WIDTH + self.__offset)
     item.setY(0)
     self.__group.addToGroup(item)
Beispiel #3
0
 def _add_feature_label(self):
     font = self.font()
     font.setPixelSize(11)
     item = QGraphicsSimpleTextItem("Feature value")
     item.setRotation(-90)
     item.setFont(font)
     item.setX(self.BAR_WIDTH + self.__offset * 2)
     item.setY(self.__bar_height / 2 + item.boundingRect().width() / 2)
     self.__group.addToGroup(item)
class PartItem(QGraphicsPathItem):
    COLOR = NotImplemented
    TIP_LEN = 13

    def __init__(self, value: float, label: Tuple[str, float],
                 norm_value: float):
        super().__init__()
        color = QColor(*self.light_rgb)
        self.value = value
        self.norm_value = norm_value
        self.setPath(self.get_path())
        pen = QPen(color)
        pen.setWidth(2)
        self.setPen(pen)

        value = np.abs(value)
        self.value_item = item = QGraphicsSimpleTextItem(_str(value))
        item.setToolTip(_str(value, 3))
        font = item.font()
        font.setPixelSize(11)
        item.setFont(font)
        width = item.boundingRect().width()
        item.setX(StripeItem.WIDTH / 2 - width / 2)
        item.setPen(color)
        item.setBrush(color)

        self.label_item = QGraphicsSimpleTextItem(
            f"{label[0]} = {_str(label[1])}")
        self.label_item.setToolTip(f"{label[0]} = {_str(label[1], 3)}")
        self.label_item.setX(StripeItem.WIDTH + StripePlot.SPACING)

    @property
    def light_rgb(self) -> List[int]:
        rgb = np.array(self.COLOR)
        return list((rgb + (255 - rgb) * 0.7).astype(int))

    @property
    def value_height(self) -> float:
        return self.value_item.boundingRect().height()

    @property
    def label_height(self) -> float:
        return self.label_item.boundingRect().height()

    def get_path(self) -> QPainterPath:
        raise NotImplementedError
class VariableItem(QGraphicsItemGroup):
    MAX_ATTR_LEN = 25
    MAX_LABEL_LEN = 150
    VALUE_FONT_SETTING = {Updater.SIZE_LABEL: 12,
                          Updater.IS_ITALIC_LABEL: True}

    def __init__(self, parent, label: str):
        self.__name: str = None
        self.__value: Optional[str] = None
        self.__name_item = QGraphicsSimpleTextItem()
        self.__value_item = QGraphicsSimpleTextItem()
        font = Updater.change_font(QFont(), self.VALUE_FONT_SETTING)
        self.__value_item.setFont(font)
        self.__max_len = self.MAX_LABEL_LEN
        super().__init__(parent)
        self._set_data(label)

    @property
    def items(self):
        return self.__name_item, self.__value_item

    def boundingRect(self):
        name_br = self.__name_item.boundingRect()
        width = name_br.width()
        height = name_br.height()
        if self.__value_item is not None:
            value_br = self.__value_item.boundingRect()
            width = max(width, value_br.width())
            height += value_br.height()
        return QRectF(-width, 0, width, height)

    def updateGeometry(self):
        self.__elide()
        self.__align_center()
        self.__align_right()

    def set_max_len(self, length: int):
        self.__max_len = length
        self.updateGeometry()

    def _set_data(self, label: str):
        split = label.split("=")
        self.__name = split[0]
        self.__name_item.setToolTip(self.__name)
        self.updateGeometry()  # align before adding to group
        self.addToGroup(self.__name_item)
        if len(split) > 1:
            self.__value = split[1]
            self.__value_item.setToolTip(self.__value)
            self.updateGeometry()  # align before adding to group
            self.addToGroup(self.__value_item)

    def __elide(self):
        fm = QFontMetrics(self.__name_item.font())
        text = fm.elidedText(self.__name, Qt.ElideRight, self.__max_len)
        self.__name_item.setText(text)
        if self.__value is not None:
            fm = QFontMetrics(self.__value_item.font())
            text = fm.elidedText(self.__value, Qt.ElideRight, self.__max_len)
            self.__value_item.setText(text)

    def __align_center(self):
        if self.__value is not None:
            self.__value_item.setY(self.__name_item.boundingRect().height())

    def __align_right(self):
        self.__name_item.setX(-self.__name_item.boundingRect().width())
        if self.__value is not None:
            self.__value_item.setX(-self.__value_item.boundingRect().width())