summaryrefslogtreecommitdiff
path: root/src/plugins/texteditor/texteditoroverlay.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/texteditor/texteditoroverlay.cpp')
-rw-r--r--src/plugins/texteditor/texteditoroverlay.cpp109
1 files changed, 95 insertions, 14 deletions
diff --git a/src/plugins/texteditor/texteditoroverlay.cpp b/src/plugins/texteditor/texteditoroverlay.cpp
index 2edcb9455f..12d630c395 100644
--- a/src/plugins/texteditor/texteditoroverlay.cpp
+++ b/src/plugins/texteditor/texteditoroverlay.cpp
@@ -32,23 +32,26 @@
**************************************************************************/
#include "texteditoroverlay.h"
+#include "basetexteditor.h"
+#include <QtCore/QDebug>
+#include <QtCore/QMap>
#include <QtGui/QPainter>
#include <QtGui/QTextBlock>
using namespace TextEditor;
using namespace TextEditor::Internal;
-
-
-TextEditorOverlay::TextEditorOverlay(BaseTextEditor *editor)
- :QObject(editor) {
- m_visible = false;
- m_borderWidth = 1;
- m_dropShadowWidth = 2;
- m_editor = editor;
- m_alpha = true;
- m_viewport = editor->viewport();
+TextEditorOverlay::TextEditorOverlay(BaseTextEditor *editor) :
+ QObject(editor),
+ m_visible(false),
+ m_borderWidth(1),
+ m_dropShadowWidth(2),
+ m_alpha(true),
+ m_firstSelectionOriginalBegin(-1),
+ m_editor(editor),
+ m_viewport(editor->viewport())
+{
}
void TextEditorOverlay::update()
@@ -72,6 +75,7 @@ void TextEditorOverlay::clear()
if (m_selections.isEmpty())
return;
m_selections.clear();
+ m_firstSelectionOriginalBegin = -1;
update();
}
@@ -97,13 +101,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();
}
@@ -453,11 +460,85 @@ void TextEditorOverlay::fill(QPainter *painter, const QColor &color, const QRect
*/
bool TextEditorOverlay::hasCursorInSelection(const QTextCursor &cursor) const
{
+ if (selectionIndexForCursor(cursor) != -1)
+ return true;
+ return false;
+}
+
+int TextEditorOverlay::selectionIndexForCursor(const QTextCursor &cursor) const
+{
for (int i = 0; i < m_selections.size(); ++i) {
const OverlaySelection &selection = m_selections.at(i);
if (cursor.position() >= selection.m_cursor_begin.position()
&& cursor.position() <= selection.m_cursor_end.position())
- return true;
+ return i;
}
- return false;
+ return -1;
+}
+
+QString TextEditorOverlay::selectionText(int selectionIndex) const
+{
+ return assembleCursorForSelection(selectionIndex).selectedText();
+}
+
+QTextCursor TextEditorOverlay::assembleCursorForSelection(int selectionIndex) const
+{
+ const OverlaySelection &selection = m_selections.at(selectionIndex);
+ QTextCursor cursor(m_editor->document());
+ cursor.setPosition(selection.m_cursor_begin.position());
+ cursor.setPosition(selection.m_cursor_end.position(), QTextCursor::KeepAnchor);
+ return cursor;
+}
+
+void TextEditorOverlay::mapEquivalentSelections()
+{
+ m_equivalentSelections.clear();
+ m_equivalentSelections.resize(m_selections.size());
+
+ QMap<QString, int> all;
+ for (int i = 0; i < m_selections.size(); ++i)
+ all.insertMulti(selectionText(i), i);
+
+ const QList<QString> &uniqueKeys = all.uniqueKeys();
+ foreach (const QString &key, uniqueKeys) {
+ QList<int> indexes;
+ QMap<QString, int>::const_iterator lbit = all.lowerBound(key);
+ QMap<QString, int>::const_iterator ubit = all.upperBound(key);
+ while (lbit != ubit) {
+ indexes.append(lbit.value());
+ ++lbit;
+ }
+
+ foreach (int index, indexes)
+ m_equivalentSelections[index] = indexes;
+ }
+}
+
+void TextEditorOverlay::updateEquivalentSelections(const QTextCursor &cursor)
+{
+ int selectionIndex = selectionIndexForCursor(cursor);
+ if (selectionIndex == -1)
+ return;
+
+ const QString &currentText = selectionText(selectionIndex);
+ const QList<int> &equivalents = m_equivalentSelections.at(selectionIndex);
+ foreach (int i, equivalents) {
+ if (i == selectionIndex)
+ continue;
+ const QString &equivalentText = selectionText(i);
+ if (currentText != equivalentText) {
+ QTextCursor selectionCursor = assembleCursorForSelection(i);
+ selectionCursor.joinPreviousEditBlock();
+ selectionCursor.removeSelectedText();
+ selectionCursor.insertText(currentText);
+ selectionCursor.endEditBlock();
+ }
+ }
+}
+
+bool TextEditorOverlay::hasFirstSelectionBeginMoved() const
+{
+ if (m_firstSelectionOriginalBegin == -1 || m_selections.isEmpty())
+ return false;
+ return m_selections.at(0).m_cursor_begin.position() != m_firstSelectionOriginalBegin;
}