本文整理汇总了C++中QTextLayout::lineForTextPosition方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextLayout::lineForTextPosition方法的具体用法?C++ QTextLayout::lineForTextPosition怎么用?C++ QTextLayout::lineForTextPosition使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextLayout
的用法示例。
在下文中一共展示了QTextLayout::lineForTextPosition方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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() );
}
示例2: UpdateStatusBar
void MainWindow::UpdateStatusBar()
{
int col = m_ui->txtedit->textCursor().positionInBlock();
QTextLayout * pLayout = m_ui->txtedit->textCursor().block().layout();
int nCurpos = m_ui->txtedit->textCursor().position() - m_ui->txtedit->textCursor().block().position();
int line = pLayout->lineForTextPosition(nCurpos).lineNumber() + m_ui->txtedit->textCursor().block().firstLineNumber() + 1;
QString title = "fake ide ";
title += QFileInfo(m_file_name).fileName();
if (m_saved != m_ui->txtedit->toPlainText())
{
title += " *";
}
setWindowTitle(title);
char buff[1024];
sprintf(buff, "line %d, col %d, total %d", line, col, m_ui->txtedit->toPlainText().size());
QString msg = buff;
m_ui->statusBar->showMessage(msg);
}
示例3: on_plainTextEdit_cursorPositionChanged
void History::on_plainTextEdit_cursorPositionChanged()
{
if(!plainTextEdit->toPlainText().isEmpty())
{
QList<QTextEdit::ExtraSelection> extraSelections;
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(0,0,170).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor =plainTextEdit->textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
//暂时标记指定的文档中的一部分,用指定的颜色
plainTextEdit->setExtraSelections(extraSelections);
usePushButton->setEnabled(true);
deletePushButton->setEnabled(true);
copyPushButton->setEnabled(true);
clearPushButton->setEnabled(true);
//获取光标所在行数
QTextCursor tc = plainTextEdit->textCursor();
QTextLayout* lo = tc.block().layout();
int pos = tc.positionInBlock();
int rowIndex = lo->lineForTextPosition(pos).lineNumber() + tc.block().firstLineNumber();
lineEdit->setText(plainTextEdit->document()->findBlockByLineNumber(rowIndex).text());
//plainTextEdit->document()->findBlockByLineNumber(rowIndex).text() 获取指定行的内容
}
else
{
usePushButton->setEnabled(false);
deletePushButton->setEnabled(false);
copyPushButton->setEnabled(false);
clearPushButton->setEnabled(false);
lineEdit->clear();
}
}
示例4: cursor
void TestDocumentLayout::placeAnchoredFrame2()
{
QFETCH(int, horizontalAlignment);
QFETCH(int, verticalAlignment);
QFETCH(QPointF, startPosition);
QFETCH(QPointF, imagePosition);
initForNewTest(QString(loremIpsum));
MockShape *picture = new MockShape();
picture->setSize(QSizeF(198, 400));
KTextAnchor *anchor = new KTextAnchor(picture);
anchor->setAlignment(KTextAnchor::AnchorHorizontal(horizontalAlignment));
anchor->setAlignment(KTextAnchor::AnchorVertical(verticalAlignment));
picture->setPosition(startPosition);
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);
QCOMPARE(cursor.position(), 1);
layout->layout();
QCOMPARE(picture->parent(), shape1);
QCOMPARE(picture->position(), imagePosition);
// test if rest of text is below picture.
QTextLayout *lay = doc->begin().layout();
QVERIFY(lay->lineCount() >= 1);
QTextLine line = lay->lineForTextPosition(1); // the first char of real text.
QVERIFY(line.isValid());
// qDebug() << line.y() << line.height();
QVERIFY(line.y() + line.height() >= 412); // test that text is below image
}
示例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);
}
//.........这里部分代码省略.........
示例6: paintEvent
void BaseEditor::paintEvent(QPaintEvent *e)
{
//copy from QPlainTextEditor
QPainter painter(viewport());
Q_ASSERT(qobject_cast<QPlainTextDocumentLayout*>(document()->documentLayout()));
QPointF offset(contentOffset());
QRect er = e->rect();
QRect viewportRect = viewport()->rect();
bool editable = !isReadOnly();
QTextBlock block = firstVisibleBlock();
qreal maximumWidth = document()->documentLayout()->documentSize().width();
//margin
qreal lineX = 0;
if (conf->isDisplayRightColumnMargin()) {
// Don't use QFontMetricsF::averageCharWidth here, due to it returning
// a fractional size even when this is not supported by the platform.
lineX = QFontMetricsF(document()->defaultFont()).width(QLatin1Char('X')) * conf->getRightMarginColumn() + offset.x() + 4;
if (lineX < viewportRect.width()) {
const QBrush background = QBrush(QColor(239, 239, 239));
painter.fillRect(QRectF(lineX, er.top(), viewportRect.width() - lineX, er.height()),
background);
const QColor col = (palette().base().color().value() > 128) ? Qt::black : Qt::white;
const QPen pen = painter.pen();
painter.setPen(blendColors(background.isOpaque() ? background.color() : palette().base().color(),
col, 32));
painter.drawLine(QPointF(lineX, er.top()), QPointF(lineX, er.bottom()));
painter.setPen(pen);
}
}
// Set a brush origin so that the WaveUnderline knows where the wave started
painter.setBrushOrigin(offset);
// keep right margin clean from full-width selection
int maxX = offset.x() + qMax((qreal)viewportRect.width(), maximumWidth)
- document()->documentMargin();
er.setRight(qMin(er.right(), maxX));
painter.setClipRect(er);
QAbstractTextDocumentLayout::PaintContext context = getPaintContext();
while (block.isValid()) {
QRectF r = blockBoundingRect(block).translated(offset);
QTextLayout *layout = block.layout();
if (!block.isVisible()) {
offset.ry() += r.height();
block = block.next();
continue;
}
if (r.bottom() >= er.top() && r.top() <= er.bottom()) {
QTextBlockFormat blockFormat = block.blockFormat();
QBrush bg = blockFormat.background();
if (bg != Qt::NoBrush) {
QRectF contentsRect = r;
contentsRect.setWidth(qMax(r.width(), maximumWidth));
fillBackground(&painter, contentsRect, bg);
}
QVector<QTextLayout::FormatRange> selections;
int blpos = block.position();
int bllen = block.length();
for (int i = 0; i < context.selections.size(); ++i) {
const QAbstractTextDocumentLayout::Selection &range = context.selections.at(i);
const int selStart = range.cursor.selectionStart() - blpos;
const int selEnd = range.cursor.selectionEnd() - blpos;
if (selStart < bllen && selEnd > 0
&& selEnd > selStart) {
QTextLayout::FormatRange o;
o.start = selStart;
o.length = selEnd - selStart;
o.format = range.format;
selections.append(o);
} else if (!range.cursor.hasSelection() && range.format.hasProperty(QTextFormat::FullWidthSelection)
&& block.contains(range.cursor.position())) {
// for full width selections we don't require an actual selection, just
// a position to specify the line. that's more convenience in usage.
QTextLayout::FormatRange o;
QTextLine l = layout->lineForTextPosition(range.cursor.position() - blpos);
o.start = l.textStart();
o.length = l.textLength();
if (o.start + o.length == bllen - 1)
++o.length; // include newline
o.format = range.format;
selections.append(o);
}
}
//.........这里部分代码省略.........