bool TextView::eventFilter(QObject *watched, QEvent *event) { if (watched == textEdit()) { if (event->type() == QEvent::FocusIn) { connect(textEdit_, SIGNAL(cursorPositionChanged()), this, SLOT(updatePositionStatus())); updatePositionStatus(); } else if (event->type() == QEvent::FocusOut) { disconnect(textEdit_, SIGNAL(cursorPositionChanged()), this, SLOT(updatePositionStatus())); } } if (watched == textEdit()->viewport()) { if (event->type() == QEvent::Resize) { updateExtraSelections(); } else if (event->type() == QEvent::Wheel) { auto wheelEvent = static_cast<QWheelEvent *>(event); if (wheelEvent->orientation() == Qt::Vertical && wheelEvent->modifiers() & Qt::ControlModifier) { if (wheelEvent->delta() > 0) { zoomIn(1 + wheelEvent->delta() / 360); } else { zoomOut(1 - wheelEvent->delta() / 360); } return true; } } } return QDockWidget::eventFilter(watched, event); }
void TextView::highlight(std::vector<Range<int>> ranges, bool ensureVisible) { if (!ranges.empty()) { std::sort(ranges.begin(), ranges.end()); if (ensureVisible) { std::vector<Range<int>> difference; set_difference( ranges.begin(), ranges.end(), highlighting_.begin(), highlighting_.end(), std::back_inserter(difference)); if (difference.empty()) { set_difference( highlighting_.begin(), highlighting_.end(), ranges.begin(), ranges.end(), std::back_inserter(difference)); } if (!difference.empty()) { textEdit()->blockSignals(true); moveCursor(difference.front().end(), true); moveCursor(difference.front().start(), true); textEdit()->blockSignals(false); } } highlighting_.swap(ranges); } else { highlighting_.clear(); } updateExtraSelections(); }
void QScriptEdit::setExecutionLineNumber(int lineNumber, bool error) { m_executionLineNumber = lineNumber; m_executionLineNumberHasError = error; m_extraArea->update(); updateExtraSelections(); gotoLine(lineNumber); }
void FakeVimProxy::requestSetBlockSelection(const QTextCursor &tc) { QPlainTextEdit *ed = qobject_cast<QPlainTextEdit *>(m_widget); if (!ed) return; QPalette pal = ed->parentWidget() != NULL ? ed->parentWidget()->palette() : QApplication::palette(); m_blockSelection.clear(); m_clearSelection.clear(); QTextCursor cur = tc; QTextEdit::ExtraSelection selection; selection.format.setBackground( pal.color(QPalette::Base) ); selection.format.setForeground( pal.color(QPalette::Text) ); selection.cursor = cur; m_clearSelection.append(selection); selection.format.setBackground( pal.color(QPalette::Highlight) ); selection.format.setForeground( pal.color(QPalette::HighlightedText) ); int from = cur.positionInBlock(); int to = cur.anchor() - cur.document()->findBlock(cur.anchor()).position(); const int min = qMin(cur.position(), cur.anchor()); const int max = qMax(cur.position(), cur.anchor()); for ( QTextBlock block = cur.document()->findBlock(min); block.isValid() && block.position() < max; block = block.next() ) { cur.setPosition( block.position() + qMin(from, block.length()) ); cur.setPosition( block.position() + qMin(to, block.length()), QTextCursor::KeepAnchor ); selection.cursor = cur; m_blockSelection.append(selection); } disconnect( ed, &QPlainTextEdit::selectionChanged, this, &FakeVimProxy::updateBlockSelection ); ed->setTextCursor(tc); connect( ed, &QPlainTextEdit::selectionChanged, this, &FakeVimProxy::updateBlockSelection ); QPalette pal2 = ed->palette(); pal2.setColor(QPalette::Highlight, Qt::transparent); pal2.setColor(QPalette::HighlightedText, Qt::transparent); ed->setPalette(pal2); updateExtraSelections(); }
void FakeVimProxy::requestDisableBlockSelection() { QPlainTextEdit *ed = qobject_cast<QPlainTextEdit *>(m_widget); if (!ed) return; QPalette pal = ed->parentWidget() != NULL ? ed->parentWidget()->palette() : QApplication::palette(); m_blockSelection.clear(); m_clearSelection.clear(); ed->setPalette(pal); disconnect( ed, &QPlainTextEdit::selectionChanged, this, &FakeVimProxy::updateBlockSelection ); updateExtraSelections(); }
void FakeVimProxy::highlightMatches(const QString &pattern) { QPlainTextEdit *ed = qobject_cast<QPlainTextEdit *>(m_widget); if (!ed) return; QTextCursor cur = ed->textCursor(); QTextEdit::ExtraSelection selection; selection.format.setBackground(Qt::yellow); selection.format.setForeground(Qt::black); // Highlight matches. QTextDocument *doc = ed->document(); QRegExp re(pattern); cur = doc->find(re); m_searchSelection.clear(); int a = cur.position(); while ( !cur.isNull() ) { if ( cur.hasSelection() ) { selection.cursor = cur; m_searchSelection.append(selection); } else { cur.movePosition(QTextCursor::NextCharacter); } cur = doc->find(re, cur); int b = cur.position(); if (a == b) { cur.movePosition(QTextCursor::NextCharacter); cur = doc->find(re, cur); b = cur.position(); if (a == b) break; } a = b; } updateExtraSelections(); }
int GenericCodeEditor::replaceAll( const QRegExp &expr, const QString &replacement, QTextDocument::FindFlags options ) { mSearchSelections.clear(); updateExtraSelections(); if(expr.isEmpty()) return 0; int replacements = 0; bool caps = expr.patternSyntax() != QRegExp::FixedString; QTextDocument *doc = QPlainTextEdit::document(); QTextBlock block = doc->begin(); QTextCursor cursor; QTextCursor(doc).beginEditBlock(); while (block.isValid()) { int blockPos = block.position(); int offset = 0; while(findInBlock(doc, block, expr, offset, options, cursor)) { QString rstr = replacement; if(caps) rstr = resolvedReplacement(rstr, expr); cursor.insertText(rstr); ++replacements; offset = cursor.selectionEnd() - blockPos; } block = block.next(); } QTextCursor(doc).endEditBlock(); return replacements; }
void ScCodeEditor::matchBrackets() { mBracketSelections.clear(); QTextCursor cursor(textCursor()); QTextBlock block( cursor.block() ); int posInBlock = cursor.positionInBlock(); TokenIterator it(block); while (it.isValid() && it.block() == block) { const Token & token = *it; if (token.positionInBlock > posInBlock) { it = TokenIterator(); break; } else if ( (token.positionInBlock == posInBlock && token.type == Token::OpeningBracket) || (token.positionInBlock == posInBlock - 1 && token.type == Token::ClosingBracket) ) break; ++it; } BracketPair match; if( it.isValid() && it.block() == block) matchBracket( it, match ); if( match.first.isValid() && match.second.isValid() ) { const Token & tok1 = *match.first; const Token & tok2 = *match.second; if ( (tok1.character == '(' && tok2.character == ')') || (tok1.character == '[' && tok2.character == ']') || (tok1.character == '{' && tok2.character == '}') ){ QTextEdit::ExtraSelection selection; selection.format = mBracketHighlight; cursor.setPosition(match.first.position()); cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); selection.cursor = cursor; mBracketSelections.append(selection); cursor.setPosition(match.second.position()); cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); selection.cursor = cursor; mBracketSelections.append(selection); if (mHighlightBracketContents) { QTextCharFormat format; format.setBackground( mBracketHighlight.background() ); selection.format = format; cursor.setPosition(match.first.position()+1); cursor.setPosition(match.second.position(), QTextCursor::KeepAnchor); selection.cursor = cursor; mBracketSelections.append(selection); } } else { QTextEdit::ExtraSelection selection; selection.format = mBracketMismatchFormat; cursor.setPosition(match.first.position()); cursor.setPosition(match.second.position()+1, QTextCursor::KeepAnchor); selection.cursor = cursor; mBracketSelections.append(selection); } } updateExtraSelections(); }
void QScriptEdit::highlightCurrentLine() { updateExtraSelections(); }