本文整理汇总了C++中QTextLine::naturalTextRect方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextLine::naturalTextRect方法的具体用法?C++ QTextLine::naturalTextRect怎么用?C++ QTextLine::naturalTextRect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextLine
的用法示例。
在下文中一共展示了QTextLine::naturalTextRect方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: placeAnchoredFrame
void TestDocumentLayout::placeAnchoredFrame()
{
initForNewTest(QString());
MockShape *picture = new MockShape();
picture->setSize(QSizeF(100, 100));
KTextAnchor *anchor = new KTextAnchor(picture);
anchor->setOffset(QPointF(23, 45));
QTextCursor cursor(doc);
KInlineTextObjectManager *manager = new KInlineTextObjectManager();
layout->setInlineTextObjectManager(manager);
MockLayoutState *state = new MockLayoutState(doc);
layout->setLayout(state);
state->shape = shape1;
QCOMPARE(doc->begin().text().length(), 0);
manager->insertInlineObject(cursor, anchor);
QCOMPARE(doc->begin().text().length(), 1);
QCOMPARE(cursor.position(), 1);
shape1->setPosition(QPointF(300, 300));
layout->layout();
QCOMPARE(picture->parent(), shape1);
QCOMPARE(picture->position(), QPointF(23, 59.4));
cursor.setPosition(0);
cursor.insertText("foo"); // moves my anchors slightly to the right/down and gives line height
layout->layout();
QCOMPARE(picture->parent(), shape1);
QPointF newPos = picture->position();
QVERIFY(newPos.x() > 23);
QVERIFY(newPos.y() > 45); // it adds the baseline now
cursor.movePosition(QTextCursor::End);
cursor.insertText("\nNew Line\nAnd another");
layout->layout();
QCOMPARE(picture->position(), newPos);
QTextLayout *firstLineLayout = doc->begin().layout();
QTextOption option = firstLineLayout->textOption();
option.setAlignment(Qt::AlignHCenter);
firstLineLayout->setTextOption(option);
layout->layout();
QTextLine first = doc->begin().layout()->lineAt(0);
QVERIFY(first.isValid());
QVERIFY(first.naturalTextRect().x() > 10);
newPos.setX(newPos.x() + first.naturalTextRect().x()); // text is moved due to alignment
QCOMPARE(picture->position(), newPos);
anchor->setOffset(QPointF());
anchor->setAlignment(KTextAnchor::Left);
anchor->setAlignment(KTextAnchor::TopOfParagraph);
layout->layout();
// image is 100 wide, now centered in a parent of 200 so X = 50
QCOMPARE(picture->position(), QPointF(50, 0));
}
示例2: setContent
void setContent(const ToolTipContent &data)
{
QString html;
if (!data.mainText().isEmpty()) {
html.append("<div><b>" + data.mainText() + "</b></div>");
}
html.append(data.subText());
m_anchor.clear();
m_document->clear();
data.registerResources(m_document);
if (!html.isEmpty()) {
m_document->setHtml("<p>" + html + "</p>");
}
m_document->adjustSize();
m_haloRects.clear();
QTextLayout *layout = m_document->begin().layout();
//layout->setPosition(QPointF(textRect.x(), textBoundingRect->y()));
QTextLine line;
for (int i = 0; i < layout->lineCount(); ++i) {
line = layout->lineAt(i);
// Add halo rect only when a non empty line is found
if (line.naturalTextWidth()) {
m_haloRects.append(line.naturalTextRect().translated(layout->position().toPoint()).toRect().translated(m_margin, m_margin));
}
}
update();
}
示例3: shape
QPainterPath Text::shape() const
{
QPainterPath pp;
#if 0
for (QTextBlock tb = doc()->begin(); tb.isValid(); tb = tb.next()) {
QTextLayout* tl = tb.layout();
int n = tl->lineCount();
for (int i = 0; i < n; ++i) {
QTextLine l = tl->lineAt(i);
QRectF r(l.naturalTextRect().translated(tl->position()));
r.adjust(-l.position().x(), 0.0, 0.0, 0.0);
pp.addRect(r);
}
}
#endif
return pp;
}
示例4: addTextBlock
void QQuickTextNodeEngine::addTextBlock(QTextDocument *textDocument, const QTextBlock &block, const QPointF &position, const QColor &textColor, const QColor &anchorColor, int selectionStart, int selectionEnd)
{
Q_ASSERT(textDocument);
#ifndef QT_NO_IM
int preeditLength = block.isValid() ? block.layout()->preeditAreaText().length() : 0;
int preeditPosition = block.isValid() ? block.layout()->preeditAreaPosition() : -1;
#endif
QVarLengthArray<QTextLayout::FormatRange> colorChanges;
mergeFormats(block.layout(), &colorChanges);
QPointF blockPosition = textDocument->documentLayout()->blockBoundingRect(block).topLeft() + position;
if (QTextList *textList = block.textList()) {
QPointF pos = blockPosition;
QTextLayout *layout = block.layout();
if (layout->lineCount() > 0) {
QTextLine firstLine = layout->lineAt(0);
Q_ASSERT(firstLine.isValid());
setCurrentLine(firstLine);
QRectF textRect = firstLine.naturalTextRect();
pos += textRect.topLeft();
if (block.textDirection() == Qt::RightToLeft)
pos.rx() += textRect.width();
const QTextCharFormat charFormat = block.charFormat();
QFont font(charFormat.font());
QFontMetricsF fontMetrics(font);
QTextListFormat listFormat = textList->format();
QString listItemBullet;
switch (listFormat.style()) {
case QTextListFormat::ListCircle:
listItemBullet = QChar(0x25E6); // White bullet
break;
case QTextListFormat::ListSquare:
listItemBullet = QChar(0x25AA); // Black small square
break;
case QTextListFormat::ListDecimal:
case QTextListFormat::ListLowerAlpha:
case QTextListFormat::ListUpperAlpha:
case QTextListFormat::ListLowerRoman:
case QTextListFormat::ListUpperRoman:
listItemBullet = textList->itemText(block);
break;
default:
listItemBullet = QChar(0x2022); // Black bullet
break;
};
QSizeF size(fontMetrics.width(listItemBullet), fontMetrics.height());
qreal xoff = fontMetrics.width(QLatin1Char(' '));
if (block.textDirection() == Qt::LeftToRight)
xoff = -xoff - size.width();
setPosition(pos + QPointF(xoff, 0));
QTextLayout layout;
layout.setFont(font);
layout.setText(listItemBullet); // Bullet
layout.beginLayout();
QTextLine line = layout.createLine();
line.setPosition(QPointF(0, 0));
layout.endLayout();
QList<QGlyphRun> glyphRuns = layout.glyphRuns();
for (int i=0; i<glyphRuns.size(); ++i)
addUnselectedGlyphs(glyphRuns.at(i));
}
}
int textPos = block.position();
QTextBlock::iterator blockIterator = block.begin();
while (!blockIterator.atEnd()) {
QTextFragment fragment = blockIterator.fragment();
QString text = fragment.text();
if (text.isEmpty())
continue;
QTextCharFormat charFormat = fragment.charFormat();
QFont font(charFormat.font());
QFontMetricsF fontMetrics(font);
int fontHeight = fontMetrics.descent() + fontMetrics.ascent();
int valign = charFormat.verticalAlignment();
if (valign == QTextCharFormat::AlignSuperScript)
setPosition(QPointF(blockPosition.x(), blockPosition.y() - fontHeight / 2));
else if (valign == QTextCharFormat::AlignSubScript)
setPosition(QPointF(blockPosition.x(), blockPosition.y() + fontHeight / 6));
else
setPosition(blockPosition);
if (text.contains(QChar::ObjectReplacementCharacter)) {
QTextFrame *frame = qobject_cast<QTextFrame *>(textDocument->objectForFormat(charFormat));
if (frame && frame->frameFormat().position() == QTextFrameFormat::InFlow) {
int blockRelativePosition = textPos - block.position();
QTextLine line = block.layout()->lineForTextPosition(blockRelativePosition);
if (!currentLine().isValid()
|| line.lineNumber() != currentLine().lineNumber()) {
//.........这里部分代码省略.........
示例5: createSelectionPath
QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, const QTextCursor &end,
const QRect &clip)
{
if (begin.isNull() || end.isNull() || begin.position() > end.position())
return QPainterPath();
QPointF offset = m_editor->contentOffset();
QRect viewportRect = rect();
QTextDocument *document = m_editor->document();
if (m_editor->blockBoundingGeometry(begin.block()).translated(offset).top() > clip.bottom() + 10
|| m_editor->blockBoundingGeometry(end.block()).translated(offset).bottom() < clip.top() - 10
)
return QPainterPath(); // nothing of the selection is visible
QTextBlock block = begin.block();
if (block.blockNumber() < m_editor->firstVisibleBlock().blockNumber() - 4)
block = m_editor->document()->findBlockByNumber(m_editor->firstVisibleBlock().blockNumber() - 4);
bool inSelection = false;
QVector<QRectF> selection;
if (begin.position() == end.position()) {
// special case empty selections
const QRectF blockGeometry = m_editor->blockBoundingGeometry(block);
QTextLayout *blockLayout = block.layout();
int pos = begin.position() - begin.block().position();
QTextLine line = blockLayout->lineForTextPosition(pos);
QRectF lineRect = line.naturalTextRect();
int x = line.cursorToX(pos);
lineRect.setLeft(x - m_borderWidth);
lineRect.setRight(x + m_borderWidth);
selection += lineRect.translated(blockGeometry.topLeft());
} else {
for (; block.isValid() && block.blockNumber() <= end.blockNumber(); block = block.next()) {
if (! block.isVisible())
continue;
const QRectF blockGeometry = m_editor->blockBoundingGeometry(block);
QTextLayout *blockLayout = block.layout();
QTextLine line = blockLayout->lineAt(0);
bool firstOrLastBlock = false;
int beginChar = 0;
if (!inSelection) {
if (block == begin.block()) {
beginChar = begin.positionInBlock();
line = blockLayout->lineForTextPosition(beginChar);
firstOrLastBlock = true;
}
inSelection = true;
} else {
// while (beginChar < block.length() && document->characterAt(block.position() + beginChar).isSpace())
// ++beginChar;
// if (beginChar == block.length())
// beginChar = 0;
}
int lastLine = blockLayout->lineCount()-1;
int endChar = -1;
if (block == end.block()) {
endChar = end.positionInBlock();
lastLine = blockLayout->lineForTextPosition(endChar).lineNumber();
inSelection = false;
firstOrLastBlock = true;
} else {
endChar = block.length();
while (endChar > beginChar && document->characterAt(block.position() + endChar - 1).isSpace())
--endChar;
}
QRectF lineRect = line.naturalTextRect();
if (beginChar < endChar) {
lineRect.setLeft(line.cursorToX(beginChar));
if (line.lineNumber() == lastLine)
lineRect.setRight(line.cursorToX(endChar));
selection += lineRect.translated(blockGeometry.topLeft());
for (int lineIndex = line.lineNumber()+1; lineIndex <= lastLine; ++lineIndex) {
line = blockLayout->lineAt(lineIndex);
lineRect = line.naturalTextRect();
if (lineIndex == lastLine)
lineRect.setRight(line.cursorToX(endChar));
selection += lineRect.translated(blockGeometry.topLeft());
}
} else { // empty lines
const int emptyLineSelectionSize = 16;
if (!firstOrLastBlock && !selection.isEmpty()) { // middle
lineRect.setLeft(selection.last().left());
} else if (inSelection) { // first line
lineRect.setLeft(line.cursorToX(beginChar));
} else { // last line
if (endChar == 0)
break;
lineRect.setLeft(line.cursorToX(endChar) - emptyLineSelectionSize);
}
//.........这里部分代码省略.........