From 0b7e56aed100c5e0a70b0cadc11a44dcfd2abd5d Mon Sep 17 00:00:00 2001 From: "Jesper K. Pedersen" Date: Thu, 23 May 2013 12:55:26 +0000 Subject: make it possible to get text from selection Change-Id: Iafbba49f5a0ca2c75034e64eaf856d3afa41c718 Reviewed-by: Nicolas Arnaud-Cormos --- objects/basetexteditor.cpp | 34 ++++++++++++++++++++++++---------- objects/basetexteditor.h | 2 ++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/objects/basetexteditor.cpp b/objects/basetexteditor.cpp index 58a427c..8f62091 100644 --- a/objects/basetexteditor.cpp +++ b/objects/basetexteditor.cpp @@ -486,16 +486,7 @@ bool BaseTextEditor::findRegexp(const QString ®exp, bool backward, bool caseS void BaseTextEditor::deleteRegion(const Position &from, const Position &to) { - QPointer start = createMark(); - gotoPosition(from); - int fromPos = nativePosition(); - gotoPosition(to); - int toPos = nativePosition(); - QTextCursor cursor( textEditorWidget()->document()); - cursor.setPosition(fromPos); - cursor.setPosition(toPos, QTextCursor::KeepAnchor); - cursor.removeSelectedText(); - gotoMark(start); + fetchSelectionAndDelete(from, to, true); } void BaseTextEditor::gotoDocumentStart() @@ -516,6 +507,23 @@ int BaseTextEditor::lineCount() return 0; } +QString BaseTextEditor::fetchSelectionAndDelete(const Position &from, const Position &to, bool del) +{ + QPointer start = createMark(); + gotoPosition(from); + int fromPos = nativePosition(); + gotoPosition(to); + int toPos = nativePosition(); + QTextCursor cursor( textEditorWidget()->document()); + cursor.setPosition(fromPos); + cursor.setPosition(toPos, QTextCursor::KeepAnchor); + QString text = cursor.selectedText(); + if (del) + cursor.removeSelectedText(); + gotoMark(start); + return text; +} + void BaseTextEditor::indent() { if (textEditorWidget()) @@ -543,3 +551,9 @@ QTextDocument::FindFlags BaseTextEditor::flags(bool backward, bool caseSensitive return textEditor->editorWidget(); return 0; } + + +QString Scripting::Internal::BaseTextEditor::text(const Scripting::Internal::Position &from, const Scripting::Internal::Position &to) +{ + return fetchSelectionAndDelete(from, to, false); +} diff --git a/objects/basetexteditor.h b/objects/basetexteditor.h index 31b9866..10c77ce 100644 --- a/objects/basetexteditor.h +++ b/objects/basetexteditor.h @@ -124,11 +124,13 @@ public slots: bool findRegexp(const QString& regexp, bool backward = false, bool caseSensitively = false, bool wholeWords = false); void indent(); void deleteRegion( const Position& from, const Position& to); + QString text( const Position& from, const Position& to); void gotoDocumentStart(); void gotoDocumentEnd(); int lineCount(); private: + QString fetchSelectionAndDelete( const Position& from, const Position& to, bool del); QTextDocument::FindFlags flags(bool backward, bool caseSensitively, bool wholeWords) const; }; -- cgit v1.2.3