本文整理汇总了C++中QTextCursor::block方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextCursor::block方法的具体用法?C++ QTextCursor::block怎么用?C++ QTextCursor::block使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextCursor
的用法示例。
在下文中一共展示了QTextCursor::block方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: contextAllowsElectricCharacters
bool GlslCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
{
const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
BackwardsScanner::previousBlockState(cursor.block()));
// XXX Duplicated from CppEditor::isInComment to avoid tokenizing twice
if (tk.isComment()) {
const unsigned pos = cursor.selectionEnd() - cursor.block().position();
if (pos == tk.utf16charsEnd()) {
if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
return false;
const int state = cursor.block().userState() & 0xFF;
if (state > 0)
return false;
}
if (pos < tk.utf16charsEnd())
return false;
} else if (tk.isStringLiteral() || tk.isCharLiteral()) {
const unsigned pos = cursor.selectionEnd() - cursor.block().position();
if (pos <= tk.utf16charsEnd())
return false;
}
return true;
}
示例2: changeUnderCursor
QString CvsEditorWidget::changeUnderCursor(const QTextCursor &c) const
{
// Try to match "1.1" strictly:
// 1) Annotation: Check for a revision number at the beginning of the line.
// Note that "cursor.select(QTextCursor::WordUnderCursor)" will
// only select the part up until the dot.
// Check if we are at the beginning of a line within a reasonable offset.
// 2) Log: check for lines like "revision 1.1", cursor past "revision"
switch (contentType()) {
case VcsBase::OtherContent:
case VcsBase::DiffOutput:
break;
case VcsBase::AnnotateOutput: {
const QTextBlock block = c.block();
if (c.atBlockStart() || (c.position() - block.position() < 3)) {
const QString line = block.text();
if (m_revisionAnnotationPattern.exactMatch(line))
return m_revisionAnnotationPattern.cap(1);
}
}
break;
case VcsBase::LogOutput: {
const QTextBlock block = c.block();
if (c.position() - block.position() > 8 && m_revisionLogPattern.exactMatch(block.text()))
return m_revisionLogPattern.cap(1);
}
break;
}
return QString();
}
示例3: initialize
CompletionContextFinder::CompletionContextFinder(const QTextCursor &cursor)
: m_cursor(cursor)
, m_colonCount(-1)
, m_behaviorBinding(false)
, m_inStringLiteral(false)
, m_inImport(false)
{
QTextBlock lastBlock = cursor.block();
if (lastBlock.next().isValid())
lastBlock = lastBlock.next();
initialize(cursor.document()->begin(), lastBlock);
m_startTokenIndex = yyLinizerState.tokens.size() - 1;
// Initialize calls readLine - which skips empty lines. We should only adjust
// the start token index if the linizer still is in the same block as the cursor.
const int cursorPos = cursor.positionInBlock();
if (yyLinizerState.iter == cursor.block()) {
for (; m_startTokenIndex >= 0; --m_startTokenIndex) {
const Token &token = yyLinizerState.tokens.at(m_startTokenIndex);
if (token.end() <= cursorPos)
break;
if (token.begin() < cursorPos && token.is(Token::String))
m_inStringLiteral = true;
}
if (m_startTokenIndex == yyLinizerState.tokens.size() - 1 && yyLinizerState.insertedSemicolon)
--m_startTokenIndex;
}
getQmlObjectTypeName(m_startTokenIndex);
checkBinding();
checkImport();
}
示例4: addMark
void LrcEditor::addMark(qint64 ms)
{
// move cursor to begin
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
if (m_expTag.indexIn(cursor.block().text()) >= 0)
gotoNextMark(cursor);
// check whether already contains the same time
QByteArray strLine = cursor.block().text().trimmed().toLocal8Bit();
if (!strLine.isEmpty())
{
LrcReader reader(strLine, strLine.length());
reader.parse();
if (reader.getLrc().getLineByTime(ms) != nullptr)
goto _end_of_insert;
}
{
QString strTag = _makeTimeTag(ms);
cursor.insertText(strTag);
}
_end_of_insert:
gotoNextMark(cursor);
}
示例5: eventFilter
bool ExpressionQueryWidget::eventFilter(QObject *obj, QEvent *event)
{
if (obj == m_textEdit) {
switch (event->type()) {
case QEvent::KeyPress:
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
int key = keyEvent->key();
if (key == Qt::Key_Return || key == Qt::Key_Enter) {
executeExpression();
return true;
} else if (key == Qt::Key_Backspace) {
// ensure m_expr doesn't contain backspace characters
QTextCursor cursor = m_textEdit->textCursor();
bool atLastLine = !(cursor.block().next().isValid());
if (!atLastLine)
return true;
if (cursor.columnNumber() <= m_prompt.count())
return true;
cursor.deletePreviousChar();
m_expr = cursor.block().text().mid(m_prompt.count());
return true;
} else {
m_textEdit->moveCursor(QTextCursor::End);
m_expr += keyEvent->text();
}
break;
}
case QEvent::FocusIn:
checkCurrentContext();
m_textEdit->moveCursor(QTextCursor::End);
break;
default:
break;
}
} else if (obj == m_lineEdit) {
switch (event->type()) {
case QEvent::KeyPress:
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
int key = keyEvent->key();
if (key == Qt::Key_Up && m_lineEdit->text() != m_lastExpr) {
m_expr = m_lineEdit->text();
if (!m_lastExpr.isEmpty())
m_lineEdit->setText(m_lastExpr);
} else if (key == Qt::Key_Down) {
m_lineEdit->setText(m_expr);
}
break;
}
case QEvent::FocusIn:
checkCurrentContext();
break;
default:
break;
}
}
return QWidget::eventFilter(obj, event);
}
示例6: fillListButtons
void SimpleParagraphWidget::fillListButtons()
{
KoZoomHandler zoomHandler;
zoomHandler.setZoom(1.2);
zoomHandler.setDpi(72, 72);
KoInlineTextObjectManager itom;
KoTextRangeManager tlm;
TextShape textShape(&itom, &tlm);
textShape.setSize(QSizeF(300, 100));
QTextCursor cursor (textShape.textShapeData()->document());
foreach(const Lists::ListStyleItem &item, Lists::genericListStyleItems()) {
QPixmap pm(48,48);
pm.fill(Qt::transparent);
QPainter p(&pm);
p.translate(0, -1.5);
p.setRenderHint(QPainter::Antialiasing);
if(item.style != KoListStyle::None) {
KoListStyle listStyle;
KoListLevelProperties llp = listStyle.levelProperties(1);
llp.setStyle(item.style);
if (KoListStyle::isNumberingStyle(item.style)) {
llp.setStartValue(1);
llp.setListItemSuffix(".");
}
listStyle.setLevelProperties(llp);
cursor.select(QTextCursor::Document);
QTextCharFormat textCharFormat=cursor.blockCharFormat();
textCharFormat.setFontPointSize(11);
textCharFormat.setFontWeight(QFont::Normal);
cursor.setCharFormat(textCharFormat);
QTextBlock cursorBlock = cursor.block();
KoTextBlockData data(cursorBlock);
cursor.insertText("----");
listStyle.applyStyle(cursor.block(),1);
cursorBlock = cursor.block();
KoTextBlockData data1(cursorBlock);
cursor.insertText("\n----");
cursorBlock = cursor.block();
KoTextBlockData data2(cursorBlock);
cursor.insertText("\n----");
cursorBlock = cursor.block();
KoTextBlockData data3(cursorBlock);
KoTextDocumentLayout *lay = dynamic_cast<KoTextDocumentLayout*>(textShape.textShapeData()->document()->documentLayout());
if(lay)
lay->layout();
KoShapePaintingContext paintContext; //FIXME
textShape.paintComponent(p, zoomHandler, paintContext);
widget.bulletListButton->addItem(pm, static_cast<int> (item.style));
}
}
示例7: keyPressEvent
void ConsoleWidget::keyPressEvent(QKeyEvent *event)
{
QString line;
if (!isReadOnly())
{
moveCursor(QTextCursor::End);
if (event->key()==Qt::Key_Return)
{
QTextCursor cursor = textCursor();
line = cursor.block().text();
line.remove(0, m_prompt.size()); // get rid of prompt (assume it's just the first character)
// propagate newline before we send text
QPlainTextEdit::keyPressEvent(event);
// send text
emit textLine(line);
return;
}
else if (event->key()==Qt::Key_Up)
{
emit controlKey(Qt::Key_Up);
return;
}
else if (event->key()==Qt::Key_Down)
{
emit controlKey(Qt::Key_Down);
return;
}
else if (event->key()==Qt::Key_Backspace)
{
QTextCursor cursor = textCursor();
line = cursor.block().text();
// don't propagate backspace if it means we're going to delete the prompt
if (line.size()<=m_prompt.size())
return;
}
else if (event->matches(QKeySequence::Copy)) // break key
emit controlKey(Qt::Key_Escape);
else
{
// make sure when we're typing, there's a prompt
QTextCursor cursor = textCursor();
line = cursor.block().text();
if (line.left(m_prompt.size())!=m_prompt)
prompt(m_prompt);
}
}
QPlainTextEdit::keyPressEvent(event);
}
示例8: cursorPositionChanged
void TextEdit::cursorPositionChanged()
{
//alignmentChanged(textEdit->alignment());
QColor col = Qt::red;
QTextCharFormat fmt;
fmt.setForeground(col);
QTextCursor cursor = textEdit->textCursor();
cout<<"#TextEdit::cursorPositionChanged:";
cout<<"cursor.selectionStart:"<<cursor.selectionStart()<<","<<cursor.selectionEnd();
int selectionStart=cursor.selectionStart(),
selectionEnd=cursor.selectionEnd();
cursor.mergeCharFormat(fmt);
colorChanged(col);
if(!cursor.hasSelection()) return;
QTextBlock block=cursor.block();
int blockStart=block.position();
QTextLayout* layout=cursor.block().layout();
QTextLine layoutLine=layout->lineForTextPosition(selectionStart-blockStart);
cout<<"layout line:";
int lineNumber= layoutLine.lineNumber();
QPoint blockOffset=layout->position().toPoint();
QPoint lineOffset=layoutLine.position().toPoint();
printPoint(blockOffset);
printPoint(lineOffset);
QPoint linePoint(blockOffset.x()+lineOffset.x(),blockOffset.y()+lineOffset.y());
QPoint lineEndPoint=QPoint(linePoint.x()+PAGEWIDTH,linePoint.y());
// cout<<"block:"<<rect.left()<<","<<rect.right()<<":"<<rect.top()<<","<<rect.bottom();
// cout<<"blockstart:"<<blockStart;
// int x=blockPoint.x()+(float)((selectionStart-blockStart)%LINELENGTH)/LINELENGTH*PAGEWIDTH;
// int y=blockPoint.y()+((selectionStart-blockStart)/LINELENGTH+1)*LINEHEIGHT;
// int x1=blockPoint.x()+(float)((selectionEnd-blockStart)%LINELENGTH)/LINELENGTH*PAGEWIDTH;
// cout<<"block position:"<<blockPoint.x()<<","<<blockPoint.y();
// cout<<"selection position:"<<x<<","<<y<<":"<<x1<<","<<y;
//// int y1=blockPoint.y()+((cursor.selectionEnd()-blockStart)/LINELENGTH+1)*15;
// QPoint selectionPoint(x,y);
// QPoint selectionEndPoint(x1,y);
image->paintLine(linePoint,lineEndPoint);
// int lineStart=blockStart,lineEnd;
// for(int i=0;i<block.lineCount();i++){
// QTextLine line = layout->lineAt(i);
// qreal lineWidth=line.width(),lineHeight=line.height();
// lineEnd=lineStart+line.textLength();
// int a=line.textStart(),b=line.textLength()+a;
// cout<<"line.cursorToX:"<<line.cursorToX(selectionStart)<<","<<line.cursorToX(selectionEnd);
// cout<<"line.textstart:"<<a<<","<<b;
// cout<<"line.width:"<<lineWidth<<" height:"<<lineHeight;
// rect=line.naturalTextRect();
// cout<<"line.naturaltextrect:"<<rect.left()<<":"<<rect.top()<<":"<<rect.right()<<":"<<rect.bottom();
// lineStart=lineEnd;
// }
}
示例9: nickAutocompletion
/** La completion n'est réalisée que si aucune sélection n'est actuellement définie */
bool RzxTextEdit::nickAutocompletion()
{
QTextCursor cursor = textCursor();
//Si y'a une sélection, on zappe
if(cursor.hasSelection())
return false;
//On récupère la position du curseur et la paragraphe concerné
int index = cursor.position();
index -= cursor.block().position();
if(!index) return false;
static const QRegExp mask("[^-A-Za-z0-9]([-A-Za-z0-9]+)$");
const QString textPara = cursor.block().text();
//Juste pour se souvenir des pseudos possibles
const QString localName = RzxComputer::localhost()->name();
const QString remoteName = chat->computer()->name();
const QString localLower = localName.toLower();
const QString remoteLower = remoteName.toLower();
for(int i = 1 ; i <= index && (localName.length() > i || remoteName.length() > i) ; i++)
{
//Chaine de caractère qui précède le curseur de taille i
QString nick = textPara.mid(index-i, i).toLower();
if(mask.indexIn(nick) != -1 || i == index)
{
if(mask.indexIn(nick) != -1) nick = mask.cap(1);
if(!remoteLower.indexOf(nick, false) && localLower.indexOf(nick, false))
{
for(int i = 0; i< nick.length();i++)
{
cursor.deletePreviousChar ();
}
cursor.insertText(remoteName + " ");
return true;
}
else if(remoteLower.indexOf(nick, false) && !localLower.indexOf(nick, false))
{
for(int i = 0; i< nick.length();i++)
{
cursor.deletePreviousChar ();
}
cursor.insertText(localName + " ");
return true;
}
return false;
}
}
return false;
}
示例10: indentCursor
void SyntaxTextEditor::indentCursor(QTextCursor cur, bool bIndent)
{
if (bIndent) {
cur.insertText("\t");
} else {
QString text = cur.block().text();
int pos = cur.position()-cur.block().position()-1;
int count = text.count();
if (count > 0 && pos >= 0 && pos < count) {
if (text.at(pos) == '\t' || text.at(pos) == ' ') {
cur.deletePreviousChar();
}
}
}
}
示例11: indentEnter
void SyntaxTextEditor::indentEnter(QTextCursor cur)
{
cur.beginEditBlock();
int pos = cur.position()-cur.block().position();
QString text = cur.block().text();
int i = 0;
int tab = 0;
int space = 0;
QString inText = "\n";
while (i < text.size()) {
if (!text.at(i).isSpace())
break;
if (text.at(0) == ' ') {
space++;
}
else if (text.at(0) == '\t') {
inText += "\t";
tab++;
}
i++;
}
text.trimmed();
if (!text.isEmpty()) {
if (pos >= text.size()) {
const QChar ch = text.at(text.size()-1);
if (ch == '{' || ch == '(') {
inText += "\t";
}
} else if (pos == text.size()-1 && text.size() >= 3) {
const QChar l = text.at(text.size()-2);
const QChar r = text.at(text.size()-1);
if ( (l == '{' && r == '}') ||
(l == '(' && r== ')') ) {
cur.insertText(inText);
int pos = cur.position();
cur.insertText(inText);
cur.setPosition(pos);
this->setTextCursor(cur);
cur.insertText("\t");
cur.endEditBlock();
return;
}
}
}
cur.insertText(inText);
cur.endEditBlock();
ensureCursorVisible();
}
示例12: deleteTab
void CodeEditor::deleteTab()
{
QString deletion = " ";
QTextCursor cursor = textCursor();
if (cursor.selectionEnd() - cursor.selectionStart() <= 0) {
//delete 4 spaces (tab)
cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, deletion.length());
QString selected = cursor.selectedText();
if (selected.startsWith(deletion))
cursor.deletePreviousChar();
} else {
QTextBlock firstBlock = document()->findBlock(cursor.selectionStart());
QTextBlock lastBlock = document()->findBlock(cursor.selectionEnd() - 1);
cursor.setPosition(firstBlock.position());
cursor.beginEditBlock();
do {
if (cursor.block().text().startsWith(deletion)) {
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, deletion.length());
cursor.removeSelectedText();
}
} while (cursor.movePosition(QTextCursor::NextBlock) && cursor.position() <= lastBlock.position());
cursor.endEditBlock();
}
}
示例13: insertLineBeforeBracket
int OpenedFile::insertLineBeforeBracket(const int start_line, const QString &new_line)
{
QTextCursor parsingCursor = textCursor();
QString new_text = new_line;
parsingCursor.setPosition(0);
while(parsingCursor.blockNumber() != start_line)
{
parsingCursor.movePosition(QTextCursor::Down);
}
while(!parsingCursor.atEnd())
{
if(parsingCursor.block().text().contains('}'))
break;
parsingCursor.movePosition(QTextCursor::Down);
}
parsingCursor.movePosition(QTextCursor::StartOfLine);
new_text.remove(".0000", Qt::CaseInsensitive);
parsingCursor.insertText(new_text);
parsingCursor.insertText("\n");
parsingCursor.movePosition(QTextCursor::Up);
setTextCursor(parsingCursor);
return parsingCursor.blockNumber();
}
示例14: itemNameFromDocument
QString EBTextEdit::itemNameFromDocument()
{
QString sItemName("");
if (m_ViewType == EditorView::XmlSource) {
//Is there any problem with QTextEdit::toPlainText and QDomDocument::setContent?
QDomDocument xmlDocument(m_FullFileName);
QString errotMsg("");
int iErrorLine(0), iErrorCol(0);
if (!xmlDocument.setContent(toPlainText(),false, &errotMsg, &iErrorLine, &iErrorCol))
{
qCritical()<< "QDomDocument::setContent: " << errotMsg << " Line " << iErrorLine << " column: " << iErrorCol;
return sItemName;
}
QDomNodeList nodeList = xmlDocument.elementsByTagName(DataOwnerSingl::XmlKeywords::KEYWORD_NAME);
if (nodeList.isEmpty())
return sItemName;
sItemName = nodeList.at(0).toElement().text();
}
else if (m_ViewType == EditorView::Text) {
QTextCursor cursor = document()->find(DataOwnerSingl::TxtKeywords::KEYWORD_NAME);
const QTextBlock block = cursor.block();
if (!block.isValid())
return QString("");
sItemName = block.text();
sItemName.remove(QRegExp("^"+ DataOwnerSingl::TxtKeywords::KEYWORD_NAME));
//qDebug() << "itemNameFromDocument:" << sItemName;
}
else if(m_ViewType == EditorView::OnlyPlainData) {
//todo
sItemName = DataOwnerSingl::XmlKeywords::itemNameFromFile(m_FullFileName);
} else
Q_ASSERT(0);
return sItemName;
}
示例15: getIndex
// Solution for getting x and y position of the cursor. Found
// them in the Qt mailing list
int Console::getIndex(const QTextCursor &crQTextCursor) {
QTextBlock b;
int column = 1;
b = crQTextCursor.block();
column = crQTextCursor.position() - b.position();
return column;
}