Пример #1
0
class ScaleLine:
    def __init__(self,
                 parent,
                 color,
                 text_color,
                 text_start="",
                 margins=(0, 0, 0, 0),
                 dp=1,
                 is_upload=False):
        self._parent = parent
        self._color = color
        self._text_color = text_color
        self._text_start = text_start
        self._left, self._top, self._right, self._bottom = margins
        self._dp = dp
        self._is_upload = is_upload

        self._line = QGraphicsLineItem(self._parent)
        self._line.setZValue(12)
        pen = QPen(self._color)
        pen.setWidth(1)
        pen.setStyle(Qt.DashLine)
        self._line.setPen(pen)

        if not self._is_upload:
            self._text_item = QGraphicsTextItem(self._parent)
            self._text_item.setZValue(11)
            self._text_item.setDefaultTextColor(self._text_color)
            font = self._text_item.font()
            font_size = 10 * self._dp
            if font_size > 0:
                self._text_item.setFont(QFont(font.family(), font_size))

    def set_line(self, max_value=0, resize=False):
        height = self._parent.size().height() + self._top + self._bottom
        shift = int(height - height / 1.1)
        y = -self._top + shift

        if not self._is_upload:
            value = 0
            max_value = int(max_value / 1.1)
            megabyte = 1024 * 1024
            if max_value > megabyte:
                value = "{} MB".format(round(max_value / megabyte, 1))
            elif max_value > 1024:
                max_value //= 1024
                if max_value >= 10:
                    max_value = max_value // 10 * 10
                value = "{} KB".format(max_value)
            elif max_value > 0:
                if max_value >= 10:
                    max_value = max_value // 10 * 10
                value = "{} B".format(max_value)
            scale_text =  self._text_start if not value \
                else "{}{}/s".format(self._text_start, value)

            font_height = QFontMetrics(self._text_item.font())\
                .boundingRect(scale_text).height()
            x = 10
            self._text_item.setPos(x, y - font_height - 10)
            self._text_item.setPlainText(scale_text)

        if not resize:
            return

        self._line.setLine(QLineF(0, y, self._parent.size().width() + 30, y))
Пример #2
0
    def _generate_map_items(self, **kwargs):  # pylint: disable=unused-argument
        """
        Generate the feature map items (memory region blocks, separating lines, etc).
        """
        cfb = self.instance.cfb.am_obj
        if cfb is None:
            return

        for item in self._map_items:
            self.scene().removeItem(item)
        self._map_items.clear()
        self._calculate_memory_region_offsets()

        func_color = Conf.feature_map_color_regular_function
        data_color = Conf.feature_map_color_data
        unknown_color = Conf.feature_map_color_unknown
        delimiter_color = Conf.feature_map_color_delimiter
        offset = 0
        current_region = None

        for addr, obj in cfb.ceiling_items():
            if obj.size is None:
                continue

            # Are we in a new region?
            new_region = False
            if current_region is None or not current_region.addr <= addr < current_region.addr + current_region.size:
                try:
                    current_region_addr = next(
                        self._addr_to_region.irange(maximum=addr,
                                                    reverse=True))
                except StopIteration:
                    # FIXME: it's not within any of the known regions
                    # we should fix this in the future. for now, let's make sure it does not crash
                    continue
                current_region = self._addr_to_region[current_region_addr]
                new_region = True

            if new_region:
                r = self._get_region_rect(current_region)
                pos = r.topLeft().x()
                pen = QPen(delimiter_color)
                hpw = pen.width() / 2
                item = QGraphicsLineItem(pos,
                                         hpw,
                                         pos,
                                         self._height - hpw,
                                         parent=self)
                item.setPen(pen)
                item.setZValue(self.ZVALUE_SEPARATOR)
                self._map_items.append(item)

            # Clip item to possibly truncated region size
            adjusted_region_size = self._get_adjusted_region_size(
                current_region)
            adjusted_size = min(
                obj.size, current_region.addr + adjusted_region_size - addr)
            if adjusted_size <= 0:
                # Item falls outside truncated region. Drop the item.
                continue

            r = self._get_offset_size_rect(offset, adjusted_size)
            offset += adjusted_size

            if isinstance(obj, MemoryData):
                brush = QBrush(data_color)
            elif isinstance(obj, Block):
                # TODO: Check if it belongs to a function or not
                brush = QBrush(func_color)
            else:
                brush = QBrush(unknown_color)

            item = QGraphicsRectItem(r, parent=self)
            item.setPen(Qt.NoPen)
            item.setBrush(brush)
            self._map_items.append(item)