Commit 28e3ba5b authored by mae's avatar mae

Fixed overlay selections

1. allow empty overlay selections
2. make it possible to bind a selection to a specified length
parent 99c9c3c3
......@@ -4219,7 +4219,7 @@ void BaseTextEditor::setExtraSelections(ExtraSelectionKind kind, const QList<QTe
if (kind == CodeSemanticsSelection) {
d->m_overlay->clear();
foreach (const QTextEdit::ExtraSelection &selection, d->m_extraSelections[kind]) {
d->m_overlay->addOverlaySelection(selection.cursor, selection.format.background().color());
d->m_overlay->addOverlaySelection(selection.cursor, selection.format.background().color(), true);
}
d->m_overlay->setVisible(!d->m_overlay->isEmpty());
......
......@@ -36,9 +36,9 @@ void TextEditorOverlay::clear()
update();
}
void TextEditorOverlay::addOverlaySelection(int begin, int end, const QColor &color)
void TextEditorOverlay::addOverlaySelection(int begin, int end, const QColor &color, bool lockSize)
{
if (end <= begin)
if (end < begin)
return;
QTextDocument *document = m_editor->document();
......@@ -52,16 +52,17 @@ void TextEditorOverlay::addOverlaySelection(int begin, int end, const QColor &co
selection.m_cursor_end = QTextCursor(document);
selection.m_cursor_end.setPosition(end);
if (lockSize)
selection.m_fixedLength = (end - begin);
m_selections += selection;
update();
}
void TextEditorOverlay::addOverlaySelection(const QTextCursor &cursor, const QColor &color)
void TextEditorOverlay::addOverlaySelection(const QTextCursor &cursor, const QColor &color, bool lockSize)
{
if (!cursor.hasSelection())
return;
addOverlaySelection(cursor.selectionStart(), cursor.selectionEnd(), color);
addOverlaySelection(cursor.selectionStart(), cursor.selectionEnd(), color, lockSize);
}
QRect TextEditorOverlay::rect() const
......@@ -243,6 +244,11 @@ void TextEditorOverlay::paint(QPainter *painter, const QRect &clip)
Q_UNUSED(clip);
for (int i = 0; i < m_selections.size(); ++i) {
const OverlaySelection &selection = m_selections.at(i);
if (selection.m_fixedLength >= 0
&& selection.m_cursor_end.position() - selection.m_cursor_begin.position()
!= selection.m_fixedLength)
continue;
paintSelection(painter,
selection.m_cursor_begin,
selection.m_cursor_end,
......
......@@ -8,9 +8,11 @@ namespace TextEditor {
namespace Internal {
struct TEXTEDITOR_EXPORT OverlaySelection {
OverlaySelection():m_fixedLength(-1){}
QTextCursor m_cursor_begin;
QTextCursor m_cursor_end;
QColor m_color;
int m_fixedLength;
};
class TEXTEDITOR_EXPORT TextEditorOverlay : public QObject
......@@ -38,8 +40,8 @@ public:
void update();
void clear();
void addOverlaySelection(const QTextCursor &cursor, const QColor &color);
void addOverlaySelection(int begin, int end, const QColor &color);
void addOverlaySelection(const QTextCursor &cursor, const QColor &color, bool lockSize = false);
void addOverlaySelection(int begin, int end, const QColor &color, bool lockSize = false);
inline bool isEmpty() const { return m_selections.isEmpty(); }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment