本文整理汇总了C++中QTextDocument::findBlock方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextDocument::findBlock方法的具体用法?C++ QTextDocument::findBlock怎么用?C++ QTextDocument::findBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextDocument
的用法示例。
在下文中一共展示了QTextDocument::findBlock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: paintLineIndicator
void GenericCodeEditor::paintLineIndicator( QPaintEvent *e )
{
QPalette plt( mLineIndicator->palette() );
QRect r( e->rect() );
QPainter p( mLineIndicator );
p.fillRect( r, plt.color( QPalette::Mid ) );
p.setPen( plt.color(QPalette::Dark) );
p.drawLine( r.topRight(), r.bottomRight() );
p.setPen( plt.color(QPalette::ButtonText) );
QTextDocument *doc = QPlainTextEdit::document();
QTextCursor cursor(textCursor());
int selStartBlock, selEndBlock;
if (cursor.hasSelection()) {
selStartBlock = doc->findBlock(cursor.selectionStart()).blockNumber();
selEndBlock = doc->findBlock(cursor.selectionEnd()).blockNumber();
}
else
selStartBlock = selEndBlock = -1;
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
qreal top = blockBoundingGeometry(block).translated(contentOffset()).top();
qreal bottom = top + blockBoundingRect(block).height();
while (block.isValid() && top <= e->rect().bottom()) {
if (block.isVisible() && bottom >= e->rect().top()) {
p.save();
QRectF numRect( 0, top, mLineIndicator->width() - 1, bottom - top );
int num = blockNumber;
if (num >= selStartBlock && num <= selEndBlock) {
num -= selStartBlock;
p.setPen(Qt::NoPen);
p.setBrush(plt.color(QPalette::Highlight));
p.drawRect(numRect);
p.setPen(plt.color(QPalette::HighlightedText));
}
QString number = QString::number(num + 1);
p.drawText(0, top, mLineIndicator->width() - 4, bottom - top,
Qt::AlignRight, number);
p.restore();
}
block = block.next();
top = bottom;
bottom = top + blockBoundingRect(block).height();
++blockNumber;
}
}
示例2: identifyMatch
void ProFileHoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
{
m_docFragment.clear();
m_manualKind = UnknownManual;
if (ProFileEditorWidget *proFileEditor = qobject_cast<ProFileEditorWidget *>(editor->widget())) {
if (!proFileEditor->extraSelectionTooltip(pos).isEmpty()) {
setToolTip(proFileEditor->extraSelectionTooltip(pos));
} else {
QTextDocument *document = proFileEditor->document();
QTextBlock block = document->findBlock(pos);
identifyQMakeKeyword(block.text(), pos - block.position());
if (m_manualKind != UnknownManual) {
QUrl url(QString::fromLatin1("qthelp://com.trolltech.qmake/qdoc/qmake-%1-reference.html#%2")
.arg(manualName()).arg(m_docFragment));
setLastHelpItemIdentified(TextEditor::HelpItem(url.toString(),
m_docFragment, TextEditor::HelpItem::QMakeVariableOfFunction));
} else {
// General qmake manual will be shown outside any function or variable
setLastHelpItemIdentified(TextEditor::HelpItem(QLatin1String("qmake"),
TextEditor::HelpItem::Unknown));
}
}
}
}
示例3: fontMetrics
template<> void
QConsoleWidget::_pf<void, UpdatePromptWidget>(
QConsoleWidget * thisp
) {
//TODO: UpdatePromptWidget
{
QFontMetricsF fontMetrics(
thisp->thisp->textCharFormat.font());
{
QTextDocument * doc = thisp->document();
QTextBlock textBlock =
doc->findBlock(thisp->promptEndPos_);
QTextCursor cursor_(textBlock);
QTextBlockFormat bf_ = textBlock.blockFormat();
float fh =
fontMetrics.height()
+1.55f;
thisp->thisp->setFixedHeight(fh);
thisp->thisp->setFixedWidth(fh);
bf_.setTextIndent( thisp->thisp->width() );
cursor_.setBlockFormat(bf_);
thisp->setTextCursor(cursor_);
}
}
_pf<void, UpdatePromptWidgetPositon>(thisp);
thisp->raise();
}
示例4: findBlock
QTextBlock QTextDocumentProto::findBlock(int pos) const
{
QTextDocument *item = qscriptvalue_cast<QTextDocument*>(thisObject());
if (item)
return item->findBlock(pos);
return QTextBlock();
}
示例5: fetchImageLinksFromRegions
void VPreviewManager::fetchImageLinksFromRegions(QVector<VElementRegion> p_imageRegions,
QVector<ImageLinkInfo> &p_imageLinks)
{
p_imageLinks.clear();
if (p_imageRegions.isEmpty()) {
return;
}
p_imageLinks.reserve(p_imageRegions.size());
QTextDocument *doc = m_editor->document();
for (int i = 0; i < p_imageRegions.size(); ++i) {
const VElementRegion ® = p_imageRegions[i];
QTextBlock block = doc->findBlock(reg.m_startPos);
if (!block.isValid()) {
continue;
}
int blockStart = block.position();
int blockEnd = blockStart + block.length() - 1;
QString text = block.text();
// Since the image links update signal is emitted after a timer, during which
// the content may be changed.
if (reg.m_endPos > blockEnd) {
continue;
}
ImageLinkInfo info(reg.m_startPos,
reg.m_endPos,
blockStart,
block.blockNumber(),
calculateBlockMargin(block, m_editor->tabStopWidthW()));
if ((reg.m_startPos == blockStart
|| isAllSpaces(text, 0, reg.m_startPos - blockStart))
&& (reg.m_endPos == blockEnd
|| isAllSpaces(text, reg.m_endPos - blockStart, blockEnd - blockStart))) {
// Image block.
info.m_isBlock = true;
fetchImageInfoToPreview(text, info);
} else {
// Inline image.
info.m_isBlock = false;
fetchImageInfoToPreview(text.mid(reg.m_startPos - blockStart,
reg.m_endPos - reg.m_startPos),
info);
}
if (info.m_linkUrl.isEmpty() || info.m_linkShortUrl.isEmpty()) {
continue;
}
p_imageLinks.append(info);
}
}
示例6: indentOrUnindent
void PlainTextEditor::indentOrUnindent( bool doIndent )
{
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
// Indent or unindent the selected lines
int pos = cursor.position();
int anchor = cursor.anchor();
int start = qMin( anchor, pos );
int end = qMax( anchor, pos );
QTextDocument* doc = document();
QTextBlock startBlock = doc->findBlock(start);
QTextBlock endBlock = doc->findBlock( end -1 ).next();
for ( QTextBlock block = startBlock; block != endBlock; block = block.next() ) {
QString text = block.text();
if ( doIndent ) {
const int indentPosition = firstNonSpace( text );
cursor.setPosition( block.position() +indentPosition );
cursor.insertText( QString( IndentSize, QLatin1Char( ' ' ) ) );
} else {
const int indentPosition = firstNonSpace( text );
const int targetColumn = indentedColumn( columnAt( text, indentPosition ), false );
cursor.setPosition( block.position() +indentPosition );
cursor.setPosition( block.position() +targetColumn, QTextCursor::KeepAnchor );
cursor.removeSelectedText();
}
}
// Reselect the selected lines
cursor.setPosition( startBlock.position() );
cursor.setPosition( endBlock.previous().position(), QTextCursor::KeepAnchor );
cursor.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor );
cursor.endEditBlock();
setTextCursor( cursor );
}
示例7: indentationLevel
int ScCodeEditor::indentationLevel(const QTextCursor & cursor)
{
QTextDocument *doc = QPlainTextEdit::document();
int startBlockNum = doc->findBlock(cursor.selectionStart()).blockNumber();
QStack<int> stack;
int level = 0;
int blockNum = 0;
QTextBlock block = QPlainTextEdit::document()->begin();
while (block.isValid()) {
if (level > 0) {
stack.push(level);
level = 0;
}
TextBlockData *data = static_cast<TextBlockData*>(block.userData());
if (data) {
int count = data->tokens.size();
for (int idx = 0; idx < count; ++idx) {
const Token & token = data->tokens[idx];
switch (token.type) {
case Token::OpeningBracket:
if (token.character != '(' || stack.size() || token.positionInBlock)
level += 1;
break;
case Token::ClosingBracket:
if (level)
level -= 1;
else if(!stack.isEmpty()) {
stack.top() -= 1;
if (stack.top() <= 0)
stack.pop();
}
break;
default:
;
}
}
}
if (blockNum == startBlockNum)
return stack.size();
block = block.next();
++blockNum;
}
return -1;
}
示例8: indentSelection
virtual void indentSelection(const QTextCursor &selection,
const QString &fileName,
const TextEditor::TextDocument *textDocument) const
{
// ### shares code with QmlJSTextEditor::indent
QTextDocument *doc = selection.document();
QTextBlock block = doc->findBlock(selection.selectionStart());
const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
const TextEditor::TabSettings &tabSettings =
ProjectExplorer::actualTabSettings(fileName, textDocument);
CreatorCodeFormatter codeFormatter(tabSettings);
codeFormatter.updateStateUntil(block);
do {
const int depth = codeFormatter.indentFor(block);
if (depth != -1)
tabSettings.indentLine(block, depth);
codeFormatter.updateLineStateChange(block);
block = block.next();
} while (block.isValid() && block != end);
}
示例9: showPosition
void GenericCodeEditor::showPosition( int pos, int selectionLength )
{
if (pos < 0) return;
QTextDocument *doc = QPlainTextEdit::document();
if (!doc) return;
int lineNumber = doc->findBlock(pos).firstLineNumber();
verticalScrollBar()->setValue(lineNumber);
QTextCursor cursor(doc);
cursor.setPosition(pos);
if (selectionLength)
cursor.setPosition(pos + selectionLength, QTextCursor::KeepAnchor);
setTextCursor(cursor);
}
示例10: currentUrl
QString ChatTextEdit::currentUrl(QMouseEvent* e) const
{
QTextDocument* doc = document();
int cursor_pos = cursorForPosition(e->pos()).position();
QTextBlock block = doc->findBlock(cursor_pos);
int block_pos = block.position();
QString text = block.text();
QVector<int> url_index, url_length;
QStringList urls;
int index = 0;
QString url;
do
{
int length = 0;
url = urlFound(text, index, length);
if(url.size())
{
url_index.push_back(index);
url_length.push_back(length);
urls.push_back(url);
}
index += length;
}
while(url.size());
url.clear();
for(int i=0;i<url_index.size();i++)
{
if(cursor_pos >= block_pos+url_index[i] &&
cursor_pos < block_pos+url_index[i]+url_length[i])
{
url = urls[i];
break;
}
}
return url;
}
示例11: rectForCursorPosition
/*!
* Returns the rectangle for the cursor including the predictive text area.
* HbAbstractEditPrivate::ensurePositionVisible() uses the same algorithm.
*/
QRectF NmEditorTextEdit::rectForCursorPosition() const
{
NM_FUNCTION;
int cursorPos = cursorPosition();
QRectF rect = rectForPosition(cursorPos);
QTextDocument *doc = document();
if (doc) {
rect.adjust(0, -doc->documentMargin(), 0, doc->documentMargin());
const QTextBlock block = doc->findBlock(cursorPos);
QTextLayout *blockLayout = block.layout();
if (block.isValid() && blockLayout) {
if (blockLayout->preeditAreaText().length()) {
// Adjust cursor rect so that predictive text will be also visible
rect.adjust(0, 0, boundingRect().width() / 2, 0);
}
}
}
return rect;
}
示例12: searchTargetFromCurrentLine
void searchTargetFromCurrentLine(QEditor *editor)
{
m_targetPos.clear();
if (editor == NULL) {
return;
}
m_currentGroup = 0;
QTextDocument *doc = editor->document();
int cursorPos = editor->textCursor().position();
QTextBlock currentLineBlock = doc->findBlock(cursorPos);
int firstPos = currentLineBlock.position();
int lastPos = firstPos + currentLineBlock.length() - 1;
for (int offset = 1; cursorPos - offset >= firstPos || cursorPos + offset <= lastPos; offset++) {
if (cursorPos + offset <= lastPos) {
m_targetPos << (cursorPos + offset);
}
if (cursorPos - offset >= firstPos) {
m_targetPos << (cursorPos - offset);
}
}
}
示例13: getFirstAndLastVisiblePosition
QPair<int, int> getFirstAndLastVisiblePosition(Editor *editor)
{
QTextCursor cursor = editor->textCursor();
QTextDocument *doc = editor->document();
int currentLine = doc->findBlock(cursor.position()).blockNumber();
int cursorHeight = editor->cursorRect().height();
int lineCountToFirstVisibleLine = editor->cursorRect().top() / cursorHeight;
int firstVisibleLineNum = currentLine - lineCountToFirstVisibleLine;
if (firstVisibleLineNum < 0) {
firstVisibleLineNum = 0;
}
int maxLineNumOnScreen = (editor->viewport()->height() / cursorHeight);
if (maxLineNumOnScreen < 1) {
maxLineNumOnScreen = 1;
}
int firstPos = doc->findBlockByNumber(firstVisibleLineNum).position();
int lastVisibleLineNum = firstVisibleLineNum + maxLineNumOnScreen - 1;
QTextBlock lastVisibleTextBlock = doc->findBlockByNumber(lastVisibleLineNum);
if (!lastVisibleTextBlock.isValid()) {
lastVisibleTextBlock = doc->lastBlock();
}
int lastPos = lastVisibleTextBlock.position() + lastVisibleTextBlock.length() - 1;
return QPair<int, int>(firstPos, lastPos);
}
示例14: updateHeaders
void VMdEditor::updateHeaders(const QVector<VElementRegion> &p_headerRegions)
{
QTextDocument *doc = document();
QVector<VTableOfContentItem> headers;
QVector<int> headerBlockNumbers;
QVector<QString> headerSequences;
if (!p_headerRegions.isEmpty()) {
headers.reserve(p_headerRegions.size());
headerBlockNumbers.reserve(p_headerRegions.size());
headerSequences.reserve(p_headerRegions.size());
}
// Assume that each block contains only one line
// Only support # syntax for now
QRegExp headerReg(VUtils::c_headerRegExp);
int baseLevel = -1;
for (auto const & reg : p_headerRegions) {
QTextBlock block = doc->findBlock(reg.m_startPos);
if (!block.isValid()) {
continue;
}
if (!block.contains(reg.m_endPos - 1)) {
qWarning() << "header accross multiple blocks, starting from block"
<< block.blockNumber()
<< block.text();
}
if ((block.userState() == HighlightBlockState::Normal)
&& headerReg.exactMatch(block.text())) {
int level = headerReg.cap(1).length();
VTableOfContentItem header(headerReg.cap(2).trimmed(),
level,
block.blockNumber(),
headers.size());
headers.append(header);
headerBlockNumbers.append(block.blockNumber());
headerSequences.append(headerReg.cap(3));
if (baseLevel == -1) {
baseLevel = level;
} else if (baseLevel > level) {
baseLevel = level;
}
}
}
m_headers.clear();
bool autoSequence = m_config.m_enableHeadingSequence
&& !isReadOnly()
&& m_file->isModifiable();
int headingSequenceBaseLevel = g_config->getHeadingSequenceBaseLevel();
if (headingSequenceBaseLevel < 1 || headingSequenceBaseLevel > 6) {
headingSequenceBaseLevel = 1;
}
QVector<int> seqs(7, 0);
QRegExp preReg(VUtils::c_headerPrefixRegExp);
int curLevel = baseLevel - 1;
for (int i = 0; i < headers.size(); ++i) {
VTableOfContentItem &item = headers[i];
while (item.m_level > curLevel + 1) {
curLevel += 1;
// Insert empty level which is an invalid header.
m_headers.append(VTableOfContentItem(c_emptyHeaderName,
curLevel,
-1,
m_headers.size()));
if (autoSequence) {
addHeaderSequence(seqs, curLevel, headingSequenceBaseLevel);
}
}
item.m_index = m_headers.size();
m_headers.append(item);
curLevel = item.m_level;
if (autoSequence) {
addHeaderSequence(seqs, item.m_level, headingSequenceBaseLevel);
QString seqStr = headerSequenceStr(seqs);
if (headerSequences[i] != seqStr) {
// Insert correct sequence.
insertSequenceToHeader(doc->findBlockByNumber(headerBlockNumbers[i]),
headerReg,
preReg,
seqStr);
}
}
}
emit headersChanged(m_headers);
updateCurrentHeader();
}
示例15: find
bool GenericCodeEditor::find( const QRegExp &expr, QTextDocument::FindFlags options )
{
// Although QTextDocument provides a find() method, we implement
// our own, because the former one is not adequate.
if(expr.isEmpty()) return true;
bool backwards = options & QTextDocument::FindBackward;
QTextCursor c( textCursor() );
int pos;
if (c.hasSelection())
{
bool matching = expr.exactMatch(c.selectedText());
if( backwards == matching )
pos = c.selectionStart();
else
pos = c.selectionEnd();
}
else
pos = c.position();
QTextDocument *doc = QPlainTextEdit::document();
QTextBlock startBlock = doc->findBlock(pos);
int startBlockOffset = pos - startBlock.position();
QTextCursor cursor;
if (!backwards) {
int blockOffset = startBlockOffset;
QTextBlock block = startBlock;
while (block.isValid()) {
if (findInBlock(doc, block, expr, blockOffset, options, cursor))
break;
blockOffset = 0;
block = block.next();
}
if(cursor.isNull())
{
blockOffset = 0;
block = doc->begin();
while(true) {
if (findInBlock(doc, block, expr, blockOffset, options, cursor)
|| block == startBlock)
break;
block = block.next();
}
}
} else {
int blockOffset = startBlockOffset;
QTextBlock block = startBlock;
while (block.isValid()) {
if (findInBlock(doc, block, expr, blockOffset, options, cursor))
break;
block = block.previous();
blockOffset = block.length() - 1;
}
if(cursor.isNull())
{
block = doc->end();
while(true) {
blockOffset = block.length() - 1;
if (findInBlock(doc, block, expr, blockOffset, options, cursor)
|| block == startBlock)
break;
block = block.previous();
}
}
}
if(!cursor.isNull()) {
setTextCursor(cursor);
return true;
}
else
return false;
}