Fixed overlay selections

1. allow empty overlay selections
2. make it possible to bind a selection to a specified length
This commit is contained in:
mae
2009-11-30 13:56:54 +01:00
parent 99c9c3c3c0
commit 28e3ba5bfb
3 changed files with 17 additions and 9 deletions

View File

@@ -4219,7 +4219,7 @@ void BaseTextEditor::setExtraSelections(ExtraSelectionKind kind, const QList<QTe
if (kind == CodeSemanticsSelection) { if (kind == CodeSemanticsSelection) {
d->m_overlay->clear(); d->m_overlay->clear();
foreach (const QTextEdit::ExtraSelection &selection, d->m_extraSelections[kind]) { 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()); d->m_overlay->setVisible(!d->m_overlay->isEmpty());

View File

@@ -36,9 +36,9 @@ void TextEditorOverlay::clear()
update(); 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; return;
QTextDocument *document = m_editor->document(); 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 = QTextCursor(document);
selection.m_cursor_end.setPosition(end); selection.m_cursor_end.setPosition(end);
if (lockSize)
selection.m_fixedLength = (end - begin);
m_selections += selection; m_selections += selection;
update(); update();
} }
void TextEditorOverlay::addOverlaySelection(const QTextCursor &cursor, const QColor &color) void TextEditorOverlay::addOverlaySelection(const QTextCursor &cursor, const QColor &color, bool lockSize)
{ {
if (!cursor.hasSelection()) addOverlaySelection(cursor.selectionStart(), cursor.selectionEnd(), color, lockSize);
return;
addOverlaySelection(cursor.selectionStart(), cursor.selectionEnd(), color);
} }
QRect TextEditorOverlay::rect() const QRect TextEditorOverlay::rect() const
@@ -243,6 +244,11 @@ void TextEditorOverlay::paint(QPainter *painter, const QRect &clip)
Q_UNUSED(clip); Q_UNUSED(clip);
for (int i = 0; i < m_selections.size(); ++i) { for (int i = 0; i < m_selections.size(); ++i) {
const OverlaySelection &selection = m_selections.at(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, paintSelection(painter,
selection.m_cursor_begin, selection.m_cursor_begin,
selection.m_cursor_end, selection.m_cursor_end,

View File

@@ -8,9 +8,11 @@ namespace TextEditor {
namespace Internal { namespace Internal {
struct TEXTEDITOR_EXPORT OverlaySelection { struct TEXTEDITOR_EXPORT OverlaySelection {
OverlaySelection():m_fixedLength(-1){}
QTextCursor m_cursor_begin; QTextCursor m_cursor_begin;
QTextCursor m_cursor_end; QTextCursor m_cursor_end;
QColor m_color; QColor m_color;
int m_fixedLength;
}; };
class TEXTEDITOR_EXPORT TextEditorOverlay : public QObject class TEXTEDITOR_EXPORT TextEditorOverlay : public QObject
@@ -38,8 +40,8 @@ public:
void update(); void update();
void clear(); void clear();
void addOverlaySelection(const QTextCursor &cursor, const QColor &color); void addOverlaySelection(const QTextCursor &cursor, const QColor &color, bool lockSize = false);
void addOverlaySelection(int begin, int end, const QColor &color); void addOverlaySelection(int begin, int end, const QColor &color, bool lockSize = false);
inline bool isEmpty() const { return m_selections.isEmpty(); } inline bool isEmpty() const { return m_selections.isEmpty(); }