Snippets: Track begin of first selection

Since in the snippets overlay the selections are created with
ExpandBegin the interest is mostly in detecting when the first one
moved to the left (for example, when an undo is performed right
after inserting the snippet). However, this tracking doesn't need
to necessarily be associated with that flag.
This commit is contained in:
Leandro Melo
2010-11-29 17:25:01 +01:00
parent b87861f9b4
commit fdbb34adb8
3 changed files with 20 additions and 7 deletions

View File

@@ -670,10 +670,7 @@ void BaseTextEditor::editorContentsChange(int position, int charsRemoved, int ch
if (d->m_snippetOverlay->isVisible()) {
QTextCursor cursor = textCursor();
cursor.setPosition(position);
if (!d->m_snippetOverlay->hasCursorInSelection(cursor)) {
d->m_snippetOverlay->hide();
d->m_snippetOverlay->clear();
}
d->snippetCheckCursor(cursor);
}
if (doc->isRedoAvailable())
@@ -2455,7 +2452,8 @@ bool BaseTextEditorPrivate::snippetCheckCursor(const QTextCursor &cursor)
QTextCursor end = cursor;
end.setPosition(cursor.selectionEnd());
if (!m_snippetOverlay->hasCursorInSelection(start)
|| !m_snippetOverlay->hasCursorInSelection(end)) {
|| !m_snippetOverlay->hasCursorInSelection(end)
|| m_snippetOverlay->hasFirstSelectionBeginMoved()) {
m_snippetOverlay->setVisible(false);
m_snippetOverlay->clear();
return false;

View File

@@ -43,6 +43,7 @@ TextEditorOverlay::TextEditorOverlay(BaseTextEditor *editor) :
m_borderWidth(1),
m_dropShadowWidth(2),
m_alpha(true),
m_firstSelectionOriginalBegin(-1),
m_editor(editor),
m_viewport(editor->viewport())
{
@@ -69,6 +70,7 @@ void TextEditorOverlay::clear()
if (m_selections.isEmpty())
return;
m_selections.clear();
m_firstSelectionOriginalBegin = -1;
update();
}
@@ -94,13 +96,16 @@ void TextEditorOverlay::addOverlaySelection(int begin, int end,
}
}
if (overlaySelectionFlags & LockSize)
selection.m_fixedLength = (end - begin);
selection.m_dropShadow = (overlaySelectionFlags & DropShadow);
if (m_selections.isEmpty())
m_firstSelectionOriginalBegin = begin;
else if (begin < m_firstSelectionOriginalBegin)
qWarning() << "overlay selections not in order";
m_selections.append(selection);
update();
}
@@ -525,3 +530,10 @@ void TextEditorOverlay::updateEquivalentSelections(const QTextCursor &cursor)
}
}
}
bool TextEditorOverlay::hasFirstSelectionBeginMoved() const
{
if (m_firstSelectionOriginalBegin == -1 || m_selections.isEmpty())
return false;
return m_selections.at(0).m_cursor_begin.position() != m_firstSelectionOriginalBegin;
}

View File

@@ -101,6 +101,8 @@ public:
void mapEquivalentSelections();
void updateEquivalentSelections(const QTextCursor &cursor);
bool hasFirstSelectionBeginMoved() const;
private:
QPainterPath createSelectionPath(const QTextCursor &begin, const QTextCursor &end, const QRect& clip);
void paintSelection(QPainter *painter, const OverlaySelection &selection);
@@ -113,6 +115,7 @@ private:
int m_borderWidth;
int m_dropShadowWidth;
bool m_alpha;
int m_firstSelectionOriginalBegin;
BaseTextEditor *m_editor;
QWidget *m_viewport;
QList<OverlaySelection> m_selections;