fix find scope scelection, special case empty overlay selections

This commit is contained in:
mae
2009-12-02 14:12:59 +01:00
parent 89693f6347
commit 8c4ede6be4
3 changed files with 91 additions and 67 deletions

View File

@@ -2097,6 +2097,7 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
TextEditorOverlay *overlay = new TextEditorOverlay(this); TextEditorOverlay *overlay = new TextEditorOverlay(this);
overlay->addOverlaySelection(d->m_findScope, d->m_searchScopeFormat.background().color().darker(120), overlay->addOverlaySelection(d->m_findScope, d->m_searchScopeFormat.background().color().darker(120),
d->m_searchScopeFormat.background().color()); d->m_searchScopeFormat.background().color());
overlay->setAlpha(false);
overlay->paint(&painter, e->rect()); overlay->paint(&painter, e->rect());
delete overlay; delete overlay;
} }

View File

@@ -42,6 +42,7 @@ TextEditorOverlay::TextEditorOverlay(BaseTextEditor *editor)
m_borderWidth = 1; m_borderWidth = 1;
m_dropShadowWidth = 2; m_dropShadowWidth = 2;
m_editor = editor; m_editor = editor;
m_alpha = true;
m_viewport = editor->viewport(); m_viewport = editor->viewport();
} }
@@ -118,7 +119,6 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
if (begin.isNull() || end.isNull() || begin.position() > end.position()) if (begin.isNull() || end.isNull() || begin.position() > end.position())
return QPainterPath(); return QPainterPath();
QPointF offset = m_editor->contentOffset(); QPointF offset = m_editor->contentOffset();
QRect viewportRect = rect(); QRect viewportRect = rect();
QTextDocument *document = m_editor->document(); QTextDocument *document = m_editor->document();
@@ -134,6 +134,18 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
QVector<QRectF> selection; 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()) { for (; block.isValid() && block.blockNumber() <= end.blockNumber(); block = block.next()) {
if (! block.isVisible()) if (! block.isVisible())
continue; continue;
@@ -194,6 +206,7 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
if (blockGeometry.translated(offset).y() > 2*viewportRect.height()) if (blockGeometry.translated(offset).y() > 2*viewportRect.height())
break; break;
} }
}
if (selection.isEmpty()) if (selection.isEmpty())
@@ -288,6 +301,7 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
painter->save(); painter->save();
QColor penColor = fg; QColor penColor = fg;
if (m_alpha)
penColor.setAlpha(220); penColor.setAlpha(220);
QPen pen(penColor, m_borderWidth); QPen pen(penColor, m_borderWidth);
painter->translate(-.5, -.5); painter->translate(-.5, -.5);
@@ -295,6 +309,13 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
QRectF pathRect = path.controlPointRect(); QRectF pathRect = path.controlPointRect();
if (bg.isValid()) { 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()); QLinearGradient linearGrad(pathRect.topLeft(), pathRect.bottomLeft());
QColor col1 = fg.lighter(150); QColor col1 = fg.lighter(150);
col1.setAlpha(20); col1.setAlpha(20);
@@ -303,6 +324,7 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
linearGrad.setColorAt(0, col1); linearGrad.setColorAt(0, col1);
linearGrad.setColorAt(1, col2); linearGrad.setColorAt(1, col2);
painter->setBrush(QBrush(linearGrad)); painter->setBrush(QBrush(linearGrad));
}
} else { } else {
painter->setBrush(QBrush()); painter->setBrush(QBrush());
} }
@@ -311,12 +333,10 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
if (selection.m_dropShadow) { if (selection.m_dropShadow) {
painter->save(); painter->save();
painter->translate(m_dropShadowWidth, m_dropShadowWidth); QPainterPath shadow = path;
shadow.translate(m_dropShadowWidth, m_dropShadowWidth);
QPainterPath clip = path; painter->setClipPath(shadow.intersected(path));
clip.translate(-m_dropShadowWidth, -m_dropShadowWidth); painter->fillPath(shadow, QColor(0, 0, 0, 100));
painter->setClipPath(clip.intersected(path));
painter->fillPath(path, QColor(0, 0, 0, 100));
painter->restore(); painter->restore();
} }

View File

@@ -59,6 +59,7 @@ private:
bool m_visible; bool m_visible;
int m_borderWidth; int m_borderWidth;
int m_dropShadowWidth; int m_dropShadowWidth;
bool m_alpha;
public: public:
TextEditorOverlay(BaseTextEditor *editor); TextEditorOverlay(BaseTextEditor *editor);
@@ -74,6 +75,8 @@ public:
void update(); void update();
void setAlpha(bool enabled) { m_alpha = enabled; }
void clear(); void clear();
void addOverlaySelection(const QTextCursor &cursor, const QColor &fg, const QColor &bg, bool lockSize = false); 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); void addOverlaySelection(int begin, int end, const QColor &fg, const QColor &bg, bool lockSize = false, bool dropShadow = false);