void EditorUtil::EnumEditor(QPlainTextEdit *ed, EnumEditorProc proc, void *param) { if (!ed) { return; } QTextCursor cur = ed->textCursor(); cur.beginEditBlock(); if (cur.hasSelection()) { QTextBlock begin = ed->document()->findBlock(cur.selectionStart()); QTextBlock end = ed->document()->findBlock(cur.selectionEnd()); if (end.position() == cur.selectionEnd()) { end = end.previous(); } QTextBlock block = begin; do { if (block.text().length() > 0) { proc(cur,block,param); } block = block.next(); } while(block.isValid() && block.position() <= end.position()); } else { QTextBlock block = cur.block(); proc(cur,block,param); } cur.endEditBlock(); ed->setTextCursor(cur); }
bool AutoCompleter::autoBackspace(QTextCursor &cursor) { m_allowSkippingOfBlockEnd = false; if (!m_autoParenthesesEnabled) return false; int pos = cursor.position(); if (pos == 0) return false; QTextCursor c = cursor; c.setPosition(pos - 1); QTextDocument *doc = cursor.document(); const QChar lookAhead = doc->characterAt(pos); const QChar lookBehind = doc->characterAt(pos - 1); const QChar lookFurtherBehind = doc->characterAt(pos - 2); const QChar character = lookBehind; if (character == QLatin1Char('(') || character == QLatin1Char('[')) { QTextCursor tmp = cursor; TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp); int blockStart = tmp.isNull() ? 0 : tmp.position(); tmp = cursor; TextBlockUserData::findNextBlockClosingParenthesis(&tmp); int blockEnd = tmp.isNull() ? (cursor.document()->characterCount()-1) : tmp.position(); QChar openChar = character; QChar closeChar = (character == QLatin1Char('(')) ? QLatin1Char(')') : QLatin1Char(']'); int errors = 0; int stillopen = 0; countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen); int errorsBeforeDeletion = errors + stillopen; errors = 0; stillopen = 0; countBrackets(cursor, blockStart, pos - 1, openChar, closeChar, &errors, &stillopen); countBrackets(cursor, pos, blockEnd, openChar, closeChar, &errors, &stillopen); int errorsAfterDeletion = errors + stillopen; if (errorsAfterDeletion < errorsBeforeDeletion) return false; // insertion fixes parentheses or bracket errors, do not auto complete } // ### this code needs to be generalized if ((lookBehind == QLatin1Char('(') && lookAhead == QLatin1Char(')')) || (lookBehind == QLatin1Char('[') && lookAhead == QLatin1Char(']')) || (lookBehind == QLatin1Char('"') && lookAhead == QLatin1Char('"') && lookFurtherBehind != QLatin1Char('\\')) || (lookBehind == QLatin1Char('\'') && lookAhead == QLatin1Char('\'') && lookFurtherBehind != QLatin1Char('\\'))) { if (! isInComment(c)) { cursor.beginEditBlock(); cursor.deleteChar(); cursor.deletePreviousChar(); cursor.endEditBlock(); return true; } } return false; }
void ChatView::doTrackBar() { // save position, because our manipulations could change it int scrollbarValue = verticalScrollBar()->value(); QTextCursor cursor = textCursor(); cursor.beginEditBlock(); PsiRichText::Selection selection = PsiRichText::saveSelection(this, cursor); //removeTrackBar(cursor); if (oldTrackBarPosition) { cursor.setPosition(oldTrackBarPosition, QTextCursor::KeepAnchor); QTextBlockFormat blockFormat = cursor.blockFormat(); blockFormat.clearProperty(QTextFormat::BlockTrailingHorizontalRulerWidth); cursor.clearSelection(); cursor.setBlockFormat(blockFormat); } //addTrackBar(cursor); cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); oldTrackBarPosition = cursor.position(); QTextBlockFormat blockFormat = cursor.blockFormat(); blockFormat.setProperty(QTextFormat::BlockTrailingHorizontalRulerWidth, QVariant(true)); cursor.clearSelection(); cursor.setBlockFormat(blockFormat); PsiRichText::restoreSelection(this, cursor, selection); cursor.endEditBlock(); setTextCursor(cursor); verticalScrollBar()->setValue(scrollbarValue); }
//段落标号、编号 void MyChild::setStyle(int style) { QTextCursor cursor = this->textCursor(); if (style != 0) { QTextListFormat::Style stylename = QTextListFormat::ListDisc; switch (style) { default: case 1: stylename = QTextListFormat::ListDisc; break; case 2: stylename = QTextListFormat::ListCircle; break; case 3: stylename = QTextListFormat::ListSquare; break; case 4: stylename = QTextListFormat::ListDecimal; break; case 5: stylename = QTextListFormat::ListLowerAlpha; break; case 6: stylename = QTextListFormat::ListUpperAlpha; break; case 7: stylename = QTextListFormat::ListLowerRoman; break; case 8: stylename = QTextListFormat::ListUpperRoman; break; } cursor.beginEditBlock(); QTextBlockFormat blockFmt = cursor.blockFormat(); QTextListFormat listFmt; if (cursor.currentList()) { listFmt = cursor.currentList()->format(); } else { listFmt.setIndent(blockFmt.indent() + 1); blockFmt.setIndent(0); cursor.setBlockFormat(blockFmt); } listFmt.setStyle(stylename); cursor.createList(listFmt); cursor.endEditBlock(); } else { QTextBlockFormat bfmt; bfmt.setObjectIndex(-1); cursor.mergeBlockFormat(bfmt); } }
static void addDeclaration(const Snapshot &snapshot, const QString &fileName, const Class *cl, const QString &functionName) { const QString declaration = "void " + functionName + ";\n"; CppTools::CppRefactoringChanges refactoring(snapshot); CppTools::InsertionPointLocator find(refactoring); const CppTools::InsertionLocation loc = find.methodDeclarationInClass( fileName, cl, CppTools::InsertionPointLocator::PrivateSlot); // //! \todo change this to use the Refactoring changes. // if (BaseTextEditor *editor = editorAt(fileName, loc.line(), loc.column() - 1)) { QTextCursor tc = editor->textCursor(); int pos = tc.position(); tc.beginEditBlock(); tc.insertText(loc.prefix() + declaration + loc.suffix()); tc.setPosition(pos, QTextCursor::KeepAnchor); editor->textDocument()->autoIndent(tc); tc.endEditBlock(); } }
void GraphicTextDialog::textStyle(int styleIndex) { QTextCursor cursor = textEdit->textCursor(); if(styleIndex != 0) { QTextListFormat::Style style = QTextListFormat::ListDisc; switch (styleIndex) { default: case 1: style = QTextListFormat::ListDisc; break; case 2: style = QTextListFormat::ListCircle; break; case 3: style = QTextListFormat::ListSquare; break; case 4: style = QTextListFormat::ListDecimal; break; case 5: style = QTextListFormat::ListLowerAlpha; break; case 6: style = QTextListFormat::ListUpperAlpha; break; } cursor.beginEditBlock(); QTextBlockFormat blockFmt = cursor.blockFormat(); QTextListFormat listFmt; if(cursor.currentList()) { listFmt = cursor.currentList()->format(); } else { listFmt.setIndent(blockFmt.indent() + 1); blockFmt.setIndent(0); cursor.setBlockFormat(blockFmt); } listFmt.setStyle(style); cursor.createList(listFmt); cursor.endEditBlock(); } else { // #### QTextBlockFormat bfmt; bfmt.setObjectIndex(-1); cursor.mergeBlockFormat(bfmt); } }
void CppQtStyleIndenter::indent(QTextDocument *doc, const QTextCursor &cursor, const QChar &typedChar, const TextEditor::TabSettings &tabSettings) { if (cursor.hasSelection()) { QTextBlock block = doc->findBlock(cursor.selectionStart()); const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next(); CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings()); codeFormatter.updateStateUntil(block); QTextCursor tc = cursor; tc.beginEditBlock(); do { int indent; int padding; codeFormatter.indentFor(block, &indent, &padding); tabSettings.indentLine(block, indent + padding, padding); codeFormatter.updateLineStateChange(block); block = block.next(); } while (block.isValid() && block != end); tc.endEditBlock(); } else { indentBlock(doc, cursor.block(), typedChar, tabSettings); } }
void StyleSheetEditorDialog::insertCssProperty(const QString &name, const QString &value) { if (!value.isEmpty()) { QTextCursor cursor = m_editor->textCursor(); if (!name.isEmpty()) { cursor.beginEditBlock(); cursor.removeSelectedText(); cursor.movePosition(QTextCursor::EndOfLine); // Simple check to see if we're in a selector scope const QTextDocument *doc = m_editor->document(); const QTextCursor closing = doc->find(QLatin1String("}"), cursor, QTextDocument::FindBackward); const QTextCursor opening = doc->find(QLatin1String("{"), cursor, QTextDocument::FindBackward); const bool inSelector = !opening.isNull() && (closing.isNull() || closing.position() < opening.position()); QString insertion; if (m_editor->textCursor().block().length() != 1) insertion += QLatin1Char('\n'); if (inSelector) insertion += QLatin1Char('\t'); insertion += name; insertion += QLatin1String(": "); insertion += value; insertion += QLatin1Char(';'); cursor.insertText(insertion); cursor.endEditBlock(); } else { cursor.insertText(value); } } }
void MRichTextEdit::list(bool checked, QTextListFormat::Style style) { QTextCursor cursor = f_textedit->textCursor(); cursor.beginEditBlock(); if (!checked) { QTextBlockFormat obfmt = cursor.blockFormat(); QTextBlockFormat bfmt; bfmt.setIndent(obfmt.indent()); cursor.setBlockFormat(bfmt); } else { QTextListFormat listFmt; if (cursor.currentList()) { listFmt = cursor.currentList()->format(); } listFmt.setStyle(style); cursor.createList(listFmt); } cursor.endEditBlock(); }
void CppCodeStylePreferencesWidget::updatePreview() { CppCodeStylePreferences *cppCodeStylePreferences = m_preferences ? m_preferences : CppToolsSettings::instance()->cppCodeStyle(); const CppCodeStyleSettings ccss = cppCodeStylePreferences->currentCodeStyleSettings(); const TextEditor::TabSettings ts = cppCodeStylePreferences->currentTabSettings(); QtStyleCodeFormatter formatter(ts, ccss); foreach (TextEditor::SnippetEditorWidget *preview, m_previews) { preview->setTabSettings(ts); preview->setCodeStyle(cppCodeStylePreferences); QTextDocument *doc = preview->document(); formatter.invalidateCache(doc); QTextBlock block = doc->firstBlock(); QTextCursor tc = preview->textCursor(); tc.beginEditBlock(); while (block.isValid()) { preview->indenter()->indentBlock(doc, block, QChar::Null, ts); block = block.next(); } applyRefactorings(doc, preview, ccss); tc.endEditBlock(); }
void TikzEditorView::editComment() { bool go = true; QTextCursor textCursor = m_tikzEditor->textCursor(); if (textCursor.hasSelection()) { textCursor.beginEditBlock(); const int start = textCursor.selectionStart(); int end = textCursor.selectionEnd(); textCursor.setPosition(start, QTextCursor::MoveAnchor); textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); while (textCursor.position() < end && go) { textCursor.insertText("% "); ++end; ++end; go = textCursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor); } textCursor.endEditBlock(); } else { textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); textCursor.insertText("% "); } }
void TikzEditorView::editUncomment() { bool go = true; QTextCursor textCursor = m_tikzEditor->textCursor(); if (textCursor.hasSelection()) { textCursor.beginEditBlock(); const int start = textCursor.selectionStart(); int end = textCursor.selectionEnd() - 2; textCursor.setPosition(start, QTextCursor::MoveAnchor); textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); while (textCursor.position() < end && go) { textCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2); if (textCursor.selectedText() == QLatin1String("% ")) { textCursor.removeSelectedText(); --end; } go = textCursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor); } textCursor.endEditBlock(); } else { textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); textCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2); if (textCursor.selectedText() == QLatin1String("% ")) textCursor.removeSelectedText(); } }
void QtChatWindow::tabComplete() { if (!completer_) { return; } QTextCursor cursor; if (tabCompleteCursor_.hasSelection()) { cursor = tabCompleteCursor_; } else { cursor = input_->textCursor(); while(cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor) && cursor.document()->characterAt(cursor.position() - 1) != ' ') { } } QString root = cursor.selectedText(); if (root.isEmpty()) { return; } QString suggestion = P2QSTRING(completer_->completeWord(Q2PSTRING(root))); if (root == suggestion) { return; } tabCompletion_ = true; cursor.beginEditBlock(); cursor.removeSelectedText(); int oldPosition = cursor.position(); cursor.insertText(suggestion); tabCompleteCursor_ = cursor; tabCompleteCursor_.setPosition(oldPosition, QTextCursor::KeepAnchor); cursor.endEditBlock(); tabCompletion_ = false; }
void QtChatWindow::beginCorrection() { boost::optional<AlertID> newCorrectingAlert; if (correctionEnabled_ == Maybe) { newCorrectingAlert = addAlert(Q2PSTRING(tr("This chat may not support message correction. If you send a correction anyway, it may appear as a duplicate message"))); } else if (correctionEnabled_ == No) { newCorrectingAlert = addAlert(Q2PSTRING(tr("This chat does not support message correction. If you send a correction anyway, it will appear as a duplicate message"))); } if (newCorrectingAlert) { if (correctingAlert_) { removeAlert(*correctingAlert_); } correctingAlert_ = newCorrectingAlert; } QTextCursor cursor = input_->textCursor(); cursor.select(QTextCursor::Document); cursor.beginEditBlock(); cursor.insertText(QString(lastSentMessage_)); cursor.endEditBlock(); isCorrection_ = true; correctingLabel_->show(); input_->setStyleSheet(alertStyleSheet_); labelsWidget_->setEnabled(false); }
void ComposerTextEdit::slotPasteAsQuotation() { QString text = qApp->clipboard()->text(); if (text.isEmpty()) return; QStringList lines = text.split(QLatin1Char('\n')); for (QStringList::iterator it = lines.begin(); it != lines.end(); ++it) { it->remove(QLatin1Char('\r')); if (it->startsWith(QLatin1Char('>'))) { *it = QLatin1Char('>') + *it; } else { *it = QLatin1String("> ") + *it; } } text = lines.join(QStringLiteral("\n")); if (!text.endsWith(QLatin1Char('\n'))) { text += QLatin1Char('\n'); } QTextCursor cursor = textCursor(); cursor.beginEditBlock(); cursor.insertBlock(); cursor.insertText(text); cursor.endEditBlock(); setTextCursor(cursor); }
void SpellCheckerCore::replaceWordsInCurrentEditor(const WordList &wordsToReplace, const QString &replacementWord) { if(wordsToReplace.count() == 0) { Q_ASSERT(wordsToReplace.count() != 0); return; } if(d->currentEditor == NULL) { Q_ASSERT(d->currentEditor != NULL); return; } TextEditor::TextEditorWidget* editorWidget = qobject_cast<TextEditor::TextEditorWidget*>(d->currentEditor->widget()); if(editorWidget == NULL) { Q_ASSERT(editorWidget != NULL); return; } QTextCursor cursor = editorWidget->textCursor(); /* Iterate the words and replace all one by one */ foreach(const Word& wordToReplace, wordsToReplace) { editorWidget->gotoLine(wordToReplace.lineNumber, wordToReplace.columnNumber - 1); int wordStartPos = editorWidget->textCursor().position(); editorWidget->gotoLine(wordToReplace.lineNumber, wordToReplace.columnNumber + wordToReplace.length - 1); int wordEndPos = editorWidget->textCursor().position(); cursor.beginEditBlock(); cursor.setPosition(wordStartPos); cursor.setPosition(wordEndPos, QTextCursor::KeepAnchor); cursor.removeSelectedText(); cursor.insertText(replacementWord); cursor.endEditBlock(); }
bool Highlighter::capitalize( const QTextBlock &block,QTextCursor cursor ){ QString text=block.text(); QColor color; int i=0,pos=cursor.position(); cursor.beginEditBlock(); for(;;){ QString t=parseToke( text,color ); if( t.isEmpty() ) break; QString kw=_keyWords.value( t.toLower() ); if( !kw.isEmpty() && t!=kw ){ int i0=block.position()+i; int i1=i0+t.length(); cursor.setPosition( i0 ); cursor.setPosition( i1,QTextCursor::KeepAnchor ); cursor.insertText( kw ); } i+=t.length(); } cursor.endEditBlock(); cursor.setPosition( pos ); return true; }
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(); } }
void QsvTextOperationsWidget::on_replaceOldText_returnPressed() { if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) || QApplication::keyboardModifiers().testFlag(Qt::AltModifier) || QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier) ) { on_replaceAll_clicked(); showReplace(); return; } QTextCursor c = m_searchCursor; QTextDocument *doc = getTextDocument(); if (!doc){ qDebug("%s:%d - no document found, using a wrong class? wrong parent?", __FILE__,__LINE__); return; } c = doc->find( replaceFormUi->findText->text(), c, getReplaceFlags() ); if (c.isNull()) return; int start = c.selectionStart(); int end = c.selectionEnd(); c.beginEditBlock(); c.deleteChar(); c.insertText( replaceFormUi->replaceText->text() ); c.setPosition(start,QTextCursor::KeepAnchor); c.setPosition(end ,QTextCursor::MoveAnchor); c.endEditBlock(); setTextCursor( c ); // is there any other apperance of this text? m_searchCursor = c; updateReplaceInput(); }
bool ScCodeEditor::removeMatchingTokens() { QTextCursor cursor = textCursor(); QTextDocument *document = cursor.document(); int cursorPosition = cursor.position(); if (cursorPosition == 0) return false; QChar previousChar = document->characterAt(cursorPosition-1); QChar nextChar; if (previousChar == '{') nextChar = '}'; else if (previousChar == '[') nextChar = ']'; else if (previousChar == '(') nextChar = ')'; else if (previousChar == '\'' || previousChar == '"') nextChar = previousChar; else return false; if (document->characterAt(cursorPosition) != nextChar) return false; cursor.beginEditBlock(); cursor.deletePreviousChar(); cursor.deleteChar(); cursor.endEditBlock(); setTextCursor(cursor); return true; }
void MarkdownEdit::insert_head(const QString &tag, bool blockStart) { QTextCursor cur = m_ed->textCursor(); cur.beginEditBlock(); if (cur.hasSelection()) { QTextBlock begin = m_ed->document()->findBlock(cur.selectionStart()); QTextBlock end = m_ed->document()->findBlock(cur.selectionEnd()); if (end.position() == cur.selectionEnd()) { end = end.previous(); } QTextBlock block = begin; do { if (block.text().length() > 0) { if (blockStart) { cur.setPosition(block.position()); } else { QString text = block.text(); foreach(QChar c, text) { if (!c.isSpace()) { cur.setPosition(block.position()+text.indexOf(c)); break; } } } cur.insertText(tag); } block = block.next(); } while(block.isValid() && block.position() <= end.position()); } else {
static void addDeclaration(const Snapshot &snapshot, const QString &fileName, const Class *cl, const QString &functionName) { QString declaration = QLatin1String("void "); declaration += functionName; declaration += QLatin1String(";\n"); CppTools::CppRefactoringChanges refactoring(snapshot); CppTools::InsertionPointLocator find(&refactoring); const CppTools::InsertionLocation loc = find.methodDeclarationInClass( fileName, cl, CppTools::InsertionPointLocator::PrivateSlot); // //! \todo change this to use the Refactoring changes. // if (ITextEditor *editable = editableAt(fileName, loc.line(), loc.column() - 1)) { BaseTextEditorWidget *editor = qobject_cast<BaseTextEditorWidget *>(editable->widget()); if (editor) { QTextCursor tc = editor->textCursor(); int pos = tc.position(); tc.beginEditBlock(); tc.insertText(loc.prefix() + declaration + loc.suffix()); tc.setPosition(pos, QTextCursor::KeepAnchor); editor->indentInsertedText(tc); tc.endEditBlock(); } } }
void SqlTextEdit::increaseSelectionIndent() { QTextCursor cursor = textCursor(); int spos = cursor.anchor(); int epos = cursor.position(); if (spos > epos) std::swap(spos, epos); cursor.setPosition(spos, QTextCursor::MoveAnchor); int sblock = cursor.block().blockNumber(); cursor.setPosition(epos, QTextCursor::MoveAnchor); int eblock = cursor.block().blockNumber(); cursor.setPosition(spos, QTextCursor::MoveAnchor); const int diff = eblock - sblock; cursor.beginEditBlock(); for (int i = 0; i <= diff; ++i) { cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); cursor.insertText(" "); cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor); } cursor.endEditBlock(); cursor.setPosition(spos, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); while (cursor.block().blockNumber() < eblock) cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor); cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); setTextCursor(cursor); }
void KNoteEdit::textList() { QTextCursor c = textCursor(); c.beginEditBlock(); if ( m_textList->isChecked() ) { QTextListFormat lf; QTextBlockFormat bf = c.blockFormat(); lf.setIndent( bf.indent() + 1 ); bf.setIndent( 0 ); lf.setStyle( QTextListFormat::ListDisc ); c.setBlockFormat( bf ); c.createList( lf ); } else { QTextBlockFormat bf; bf.setObjectIndex( -1 ); c.setBlockFormat( bf ); } c.endEditBlock(); }
void CodeEditor::deleteLine(){ QTextCursor selection = getSelectedLines(); selection.beginEditBlock(); selection.removeSelectedText(); selection.deleteChar(); selection.endEditBlock(); }
void LrcEditor::removeMark() { QTextCursor cursor = textCursor(); QString strText = cursor.block().text(); if (strText.isEmpty()) return ; const QRegExp exp("^\\[\\d+:\\d+\\.\\d+\\]"); int index = exp.indexIn(strText); // no time tags if (index == -1) return ; strText.remove(index, exp.matchedLength()); cursor.beginEditBlock(); cursor.select(QTextCursor::LineUnderCursor); cursor.removeSelectedText(); cursor.insertText(strText); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor); cursor.endEditBlock(); setTextCursor(cursor); }
void GenericCodeEditor::handleKeyBackspace(QKeyEvent * event, QTextCursor & textCursor, bool & updateCursor) { if (event->modifiers() & Qt::META) { textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor); textCursor.removeSelectedText(); } else { if ( !overwriteMode() || (textCursor.positionInBlock() == 0) || textCursor.hasSelection() ) { QPlainTextEdit::keyPressEvent(event); } else { // in overwrite mode, backspace should insert a space textCursor.beginEditBlock(); textCursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor); QString selectedText = textCursor.selectedText(); if (selectedText == QStringLiteral(" ") || selectedText == QStringLiteral("\t") ) { textCursor.clearSelection(); } else { textCursor.insertText(QString(QChar(' '))); textCursor.movePosition(QTextCursor::PreviousCharacter); } textCursor.endEditBlock(); } updateCursor = true; } }
void MainWindow::highlightListItems() { QTextCursor cursor = editor->textCursor(); QTextList *list = cursor.currentList(); if (!list) return; cursor.beginEditBlock(); //! [0] for (int index = 0; index < list->count(); ++index) { QTextBlock listItem = list->item(index); //! [0] QTextBlockFormat newBlockFormat = listItem.blockFormat(); newBlockFormat.setBackground(Qt::lightGray); QTextCursor itemCursor = cursor; itemCursor.setPosition(listItem.position()); //itemCursor.movePosition(QTextCursor::StartOfBlock); itemCursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); itemCursor.setBlockFormat(newBlockFormat); /* //! [1] processListItem(listItem); //! [1] */ //! [2] } //! [2] cursor.endEditBlock(); }
/** * Delete all chars from current cursor position to end of word */ void HaiQTextEdit::delete_end_of_word() { QTextCursor cursor = textCursor(); cursor.beginEditBlock(); cursor.clearSelection(); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); cursor.removeSelectedText(); cursor.endEditBlock(); }
void GenericCodeEditor::moveLineUpDown(bool up) { // directly taken from qtcreator // Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). // GNU Lesser General Public License QTextCursor cursor = textCursor(); QTextCursor move = cursor; move.setVisualNavigation(false); // this opens folded items instead of destroying them move.beginEditBlock(); bool hasSelection = cursor.hasSelection(); if (cursor.hasSelection()) { move.setPosition(cursor.selectionStart()); move.movePosition(QTextCursor::StartOfBlock); move.setPosition(cursor.selectionEnd(), QTextCursor::KeepAnchor); move.movePosition(move.atBlockStart() ? QTextCursor::Left: QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); } else { move.movePosition(QTextCursor::StartOfBlock); move.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); } QString text = move.selectedText(); move.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); move.removeSelectedText(); if (up) { move.movePosition(QTextCursor::PreviousBlock); move.insertBlock(); move.movePosition(QTextCursor::Left); } else { move.movePosition(QTextCursor::EndOfBlock); if (move.atBlockStart()) { // empty block move.movePosition(QTextCursor::NextBlock); move.insertBlock(); move.movePosition(QTextCursor::Left); } else { move.insertBlock(); } } int start = move.position(); move.clearSelection(); move.insertText(text); int end = move.position(); if (hasSelection) { move.setPosition(start); move.setPosition(end, QTextCursor::KeepAnchor); } move.endEditBlock(); setTextCursor(move); }