Word wrapping to next line.
This commit is contained in:
@@ -73,27 +73,51 @@ class Fragment:
|
||||
def repaintEvent(self, painter: QPainter) -> int:
|
||||
painter.save()
|
||||
painter.setFont(self._font)
|
||||
painter.setPen(self._color)
|
||||
rect = QRect()
|
||||
rect.setLeft(self._position.x())
|
||||
rect.setTop(
|
||||
top = (
|
||||
self._position.y()
|
||||
+ painter.fontMetrics().descent()
|
||||
- painter.fontMetrics().height()
|
||||
)
|
||||
rect.setWidth(painter.viewport().width() - self._position.x())
|
||||
rect.setHeight(2000)
|
||||
left = self._position.x()
|
||||
width = painter.viewport().width() - left
|
||||
height = 2000
|
||||
rect = QRect(left, top, width, height)
|
||||
indent = self._indent * 30
|
||||
flags = (
|
||||
Qt.AlignmentFlag.AlignLeft
|
||||
| Qt.AlignmentFlag.AlignBaseline
|
||||
)
|
||||
boundingNoWrap = painter.fontMetrics().boundingRect(
|
||||
rect, flags|Qt.TextFlag.TextSingleLine, self._text
|
||||
)
|
||||
flags = (
|
||||
Qt.AlignmentFlag.AlignLeft
|
||||
| Qt.AlignmentFlag.AlignBaseline
|
||||
| Qt.TextFlag.TextWordWrap
|
||||
)
|
||||
bounding = painter.boundingRect(rect, flags, self._text)
|
||||
bounding = painter.fontMetrics().boundingRect(
|
||||
rect, flags|Qt.TextFlag.TextWordWrap, self._text
|
||||
)
|
||||
text = self._text
|
||||
remainingText = ''
|
||||
if boundingNoWrap.height() < bounding.height():
|
||||
#
|
||||
# We need to determine if the text caused a word wrap.
|
||||
# If it has wrapped. We need to locate that exact break point
|
||||
# This is not optimal, but it is only a few iterations
|
||||
#
|
||||
size = bounding.size()
|
||||
lastSpace = 0
|
||||
char = 0
|
||||
pos = rect.x()
|
||||
while pos < rect.right():
|
||||
if text[char] == ' ':
|
||||
lastSpace = char
|
||||
pos += painter.fontMetrics().horizontalAdvance(
|
||||
text[char]
|
||||
)
|
||||
char += 1
|
||||
remainingText = text[lastSpace:]
|
||||
text = text[:lastSpace]
|
||||
|
||||
size = boundingNoWrap.size()
|
||||
|
||||
painter.setPen(QColor("#f00"))
|
||||
if self._audio.isValid():
|
||||
@@ -104,7 +128,22 @@ class Fragment:
|
||||
end = bounding.bottomRight()
|
||||
painter.drawLine(start, end)
|
||||
painter.setPen(self._color)
|
||||
painter.drawText(rect, flags, self._text)
|
||||
painter.drawText(rect, flags, text)
|
||||
if remainingText != '':
|
||||
top += size.height()
|
||||
rect = QRect(
|
||||
indent, top,
|
||||
painter.viewport().width() - indent, height
|
||||
)
|
||||
painter.drawText(rect, flags|Qt.TextFlag.TextWordWrap, remainingText)
|
||||
size = size.grownBy(
|
||||
QMargins(
|
||||
0,0,0,
|
||||
painter.fontMetrics().boundingRect(
|
||||
rect, flags | Qt.TextFlag.TextWordWrap, remainingText
|
||||
).height()
|
||||
)
|
||||
)
|
||||
painter.restore()
|
||||
size = size.grownBy(self._margin)
|
||||
size = size.grownBy(self._border)
|
||||
|
||||
Reference in New Issue
Block a user