def adjust(self): """Adjust the item's geometry in response to changes.""" metrics = QFontMetricsF(self.font()) # Get bounding rectangle for full text self._bounding_rect = metrics.boundingRect(self.text()) # Constrain by maximum width if self.maximumWidth() is not None: self._bounding_rect.setWidth(self.maximumWidth()) # Compute elided text self._elided_text = metrics.elidedText(self.text(), self.elideMode(), self.boundingRect().width()) # Get bounding rectangle for elided text self._bounding_rect = metrics.boundingRect(self.elidedText()) # It seems that for small characters like "..." the bounding rect returned is too small. Adjust it by a small # value. metrics_correction = px(1 / 72) self._bounding_rect.adjust(-metrics_correction, 0, metrics_correction, 0) # Move origin point according to the alignment if self.alignMode() & Qt.AlignLeft: self._bounding_rect.moveLeft(0) elif self.alignMode() & Qt.AlignRight: self._bounding_rect.moveRight(0) else: self._bounding_rect.moveLeft(-self._bounding_rect.width() / 2) # Set background rect self.background.setRect(self.boundingRect())
def __init__(self, port: 'Port', parent: QGraphicsItem = None, menuEnabled: bool = True): """ Constructs a portGraphics object for the given Port object. :param port: The port for which this graphics item represents. :type port: Port :param parent: A QGraphicsItem (probably an actionGraphics object) :type parent: QGraphicsItem :param menuEnabled: If true, a context menu will be shown on right-click. :type menuEnabled: bool """ QGraphicsItem.__init__(self, parent) self.setAcceptDrops(True) self.setFlag(QGraphicsItem.ItemIsSelectable) self._port = port self._menuEnabled = menuEnabled self.menu = PortMenu() # If port is required and it's an input, make the border thicker if not self._port.isOptional() and self._port in self._port.getAction( ).getInputPorts(): self.borderWidth = PortGraphics.REQUIRED_PEN_WIDTH else: self.borderWidth = PortGraphics.OPTIONAL_PEN_WIDTH # show port name and type if self._menuEnabled: fm = QFontMetricsF(PortGraphics.NAME_FONT) name = fm.elidedText(self._port.getName(), Qt.ElideRight, PortGraphics.SIDE_HEIGHT) self.nameItem = QGraphicsTextItem(name, self) self.nameItem.setFont(PortGraphics.NAME_FONT) self.nameItem.setRotation(90) self.nameItem.setPos(PortGraphics.WIDTH / 2 + 5, -PortGraphics.TOTAL_HEIGHT / 2) fm = QFontMetricsF(PortGraphics.TYPE_FONT) t = fm.elidedText(self._port.getDataType().__name__, Qt.ElideRight, PortGraphics.SIDE_HEIGHT) self.typeItem = QGraphicsTextItem(t, self) self.typeItem.setFont(PortGraphics.TYPE_FONT) self.typeItem.setRotation(90) self.typeItem.setPos(5, -PortGraphics.TOTAL_HEIGHT / 2)
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, index: QWidget) -> None: """ Paint the graphics of the action wrapper including action name, number, and ports. :param painter: This draws the widget. :type painter: QPainter :param option: Option for the style of graphic. :type option: QStyleOptionGraphicsItem :param index: Index for the painted graphic. :type index: QWidget :return: None :rtype: NoneType """ ActionGraphics.paint(self, painter, option, index) # Get dimensions of the action x, y, width, height = self.getActionRect(self._action.getInputPorts(), self._action.getOutputPorts()) # Draw the number tag. number = str( self._action.getParent().getActions().index(self._action) + 1) offset = 5 radius = 15 size = ActionGraphics.H_SPACE / 2 - offset * 2 painter.setBrush(QColor(29, 110, 37)) painter.drawRoundedRect(QRectF(x + offset, y + offset, size, size), radius, radius) painter.setPen(ActionWrapperGraphics.TAG_TEXT_COLOR) painter.setBrush(ActionWrapperGraphics.TAG_TEXT_COLOR) painter.setFont(ActionWrapperGraphics.TAG_FONT) fm = QFontMetricsF(ActionWrapperGraphics.TAG_FONT) pixelsWide = fm.width(number) pixelsHigh = fm.height() # TODO: fix text positioning - font metrics aren't working well painter.drawText(x + offset + size / 2 - pixelsWide, y + offset + size / 2 + pixelsHigh / 2, number) # Draw the name of the action painter.setPen(ActionWrapperGraphics.NAME_TEXT_COLOR) painter.setBrush(ActionWrapperGraphics.NAME_TEXT_COLOR) painter.setFont(ActionWrapperGraphics.NAME_FONT) fm = QFontMetricsF(ActionWrapperGraphics.NAME_FONT) br = fm.boundingRect(self._action.getName()) # TODO: fix text positioning - font metrics aren't working well t = fm.elidedText(self._action.getName(), Qt.ElideRight, self._width - offset * 2) painter.drawText(x + offset, br.height(), t)
def paint(self, painter, option, widget): """ Paints the contents of the component. Override the parent paint function :param painter: Use a Qpainter object. :type painter: QPainter :param option: It provides style options for the item. :type option: QStyleOptionGraphicsItem :param widget: QWidget :type widget: It points to the widget that is being painted on; or make it = None. :return: None :rtype: NoneType """ boundingRect = self.boundingRect() if self.isRoot or boundingRect.width() == 0 and boundingRect.height( ) == 0: painter.setPen(QPen(QColor(Qt.transparent))) painter.setBrush(QColor(Qt.transparent)) return pen = QPen(QColor(100, 200, 255)) if self.isSelected(): pen.setStyle(Qt.DashDotLine) pen.setColor(QColor(255, 0, 0)) else: pen.setStyle(Qt.SolidLine) pen.setColor(QColor(0, 0, 0)) painter.setPen(pen) painter.setBrush(QColor(88, 183, 255)) id = self._dataComponent.getId() painter.drawRoundedRect(boundingRect, 5, 5) br = self.boundingRect(withMargins=False) # draw name label name = self.getLabel() # TODO: make a better algorithm on font size in the future # 44 width -> only cover 12 words with 5 -> 5Fonts one is 3.6 (added 5) # 48 width -> only cover 18 words with 4 -> 4Fonts one is 2.6 (added 5) if len(name) * 3.5 > self.boundingRect(withMargins=False).width(): if len(name) * 2.5 > self.boundingRect(withMargins=False).width(): nameFont = QFont("Times", 2) else: nameFont = QFont("Times", 4) else: nameFont = QFont("Times", 5) painter.setFont(nameFont) fm = QFontMetricsF(nameFont) name = fm.elidedText(name, Qt.ElideRight, br.width() - ComponentGraphics.TITLEBAR_H) painter.setBrush(QColor(100, 200, 255)) painter.drawText( self.boundingRect(withMargins=False).x() + 5, self._margin + 13, name) if sm.StateMachine.instance.configVars.showTokenTags: self.drawTokenTag(br, painter)