本文整理汇总了Python中AnyQt.QtWidgets.QGraphicsTextItem.document方法的典型用法代码示例。如果您正苦于以下问题:Python QGraphicsTextItem.document方法的具体用法?Python QGraphicsTextItem.document怎么用?Python QGraphicsTextItem.document使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AnyQt.QtWidgets.QGraphicsTextItem
的用法示例。
在下文中一共展示了QGraphicsTextItem.document方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GraphicsTextWidget
# 需要导入模块: from AnyQt.QtWidgets import QGraphicsTextItem [as 别名]
# 或者: from AnyQt.QtWidgets.QGraphicsTextItem import document [as 别名]
class GraphicsTextWidget(QGraphicsWidget):
def __init__(self, text, parent=None, **kwargs):
super().__init__(parent, **kwargs)
self.labelItem = QGraphicsTextItem(self)
self.setHtml(text)
self.labelItem.document().documentLayout().documentSizeChanged.connect(
self.onLayoutChanged
)
def setGeometry(self, rect):
self.prepareGeometryChange()
super().setGeometry(rect)
def onLayoutChanged(self, *args):
self.updateGeometry()
def sizeHint(self, which, constraint=QSizeF()):
if which == Qt.MinimumSize:
return self.labelItem.boundingRect().size()
else:
return self.labelItem.boundingRect().size()
def setTextWidth(self, width):
self.labelItem.setTextWidth(width)
def setHtml(self, text):
self.labelItem.setHtml(text)
示例2: VennIntersectionArea
# 需要导入模块: from AnyQt.QtWidgets import QGraphicsTextItem [as 别名]
# 或者: from AnyQt.QtWidgets.QGraphicsTextItem import document [as 别名]
class VennIntersectionArea(QGraphicsPathItem):
def __init__(self, parent=None, text=""):
super(QGraphicsPathItem, self).__init__(parent)
self.setAcceptHoverEvents(True)
self.setPen(QPen(Qt.NoPen))
self.text = QGraphicsTextItem(self)
layout = self.text.document().documentLayout()
layout.documentSizeChanged.connect(self._onLayoutChanged)
self._text = ""
self._anchor = QPointF()
def setText(self, text):
if self._text != text:
self._text = text
self.text.setPlainText(text)
def text(self):
return self._text
def setTextAnchor(self, pos):
if self._anchor != pos:
self._anchor = pos
self._updateTextAnchor()
def hoverEnterEvent(self, event):
self.setZValue(self.zValue() + 1)
return QGraphicsPathItem.hoverEnterEvent(self, event)
def hoverLeaveEvent(self, event):
self.setZValue(self.zValue() - 1)
return QGraphicsPathItem.hoverLeaveEvent(self, event)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
if event.modifiers() & Qt.AltModifier:
self.setSelected(False)
elif event.modifiers() & Qt.ControlModifier:
self.setSelected(not self.isSelected())
elif event.modifiers() & Qt.ShiftModifier:
self.setSelected(True)
else:
for area in self.parentWidget().vennareas():
area.setSelected(False)
self.setSelected(True)
def mouseReleaseEvent(self, event):
pass
def paint(self, painter, option, widget=None):
painter.save()
path = self.path()
brush = QBrush(self.brush())
pen = QPen(self.pen())
if option.state & QStyle.State_Selected:
pen.setColor(Qt.red)
brush.setStyle(Qt.DiagCrossPattern)
brush.setColor(QColor(40, 40, 40, 100))
elif option.state & QStyle.State_MouseOver:
pen.setColor(Qt.blue)
if option.state & QStyle.State_MouseOver:
brush.setColor(QColor(100, 100, 100, 100))
if brush.style() == Qt.NoBrush:
# Make sure the highlight is actually visible.
brush.setStyle(Qt.SolidPattern)
painter.setPen(pen)
painter.setBrush(brush)
painter.drawPath(path)
painter.restore()
def itemChange(self, change, value):
if change == QGraphicsPathItem.ItemSelectedHasChanged:
self.setZValue(self.zValue() + (1 if value else -1))
return QGraphicsPathItem.itemChange(self, change, value)
def _updateTextAnchor(self):
rect = self.text.boundingRect()
pos = anchor_rect(rect, self._anchor)
self.text.setPos(pos)
def _onLayoutChanged(self):
self._updateTextAnchor()
示例3: LinkItem
# 需要导入模块: from AnyQt.QtWidgets import QGraphicsTextItem [as 别名]
# 或者: from AnyQt.QtWidgets.QGraphicsTextItem import document [as 别名]
#.........这里部分代码省略.........
self.setHoverState(False)
self.curveItem.setPath(QPainterPath())
def __updateText(self):
self.prepareGeometryChange()
self.__boundingRect = None
if self.__sourceName or self.__sinkName:
if self.__sourceName != self.__sinkName:
text = ("<nobr>{0}</nobr> \u2192 <nobr>{1}</nobr>"
.format(escape(self.__sourceName),
escape(self.__sinkName)))
else:
# If the names are the same show only one.
# Is this right? If the sink has two input channels of the
# same type having the name on the link help elucidate
# the scheme.
text = escape(self.__sourceName)
else:
text = ""
self.linkTextItem.setHtml('<div align="center">{0}</div>'
.format(text))
path = self.curveItem.curvePath()
# Constrain the text width if it is too long to fit on a single line
# between the two ends
if not path.isEmpty():
# Use the distance between the start/end points as a measure of
# available space
diff = path.pointAtPercent(0.0) - path.pointAtPercent(1.0)
available_width = math.sqrt(diff.x() ** 2 + diff.y() ** 2)
# Get the ideal text width if it was unconstrained
doc = self.linkTextItem.document().clone(self)
doc.setTextWidth(-1)
idealwidth = doc.idealWidth()
doc.deleteLater()
# Constrain the text width but not below a certain min width
minwidth = 100
textwidth = max(minwidth, min(available_width, idealwidth))
self.linkTextItem.setTextWidth(textwidth)
else:
# Reset the fixed width
self.linkTextItem.setTextWidth(-1)
if not path.isEmpty():
center = path.pointAtPercent(0.5)
angle = path.angleAtPercent(0.5)
brect = self.linkTextItem.boundingRect()
transform = QTransform()
transform.translate(center.x(), center.y())
transform.rotate(-angle)
# Center and move above the curve path.
transform.translate(-brect.width() / 2, -brect.height())
self.linkTextItem.setTransform(transform)
def removeLink(self):
self.setSinkItem(None)
self.setSourceItem(None)
self.__updateCurve()