本文整理汇总了C++中QTextBlock::layout方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextBlock::layout方法的具体用法?C++ QTextBlock::layout怎么用?C++ QTextBlock::layout使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextBlock
的用法示例。
在下文中一共展示了QTextBlock::layout方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: qMax
void KTextDocumentLayout::Private::adjustSize()
{
if (parent->resizeMethod() == KTextDocument::NoResize)
return;
if (parent->shapes().isEmpty())
return;
// Limit auto-resizing to the first shape only (there won't be more
// with auto-resizing turned on, unless specifically set)
KShape *shape = parent->shapes().first();
// Determine the maximum width of all text lines
qreal width = 0;
for (QTextBlock block = parent->document()->begin(); block.isValid(); block = block.next()) {
// The block layout's wrap mode must be QTextOption::NoWrap, thus the line count
// of a valid block must be 1 (otherwise this resizing scheme wouldn't work)
Q_ASSERT(block.layout()->lineCount() == 1);
QTextLine line = block.layout()->lineAt(0);
width = qMax(width, line.naturalTextWidth());
}
// Use position and height of last text line to calculate height
QTextLine line = parent->document()->lastBlock().layout()->lineAt(0);
qreal height = line.position().y() + line.height();
shape->setSize(QSizeF(width, height));
}
示例2: calculateBoundingRect
static void calculateBoundingRect( QTextDocument *document, int startPosition, int endPosition,
QRectF &rect )
{
const QTextBlock startBlock = document->findBlock( startPosition );
const QRectF startBoundingRect = document->documentLayout()->blockBoundingRect( startBlock );
const QTextBlock endBlock = document->findBlock( endPosition );
const QRectF endBoundingRect = document->documentLayout()->blockBoundingRect( endBlock );
QTextLayout *startLayout = startBlock.layout();
QTextLayout *endLayout = endBlock.layout();
int startPos = startPosition - startBlock.position();
int endPos = endPosition - endBlock.position();
const QTextLine startLine = startLayout->lineForTextPosition( startPos );
const QTextLine endLine = endLayout->lineForTextPosition( endPos );
double x = startBoundingRect.x() + startLine.cursorToX( startPos );
double y = startBoundingRect.y() + startLine.y();
double r = endBoundingRect.x() + endLine.cursorToX( endPos );
double b = endBoundingRect.y() + endLine.y() + endLine.height();
const QSizeF size = document->size();
rect = QRectF( x / size.width(), y / size.height(),
(r - x) / size.width(), (b - y) / size.height() );
}
示例3: testCenteredItems
void TestDocumentLayout::testCenteredItems()
{
initForNewTest("ListItem\nListItem\nListItem");
KoListStyle listStyle;
KoListLevelProperties llp;
llp.setStyle(KoListStyle::DecimalItem);
listStyle.setLevelProperties(llp);
QTextBlock block = m_doc->begin(); // normal block
QVERIFY(block.isValid());
listStyle.applyStyle(block);
block = block.next(); // centered block
QVERIFY(block.isValid());
listStyle.applyStyle(block);
QTextBlockFormat fmt;
fmt.setAlignment(Qt::AlignHCenter);
QTextCursor cursor(block);
cursor.mergeBlockFormat(fmt);
block = block.next(); // centered RTL text.
listStyle.applyStyle(block);
cursor = QTextCursor(block);
fmt.setProperty(KoParagraphStyle::TextProgressionDirection, KoText::RightLeftTopBottom);
cursor.mergeBlockFormat(fmt);
m_layout->layout();
block = m_doc->begin();
QTextLayout *layout = block.layout();
QTextLine line1 = layout->lineAt(0);
KoTextBlockData *data1 = dynamic_cast<KoTextBlockData *>(block.userData());
QVERIFY(line1.isValid());
QVERIFY(line1.width() < 200); // the counter takes some space.
block = block.next();
layout = block.layout();
QTextLine line2 = layout->lineAt(0);
KoTextBlockData *data2 = dynamic_cast<KoTextBlockData *>(block.userData());
QVERIFY(line2.isValid());
QVERIFY(line2.width() < 200); // the counter takes some space.
QCOMPARE(line1.width(), line2.width());
const qreal width1 = line1.naturalTextWidth() + data1->counterWidth() + data1->counterSpacing();
const qreal width2 = line2.naturalTextWidth() + data2->counterWidth() + data2->counterSpacing();
QCOMPARE(width1, width2);
QVERIFY(data1->counterPosition().x() < data2->counterPosition().x());
const qreal padding = (200 - width2) / 2;
QVERIFY(padding > 0);// not really a layout test, but the rest will be bogus otherwise.
QCOMPARE(data2->counterPosition().x(), padding); // close to the centered text.
// right to left parag places the counter on the right. Its centered, so not the far right.
block = block.next();
layout = block.layout();
QTextLine line = layout->lineAt(0);
KoTextBlockData *data = dynamic_cast<KoTextBlockData *>(block.userData());
QCOMPARE(data->counterPosition().x(), 200 - padding - data->counterWidth());
}
示例4: updateCache
void KPrAttributeHeight::updateCache(KPrAnimationCache *cache, KPrShapeAnimation *shapeAnimation, qreal value)
{
qreal tx = 0.0, ty = 0.0;
KoShape * shape = shapeAnimation->shape();
KoTextBlockData * textBlockData = shapeAnimation->textBlockData();
QTransform transform;
if (textBlockData) {
if (KoTextShapeData *textShapeData = dynamic_cast<KoTextShapeData*>(shape->userData())) {
QTextDocument *textDocument = textShapeData->document();
for (int i = 0; i < textDocument->blockCount(); i++) {
QTextBlock textBlock = textDocument->findBlockByNumber(i);
if (textBlock.userData() == textBlockData) {
QTextLayout *layout = textBlock.layout();
value = value * cache->pageSize().height() / layout->boundingRect().height();
tx = layout->minimumWidth() * cache->zoom() / 2;
ty = layout->boundingRect().height() * cache->zoom() / 2;
}
}
}
}
else {
value = value * cache->pageSize().height() / shape->size().height();
tx = shape->size().width() * cache->zoom() / 2;
ty = shape->size().height() * cache->zoom() / 2;
}
transform.translate(tx, ty).scale(1, value).translate(-tx, -ty);
cache->update(shape, shapeAnimation->textBlockData(), "transform", transform);
}
示例5: 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());
}
示例6: testRightToLeftList
void TestDocumentLayout::testRightToLeftList()
{
initForNewTest("a\nb\nc");
KoParagraphStyle h1;
h1.setTextProgressionDirection(KoText::RightLeftTopBottom);
m_styleManager->add(&h1);
KoListStyle listStyle;
KoListLevelProperties llp = listStyle.levelProperties(1);
llp.setStyle(KoListStyle::DecimalItem);
listStyle.setLevelProperties(llp);
h1.setListStyle(&listStyle);
QTextBlock block = m_doc->begin();
h1.applyStyle(block);
block = block.next();
h1.applyStyle(block);
block = block.next();
h1.applyStyle(block);
block = block.next();
m_layout->layout();
block = m_doc->begin();
while (block.isValid()) {
KoTextBlockData *data = dynamic_cast<KoTextBlockData *>(block.userData());
QVERIFY(data);
QVERIFY(data->counterWidth() > 2);
QVERIFY(data->counterPosition().x() > 100);
QTextLine line = block.layout()->lineAt(0);
QVERIFY(line.isValid());
QCOMPARE(line.x(), (qreal)0);
QCOMPARE(line.width() + data->counterWidth() + data->counterSpacing(), (qreal)200);
block = block.next();
}
}
示例7: initForNewTest
void TestDocumentLayout::initForNewTest(const QString &initText)
{
// this leaks memory like mad, but who cares ;)
frameSet = new KWTextFrameSet(0);
shape1 = new MockTextShape();
shape1->setSize(QSizeF(200, 1000));
new KWTextFrame(shape1, frameSet);
doc = frameSet->document();
Q_ASSERT(doc);
layout = dynamic_cast<KWTextDocumentLayout*>(doc->documentLayout());
Q_ASSERT(layout);
styleManager = new KStyleManager();
KTextDocument(doc).setStyleManager(styleManager);
QTextBlock block = doc->begin();
if (initText.length() > 0) {
QTextCursor cursor(doc);
cursor.insertText(initText);
KParagraphStyle style;
style.setStyleId(101); // needed to do manually since we don't use the stylemanager
QTextBlock b2 = doc->begin();
while (b2.isValid()) {
style.applyStyle(b2);
b2 = b2.next();
}
}
blockLayout = block.layout();
}
示例8: fillBlock
void TextDocumentModel::fillBlock(const QTextBlock &block, QStandardItem *parent)
{
for (auto it = block.begin(); it != block.end(); ++it) {
QStandardItem *item = new QStandardItem(tr("Fragment: %1").arg(it.fragment().text()));
const QRectF b = m_document->documentLayout()->blockBoundingRect(block);
appendRow(parent, item, it.fragment().charFormat(), b);
if (!block.layout())
continue;
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
foreach (const auto &range, block.layout()->formats()) {
const auto start = std::max(range.start, it.fragment().position() - block.position());
const auto end = std::min(range.start + range.length,
it.fragment().position() + it.fragment().length()
- block.position());
if (start >= end)
continue;
auto child
= new QStandardItem(tr("Layout Range: %1").arg(it.fragment().text().mid(start,
end
-start)));
appendRow(item, child, range.format, QRectF());
}
#endif
}
}
示例9: initCache
void KPrAttributeHeight::initCache(KPrAnimationCache *animationCache, int step, KPrShapeAnimation * shapeAnimation, qreal startValue, qreal endValue)
{
qreal v1 = 0.0, v2 = 0.0, tx = 0.0, ty = 0.0;
KoShape * shape = shapeAnimation->shape();
KoTextBlockData * textBlockData = shapeAnimation->textBlockData();
if (textBlockData) {
if (KoTextShapeData *textShapeData = dynamic_cast<KoTextShapeData*>(shape->userData())) {
QTextDocument *textDocument = textShapeData->document();
for (int i = 0; i < textDocument->blockCount(); i++) {
QTextBlock textBlock = textDocument->findBlockByNumber(i);
if (textBlock.userData() == textBlockData) {
QTextLayout *layout = textBlock.layout();
v1 = startValue * animationCache->pageSize().height() / layout->boundingRect().height();
v2 = endValue * animationCache->pageSize().height() / layout->boundingRect().height();
tx = layout->minimumWidth() * animationCache->zoom() / 2;
ty = layout->boundingRect().height() * animationCache->zoom() / 2;
}
}
}
}
else {
v1 = startValue * animationCache->pageSize().height() / shape->size().height();
v2 = endValue * animationCache->pageSize().height() / shape->size().height();
tx = shape->size().width() * animationCache->zoom() / 2;
ty = shape->size().height() * animationCache->zoom() / 2;
}
animationCache->init(step, shape, textBlockData, "transform", QTransform().translate(tx, ty).scale(1, v1).translate(-tx, -ty));
animationCache->init(step + 1, shape, textBlockData, "transform", QTransform().translate(tx, ty).scale(1, v2).translate(-tx, -ty));
}
示例10: updateFormatting
void CodeHighlighter::updateFormatting(QTextDocument* _document, CodeHighlighterSettings const& _settings)
{
QTextBlock block = _document->firstBlock();
QList<QTextLayout::FormatRange> ranges;
Formats::const_iterator format = m_formats.begin();
while (true)
{
while ((format == m_formats.end() || (block.position() + block.length() <= format->start)) && block.isValid())
{
auto layout = block.layout();
layout->clearAdditionalFormats();
layout->setAdditionalFormats(ranges);
_document->markContentsDirty(block.position(), block.length());
block = block.next();
ranges.clear();
}
if (!block.isValid())
break;
int intersectionStart = std::max(format->start, block.position());
int intersectionLength = std::min(format->start + format->length, block.position() + block.length()) - intersectionStart;
if (intersectionLength > 0)
{
QTextLayout::FormatRange range;
range.format = _settings.formats[format->token];
range.start = format->start - block.position();
range.length = format->length;
ranges.append(range);
}
++format;
}
}
示例11: anchorAt
/*!
\fn QString QAbstractTextDocumentLayout::anchorAt(const QPointF &position) const
Returns the reference of the anchor the given \a position, or an empty
string if no anchor exists at that point.
*/
QString QAbstractTextDocumentLayout::anchorAt(const QPointF& pos) const
{
int cursorPos = hitTest(pos, Qt::ExactHit);
if (cursorPos == -1)
return QString();
// compensate for preedit in the hit text block
QTextBlock block = document()->firstBlock();
while (block.isValid()) {
QRectF blockBr = blockBoundingRect(block);
if (blockBr.contains(pos)) {
QTextLayout *layout = block.layout();
int relativeCursorPos = cursorPos - block.position();
const int preeditLength = layout ? layout->preeditAreaText().length() : 0;
if (preeditLength > 0 && relativeCursorPos > layout->preeditAreaPosition())
cursorPos -= qMin(cursorPos - layout->preeditAreaPosition(), preeditLength);
break;
}
block = block.next();
}
QTextDocumentPrivate *pieceTable = qobject_cast<const QTextDocument *>(parent())->docHandle();
QTextDocumentPrivate::FragmentIterator it = pieceTable->find(cursorPos);
QTextCharFormat fmt = pieceTable->formatCollection()->charFormat(it->format);
return fmt.anchorHref();
}
示例12: clearFormatting
void HGMarkdownHighlighter::clearFormatting()
{
QTextBlock block = document->firstBlock();
while (block.isValid()) {
block.layout()->clearAdditionalFormats();
block = block.next();
}
}
示例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: atEnd
///Indique si on est à la dernière ligne
bool RzxTextEdit::atEnd() const
{
QTextBlock block = textCursor().block();
if(block.next().isValid()) return false;
QTextLine line = currentTextLine();
return line.isValid() && line.lineNumber() == block.layout()->lineCount() - 1;
}
示例15: paintEvent
void DevLineNumber::paintEvent(QPaintEvent *e)
{
const QPixmap pm[] ={
QPixmap(":/break.png"),
QPixmap(":/error.png")
};
int n = 1, contentsY = editor->verticalScrollBar()->value();
qreal pageBottom = contentsY + editor->viewport()->height();
const QFontMetrics fm( editor->document()->defaultFont() );
QPainter p(this);
for (QTextBlock block = editor->document()->begin();
block.isValid();
block = block.next(), ++n )
{
QTextLayout *layout = block.layout();
const QRectF boundingRect = layout->boundingRect();
QPointF position = layout->position();
if (position.y() + boundingRect.height() < contentsY)
continue;
if (position.y() > pageBottom)
break;
int posY = qRound(position.y());
BlockData *dat = BlockData::data(block);
const QString txt = QString::number(n);
p.drawText( 0,
posY - contentsY,
width(),
fm.lineSpacing(),
Qt::AlignRight | Qt::AlignVCenter,
txt);
if ( !dat )
continue;
int x = 0;
if ( dat->s & BlockData::BreakPoint )
p.drawPixmap( 15*(x++),
posY - contentsY +(fm.lineSpacing() - (*pm).height())/2,
(*pm).width(),
(*pm).height(),
*pm);
if ( dat->s & BlockData::Error )
p.drawPixmap( 15*(x++),
posY - contentsY +(fm.lineSpacing() - pm[1].height())/2,
pm[1].width(),
pm[1].height(),
pm[1]);
}
}