forked from qt-creator/qt-creator
refactor search result overlay painting
This commit is contained in:
@@ -177,7 +177,6 @@ BaseTextEditor::BaseTextEditor(QWidget *parent)
|
|||||||
|
|
||||||
d->m_overlay = new TextEditorOverlay(this);
|
d->m_overlay = new TextEditorOverlay(this);
|
||||||
d->m_searchResultOverlay = new TextEditorOverlay(this);
|
d->m_searchResultOverlay = new TextEditorOverlay(this);
|
||||||
d->m_searchResultUnderlay = new TextEditorOverlay(this);
|
|
||||||
|
|
||||||
d->setupDocumentSignals(d->m_document);
|
d->setupDocumentSignals(d->m_document);
|
||||||
d->setupDocumentSignals(d->m_document);
|
d->setupDocumentSignals(d->m_document);
|
||||||
@@ -1865,8 +1864,7 @@ QTextBlock BaseTextEditor::collapsedBlockAt(const QPoint &pos, QRect *box) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BaseTextEditorPrivate::highlightSearchResults(const QTextBlock &block,
|
void BaseTextEditorPrivate::highlightSearchResults(const QTextBlock &block,
|
||||||
TextEditorOverlay *overlay,
|
TextEditorOverlay *overlay)
|
||||||
QVector<QTextLayout::FormatRange> *selections )
|
|
||||||
{
|
{
|
||||||
if (m_searchExpr.isEmpty())
|
if (m_searchExpr.isEmpty())
|
||||||
return;
|
return;
|
||||||
@@ -1890,13 +1888,6 @@ void BaseTextEditorPrivate::highlightSearchResults(const QTextBlock &block,
|
|||||||
if (m_findScope.isNull()
|
if (m_findScope.isNull()
|
||||||
|| (blockPosition + idx >= m_findScope.selectionStart()
|
|| (blockPosition + idx >= m_findScope.selectionStart()
|
||||||
&& blockPosition + idx + l <= m_findScope.selectionEnd())) {
|
&& blockPosition + idx + l <= m_findScope.selectionEnd())) {
|
||||||
if (selections) {
|
|
||||||
QTextLayout::FormatRange selection;
|
|
||||||
selection.start = idx;
|
|
||||||
selection.length = l;
|
|
||||||
selection.format = m_searchResultFormat;
|
|
||||||
selections->append(selection);
|
|
||||||
}
|
|
||||||
|
|
||||||
overlay->addOverlaySelection(blockPosition + idx,
|
overlay->addOverlaySelection(blockPosition + idx,
|
||||||
blockPosition + idx + l,
|
blockPosition + idx + l,
|
||||||
@@ -2096,7 +2087,6 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
|
|||||||
// painter.setClipRect(er);
|
// painter.setClipRect(er);
|
||||||
|
|
||||||
bool editable = !isReadOnly();
|
bool editable = !isReadOnly();
|
||||||
|
|
||||||
QTextBlock block = firstVisibleBlock();
|
QTextBlock block = firstVisibleBlock();
|
||||||
|
|
||||||
QAbstractTextDocumentLayout::PaintContext context = getPaintContext();
|
QAbstractTextDocumentLayout::PaintContext context = getPaintContext();
|
||||||
@@ -2134,12 +2124,39 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
|
|||||||
QPen cursor_pen;
|
QPen cursor_pen;
|
||||||
|
|
||||||
d->m_searchResultOverlay->clear();
|
d->m_searchResultOverlay->clear();
|
||||||
d->m_searchResultUnderlay->clear();
|
if (!d->m_searchExpr.isEmpty()) { // first pass for the search result overlays
|
||||||
|
|
||||||
|
const int margin = 5;
|
||||||
|
QTextBlock blockFP = block;
|
||||||
|
QPointF offsetFP = offset;
|
||||||
|
while (blockFP.isValid()) {
|
||||||
|
QRectF r = blockBoundingRect(blockFP).translated(offsetFP);
|
||||||
|
|
||||||
|
if (r.bottom() >= er.top() - margin && r.top() <= er.bottom() + margin) {
|
||||||
|
d->highlightSearchResults(blockFP,
|
||||||
|
d->m_searchResultOverlay);
|
||||||
|
}
|
||||||
|
offsetFP.ry() += r.height();
|
||||||
|
|
||||||
|
if (offsetFP.y() > viewportRect.height() + margin)
|
||||||
|
break;
|
||||||
|
|
||||||
|
blockFP = blockFP.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end first pass
|
||||||
|
|
||||||
|
d->m_searchResultOverlay->fill(&painter,
|
||||||
|
d->m_searchResultFormat.background().color(),
|
||||||
|
e->rect());
|
||||||
|
|
||||||
|
|
||||||
while (block.isValid()) {
|
while (block.isValid()) {
|
||||||
|
|
||||||
QRectF r = blockBoundingRect(block).translated(offset);
|
QRectF r = blockBoundingRect(block).translated(offset);
|
||||||
|
|
||||||
|
if (r.bottom() >= er.top() && r.top() <= er.bottom()) {
|
||||||
|
|
||||||
if (TextEditDocumentLayout::ifdefedOut(block)) {
|
if (TextEditDocumentLayout::ifdefedOut(block)) {
|
||||||
QRectF rr = r;
|
QRectF rr = r;
|
||||||
rr.setWidth(viewport()->width());
|
rr.setWidth(viewport()->width());
|
||||||
@@ -2187,8 +2204,6 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
|
|||||||
layout->setTextOption(option);
|
layout->setTextOption(option);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (r.bottom() >= er.top() && r.top() <= er.bottom()) {
|
|
||||||
|
|
||||||
int blpos = block.position();
|
int blpos = block.position();
|
||||||
int bllen = block.length();
|
int bllen = block.length();
|
||||||
|
|
||||||
@@ -2230,9 +2245,6 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
|
|||||||
selections.append(o);
|
selections.append(o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
d->highlightSearchResults(block, d->m_searchResultUnderlay, &selections);
|
|
||||||
d->m_searchResultOverlay->m_selections.append(d->m_searchResultUnderlay->m_selections);
|
|
||||||
selections += prioritySelections;
|
selections += prioritySelections;
|
||||||
|
|
||||||
bool drawCursor = ((editable || true) // we want the cursor in read-only mode
|
bool drawCursor = ((editable || true) // we want the cursor in read-only mode
|
||||||
@@ -2254,13 +2266,6 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d->m_searchResultUnderlay && !d->m_searchExpr.isEmpty()) {
|
|
||||||
d->m_searchResultUnderlay->fill(&painter,
|
|
||||||
d->m_searchResultFormat.background().color(),
|
|
||||||
e->rect());
|
|
||||||
d->m_searchResultUnderlay->clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
layout->draw(&painter, offset, selections, er);
|
layout->draw(&painter, offset, selections, er);
|
||||||
|
|
||||||
if ((drawCursor && !drawCursorAsBlock)
|
if ((drawCursor && !drawCursorAsBlock)
|
||||||
@@ -2276,17 +2281,15 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
|
|||||||
cursor_cpos = cpos;
|
cursor_cpos = cpos;
|
||||||
cursor_pen = painter.pen();
|
cursor_pen = painter.pen();
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (r.bottom() >= er.top()-10 && r.top() <= er.bottom()+10) {
|
|
||||||
// search result overlays can cover adjacent blocks
|
|
||||||
d->highlightSearchResults(block, d->m_searchResultOverlay);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
offset.ry() += r.height();
|
offset.ry() += r.height();
|
||||||
|
|
||||||
if (offset.y() > viewportRect.height())
|
if (offset.y() > viewportRect.height())
|
||||||
break;
|
break;
|
||||||
|
|
||||||
block = block.next();
|
block = block.next();
|
||||||
|
|
||||||
if (!block.isVisible()) {
|
if (!block.isVisible()) {
|
||||||
if (block.blockNumber() == d->visibleCollapsedBlockNumber) {
|
if (block.blockNumber() == d->visibleCollapsedBlockNumber) {
|
||||||
visibleCollapsedBlock = block;
|
visibleCollapsedBlock = block;
|
||||||
@@ -2496,11 +2499,10 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
|
|||||||
if (d->m_overlay && d->m_overlay->isVisible())
|
if (d->m_overlay && d->m_overlay->isVisible())
|
||||||
d->m_overlay->paint(&painter, e->rect());
|
d->m_overlay->paint(&painter, e->rect());
|
||||||
|
|
||||||
if (d->m_searchResultOverlay && !d->m_searchExpr.isEmpty())
|
if (!d->m_searchResultOverlay->isEmpty()) {
|
||||||
d->m_searchResultOverlay->paint(&painter, e->rect());
|
d->m_searchResultOverlay->paint(&painter, e->rect());
|
||||||
// d->m_searchResultOverlay->paintInverted(&painter, e->rect(), d->m_searchResultFormat.background().color());
|
d->m_searchResultOverlay->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// draw the cursor last, on top of everything
|
// draw the cursor last, on top of everything
|
||||||
if (cursor_layout) {
|
if (cursor_layout) {
|
||||||
|
@@ -189,7 +189,6 @@ public:
|
|||||||
|
|
||||||
TextEditorOverlay *m_overlay;
|
TextEditorOverlay *m_overlay;
|
||||||
TextEditorOverlay *m_searchResultOverlay;
|
TextEditorOverlay *m_searchResultOverlay;
|
||||||
TextEditorOverlay *m_searchResultUnderlay;
|
|
||||||
|
|
||||||
QBasicTimer collapsedBlockTimer;
|
QBasicTimer collapsedBlockTimer;
|
||||||
int visibleCollapsedBlockNumber;
|
int visibleCollapsedBlockNumber;
|
||||||
@@ -224,9 +223,7 @@ public:
|
|||||||
QTextCharFormat m_searchScopeFormat;
|
QTextCharFormat m_searchScopeFormat;
|
||||||
QTextCharFormat m_currentLineFormat;
|
QTextCharFormat m_currentLineFormat;
|
||||||
QTextCharFormat m_currentLineNumberFormat;
|
QTextCharFormat m_currentLineNumberFormat;
|
||||||
void highlightSearchResults(const QTextBlock &block,
|
void highlightSearchResults(const QTextBlock &block, TextEditorOverlay *overlay);
|
||||||
TextEditorOverlay *overlay,
|
|
||||||
QVector<QTextLayout::FormatRange> *selections = 0);
|
|
||||||
QTimer *m_delayedUpdateTimer;
|
QTimer *m_delayedUpdateTimer;
|
||||||
|
|
||||||
BaseTextEditorEditable *m_editable;
|
BaseTextEditorEditable *m_editable;
|
||||||
|
@@ -61,6 +61,8 @@ void TextEditorOverlay::setVisible(bool b)
|
|||||||
|
|
||||||
void TextEditorOverlay::clear()
|
void TextEditorOverlay::clear()
|
||||||
{
|
{
|
||||||
|
if (m_selections.isEmpty())
|
||||||
|
return;
|
||||||
m_selections.clear();
|
m_selections.clear();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
@@ -90,7 +92,10 @@ void TextEditorOverlay::addOverlaySelection(int begin, int end,
|
|||||||
|
|
||||||
selection.m_dropShadow = dropShadow;
|
selection.m_dropShadow = dropShadow;
|
||||||
|
|
||||||
m_selections += selection;
|
if (dropShadow)
|
||||||
|
m_selections.append(selection);
|
||||||
|
else
|
||||||
|
m_selections.prepend(selection);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user