本文整理汇总了C++中QTextLayout::lineCount方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextLayout::lineCount方法的具体用法?C++ QTextLayout::lineCount怎么用?C++ QTextLayout::lineCount使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextLayout
的用法示例。
在下文中一共展示了QTextLayout::lineCount方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: noRunAroundFrame
void TestDocumentLayout::noRunAroundFrame()
{
// With this test we want to make sure a shape that is set to not run around
// will simply put the text further down.
initForNewTest(loremIpsum);
MockShape *picture = new MockShape();
KWFrame frame(picture, frameSet);
frame.setTextRunAround(KWord::NoRunAround);
picture->setSize(QSizeF(100, 100));
picture->setPosition(QPointF(0, 0));
MockLayoutState *state = new MockLayoutState(doc);
layout->setLayout(state);
state->shape = shape1;
layout->layout();
QTextLayout *lay = doc->begin().layout();
QVERIFY(lay->lineCount() >= 4);
QTextLine line = doc->begin().layout()->lineAt(0);
QVERIFY(line.isValid());
double preY = line.position().y();
int linenumber=1;
line = doc->begin().layout()->lineAt(linenumber);
while(linenumber < lay->lineCount()) {
qDebug() << line.position().y() << (preY + 14.4);
QVERIFY(line.position().y() > (preY + 14.4 - ROUNDING));
preY = line.position().y();
++linenumber;
line = doc->begin().layout()->lineAt(linenumber);
}
}
示例2: blockWidth
qreal TextDocumentLayout::blockWidth(const QTextBlock &block)
{
QTextLayout *layout = block.layout();
if (!layout->lineCount())
return 0; // only for layouted blocks
qreal blockWidth = 0;
for (int i = 0; i < layout->lineCount(); ++i) {
QTextLine line = layout->lineAt(i);
blockWidth = qMax(line.naturalTextWidth() + 8, blockWidth);
}
return blockWidth;
}
示例3: drawTextLayout
void TextLabel::drawTextLayout(QPainter *painter, const QTextLayout &layout, const QRect &rect)
{
if (rect.width() < 1 || rect.height() < 1) {
return;
}
QPixmap pixmap(rect.size());
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
p.setPen(painter->pen());
// Create the alpha gradient for the fade out effect
QLinearGradient alphaGradient(0, 0, 1, 0);
alphaGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
if (layout.textOption().textDirection() == Qt::LeftToRight) {
alphaGradient.setColorAt(0, QColor(0, 0, 0, 255));
alphaGradient.setColorAt(1, QColor(0, 0, 0, 0));
} else {
alphaGradient.setColorAt(0, QColor(0, 0, 0, 0));
alphaGradient.setColorAt(1, QColor(0, 0, 0, 255));
}
QFontMetrics fm(layout.font());
int textHeight = layout.lineCount() * fm.lineSpacing();
QPointF position(0, (rect.height() - textHeight) / 2);
QList<QRect> fadeRects;
int fadeWidth = 30;
// Draw each line in the layout
for (int i = 0; i < layout.lineCount(); i++) {
QTextLine line = layout.lineAt(i);
line.draw(&p, position);
// Add a fade out rect to the list if the line is too long
if (line.naturalTextWidth() > rect.width())
{
int x = int(qMin(line.naturalTextWidth(), (qreal)pixmap.width())) - fadeWidth;
int y = int(line.position().y() + position.y());
QRect r = QStyle::visualRect(layout.textOption().textDirection(), pixmap.rect(),
QRect(x, y, fadeWidth, int(line.height())));
fadeRects.append(r);
}
}
// Reduce the alpha in each fade out rect using the alpha gradient
if (!fadeRects.isEmpty()) {
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
foreach (const QRect &rect, fadeRects) {
p.fillRect(rect, alphaGradient);
}
示例4: cursor
void TestDocumentLayout::placeAnchoredFrame3()
{
// basic inline frame that acts like a really big character
initForNewTest(QString(loremIpsum));
MockShape *picture = new MockShape();
picture->setSize(QSizeF(100, 100));
KTextAnchor *anchor = new KTextAnchor(picture);
anchor->setAlignment(KTextAnchor::VerticalOffset);
anchor->setAlignment(KTextAnchor::HorizontalOffset);
QTextCursor cursor(doc);
KInlineTextObjectManager *manager = new KInlineTextObjectManager();
layout->setInlineTextObjectManager(manager);
MockLayoutState *state = new MockLayoutState(doc);
layout->setLayout(state);
state->shape = shape1;
manager->insertInlineObject(cursor, anchor);
layout->layout();
/*
I have two goals with 'offset'.
One is that I want to be able to change the baseline of my anchored object.
The other is that OOo / ODF allows me to have an arbitairy distance from my anchor
so I can place something at the center of my page or whatever.
So what about I switch from the first to the latter based on the font height.
If my offset 'x' != 0, make the image floating.
If my offset 'y' is such that it would be above or below my line; make floating.
*/
QTextLayout *lay = doc->begin().layout();
QVERIFY(lay->lineCount() >= 2);
QTextLine line = lay->lineAt(0);
QCOMPARE(line.descent(), (qreal) 100);
QCOMPARE(line.position(), QPointF());
line = lay->lineAt(1);
QVERIFY(line.height() < 20);
// now move the character which makes it a shape to run around and no longer
// a big character.
anchor->setOffset(QPointF(50, 20));
layout->layout();
lay = doc->begin().layout();
QVERIFY(lay->lineCount() >= 2);
line = lay->lineAt(0);
QVERIFY(line.height() < 20);
QCOMPARE(line.position(), QPointF());
line = lay->lineAt(1);
QVERIFY(line.height() < 20);
QCOMPARE(line.position().x(), 0.);
QVERIFY(qAbs(line.position().y() - 14.4) < 0.125);
}
示例5: layoutText
void YTDelegate::layoutText(QTextLayout& textLayout, QString text, QSize constraint) const
{
QTextOption textOption(Qt::AlignJustify);
textLayout.setTextOption(textOption);
textLayout.setText(text);
textLayout.beginLayout();
int lHeight = 0;
while(true){
QTextLine line = textLayout.createLine();
if(!line.isValid())
break;
line.setLineWidth(constraint.width());
line.setPosition(QPointF(0, lHeight));
if(lHeight + line.height() > constraint.height())
{
QTextLine lastLine = textLayout.lineAt(textLayout.lineCount() - 2);
QString lastString = text.mid(lastLine.textStart());
QFontMetrics fm(textLayout.font());
text.chop(lastString.length());
text += fm.elidedText(lastString, Qt::ElideRight, constraint.width()-1);
textLayout.endLayout();
layoutText(textLayout, text, constraint);
return;
}
lHeight += line.height();
lHeight += line.leading();
}
textLayout.endLayout();
}
示例6: testTabsUsed
void tst_QTextFormat::testTabsUsed()
{
QTextDocument doc;
QTextCursor cursor(&doc);
QList<QTextOption::Tab> tabs;
QTextBlockFormat format;
QTextOption::Tab tab;
tab.position = 100;
tabs.append(tab);
format.setTabPositions(tabs);
cursor.mergeBlockFormat(format);
cursor.insertText("foo\tbar");
//doc.setPageSize(QSizeF(200, 200));
doc.documentLayout()->pageCount(); // force layout;
QTextBlock block = doc.begin();
QTextLayout *layout = block.layout();
QVERIFY(layout);
QCOMPARE(layout->lineCount(), 1);
QTextLine line = layout->lineAt(0);
QCOMPARE(line.cursorToX(4), 100.);
QTextOption option = layout->textOption();
QCOMPARE(option.tabs().count(), tabs.count());
}
示例7: position
QList<QGlyphRun> QTextFragment::glyphRuns(int pos, int len) const
{
if (!p || !n)
return QList<QGlyphRun>();
int blockNode = p->blockMap().findNode(position());
const QTextBlockData *blockData = p->blockMap().fragment(blockNode);
QTextLayout *layout = blockData->layout;
int blockPosition = p->blockMap().position(blockNode);
if (pos < 0)
pos = position() - blockPosition;
if (len < 0)
len = length();
if (len == 0)
return QList<QGlyphRun>();
QList<QGlyphRun> ret;
for (int i=0; i<layout->lineCount(); ++i) {
QTextLine textLine = layout->lineAt(i);
ret += textLine.glyphRuns(pos, len);
}
return ret;
}
示例8: 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();
}
示例9: hitTestIterated
int KTextDocumentLayout::hitTestIterated(QTextFrame::iterator begin, QTextFrame::iterator end, const QPointF &point, Qt::HitTestAccuracy accuracy) const
{
int position = -1;
QTextFrame::iterator it = begin;
for (it = begin; it != end; ++it) {
QTextBlock block = it.currentBlock();
QTextTable *table = qobject_cast<QTextTable*>(it.currentFrame());
QTextFrame *subFrame = it.currentFrame();
if (table) {
QTextTableCell cell = m_state->hitTestTable(table, point);
if (cell.isValid()) {
position = hitTestIterated(cell.begin(), cell.end(), point,
accuracy);
if (position == -1)
position = cell.lastPosition();
return position;
}
continue;
} else if (subFrame) {
position = hitTestIterated(subFrame->begin(), subFrame->end(), point, accuracy);
if (position != -1)
return position;
continue;
} else {
if (!block.isValid())
continue;
}
// kDebug(32500) <<"hitTest[" << point.x() <<"," << point.y() <<"]";
QTextLayout *layout = block.layout();
if (point.y() > layout->boundingRect().bottom()) {
// just skip this block. position = block.position() + block.length() - 1;
continue;
}
for (int i = 0; i < layout->lineCount(); i++) {
QTextLine line = layout->lineAt(i);
// kDebug(32500) <<" + line[" << line.textStart() <<"]:" << line.y() <<"-" << line.height();
if (point.y() > line.y() + line.height()) {
position = line.textStart() + line.textLength();
continue;
}
if (accuracy == Qt::ExactHit && point.y() < line.y()) // between lines
return -1;
if (accuracy == Qt::ExactHit && // left or right of line
(point.x() < line.x() || point.x() > line.x() + line.width()))
return -1;
if (point.x() > line.width() && layout->textOption().textDirection() == Qt::RightToLeft) {
// totally right of RTL text means the position is the start of the text.
return block.position() + line.textStart();
}
return block.position() + line.xToCursor(point.x());
}
}
return -1;
}
示例10: lineBreak
ParagraphFragment::ParagraphFragment(KShape *shape, const QTextBlock &textBlock, KParagraphStyle *style)
: m_shape(shape)
{
QTextLayout *layout = textBlock.layout();
m_isSingleLine = (layout->lineCount() == 1);
// border rectangle left and right
m_border.setLeft(0.0);
m_border.setRight(shape->size().width());
// first line rectangle
m_firstLine = layout->lineAt(0).rect();
m_firstLine.setRight(m_border.right() - style->rightMargin());
// counter rectangle
KTextBlockData *blockData = static_cast<KTextBlockData*>(textBlock.userData());
if (blockData != NULL) {
m_counter = QRectF(blockData->counterPosition(), QSizeF(blockData->counterWidth() - blockData->counterSpacing(), m_firstLine.height()));
}
// following lines rectangle
if (!m_isSingleLine) {
m_followingLines = QRectF(layout->lineAt(1).rect().topLeft(), layout->lineAt(layout->lineCount() - 1).rect().bottomRight());
} else {
m_followingLines = m_firstLine;
}
// border rectangle top and bottom
m_border.setTop(m_firstLine.top() - style->topMargin());
m_border.setBottom(m_isSingleLine ? m_firstLine.bottom() + style->bottomMargin() : m_followingLines.bottom() + style->bottomMargin());
// TODO: the lines overlap slightly so right now we simply
// calculate the mean of the two y-values, should be handled properly
if (!m_isSingleLine) {
qreal lineBreak((m_firstLine.bottom() + m_followingLines.top()) / 2.0);
m_firstLine.setBottom(lineBreak);
m_counter.setBottom(lineBreak);
m_followingLines.setTop(lineBreak);
}
}
示例11: testColumnWidthUndefined
void TestTableLayout::testColumnWidthUndefined()
{
setupTest("","","","","","");
m_layout->layout();
QCOMPARE(m_table->columns(), 3);
QCOMPARE(m_table->rows(), 3);
QVERIFY(qAbs(m_block.layout()->lineAt(0).width() - 200.0) < ROUNDING);
QTextLayout *lay = bottomLeftCellBlock().layout();
QVERIFY(lay);
QCOMPARE(lay->lineCount(), 1);
QVERIFY(qAbs(lay->lineAt(0).width() - 200.0/3) < ROUNDING);
lay = bottomMidCellBlock().layout();
QVERIFY(lay);
QCOMPARE(lay->lineCount(), 1);
QVERIFY(qAbs(lay->lineAt(0).width() - 200.0/3) < ROUNDING);
lay = bottomRightCellBlock().layout();
QVERIFY(lay);
QCOMPARE(lay->lineCount(), 1);
QVERIFY(qAbs(lay->lineAt(0).width() - 200.0/3) < ROUNDING);
}
示例12: blockBoundingRect
QRectF TextDocumentLayout::blockBoundingRect(const QTextBlock &block) const
{
if (!block.isValid()) { return QRectF(); }
QTextLayout *tl = block.layout();
if (!tl->lineCount())
const_cast<TextDocumentLayout*>(this)->layoutBlock(block);
QRectF br;
if (block.isVisible()) {
br = QRectF(QPointF(0, 0), tl->boundingRect().bottomRight());
if (tl->lineCount() == 1)
br.setWidth(qMax(br.width(), tl->lineAt(0).naturalTextWidth()));
qreal margin = document()->documentMargin();
br.adjust(0, 0, margin, 0);
if (!block.next().isValid())
br.adjust(0, 0, 0, margin);
}
return br;
}
示例13: testBasicList
void TestDocumentLayout::testBasicList()
{
initForNewTest("Base\nListItem\nListItem2: The quick brown fox jums over the lazy dog.\nNormal\nNormal");
KoParagraphStyle style;
QTextBlock block = m_doc->begin();
style.applyStyle(block);
block = block.next();
QVERIFY(block.isValid());
KoListStyle listStyle;
KoListLevelProperties level1;
level1.setStyle(KoListStyle::Bullet);
listStyle.setLevelProperties(level1);
style.setListStyle(&listStyle);
style.applyStyle(block); // make this a listStyle
QVERIFY(block.textList());
QCOMPARE(block.textList()->format().intProperty(QTextListFormat::ListStyle), (int) KoListStyle::Bullet);
block = block.next();
QVERIFY(block.isValid());
style.applyStyle(block); // make this a listStyle
m_layout->layout();
QTextLayout *blockLayout = m_block.layout();
QCOMPARE(blockLayout->lineAt(0).x(), 0.0);
block = m_doc->begin().next();
QVERIFY(block.isValid());
blockLayout = block.layout(); // parag 2
KoTextBlockData *data = dynamic_cast<KoTextBlockData *>(block.userData());
QVERIFY(data);
qreal counterSpacing = data->counterSpacing();
QVERIFY(counterSpacing > 0.);
// 12 is hardcoded to be the width of a discitem (taken from the default font):
QCOMPARE(blockLayout->lineAt(0).x(), 12.0 + counterSpacing);
block = block.next();
QVERIFY(block.isValid());
blockLayout = block.layout(); // parag 3
QCOMPARE(blockLayout->lineAt(0).x(), 12.0 + counterSpacing);
QVERIFY(blockLayout->lineCount() > 1);
QCOMPARE(blockLayout->lineAt(1).x(), 12.0 + counterSpacing); // make sure not only the first line is indented
block = block.next();
QVERIFY(block.isValid());
blockLayout = block.layout(); // parag 4
QCOMPARE(blockLayout->lineAt(0).x(), 0.0);
}
示例14: 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;
}
示例15: 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()) {
//.........这里部分代码省略.........