bool QsvTextOperationsWidget::issue_search( const QString &text, QTextCursor newCursor, QFlags<QTextDocument::FindFlag> findOptions, QLineEdit *l, bool moveCursor ) { QTextCursor c = m_document->find( text, newCursor, findOptions ); bool found = ! c.isNull(); //lets try again, from the start if (!found) { c.movePosition(findOptions.testFlag(QTextDocument::FindBackward)? QTextCursor::End : QTextCursor::Start); c = m_document->find(text, c, findOptions); found = ! c.isNull(); } QPalette p = l->palette(); if (found) { p.setColor(QPalette::Base, searchFoundColor); } else { if (!text.isEmpty()) p.setColor(QPalette::Base, searchNotFoundColor); else p.setColor(QPalette::Base, l->style()->standardPalette().base().color() ); c = m_searchCursor; } l->setPalette(p); if (moveCursor){ int start = c.selectionStart(); int end = c.selectionEnd(); c.setPosition(end ,QTextCursor::MoveAnchor); c.setPosition(start,QTextCursor::KeepAnchor); setTextCursor(c); } return found; }
void MainWindow::displayTooltip(QMouseEvent *e) { static bool isFirstTime = true; static int selectionStart; static QString selectedText; static bool isExisted = false; //мы выделяли существующее в словаре? bool isExist = false; int i; QPoint p = e->pos(); QTextCursor tc = ui->textEdit->cursorForPosition(p); tc.select(QTextCursor::WordUnderCursor); if (selectionStart != tc.selectionStart()) { //проверяем, есть ли слово в словаре for(i = 0; i < bptr->getListWords().size(); i++) { if(!bptr->getListWords().at(i).word.compare(tc.selectedText(), Qt::CaseInsensitive)) { isExist = true; break; } if(!bptr->getListWords().at(i).word.compare(selectedText, Qt::CaseInsensitive)) { isExisted = true; } } if (!isFirstTime) { QToolTip::hideText(); //скрываем предыдущий тултип //Надо удалить выделение предыдущего слова QTextCursor tc(ui->textEdit->document()); tc.setPosition(selectionStart); tc.select(QTextCursor::WordUnderCursor); if(isExisted) tc.insertHtml(QString("<u>" + selectedText + "</b>")); else { tc.insertHtml(QString(selectedText)); } isExisted = false; } selectionStart = tc.selectionStart(); selectedText = tc.selectedText(); if (tc.selectedText().length() > 0 && isExist) { // QToolTip::showText(e->globalPos(), bptr->getListWords().at(i).toString(), // ui->textEdit, ui->textEdit->cursorRect()); QToolTip::showText(e->globalPos(), bptr->getListWords().at(i).toString(), ui->textEdit, ui->textEdit->cursorRect(tc)); isFirstTime = false; tc.insertHtml(QString("<b>" + tc.selectedText() + "</b>")); } } }
inline void extendSelectionForEnvVar(QPlainTextEdit * textEdit, QTextCursor selection) { if (selection.hasSelection()) { if (selection.selectedText() == QStringLiteral("~")) { QTextCursor wordAfter(selection); wordAfter.movePosition(QTextCursor::NextCharacter); wordAfter.select(QTextCursor::WordUnderCursor); if ( wordAfter.hasSelection() && (selection.block() == wordAfter.block()) ) { selection.setPosition(selection.selectionStart()); selection.setPosition(wordAfter.selectionEnd(), QTextCursor::KeepAnchor); textEdit->setTextCursor(selection); } } else { int positionBeforeSelection = selection.selectionStart() - 1; if (positionBeforeSelection >= 0) { QChar charBeforeSelection = textEdit->document()->characterAt(positionBeforeSelection); if (charBeforeSelection == QChar('~')) { QTextCursor extendedSelection = textEdit->textCursor(); extendedSelection.setPosition(positionBeforeSelection); extendedSelection.setPosition(selection.selectionEnd(), QTextCursor::KeepAnchor); textEdit->setTextCursor(extendedSelection); } } } } }
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 GolangEdit::updateLink(const QTextCursor &_cursor) { QTextCursor cursor = _cursor; LiteApi::selectWordUnderCursor(cursor); if (cursor.selectionStart() == cursor.selectionEnd()) { m_editor->clearLink(); return; } if (m_linkCursor.selectionStart() == cursor.selectionStart() && m_linkCursor.selectionEnd() == cursor.selectionEnd()) { if (m_lastLink.hasValidTarget()) { m_editor->showLink(m_lastLink); } return; } m_linkCursor = cursor; m_lastLink = LiteApi::Link(); if (m_findLinkProcess->isRunning()) { m_findLinkProcess->kill(); m_findLinkProcess->waitForFinished(100); //return; } QString cmd = LiteApi::liteide_stub_cmd(m_liteApp); QString src = cursor.document()->toPlainText(); m_srcData = src.toUtf8(); int offset = src.left(cursor.selectionStart()).length(); QFileInfo info(m_editor->filePath()); m_findLinkProcess->setEnvironment(LiteApi::getGoEnvironment(m_liteApp).toStringList()); m_findLinkProcess->setWorkingDirectory(info.path()); m_findLinkProcess->startEx(cmd,QString("type -cursor %1:%2 -cursor_stdin -def -info ."). arg(info.fileName()). arg(offset)); }
void CCodeEdit::mouseMoveLineNumEvent(QMouseEvent* event) { QPoint pt(0, event->y()); if( m_lineIconSize.width() < event->x() && 0 != (event->buttons()& Qt::LeftButton) ) { QTextCursor cursor = textCursor(); if( cursor.position() == cursor.selectionStart() ) { // 選択開始位置より前を選択 cursor.setPosition(cursor.selectionEnd()); cursor.movePosition(QTextCursor::EndOfLine); cursor.setPosition(cursorForPosition(pt).position(), QTextCursor::KeepAnchor); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); } else { // 選択開始位置より後ろを選択 cursor.setPosition(cursor.selectionStart()); cursor.movePosition(QTextCursor::StartOfLine); cursor.setPosition(cursorForPosition(pt).position(), QTextCursor::KeepAnchor); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); } setTextCursor(cursor); } }
void mousePressEvent(QMouseEvent * event) { if( event->buttons() != Qt::LeftButton ) return; if( event->modifiers() == Qt::ShiftModifier ) { QTextCursor cur = d_codeEditor->textCursor(); const int selStart = d_codeEditor->document()->findBlock( cur.selectionStart() ).blockNumber(); const int selEnd = d_codeEditor->document()->findBlock( cur.selectionEnd() ).blockNumber(); Q_ASSERT( selStart <= selEnd ); // bei position und anchor nicht erfllt const int clicked = d_codeEditor->lineAt( event->pos() ); if( clicked <= selEnd ) { if( cur.selectionStart() == cur.position() ) d_codeEditor->selectLines( selEnd, clicked ); else d_codeEditor->selectLines( selStart, clicked ); }else d_codeEditor->selectLines( selStart, clicked ); }else { // Beginne Drag d_start = d_codeEditor->lineAt( event->pos() ); d_codeEditor->selectLines( d_start, d_start ); } }
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; // } }
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); }
void CodeEditer::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar) { if (cursor.hasSelection()) { QTextBlock block = doc->findBlock(qMin(cursor.selectionStart(), cursor.selectionEnd())); const QTextBlock end = doc->findBlock(qMax(cursor.selectionStart(), cursor.selectionEnd())).next(); do { indentBlock(doc, block, typedChar); block = block.next(); } while (block.isValid() && block != end); } else { indentBlock(doc, cursor.block(), typedChar); } }
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(); }
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 ClangFormat::disableFormattingSelectedText() { TextEditor::TextEditorWidget *widget = TextEditor::TextEditorWidget::currentTextEditorWidget(); if (!widget) return; const QTextCursor tc = widget->textCursor(); if (!tc.hasSelection()) return; // Insert start marker const QTextBlock selectionStartBlock = tc.document()->findBlock(tc.selectionStart()); QTextCursor insertCursor(tc.document()); insertCursor.beginEditBlock(); insertCursor.setPosition(selectionStartBlock.position()); insertCursor.insertText("// clang-format off\n"); const int positionToRestore = tc.position(); // Insert end marker QTextBlock selectionEndBlock = tc.document()->findBlock(tc.selectionEnd()); insertCursor.setPosition(selectionEndBlock.position() + selectionEndBlock.length() - 1); insertCursor.insertText("\n// clang-format on"); insertCursor.endEditBlock(); // Reset the cursor position in order to clear the selection. QTextCursor restoreCursor(tc.document()); restoreCursor.setPosition(positionToRestore); widget->setTextCursor(restoreCursor); // The indentation of these markers might be undesired, so reformat. // This is not optimal because two undo steps will be needed to remove the markers. const int reformatTextLength = insertCursor.position() - selectionStartBlock.position(); BeautifierPlugin::formatCurrentFile(command(selectionStartBlock.position(), reformatTextLength)); }
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 {
QString CWidgetReturnEmitTextEdit::textUnderCursor() const { QTextCursor tc = textCursor(); tc.select(QTextCursor::WordUnderCursor); // Save selected positions of current word. int selectionStart = tc.selectionStart(); int selectionEnd = tc.selectionEnd(); // If selection is at beginning of text edit then there can't be a slash to check for if (selectionStart == 0) return tc.selectedText(); // Modify selection to include previous character tc.setPosition(selectionStart - 1, QTextCursor::MoveAnchor); tc.setPosition(selectionEnd, QTextCursor::KeepAnchor); // If previous character was / return current selection for command completion if(tc.selectedText().startsWith('/')) return tc.selectedText(); else { // Else restore original selection and return for nick completion tc.setPosition(selectionStart, QTextCursor::MoveAnchor); tc.setPosition(selectionEnd, QTextCursor::KeepAnchor); return tc.selectedText(); } }
static void get_selection(QTextEdit *wid, int *start, int *length) { QTextCursor cursor = wid->textCursor(); *start = cursor.selectionStart(); *length = cursor.selectionEnd() - *start; }
void TerminalEdit::cursorPositionChanged() { QTextCursor cur = this->textCursor(); int pos = cur.position(); if (cur.hasSelection()) { pos = cur.selectionStart(); m_copy->setEnabled(true); if (pos < m_endPostion) { m_cut->setEnabled(false); } else { m_cut->setEnabled(!this->isReadOnly()); } } else { m_copy->setEnabled(false); m_cut->setEnabled(false); } if (pos < m_endPostion) { m_paste->setEnabled(false); } else { QClipboard *clipboard = QApplication::clipboard(); if (clipboard->mimeData()->hasText() || clipboard->mimeData()->hasHtml()){ m_paste->setEnabled(true); } else { m_paste->setEnabled(false); } } }
void CodeEditorAndSearchPaneWidget::showSearchPane() { if(!isEnabled()){ return; } QString selectedText = codeEditor->textCursor().selectedText(); bool hasSelection = selectedText.size() > 1 && selectedText.size() <= 150; if(hasSelection){ searchPane->setSearchText(selectedText); } if(!searchPane->isVisible()){ searchPane->show(); } searchPane->searchTextChanged(!hasSelection); searchPane->activateFindTextBox(); if(hasSelection){ QTextCursor cur = codeEditor->textCursor(); int startPos = qMin(cur.selectionStart(), cur.selectionEnd()); cur.setPosition(startPos); searchPane->findNextOrPrevious(true, cur); } }
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); }
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 Indenter::reindent(QTextDocument *doc, const QTextCursor &cursor, BaseTextEditorWidget *editor) { if (cursor.hasSelection()) { QTextBlock block = doc->findBlock(cursor.selectionStart()); const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next(); const TabSettings &ts = editor->tabSettings(); // skip empty blocks while (block.isValid() && block != end) { QString bt = block.text(); if (ts.firstNonSpace(bt) < bt.size()) break; indentBlock(doc, block, QChar::Null, editor); block = block.next(); } int previousIndentation = ts.indentationColumn(block.text()); indentBlock(doc, block, QChar::Null, editor); int currentIndentation = ts.indentationColumn(block.text()); int delta = currentIndentation - previousIndentation; block = block.next(); while (block.isValid() && block != end) { ts.reindentLine(block, delta); block = block.next(); } } else { indentBlock(doc, cursor.block(), QChar::Null, editor); } }
void GenericCodeEditor::updateDocLastSelection() { QTextCursor cursor = textCursor(); int start = cursor.selectionStart(); int range = cursor.selectionEnd() - start; Main::scProcess()->updateSelectionMirrorForDocument(mDoc, start, range); }
void GolangEdit::findLinkOutput(QByteArray data, bool bStdErr) { if (bStdErr) { return; } if ( m_editor == m_liteApp->editorManager()->currentEditor()) { QTextCursor cur = this->textCursorForPos(QCursor::pos()); cur.select(QTextCursor::WordUnderCursor); if (cur.selectionStart() == m_linkCursor.selectionStart() && cur.selectionEnd() == m_linkCursor.selectionEnd()) { QStringList info = QString::fromUtf8(data).trimmed().split("\n"); if (info.size() == 2) { if (info[0] != "-") { QRegExp reg(":(\\d+):(\\d+)"); int pos = reg.lastIndexIn(info[0]); if (pos >= 0) { QString fileName = info[0].left(pos); int line = reg.cap(1).toInt(); int col = reg.cap(2).toInt(); LiteApi::Link link(fileName,line-1,col-1); m_lastCursor.select(QTextCursor::WordUnderCursor); link.linkTextStart = m_linkCursor.selectionStart(); link.linkTextEnd = m_linkCursor.selectionEnd(); m_lastLink = link; m_editor->showLink(link); } } QRect rc = m_plainTextEdit->cursorRect(m_linkCursor); QPoint pt = m_plainTextEdit->mapToGlobal(rc.topRight()); QToolTip::showText(pt,info[1],m_plainTextEdit); } } } }
void QConsoleWidget::dragEnterEvent(QDragEnterEvent *e){ TP::dragEnterEvent(e); if(e->isAccepted()){/*调整选区为可编辑区域*/ QTextCursor tc = this->textCursor(); if (tc.hasSelection()) { if (tc.selectionStart()>= this->promptEndPos_) { return; } else { if (tc.selectionEnd()<= this->promptEndPos_) { tc.clearSelection(); this->setTextCursor(tc); } else { auto se_ = tc.selectionEnd(); tc.setPosition(this->promptEndPos_); tc.setPosition(se_,QTextCursor::KeepAnchor); this->setTextCursor(tc); } } } } }
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 DictionaryTextEdit::TextEditState::saveState(QTextEdit *textEidt) { QTextCursor cursor = textEidt->textCursor(); _selectionStart = cursor.selectionStart(); _selectionEnd = cursor.selectionEnd(); _htmlDoc = textEidt->toHtml(); hasSavedState = true; }
int CWidgetReturnEmitTextEdit::currentWordStartIndex() { QTextCursor tc = textCursor(); tc.select(QTextCursor::WordUnderCursor); // Return start position of current word. return tc.selectionStart(); }
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); }
void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format) { QTextCursor cursor = textEdit->textCursor(); if (!cursor.hasSelection()) cursor.select(QTextCursor::WordUnderCursor); cout<<"TextEdit.mergeFormat..:"<<cursor.selectionStart()<<","<<cursor.selectionEnd(); cursor.mergeCharFormat(format); textEdit->mergeCurrentCharFormat(format); }
//----------------------------------------------------------------------------- void PythonQtScriptingConsole::handleTabCompletion() { QTextCursor textCursor = this->textCursor(); int pos = textCursor.position(); textCursor.setPosition(commandPromptPosition()); textCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); int startPos = textCursor.selectionStart(); int offset = pos-startPos; QString text = textCursor.selectedText(); QString textToComplete; int cur = offset; while(cur--) { QChar c = text.at(cur); if (c.isLetterOrNumber() || c == '.' || c == '_') { textToComplete.prepend(c); } else { break; } } QString lookup; QString compareText = textToComplete; int dot = compareText.lastIndexOf('.'); if (dot!=-1) { lookup = compareText.mid(0, dot); compareText = compareText.mid(dot+1, offset); } if (!lookup.isEmpty() || !compareText.isEmpty()) { compareText = compareText.toLower(); QStringList found; QStringList l = PythonQt::self()->introspection(_context, lookup, PythonQt::Anything); foreach (QString n, l) { if (n.toLower().startsWith(compareText)) { found << n; } } if (!found.isEmpty()) { _completer->setCompletionPrefix(compareText); _completer->setCompletionMode(QCompleter::PopupCompletion); _completer->setModel(new QStringListModel(found, _completer)); _completer->setCaseSensitivity(Qt::CaseInsensitive); QTextCursor c = this->textCursor(); c.movePosition(QTextCursor::StartOfWord); QRect cr = cursorRect(c); cr.setWidth(_completer->popup()->sizeHintForColumn(0) + _completer->popup()->verticalScrollBar()->sizeHint().width()); cr.translate(0,8); _completer->complete(cr); } else { _completer->popup()->hide(); } } else {