DiffEditor: Use settings' foreground color for highlights

Simplify drawing highlights by passing selections
into a drawing function.
Reduce selections which are covered completely by
other selections.

Task-number: QTCREATORBUG-12844
Change-Id: If282d5c881d25cac1b8db4ac8dfc45d47b0b8580
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
jkobus
2014-09-25 15:48:21 +02:00
committed by Orgad Shaneh
parent cb4f04f416
commit 5c6b1373c4
4 changed files with 116 additions and 66 deletions

View File

@@ -46,75 +46,116 @@ SelectableTextEditorWidget::~SelectableTextEditorWidget()
{ {
} }
void SelectableTextEditorWidget::innerPaintEvent(QPaintEvent *e) static QList<DiffSelection> subtractSelection(
const DiffSelection &minuendSelection,
const DiffSelection &subtrahendSelection)
{ {
QPainter painter(viewport()); // tha case that whole minuend is before the whole subtrahend
if (minuendSelection.end >= 0 && minuendSelection.end <= subtrahendSelection.start)
return QList<DiffSelection>() << minuendSelection;
QPointF offset = contentOffset(); // the case that whole subtrahend is before the whole minuend
QTextBlock firstBlock = firstVisibleBlock(); if (subtrahendSelection.end >= 0 && subtrahendSelection.end <= minuendSelection.start)
QTextBlock currentBlock = firstBlock; return QList<DiffSelection>() << minuendSelection;
while (currentBlock.isValid()) { bool makeMinuendSubtrahendStart = false;
if (currentBlock.isVisible()) { bool makeSubtrahendMinuendEnd = false;
qreal top = blockBoundingGeometry(currentBlock).translated(offset).top();
qreal bottom = top + blockBoundingRect(currentBlock).height();
if (top > e->rect().bottom()) if (minuendSelection.start < subtrahendSelection.start)
break; makeMinuendSubtrahendStart = true;
if (subtrahendSelection.end >= 0 && (subtrahendSelection.end < minuendSelection.end || minuendSelection.end < 0))
makeSubtrahendMinuendEnd = true;
if (bottom >= e->rect().top()) { QList<DiffSelection> diffList;
const int blockNumber = currentBlock.blockNumber(); if (makeMinuendSubtrahendStart)
diffList << DiffSelection(minuendSelection.start, subtrahendSelection.start, minuendSelection.format);
if (makeSubtrahendMinuendEnd)
diffList << DiffSelection(subtrahendSelection.end, minuendSelection.end, minuendSelection.format);
paintSelections(painter, m_selections.value(blockNumber), return diffList;
currentBlock, top); }
void SelectableTextEditorWidget::setSelections(const QMap<int, QList<DiffSelection> > &selections)
{
m_diffSelections.clear();
QMapIterator<int, QList<DiffSelection> > itBlock(selections);
while (itBlock.hasNext()) {
itBlock.next();
const QList<DiffSelection> diffSelections = itBlock.value();
QList<DiffSelection> workingList;
for (int i = 0; i < diffSelections.count(); i++) {
const DiffSelection &diffSelection = diffSelections.at(i);
if (diffSelection.start == -1 && diffSelection.end == 0)
continue;
if (diffSelection.start == diffSelection.end && diffSelection.start >= 0)
continue;
int j = 0;
while (j < workingList.count()) {
const DiffSelection existingSelection = workingList.takeAt(j);
const QList<DiffSelection> newSelection = subtractSelection(existingSelection, diffSelection);
for (int k = 0; k < newSelection.count(); k++)
workingList.insert(j + k, newSelection.at(k));
j += newSelection.count();
}
workingList.append(diffSelection);
}
const int blockNumber = itBlock.key();
QVector<QTextLayout::FormatRange> selList;
for (int i = 0; i < workingList.count(); i++) {
const DiffSelection &diffSelection = workingList.at(i);
if (diffSelection.format) {
QTextLayout::FormatRange formatRange;
formatRange.start = diffSelection.start;
if (formatRange.start < 0)
formatRange.start = 0;
formatRange.length = diffSelection.end < 0
? INT_MAX
: diffSelection.end - diffSelection.start;
formatRange.format = *diffSelection.format;
if (diffSelection.end < 0)
formatRange.format.setProperty(QTextFormat::FullWidthSelection, true);
selList.append(formatRange);
} }
} }
currentBlock = currentBlock.next(); m_diffSelections.insert(blockNumber, workingList);
} }
} }
void SelectableTextEditorWidget::paintSelections(QPainter &painter, void SelectableTextEditorWidget::paintBlock(QPainter *painter,
const QList<DiffSelection> &selections, const QTextBlock &block,
const QTextBlock &block, const QPointF &offset,
int top) const QVector<QTextLayout::FormatRange> &selections,
const QRect &clipRect) const
{ {
QPointF offset = contentOffset(); const int blockNumber = block.blockNumber();
painter.save(); QList<DiffSelection> diffs = m_diffSelections.value(blockNumber);
QTextLayout *layout = block.layout(); QVector<QTextLayout::FormatRange> newSelections;
QTextLine textLine = layout->lineAt(0); for (int i = 0; i < diffs.count(); i++) {
QRectF lineRect = textLine.naturalTextRect().translated(offset.x(), top); const DiffSelection &diffSelection = diffs.at(i);
QRect clipRect = contentsRect(); if (diffSelection.format) {
painter.setClipRect(clipRect); QTextLayout::FormatRange formatRange;
for (int i = 0; i < selections.count(); i++) { formatRange.start = qMax(0, diffSelection.start);
const DiffSelection &selection = selections.at(i); const int end = diffSelection.end < 0
? block.text().count() + 1
: qMin(block.text().count(), diffSelection.end);
if (!selection.format) formatRange.length = end - formatRange.start;
continue; formatRange.format = *diffSelection.format;
if (selection.start == -1 && selection.end == 0) if (diffSelection.end < 0)
continue; formatRange.format.setProperty(QTextFormat::FullWidthSelection, true);
if (selection.start == selection.end && selection.start >= 0) newSelections.append(formatRange);
continue; }
painter.save();
const QBrush &brush = selection.format->background();
painter.setPen(brush.color());
painter.setBrush(brush);
const int x1 = selection.start <= 0
? -1
: textLine.cursorToX(selection.start) + offset.x();
const int x2 = selection.end < 0
? clipRect.right()
: textLine.cursorToX(selection.end) + offset.x();
painter.drawRect(QRectF(QPointF(x1, lineRect.top()),
QPointF(x2, lineRect.bottom())));
painter.restore();
} }
painter.restore(); newSelections += selections;
}
BaseTextEditorWidget::paintBlock(painter, block, offset, newSelections, clipRect);
}
} // namespace DiffEditor } // namespace DiffEditor

View File

@@ -54,21 +54,19 @@ class DIFFEDITOR_EXPORT SelectableTextEditorWidget
public: public:
SelectableTextEditorWidget(Core::Id id, QWidget *parent = 0); SelectableTextEditorWidget(Core::Id id, QWidget *parent = 0);
~SelectableTextEditorWidget(); ~SelectableTextEditorWidget();
void setSelections(const QMap<int, QList<DiffSelection> > &selections) { void setSelections(const QMap<int, QList<DiffSelection> > &selections);
m_selections = selections;
}
private: private:
void innerPaintEvent(QPaintEvent *e); void paintBlock(QPainter *painter,
void paintSelections(QPainter &painter, const QTextBlock &block,
const QList<DiffSelection> &selections, const QPointF &offset,
const QTextBlock &block, const QVector<QTextLayout::FormatRange> &selections,
int top); const QRect &clipRect) const;
// block number, list of ranges // block number, list of ranges
// DiffSelection.start - can be -1 (continues from the previous line) // DiffSelection.start - can be -1 (continues from the previous line)
// DiffSelection.end - can be -1 (spans to the end of line, even after the last character in line) // DiffSelection.end - can be -1 (spans to the end of line, even after the last character in line)
QMap<int, QList<DiffSelection> > m_selections; QMap<int, QList<DiffSelection> > m_diffSelections;
}; };
} // namespace DiffEditor } // namespace DiffEditor

View File

@@ -3454,7 +3454,6 @@ void BaseTextEditorWidget::paintEvent(QPaintEvent *e)
ifdefedOutFormat.background()); ifdefedOutFormat.background());
} }
innerPaintEvent(e);
/* /*
Here comes an almost verbatim copy of Here comes an almost verbatim copy of
QPlainTextEdit::paintEvent() so we can adjust the extra QPlainTextEdit::paintEvent() so we can adjust the extra
@@ -3902,8 +3901,7 @@ void BaseTextEditorWidget::paintEvent(QPaintEvent *e)
} }
paintBlock(&painter, block, offset, selections, er);
layout->draw(&painter, offset, selections, er);
if ((drawCursor && !drawCursorAsBlock) if ((drawCursor && !drawCursorAsBlock)
|| (editable && context.cursorPosition < -1 && !layout->preeditAreaText().isEmpty())) { || (editable && context.cursorPosition < -1 && !layout->preeditAreaText().isEmpty())) {
@@ -4113,6 +4111,15 @@ void BaseTextEditorWidget::paintEvent(QPaintEvent *e)
} }
} }
void BaseTextEditorWidget::paintBlock(QPainter *painter,
const QTextBlock &block,
const QPointF &offset,
const QVector<QTextLayout::FormatRange> &selections,
const QRect &clipRect) const
{
block.layout()->draw(painter, offset, selections, clipRect);
}
int BaseTextEditorWidget::visibleFoldedBlockNumber() const int BaseTextEditorWidget::visibleFoldedBlockNumber() const
{ {
return d->visibleFoldedBlockNumber; return d->visibleFoldedBlockNumber;

View File

@@ -495,7 +495,11 @@ protected:
bool viewportEvent(QEvent *event); bool viewportEvent(QEvent *event);
void resizeEvent(QResizeEvent *); void resizeEvent(QResizeEvent *);
void paintEvent(QPaintEvent *); void paintEvent(QPaintEvent *);
virtual void innerPaintEvent(QPaintEvent *) {} virtual void paintBlock(QPainter *painter,
const QTextBlock &block,
const QPointF &offset,
const QVector<QTextLayout::FormatRange> &selections,
const QRect &clipRect) const;
void timerEvent(QTimerEvent *); void timerEvent(QTimerEvent *);
void mouseMoveEvent(QMouseEvent *); void mouseMoveEvent(QMouseEvent *);
void mousePressEvent(QMouseEvent *); void mousePressEvent(QMouseEvent *);