示例#1
0
文件: views.py 项目: herstky/DES
class ReadoutView(View):
    class Orientation(Enum):
        horizontal = 1
        vertical = 2

    def __init__(self, model):
        super().__init__(model)
        self.graphics_item = QGraphicsRectItem(0, 0, 9, 9)
        self.graphics_item.setBrush(Qt.black)

        inner_rect_item = QGraphicsRectItem(1, 1, 7, 7, self.graphics_item)
        inner_rect_item.setBrush(Qt.white)

        self.text_item = None
        self.orientation = ReadoutView.Orientation.horizontal

    def line(self):
        for child in self.graphics_item.childItems():
            if isinstance(child, QGraphicsLineItem):
                return child.line()

    def line_item(self):
        for child in self.graphics_item.childItems():
            if isinstance(child, QGraphicsLineItem):
                return child

    def init_text(self):
        width = self.graphics_item.boundingRect().width()
        height = self.graphics_item.boundingRect().height()
        self.text_item = QGraphicsTextItem(self.graphics_item)
        self.text_item.setPos(width / 2 + 2, -height)
示例#2
0
 def create_info_display(self, x, y, attributes):
     """
     Creates view elements for the info display
     :param x: x position of the node
     :param y: y position of the node
     :param attributes: attributes that will be displayed in the view
     :return:
     """
     start_height = y + (self.NODE_HEIGHT / 2)
     # unfold dictionary values at the bottom of the list
     sorted_attributes = []
     for k, v in sorted(attributes.items(), key=lambda tup: isinstance(tup[1], dict)):
         if isinstance(v, dict):
             sorted_attributes.append((k, v))
             sorted_attributes.extend(v.items())
         else:
             sorted_attributes.append((k, v))
     # create property rows
     for i, (k, v) in enumerate(sorted_attributes):
         value_text = None
         value_height = 0
         if isinstance(v, dict):
             # display dictionary key as title
             text = "{}".format(k)
             if len(text) > 20:
                 text = text[:20] + "..."
             key_text = QGraphicsSimpleTextItem(text)
             f = key_text.font()
             f.setBold(True)
             key_text.setFont(f)
             text_width = key_text.boundingRect().width()
         else:
             key_text = QGraphicsSimpleTextItem("{}:".format(k) if k else " ")
             text = str(v)
             if len(text) > 20:
                 text = text[:20] + "..."
             value_text = QGraphicsSimpleTextItem(text)
             value_height = value_text.boundingRect().height()
             text_width = key_text.boundingRect().width() + value_text.boundingRect().width()
         # create box around property
         attribute_container = QGraphicsRectItem(x, start_height, text_width + 10,
                                                 max(key_text.boundingRect().height(),
                                                     value_height) + 10)
         attribute_container.setBrush(QBrush(Qt.white))
         self.total_height += attribute_container.rect().height()
         key_text.setParentItem(attribute_container)
         if value_text:
             value_text.setParentItem(attribute_container)
         self.max_width = max(self.max_width, attribute_container.rect().width())
         attribute_container.setParentItem(self)
         self.info_display.append(attribute_container)
         start_height += max(key_text.boundingRect().height(), value_height) + 10
     # calculate correct coordinates for positioning of the attribute boxes
     if self.max_width > self.NODE_MIN_WIDTH - 10:
         x -= (self.max_width + 10) / 2
         y -= self.total_height / 2
         self.max_width += 10
     else:
         x -= self.NODE_MIN_WIDTH / 2
         y -= self.total_height / 2
         self.max_width = self.NODE_MIN_WIDTH
     h = 0
     # position all the elements previously created
     for attribute_container in self.info_display:
         rect: QRectF = attribute_container.rect()
         rect.setX(x)
         rect_height = rect.height()
         rect.setY(y + self.NODE_HEIGHT + h)
         rect.setHeight(rect_height)
         key_child = attribute_container.childItems()[0]
         if len(attribute_container.childItems()) == 2:
             key_child.setX(x + 5)
             value_child = attribute_container.childItems()[1]
             value_child.setX(x + self.max_width - value_child.boundingRect().width() - 5)
             value_child.setY(y + self.NODE_HEIGHT + h + 5)
         else:
             key_child.setX(x - key_child.boundingRect().width() / 2 + self.max_width / 2)
         key_child.setY(y + self.NODE_HEIGHT + h + 5)
         h += rect.height()
         rect.setWidth(self.max_width)
         attribute_container.setRect(rect)