class NodeTitle(QtWidgets.QGraphicsWidget): __color = QtGui.QColor(25, 25, 25) __font = QtGui.QFont('Decorative', 14) __font.setLetterSpacing(QtGui.QFont.PercentageSpacing, 115) __labelBottomSpacing = 12 def __init__(self, text, parent=None): super(NodeTitle, self).__init__(parent) self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)) self.__textItem = QtWidgets.QGraphicsTextItem(text, self) self.__textItem.setDefaultTextColor(self.__color) self.__textItem.setFont(self.__font) self.__textItem.setPos(0, -2) option = self.__textItem.document().defaultTextOption() option.setWrapMode(QtGui.QTextOption.NoWrap) self.__textItem.document().setDefaultTextOption(option) self.__textItem.adjustSize() self.setPreferredSize(self.textSize()) def setText(self, text): self.__textItem.setPlainText(text) self.__textItem.adjustSize() self.setPreferredSize(self.textSize()) def textSize(self): return QtCore.QSizeF( self.__textItem.textWidth(), self.__font.pointSizeF() + self.__labelBottomSpacing )
class KBackdropTitle(QtWidgets.QGraphicsWidget): __color = QtGui.QColor(255, 255, 255) __font = QtGui.QFont('Helvetica', 11) __font.setLetterSpacing(QtGui.QFont.PercentageSpacing, 120) __fontMetrics = QtGui.QFontMetrics(__font) __labelBottomSpacing = 4 def __init__(self, text, parent=None): super(KBackdropTitle, self).__init__(parent) self.parentWidget = parent self.__textItem = QtWidgets.QGraphicsTextItem(text, self) self.__textItem.setDefaultTextColor(self.__color) self.__textItem.setFont(self.__font) self.__textItem.setPos(0, 1) option = self.__textItem.document().defaultTextOption() self.__textItem.document().setDefaultTextOption(option) self.__textItem.adjustSize() self.__textItem.setTextWidth(120) self.setPreferredSize(self.textSize()) def setText(self, text): self.__textItem.setPlainText(text) # self.__textItem.adjustSize() self.nodeResized(self.parentWidget.parentWidget.size().width()) def setTextColor(self, color): self.__color = color self.update() def textSize(self): return QtCore.QSizeF(self.__textItem.textWidth(), self.textHeight()) def textHeight(self): return self.__textItem.document().documentLayout().documentSize( ).height() + self.__labelBottomSpacing def nodeResized(self, width=None): fmWidth = self.__fontMetrics.width(self.__textItem.toPlainText()) newWidth = min(fmWidth, width) if width > fmWidth: newWidth = width self.__textItem.setTextWidth(newWidth) self.setPreferredSize(newWidth, self.textHeight()) def getBackdropWidget(self): return self.parent().parent()
class PortLabel(QtWidgets.QGraphicsWidget): __font = QtGui.QFont('Decorative', 12) __fontMetrics = QtGui.QFontMetrics(__font) def __init__(self, port, text, hOffset, color, highlightColor): super(PortLabel, self).__init__(port) self.__port = port self.__text = text self.__textItem = QtWidgets.QGraphicsTextItem(text, self) self._labelColor = color self.__highlightColor = highlightColor self.__textItem.setDefaultTextColor(self._labelColor) self.__textItem.setFont(self.__font) option = self.__textItem.document().defaultTextOption() option.setWrapMode(QtGui.QTextOption.NoWrap) self.__textItem.document().setDefaultTextOption(option) self.__textItem.document().setDocumentMargin(0) self.__textItem.adjustSize() self.__textItem.transform().translate( 0, self.__fontMetrics.height() * -0.5) self.setPreferredSize(self.textSize()) self.setSizePolicy( QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)) self.setWindowFrameMargins(0, 0, 0, 0) self.setHOffset(hOffset) self.setAcceptHoverEvents(True) self.__mousDownPos = None def text(self): return self.__text def setHOffset(self, hOffset): self.transform().translate(hOffset, 0) def setColor(self, color): self.__textItem.setDefaultTextColor(color) self.update() def textSize(self): return QtCore.QSizeF(self.__fontMetrics.width(self.__text), self.__fontMetrics.height()) def getPort(self): return self.__port def highlight(self): self.setColor(self.__highlightColor) def unhighlight(self): self.setColor(self._labelColor) def hoverEnterEvent(self, event): self.highlight() super(PortLabel, self).hoverEnterEvent(event) def hoverLeaveEvent(self, event): self.unhighlight() super(PortLabel, self).hoverLeaveEvent(event) def mousePressEvent(self, event): self.__mousDownPos = self.mapToScene(event.pos()) def mouseMoveEvent(self, event): self.unhighlight() scenePos = self.mapToScene(event.pos()) # When clicking on an UI port label, it is ambigous which connection point should be activated. # We let the user drag the mouse in either direction to select the conneciton point to activate. delta = scenePos - self.__mousDownPos if delta.x() < 0: if self.__port.inCircle() is not None: self.__port.inCircle().mousePressEvent(event) else: if self.__port.outCircle() is not None: self.__port.outCircle().mousePressEvent(event)