"Fix" overwrite cursor for fake vim mode

It now behaves less logical but closer to what vim does. Since
this is the main target group, the change is probably right.
In detail: Overwrite cursor no longer shows *what* will get overwritten,
but instead it simply indicates *that* the editor is in overwrite mode,
with a more or less fixed size (fixed when the font is truly monospace).
This commit is contained in:
mae
2010-04-16 13:04:33 +02:00
parent 9bd3c7d8f5
commit db0a0f2996

View File

@@ -2596,6 +2596,7 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
painter.setPen(context.palette.text().color()); painter.setPen(context.palette.text().color());
} }
layout->setTextOption(option); layout->setTextOption(option);
layout->setFont(doc->defaultFont()); // this really should be in qplaintextedit when creating the layout!
int blpos = block.position(); int blpos = block.position();
int bllen = block.length(); int bllen = block.length();
@@ -2645,25 +2646,6 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
} }
selections += prioritySelections; selections += prioritySelections;
bool drawCursor = ((editable || true) // we want the cursor in read-only mode
&& context.cursorPosition >= blpos
&& context.cursorPosition < blpos + bllen);
bool drawCursorAsBlock = drawCursor && overwriteMode() ;
if (drawCursorAsBlock) {
if (context.cursorPosition == blpos + bllen - 1) {
drawCursorAsBlock = false;
} else {
QTextLayout::FormatRange o;
o.start = context.cursorPosition - blpos;
o.length = 1;
o.format.setForeground(palette().base());
o.format.setBackground(palette().text());
selections.append(o);
}
}
if (d->m_highlightCurrentLine && block == textCursorBlock) { if (d->m_highlightCurrentLine && block == textCursorBlock) {
QRectF rr = layout->lineForTextPosition(textCursor().positionInBlock()).rect(); QRectF rr = layout->lineForTextPosition(textCursor().positionInBlock()).rect();
@@ -2692,6 +2674,43 @@ void BaseTextEditor::paintEvent(QPaintEvent *e)
painter.fillRect(rr, d->m_currentLineFormat.background()); painter.fillRect(rr, d->m_currentLineFormat.background());
} }
bool drawCursor = ((editable || true) // we want the cursor in read-only mode
&& context.cursorPosition >= blpos
&& context.cursorPosition < blpos + bllen);
bool drawCursorAsBlock = drawCursor && overwriteMode() ;
if (drawCursorAsBlock) {
QTextLayout::FormatRange o;
int relativePos = context.cursorPosition - blpos;
o.start = relativePos;
o.length = 1;
o.format.setForeground(palette().base());
selections.append(o);
QTextLine line = layout->lineForTextPosition(relativePos);
qreal x = line.cursorToX(relativePos);
qreal w = 0;
if (relativePos < line.textLength() - line.textStart()) {
w = line.cursorToX(relativePos + 1) - x;
if (doc->characterAt(context.cursorPosition) == QLatin1Char('\t')) {
int space = QFontMetrics(layout->font()).width(QLatin1Char(' '));
if (w > space) {
x += w-space;
w = space;
}
}
} else
w = QFontMetrics(layout->font()).width(QLatin1Char(' ')); // in sync with QTextLine::draw()
QRectF rr = line.rect();
rr.moveTop(rr.top() + r.top());
rr.moveLeft(r.left() + x);
rr.setWidth(w);
painter.fillRect(rr, palette().text());
}
layout->draw(&painter, offset, selections, er); layout->draw(&painter, offset, selections, er);
if ((drawCursor && !drawCursorAsBlock) if ((drawCursor && !drawCursorAsBlock)