forked from qt-creator/qt-creator
Editor: do not paint rounded corners in editor overlay
Calculating and optimizing a painter path with rounded corners can freeze or under certain circumstances even crash Qt Creator. Since we are using a less rounded design nowadays anyways we can also cut this overhead. Fixes: QTCREATORBUG-21056 Change-Id: I9cd5eaea00ab040d529f51ea6d2f337dd0a1f6b8 Reviewed-by: Alessandro Portale <alessandro.portale@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -16,11 +16,12 @@
|
|||||||
using namespace TextEditor;
|
using namespace TextEditor;
|
||||||
using namespace TextEditor::Internal;
|
using namespace TextEditor::Internal;
|
||||||
|
|
||||||
|
constexpr int borderWidth = 1;
|
||||||
|
|
||||||
TextEditorOverlay::TextEditorOverlay(TextEditorWidget *editor) :
|
TextEditorOverlay::TextEditorOverlay(TextEditorWidget *editor) :
|
||||||
QObject(editor),
|
QObject(editor),
|
||||||
m_visible(false),
|
m_visible(false),
|
||||||
m_alpha(true),
|
m_alpha(true),
|
||||||
m_borderWidth(1),
|
|
||||||
m_dropShadowWidth(2),
|
m_dropShadowWidth(2),
|
||||||
m_firstSelectionOriginalBegin(-1),
|
m_firstSelectionOriginalBegin(-1),
|
||||||
m_editor(editor),
|
m_editor(editor),
|
||||||
@@ -112,19 +113,15 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
|
|||||||
QTextDocument *document = m_editor->document();
|
QTextDocument *document = m_editor->document();
|
||||||
|
|
||||||
if (m_editor->blockBoundingGeometry(begin.block()).translated(offset).top() > clip.bottom() + 10
|
if (m_editor->blockBoundingGeometry(begin.block()).translated(offset).top() > clip.bottom() + 10
|
||||||
|| m_editor->blockBoundingGeometry(end.block()).translated(offset).bottom() < clip.top() - 10
|
|| m_editor->blockBoundingGeometry(end.block()).translated(offset).bottom()
|
||||||
)
|
< clip.top() - 10) {
|
||||||
return QPainterPath(); // nothing of the selection is visible
|
return QPainterPath(); // nothing of the selection is visible
|
||||||
|
}
|
||||||
|
|
||||||
QTextBlock block = begin.block();
|
QTextBlock block = begin.block();
|
||||||
|
|
||||||
if (block.blockNumber() < m_editor->firstVisibleBlock().blockNumber() - 4)
|
if (block.blockNumber() < m_editor->firstVisibleBlock().blockNumber() - 1)
|
||||||
block = m_editor->document()->findBlockByNumber(m_editor->firstVisibleBlock().blockNumber() - 4);
|
block = document->findBlockByNumber(m_editor->firstVisibleBlock().blockNumber() - 1);
|
||||||
|
|
||||||
bool inSelection = false;
|
|
||||||
|
|
||||||
QVector<QRectF> selection;
|
|
||||||
|
|
||||||
if (begin.position() == end.position()) {
|
if (begin.position() == end.position()) {
|
||||||
// special case empty selections
|
// special case empty selections
|
||||||
@@ -133,11 +130,20 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
|
|||||||
int pos = begin.position() - begin.block().position();
|
int pos = begin.position() - begin.block().position();
|
||||||
QTextLine line = blockLayout->lineForTextPosition(pos);
|
QTextLine line = blockLayout->lineForTextPosition(pos);
|
||||||
QRectF lineRect = line.naturalTextRect();
|
QRectF lineRect = line.naturalTextRect();
|
||||||
|
lineRect = lineRect.translated(blockGeometry.topLeft());
|
||||||
int x = line.cursorToX(pos);
|
int x = line.cursorToX(pos);
|
||||||
lineRect.setLeft(x - m_borderWidth);
|
lineRect.setLeft(x - borderWidth);
|
||||||
lineRect.setRight(x + m_borderWidth);
|
lineRect.setRight(x + borderWidth);
|
||||||
selection += lineRect.translated(blockGeometry.topLeft());
|
QPainterPath path;
|
||||||
} else {
|
path.addRect(lineRect);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
QPointF top; // *------|
|
||||||
|
QPointF left; // *---| |
|
||||||
|
QPointF right; // | |---*
|
||||||
|
QPointF bottom; // |------*
|
||||||
|
|
||||||
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;
|
||||||
@@ -145,22 +151,17 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
|
|||||||
const QRectF blockGeometry = m_editor->blockBoundingGeometry(block);
|
const QRectF blockGeometry = m_editor->blockBoundingGeometry(block);
|
||||||
QTextLayout *blockLayout = block.layout();
|
QTextLayout *blockLayout = block.layout();
|
||||||
|
|
||||||
QTextLine line = blockLayout->lineAt(0);
|
int firstLine = 0;
|
||||||
bool firstOrLastBlock = false;
|
QTextLine line = blockLayout->lineAt(firstLine);
|
||||||
|
|
||||||
int beginChar = 0;
|
int beginChar = 0;
|
||||||
if (!inSelection) {
|
|
||||||
if (block == begin.block()) {
|
if (block == begin.block()) {
|
||||||
beginChar = begin.positionInBlock();
|
beginChar = begin.positionInBlock();
|
||||||
line = blockLayout->lineForTextPosition(beginChar);
|
line = blockLayout->lineForTextPosition(beginChar);
|
||||||
firstOrLastBlock = true;
|
firstLine = line.lineNumber();
|
||||||
}
|
const int lineEnd = line.textStart() + line.textLength();
|
||||||
inSelection = true;
|
if (beginChar == lineEnd)
|
||||||
} else {
|
continue;
|
||||||
// while (beginChar < block.length() && document->characterAt(block.position() + beginChar).isSpace())
|
|
||||||
// ++beginChar;
|
|
||||||
// if (beginChar == block.length())
|
|
||||||
// beginChar = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int lastLine = blockLayout->lineCount() - 1;
|
int lastLine = blockLayout->lineCount() - 1;
|
||||||
@@ -168,125 +169,69 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
|
|||||||
if (block == end.block()) {
|
if (block == end.block()) {
|
||||||
endChar = end.positionInBlock();
|
endChar = end.positionInBlock();
|
||||||
lastLine = blockLayout->lineForTextPosition(endChar).lineNumber();
|
lastLine = blockLayout->lineForTextPosition(endChar).lineNumber();
|
||||||
inSelection = false;
|
if (endChar == beginChar)
|
||||||
firstOrLastBlock = true;
|
break; // Do not expand overlay to empty selection at end
|
||||||
} else {
|
} else {
|
||||||
endChar = block.length();
|
endChar = block.length();
|
||||||
while (endChar > beginChar && document->characterAt(block.position() + endChar - 1).isSpace())
|
while (endChar > beginChar
|
||||||
|
&& document->characterAt(block.position() + endChar - 1).isSpace())
|
||||||
--endChar;
|
--endChar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int i = firstLine; i <= lastLine; ++i) {
|
||||||
|
line = blockLayout->lineAt(i);
|
||||||
QRectF lineRect = line.naturalTextRect();
|
QRectF lineRect = line.naturalTextRect();
|
||||||
if (beginChar < endChar) {
|
if (i == firstLine && beginChar > 0)
|
||||||
lineRect.setLeft(line.cursorToX(beginChar));
|
lineRect.setLeft(line.cursorToX(beginChar));
|
||||||
if (line.lineNumber() == lastLine)
|
if (line.lineNumber() == lastLine)
|
||||||
lineRect.setRight(line.cursorToX(endChar));
|
lineRect.setRight(line.cursorToX(endChar));
|
||||||
selection += lineRect.translated(blockGeometry.topLeft());
|
lineRect = lineRect.translated(blockGeometry.topLeft());
|
||||||
|
if (top.isNull())
|
||||||
for (int lineIndex = line.lineNumber()+1; lineIndex <= lastLine; ++lineIndex) {
|
top = lineRect.topLeft();
|
||||||
line = blockLayout->lineAt(lineIndex);
|
else if (left.isNull())
|
||||||
lineRect = line.naturalTextRect();
|
left = lineRect.topLeft();
|
||||||
if (lineIndex == lastLine)
|
else
|
||||||
lineRect.setRight(line.cursorToX(endChar));
|
left.setX(std::min(left.x(), lineRect.left()));
|
||||||
selection += lineRect.translated(blockGeometry.topLeft());
|
if (i == lastLine && block == end.block() && lineRect.right() <= right.x())
|
||||||
|
bottom = lineRect.bottomRight();
|
||||||
|
else
|
||||||
|
right = {std::max(lineRect.right(), right.x()), lineRect.bottom()};
|
||||||
}
|
}
|
||||||
} else { // empty lines
|
|
||||||
const int emptyLineSelectionSize = 16;
|
|
||||||
if (!firstOrLastBlock && !selection.isEmpty()) { // middle
|
|
||||||
lineRect.setLeft(selection.last().left());
|
|
||||||
} else if (inSelection) { // first line
|
|
||||||
lineRect.setLeft(line.cursorToX(beginChar));
|
|
||||||
} else { // last line
|
|
||||||
if (endChar == 0)
|
|
||||||
break;
|
|
||||||
lineRect.setLeft(line.cursorToX(endChar) - emptyLineSelectionSize);
|
|
||||||
}
|
|
||||||
lineRect.setRight(lineRect.left() + emptyLineSelectionSize);
|
|
||||||
selection += lineRect.translated(blockGeometry.topLeft());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!inSelection)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (blockGeometry.translated(offset).y() > 2 * viewportRect.height())
|
if (blockGeometry.translated(offset).y() > 2 * viewportRect.height())
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if (top.isNull())
|
||||||
|
return {};
|
||||||
|
|
||||||
if (selection.isEmpty())
|
if (bottom.isNull())
|
||||||
return QPainterPath();
|
bottom = right;
|
||||||
|
|
||||||
QVector<QPointF> points;
|
if (left.isNull())
|
||||||
|
left = top;
|
||||||
|
|
||||||
const int margin = m_borderWidth/2;
|
if (right.isNull())
|
||||||
const int extra = 0;
|
right = bottom;
|
||||||
|
|
||||||
const QRectF &firstSelection = selection.at(0);
|
|
||||||
points += (firstSelection.topLeft() + firstSelection.topRight()) / 2 + QPointF(0, -margin);
|
|
||||||
points += firstSelection.topRight() + QPointF(margin+1, -margin);
|
|
||||||
points += firstSelection.bottomRight() + QPointF(margin+1, 0);
|
|
||||||
|
|
||||||
const int count = selection.count();
|
|
||||||
for (int i = 1; i < count-1; ++i) {
|
|
||||||
qreal x = std::max({selection.at(i - 1).right(),
|
|
||||||
selection.at(i).right(),
|
|
||||||
selection.at(i + 1).right()})
|
|
||||||
+ margin;
|
|
||||||
|
|
||||||
points += QPointF(x+1, selection.at(i).top());
|
|
||||||
points += QPointF(x+1, selection.at(i).bottom());
|
|
||||||
}
|
|
||||||
|
|
||||||
const QRectF &lastSelection = selection.at(count-1);
|
|
||||||
points += lastSelection.topRight() + QPointF(margin+1, 0);
|
|
||||||
points += lastSelection.bottomRight() + QPointF(margin+1, margin+extra);
|
|
||||||
points += lastSelection.bottomLeft() + QPointF(-margin, margin+extra);
|
|
||||||
points += lastSelection.topLeft() + QPointF(-margin, 0);
|
|
||||||
|
|
||||||
for (int i = count-2; i > 0; --i) {
|
|
||||||
qreal x = std::min({selection.at(i - 1).left(),
|
|
||||||
selection.at(i).left(),
|
|
||||||
selection.at(i + 1).left()})
|
|
||||||
- margin;
|
|
||||||
|
|
||||||
points += QPointF(x, selection.at(i).bottom()+extra);
|
|
||||||
points += QPointF(x, selection.at(i).top());
|
|
||||||
}
|
|
||||||
|
|
||||||
points += firstSelection.bottomLeft() + QPointF(-margin, extra);
|
|
||||||
points += firstSelection.topLeft() + QPointF(-margin, -margin);
|
|
||||||
|
|
||||||
|
constexpr QPointF marginOffset = {borderWidth, borderWidth};
|
||||||
|
right += marginOffset;
|
||||||
|
bottom += marginOffset;
|
||||||
|
|
||||||
QPainterPath path;
|
QPainterPath path;
|
||||||
const int corner = 4;
|
path.moveTo(top);
|
||||||
path.moveTo(points.at(0));
|
path.lineTo(right.x(), top.y());
|
||||||
points += points.at(0);
|
path.lineTo(right);
|
||||||
QPointF previous = points.at(0);
|
path.lineTo(bottom.x(), right.y());
|
||||||
for (int i = 1; i < points.size(); ++i) {
|
path.lineTo(bottom);
|
||||||
QPointF point = points.at(i);
|
path.lineTo(left.x(), bottom.y());
|
||||||
if (point.y() == previous.y() && qAbs(point.x() - previous.x()) > 2*corner) {
|
path.lineTo(left);
|
||||||
QPointF tmp = QPointF(previous.x() + corner * ((point.x() > previous.x())?1:-1), previous.y());
|
path.lineTo(top.x(), left.y());
|
||||||
path.quadTo(previous, tmp);
|
path.lineTo(top);
|
||||||
previous = tmp;
|
|
||||||
i--;
|
|
||||||
continue;
|
|
||||||
} else if (point.x() == previous.x() && qAbs(point.y() - previous.y()) > 2*corner) {
|
|
||||||
QPointF tmp = QPointF(previous.x(), previous.y() + corner * ((point.y() > previous.y())?1:-1));
|
|
||||||
path.quadTo(previous, tmp);
|
|
||||||
previous = tmp;
|
|
||||||
i--;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
QPointF target = (previous + point) / 2;
|
|
||||||
path.quadTo(previous, target);
|
|
||||||
previous = points.at(i);
|
|
||||||
}
|
|
||||||
path.closeSubpath();
|
path.closeSubpath();
|
||||||
path.translate(offset);
|
path.translate(offset);
|
||||||
return path.simplified();
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorOverlay::paintSelection(QPainter *painter,
|
void TextEditorOverlay::paintSelection(QPainter *painter,
|
||||||
@@ -310,7 +255,7 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
|
|||||||
QColor penColor = fg;
|
QColor penColor = fg;
|
||||||
if (m_alpha)
|
if (m_alpha)
|
||||||
penColor.setAlpha(220);
|
penColor.setAlpha(220);
|
||||||
QPen pen(penColor, m_borderWidth);
|
QPen pen(penColor, borderWidth);
|
||||||
painter->translate(-.5, -.5);
|
painter->translate(-.5, -.5);
|
||||||
|
|
||||||
QRectF pathRect = path.controlPointRect();
|
QRectF pathRect = path.controlPointRect();
|
||||||
@@ -332,8 +277,6 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
|
|||||||
painter->setBrush(QBrush(linearGrad));
|
painter->setBrush(QBrush(linearGrad));
|
||||||
}
|
}
|
||||||
|
|
||||||
painter->setRenderHint(QPainter::Antialiasing);
|
|
||||||
|
|
||||||
if (selection.m_dropShadow) {
|
if (selection.m_dropShadow) {
|
||||||
painter->save();
|
painter->save();
|
||||||
QPainterPath shadow = path;
|
QPainterPath shadow = path;
|
||||||
@@ -345,7 +288,7 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
|
|||||||
painter->restore();
|
painter->restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
pen.setJoinStyle(Qt::RoundJoin);
|
pen.setJoinStyle(Qt::MiterJoin);
|
||||||
painter->setPen(pen);
|
painter->setPen(pen);
|
||||||
painter->drawPath(path);
|
painter->drawPath(path);
|
||||||
painter->restore();
|
painter->restore();
|
||||||
|
@@ -45,8 +45,6 @@ public:
|
|||||||
inline void hide() { setVisible(false); }
|
inline void hide() { setVisible(false); }
|
||||||
inline void show() { setVisible(true); }
|
inline void show() { setVisible(true); }
|
||||||
|
|
||||||
void setBorderWidth(int bw) {m_borderWidth = bw; }
|
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
void setAlpha(bool enabled) { m_alpha = enabled; }
|
void setAlpha(bool enabled) { m_alpha = enabled; }
|
||||||
@@ -83,7 +81,6 @@ private:
|
|||||||
|
|
||||||
bool m_visible;
|
bool m_visible;
|
||||||
bool m_alpha;
|
bool m_alpha;
|
||||||
int m_borderWidth;
|
|
||||||
int m_dropShadowWidth;
|
int m_dropShadowWidth;
|
||||||
int m_firstSelectionOriginalBegin;
|
int m_firstSelectionOriginalBegin;
|
||||||
TextEditorWidget *m_editor;
|
TextEditorWidget *m_editor;
|
||||||
|
Reference in New Issue
Block a user