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 CppCodeStylePreferencesWidget::updatePreview() { CppCodeStylePreferences *cppCodeStylePreferences = m_preferences ? m_preferences : CppToolsSettings::instance()->cppCodeStyle(); const CppCodeStyleSettings ccss = cppCodeStylePreferences->currentCodeStyleSettings(); const TabSettings ts = cppCodeStylePreferences->currentTabSettings(); QtStyleCodeFormatter formatter(ts, ccss); foreach (SnippetEditorWidget *preview, m_previews) { preview->textDocument()->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->textDocument()->indenter()->indentBlock(doc, block, QChar::Null, ts); block = block.next(); } applyRefactorings(doc, preview, ccss); tc.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; }
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; TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp); int blockStart = tmp.isNull() ? 0 : tmp.position(); tmp = cursor; TextEditor::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 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); } }
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 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 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(QStringLiteral("}"), cursor, QTextDocument::FindBackward); const QTextCursor opening = doc->find(QStringLiteral("{"), 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 += QStringLiteral(": "); insertion += value; insertion += QLatin1Char(';'); cursor.insertText(insertion); cursor.endEditBlock(); } else { cursor.insertText(value); } } }
/** * 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); }
inline void rehighlight(QTextCursor &cursor, QTextCursor::MoveOperation operation) { inReformatBlocks = true; cursor.beginEditBlock(); int from = cursor.position(); cursor.movePosition(operation); reformatBlocks(from, 0, cursor.position() - from); cursor.endEditBlock(); inReformatBlocks = false; }
void ScCodeEditor::toggleCommentSingleLine() { QTextCursor cursor = textCursor(); cursor.beginEditBlock(); toggleCommentSingleLine( cursor ); cursor.endEditBlock(); }
void PostWindow::mouseDoubleClickEvent(QMouseEvent *e) { QTextCursor cursor = textCursor(); cursor.beginEditBlock(); QPlainTextEdit::mouseDoubleClickEvent(e); extendSelectionForEnvVar(this, textCursor()); cursor.endEditBlock(); }
void CreateBlogMsg::changeFormatType(int styleIndex ) { ui.msgEdit->setFocus( Qt::OtherFocusReason ); QTextCursor cursor = ui.msgEdit->textCursor(); //QTextBlockFormat bformat = cursor.blockFormat(); QTextBlockFormat bformat; QTextCharFormat cformat; switch (styleIndex) { default: case 0: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 0 ) ); cformat.setFontWeight( QFont::Normal ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( 0 ) ); break; case 1: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 1 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( 3 ) ); break; case 2: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 2 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( 2 ) ); break; case 3: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 3 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( 1 ) ); break; case 4: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 4 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( 0 ) ); break; case 5: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 5 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( -1 ) ); break; case 6: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 6 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( -2 ) ); break; } //cformat.clearProperty( TextFormat::HasCodeStyle ); cursor.beginEditBlock(); cursor.mergeBlockFormat( bformat ); cursor.select( QTextCursor::BlockUnderCursor ); cursor.mergeCharFormat( cformat ); cursor.endEditBlock(); }
void GenericCodeEditor::deleteWord() { QTextCursor cur = textCursor(); cur.beginEditBlock(); cur.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); cur.deletePreviousChar(); cur.endEditBlock(); }
void SimpleRichTextEdit::undoableClear() { // Taken from KTextEdit QTextCursor cursor = textCursor(); cursor.beginEditBlock(); cursor.movePosition(QTextCursor::Start); cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); cursor.removeSelectedText(); cursor.endEditBlock(); }
void XYZTextEditor::textStyle(int styleIndex) { QTextCursor cursor = m_ui->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 MainWindow::selectFrame() { QTextCursor cursor = editor->textCursor(); QTextFrame *frame = cursor.currentFrame(); cursor.beginEditBlock(); cursor.setPosition(frame->firstPosition()); cursor.setPosition(frame->lastPosition(), QTextCursor::KeepAnchor); cursor.endEditBlock(); editor->setTextCursor(cursor); }
void MainWindow::selectBlock() { QTextCursor cursor = editor->textCursor(); QTextBlock block = cursor.block(); cursor.beginEditBlock(); cursor.movePosition(QTextCursor::StartOfBlock); cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); cursor.endEditBlock(); editor->setTextCursor(cursor); }
FindResult batchReplace(QTextCursor &cursor, QTextDocument const &document, ReplaceQuery const &query, boost::optional<int> start, boost::optional<int> end) { // If we weren't given a start position, use the current cursor. if(!start) start = std::min(cursor.anchor(), cursor.position()); // We can't enable wrapping, when replacing inside a specific range. ReplaceQuery rangeQuery = query; rangeQuery.wrap = false; // Replace each match we find after our start position. cursor.setPosition(*start, QTextCursor::MoveAnchor); FindResult result = find(cursor, document, true, rangeQuery); bool foundAny = false; cursor.beginEditBlock(); while(result == FindResult::Found) { // Make sure this match is in range. int foundAnchor = cursor.anchor(); int foundPosition = cursor.position(); if(!!end && (std::max(foundAnchor, foundPosition) > *end)) break; foundAny = true; // Replace this match, and move the cursor after it. We also // need to update the "end" value, since we may have changed // the length of the document. if(!!end) { int selectionLength = cursor.selectedText().length(); end = *end + (rangeQuery.replaceValue.length() - selectionLength); } cursor.insertText(rangeQuery.replaceValue); cursor.setPosition(std::min(foundAnchor, foundPosition), QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, rangeQuery.replaceValue.length()); // Find the next match (if any). result = find(cursor, document, true, rangeQuery); }; cursor.endEditBlock(); return foundAny ? FindResult::Found : result; }
void TikzEditorView::editIndent() { QPointer<IndentDialog> indentDialog = new IndentDialog(this, tr("Indent")); if (!indentDialog->exec()) { delete indentDialog; return; } const QString insertString = indentDialog->insertChar(); const int numOfInserts = indentDialog->numOfInserts(); delete indentDialog; 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) { for (int i = 0; i < numOfInserts; ++i) textCursor.insertText(insertString); end++; go = textCursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor); } textCursor.endEditBlock(); } else { textCursor.beginEditBlock(); textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); for (int i = 0; i < numOfInserts; ++i) textCursor.insertText(insertString); textCursor.endEditBlock(); } }
void GenericCodeEditor::copyUpDown(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.beginEditBlock(); bool hasSelection = cursor.hasSelection(); if (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(); if (up) { move.setPosition(cursor.selectionStart()); move.movePosition(QTextCursor::StartOfBlock); move.insertBlock(); move.movePosition(QTextCursor::Left); } else { move.movePosition(QTextCursor::EndOfBlock); if (move.atBlockStart()) { 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(); move.setPosition(start); move.setPosition(end, QTextCursor::KeepAnchor); move.endEditBlock(); setTextCursor(move); }
bool TextBrowserHelpViewer::findText(const QString &text, Core::FindFlags flags, bool incremental, bool fromSearch, bool *wrapped) { if (wrapped) *wrapped = false; QTextDocument *doc = m_textBrowser->document(); QTextCursor cursor = m_textBrowser->textCursor(); if (!doc || cursor.isNull()) return false; const int position = cursor.selectionStart(); if (incremental) cursor.setPosition(position); QTextDocument::FindFlags f = Core::textDocumentFlagsForFindFlags(flags); QTextCursor found = doc->find(text, cursor, f); if (found.isNull()) { if ((flags & Core::FindBackward) == 0) cursor.movePosition(QTextCursor::Start); else cursor.movePosition(QTextCursor::End); found = doc->find(text, cursor, f); if (!found.isNull() && wrapped) *wrapped = true; } if (fromSearch) { cursor.beginEditBlock(); m_textBrowser->viewport()->setUpdatesEnabled(false); QTextCharFormat marker; marker.setForeground(Qt::red); cursor.movePosition(QTextCursor::Start); m_textBrowser->setTextCursor(cursor); while (m_textBrowser->find(text)) { QTextCursor hit = m_textBrowser->textCursor(); hit.mergeCharFormat(marker); } m_textBrowser->viewport()->setUpdatesEnabled(true); cursor.endEditBlock(); } bool cursorIsNull = found.isNull(); if (cursorIsNull) { found = m_textBrowser->textCursor(); found.setPosition(position); } m_textBrowser->setTextCursor(found); return !cursorIsNull; }
void MainWindow::insertCustomer(const QString &customer){ if (customer.isEmpty()) return; QStringList customerList = customer.split(", "); QTextDocument *document = chatbox->document(); QTextCursor cursor = document->find("NAME"); if (!cursor.isNull()) { cursor.beginEditBlock(); cursor.insertText(customerList.at(0)); QTextCursor oldcursor = cursor; cursor = document->find("ADDRESS"); if (!cursor.isNull()) { for (int i = 1; i < customerList.size(); ++i) { cursor.insertBlock(); cursor.insertText(customerList.at(i)); } cursor.endEditBlock(); } else oldcursor.endEditBlock(); } }
void CodeEditor::copyLineDown(){ QTextCursor selectionCursor = getSelectedLines(); QString selectedLines = selectionCursor.selection().toPlainText(); selectionCursor.beginEditBlock(); selectionCursor.movePosition(QTextCursor::EndOfLine); selectionCursor.insertText(QString("\n" + selectedLines)); selectionCursor.movePosition(QTextCursor::EndOfBlock); selectionCursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, selectedLines.size()); selectionCursor.endEditBlock(); setTextCursor(selectionCursor); if(codeCompleter->popup()->isVisible()) codeCompleter->popup()->hide(); }
/** * Duplicates the current line and inserts the copy below the line */ void HaiQTextEdit::duplicate_line() { QTextCursor cursor = textCursor(); int pos(cursor.position()); cursor.beginEditBlock(); cursor.clearSelection(); cursor.movePosition(QTextCursor::EndOfLine); cursor.select(QTextCursor::LineUnderCursor); QTextDocumentFragment text( cursor.selection() ); cursor.clearSelection(); cursor.insertText( QString(QChar::LineSeparator) ); cursor.insertFragment( text ); cursor.setPosition(pos); cursor.endEditBlock(); }
/** * Deletes the current line */ void HaiQTextEdit::delete_current_line() { QTextCursor cursor = textCursor(); cursor.beginEditBlock(); cursor.clearSelection(); cursor.movePosition(QTextCursor::StartOfLine); int pos(cursor.position()); cursor.select(QTextCursor::LineUnderCursor); // remove line (and line feed char) cursor.removeSelectedText(); cursor.deleteChar(); // goto start of next line cursor.setPosition(pos); cursor.endEditBlock(); }
int Editor::autoIndent() { QTextCursor cur = this->textCursor(); if(cur.selectedText().length() > 0) { return 0; } int col = cur.columnNumber(); cur.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor); cur.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,col); QString text = cur.selectedText(); cur.clearSelection(); int stop = -1; int indent = -1; int slcm = text.indexOf("'"); if(slcm > -1) { stop = slcm; } cur.beginEditBlock(); cur.insertBlock(); for(int n = 0; n <= stop || isspace(text[n].toLatin1()); n++) { if(n == slcm) { if(text.indexOf("''") > -1) cur.insertText("''"); else cur.insertText("'"); } else { cur.insertText(" "); } } if(indent > 0) { for(int n = 0; n < indent; n++) { cur.insertText(" "); } } this->setTextCursor(cur); cur.endEditBlock(); return 1; }
QList<Find::SearchResultItem> ReplaceDocument::replace(const QString &fileName, const QString &text, const QList<Find::SearchResultItem> &items) { QTextCursor cursor; bool crlf = false; QTextDocument *doc = fileDocument(fileName,cursor,crlf); if (!doc) { return QList<Find::SearchResultItem>(); } cursor.movePosition(QTextCursor::Start); cursor.beginEditBlock(); QList<Find::SearchResultItem> update_items; QTextBlock block = cursor.block(); int offset = 0; foreach(Find::SearchResultItem item, items) { if (!block.isValid()) { break; } while (block.blockNumber() < item.lineNumber-1 ) { block = block.next(); offset = 0; if (!block.isValid()) { break; } } cursor.setPosition(block.position()); cursor.movePosition(QTextCursor::Right,QTextCursor::MoveAnchor,offset+item.textMarkPos); cursor.movePosition(QTextCursor::Right,QTextCursor::KeepAnchor,item.textMarkLength); cursor.removeSelectedText(); cursor.insertText(text); item.textMarkPos -= offset; item.text.replace(item.textMarkPos,item.textMarkLength,text); offset += text.length()-item.textMarkLength; item.textMarkLength = text.length(); update_items.push_back(item); } cursor.endEditBlock(); if (m_document) { QFile f(fileName); if (!f.open(QFile::WriteOnly)) { return QList<Find::SearchResultItem>(); } QString text = m_document->toPlainText(); if (crlf) { text.replace(QLatin1Char('\n'), QLatin1String("\r\n")); } f.write(text.toUtf8()); } return update_items; }