forked from qt-creator/qt-creator
fix find scope scelection, special case empty overlay selections
This commit is contained in:
@@ -2097,6 +2097,7 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
|
||||
TextEditorOverlay *overlay = new TextEditorOverlay(this);
|
||||
overlay->addOverlaySelection(d->m_findScope, d->m_searchScopeFormat.background().color().darker(120),
|
||||
d->m_searchScopeFormat.background().color());
|
||||
overlay->setAlpha(false);
|
||||
overlay->paint(&painter, e->rect());
|
||||
delete overlay;
|
||||
}
|
||||
|
@@ -42,6 +42,7 @@ TextEditorOverlay::TextEditorOverlay(BaseTextEditor *editor)
|
||||
m_borderWidth = 1;
|
||||
m_dropShadowWidth = 2;
|
||||
m_editor = editor;
|
||||
m_alpha = true;
|
||||
m_viewport = editor->viewport();
|
||||
}
|
||||
|
||||
@@ -118,7 +119,6 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
|
||||
if (begin.isNull() || end.isNull() || begin.position() > end.position())
|
||||
return QPainterPath();
|
||||
|
||||
|
||||
QPointF offset = m_editor->contentOffset();
|
||||
QRect viewportRect = rect();
|
||||
QTextDocument *document = m_editor->document();
|
||||
@@ -134,6 +134,18 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
|
||||
|
||||
QVector<QRectF> selection;
|
||||
|
||||
if (begin.position() == end.position()) {
|
||||
// special case empty selections
|
||||
const QRectF blockGeometry = m_editor->blockBoundingGeometry(block);
|
||||
QTextLayout *blockLayout = block.layout();
|
||||
int pos = begin.position() - begin.block().position();
|
||||
QTextLine line = blockLayout->lineForTextPosition(pos);
|
||||
QRectF lineRect = line.naturalTextRect();
|
||||
int x = line.cursorToX(pos);
|
||||
lineRect.setLeft(x - m_borderWidth);
|
||||
lineRect.setRight(x + m_borderWidth);
|
||||
selection += lineRect.translated(blockGeometry.topLeft());
|
||||
} else {
|
||||
for (; block.isValid() && block.blockNumber() <= end.blockNumber(); block = block.next()) {
|
||||
if (! block.isVisible())
|
||||
continue;
|
||||
@@ -194,6 +206,7 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
|
||||
if (blockGeometry.translated(offset).y() > 2*viewportRect.height())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (selection.isEmpty())
|
||||
@@ -288,6 +301,7 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
|
||||
|
||||
painter->save();
|
||||
QColor penColor = fg;
|
||||
if (m_alpha)
|
||||
penColor.setAlpha(220);
|
||||
QPen pen(penColor, m_borderWidth);
|
||||
painter->translate(-.5, -.5);
|
||||
@@ -295,6 +309,13 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
|
||||
QRectF pathRect = path.controlPointRect();
|
||||
|
||||
if (bg.isValid()) {
|
||||
if (!m_alpha || begin.blockNumber() != end.blockNumber()) {
|
||||
// gradients are too slow for larger selections :(
|
||||
QColor col = bg;
|
||||
if (m_alpha)
|
||||
col.setAlpha(50);
|
||||
painter->setBrush(col);
|
||||
} else {
|
||||
QLinearGradient linearGrad(pathRect.topLeft(), pathRect.bottomLeft());
|
||||
QColor col1 = fg.lighter(150);
|
||||
col1.setAlpha(20);
|
||||
@@ -303,6 +324,7 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
|
||||
linearGrad.setColorAt(0, col1);
|
||||
linearGrad.setColorAt(1, col2);
|
||||
painter->setBrush(QBrush(linearGrad));
|
||||
}
|
||||
} else {
|
||||
painter->setBrush(QBrush());
|
||||
}
|
||||
@@ -311,12 +333,10 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
|
||||
|
||||
if (selection.m_dropShadow) {
|
||||
painter->save();
|
||||
painter->translate(m_dropShadowWidth, m_dropShadowWidth);
|
||||
|
||||
QPainterPath clip = path;
|
||||
clip.translate(-m_dropShadowWidth, -m_dropShadowWidth);
|
||||
painter->setClipPath(clip.intersected(path));
|
||||
painter->fillPath(path, QColor(0, 0, 0, 100));
|
||||
QPainterPath shadow = path;
|
||||
shadow.translate(m_dropShadowWidth, m_dropShadowWidth);
|
||||
painter->setClipPath(shadow.intersected(path));
|
||||
painter->fillPath(shadow, QColor(0, 0, 0, 100));
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
|
@@ -59,6 +59,7 @@ private:
|
||||
bool m_visible;
|
||||
int m_borderWidth;
|
||||
int m_dropShadowWidth;
|
||||
bool m_alpha;
|
||||
|
||||
public:
|
||||
TextEditorOverlay(BaseTextEditor *editor);
|
||||
@@ -74,6 +75,8 @@ public:
|
||||
|
||||
void update();
|
||||
|
||||
void setAlpha(bool enabled) { m_alpha = enabled; }
|
||||
|
||||
void clear();
|
||||
void addOverlaySelection(const QTextCursor &cursor, const QColor &fg, const QColor &bg, bool lockSize = false);
|
||||
void addOverlaySelection(int begin, int end, const QColor &fg, const QColor &bg, bool lockSize = false, bool dropShadow = false);
|
||||
|
Reference in New Issue
Block a user