/************************************************************************** ** ** This file is part of Qt Creator ** ** Copyright (C) 2011 Kläralvdalens Datakonsult AB, a KDAB Group company. ** ** Contact: Kläralvdalens Datakonsult AB (info@kdab.com) ** ** ** GNU Lesser General Public License Usage ** ** This file may be used under the terms of the GNU Lesser General Public ** License version 2.1 as published by the Free Software Foundation and ** appearing in the file LICENSE.LGPL included in the packaging of this file. ** Please review the following information to ensure the GNU Lesser General ** Public License version 2.1 requirements will be met: ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Nokia gives you certain additional ** rights. These rights are described in the Nokia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** Other Usage ** ** Alternatively, this file may be used in accordance with the terms and ** conditions contained in a signed written agreement between you and Nokia. ** ** If you have questions regarding the use of this file, please contact ** Nokia at info@qt.nokia.com. ** **************************************************************************/ #include "basetexteditor.h" #include #include #include #include "mark.h" using namespace Scripting::Internal; BaseTextEditor::BaseTextEditor(QObject *parent) : TextEditor(parent) { } void BaseTextEditor::copy() { if (textEditorWidget()) textEditorWidget()->copy(); } void BaseTextEditor::paste() { if (textEditorWidget()) textEditorWidget()->paste(); } void BaseTextEditor::cut() { if (textEditorWidget()) textEditorWidget()->cut(); } void BaseTextEditor::cutLine() { if (textEditorWidget()) textEditorWidget()->cutLine(); } void BaseTextEditor::copyLine() { if (textEditorWidget()) textEditorWidget()->copyLine(); } void BaseTextEditor::deleteSelection() { if (textEditorWidget() && hasSelection()) textEditorWidget()->textCursor().deleteChar(); } void BaseTextEditor::deleteLine(int count) { if (textEditorWidget()) for(int i=0; ideleteLine(); } void BaseTextEditor::deleteEndOfWord(int count) { if (textEditorWidget()) for(int i=0; ideleteEndOfWord(); } void BaseTextEditor::deleteEndOfWordCamelCase(int count) { if (textEditorWidget()) for(int i=0; ideleteEndOfWordCamelCase(); } void BaseTextEditor::deleteStartOfWord(int count) { if (textEditorWidget()) for(int i=0; ideleteStartOfWord(); } void BaseTextEditor::deleteStartOfWordCamelCase(int count) { if (textEditorWidget()) for(int i=0; ideleteStartOfWordCamelCase(); } void BaseTextEditor::gotoBlockStart(int count) { if (textEditorWidget()) for(int i=0; igotoBlockStart(); } void BaseTextEditor::gotoBlockEnd(int count) { if (textEditorWidget()) for(int i=0; igotoBlockEnd(); } void BaseTextEditor::gotoLineStart(int count) { if (textEditorWidget()) for(int i=0; igotoLineStart(); } void BaseTextEditor::gotoLineEnd(int count) { if (textEditorWidget()) for(int i=0; igotoLineEnd(); } void BaseTextEditor::gotoNextLine(int count) { if (textEditorWidget()) for(int i=0; igotoNextLine(); } void BaseTextEditor::gotoPreviousLine(int count) { if (textEditorWidget()) for(int i=0; igotoPreviousLine(); } void BaseTextEditor::gotoPreviousCharacter(int count) { if (textEditorWidget()) for(int i=0; igotoPreviousCharacter(); } void BaseTextEditor::gotoNextCharacter(int count) { if (textEditorWidget()) for(int i=0; igotoNextCharacter(); } void BaseTextEditor::gotoPreviousWord(int count) { if (textEditorWidget()) for(int i=0; igotoPreviousWord(); } void BaseTextEditor::gotoNextWord(int count) { if (textEditorWidget()) for(int i=0; igotoNextWord(); } void BaseTextEditor::gotoPreviousWordCamelCase(int count) { if (textEditorWidget()) for(int i=0; igotoPreviousWordCamelCase(); } void BaseTextEditor::gotoNextWordCamelCase(int count) { if (textEditorWidget()) for(int i=0; igotoNextWordCamelCase(); } void BaseTextEditor::clearSelection() { if (textEditorWidget()) textEditorWidget()->textCursor().clearSelection(); } bool BaseTextEditor::hasSelection() { if (textEditorWidget()) return textEditorWidget()->textCursor().hasSelection(); return false; } void BaseTextEditor::selectAll() { if (textEditorWidget()) textEditorWidget()->selectAll(); } void BaseTextEditor::selectBlockStart(int count) { if (textEditorWidget()) for(int i=0; igotoBlockStartWithSelection(); } void BaseTextEditor::selectBlockEnd(int count) { if (textEditorWidget()) for(int i=0; igotoBlockEndWithSelection(); } void BaseTextEditor::selectLineStart(int count) { if (textEditorWidget()) for(int i=0; igotoLineStartWithSelection(); } void BaseTextEditor::selectLineEnd(int count) { if (textEditorWidget()) for(int i=0; igotoLineEndWithSelection(); } void BaseTextEditor::selectNextLine(int count) { if (textEditorWidget()) for(int i=0; igotoNextLineWithSelection(); } void BaseTextEditor::selectPreviousLine(int count) { if (textEditorWidget()) for(int i=0; igotoPreviousLineWithSelection(); } void BaseTextEditor::selectPreviousCharacter(int count) { if (textEditorWidget()) for(int i=0; igotoPreviousCharacterWithSelection(); } void BaseTextEditor::selectNextCharacter(int count) { if (textEditorWidget()) for(int i=0; igotoNextCharacterWithSelection(); } void BaseTextEditor::selectPreviousWord(int count) { if (textEditorWidget()) for(int i=0; igotoPreviousWordWithSelection(); } void BaseTextEditor::selectNextWord(int count) { if (textEditorWidget()) for(int i=0; igotoNextWordWithSelection(); } void BaseTextEditor::selectPreviousWordCamelCase(int count) { if (textEditorWidget()) for(int i=0; igotoPreviousWordCamelCaseWithSelection(); } void BaseTextEditor::selectNextWordCamelCase(int count) { if (textEditorWidget()) for(int i=0; igotoNextWordCamelCaseWithSelection(); } void BaseTextEditor::selectBlockUp(int count) { if (textEditorWidget()) for(int i=0; iselectBlockUp(); } void BaseTextEditor::selectBlockDown(int count) { if (textEditorWidget()) for(int i=0; iselectBlockDown(); } void BaseTextEditor::moveLineUp(int count) { if (textEditorWidget()) for(int i=0; imoveLineUp(); } void BaseTextEditor::moveLineDown(int count) { if (textEditorWidget()) for(int i=0; imoveLineDown(); } void BaseTextEditor::copyLineUp(int count) { if (textEditorWidget()) for(int i=0; icopyLineUp(); } void BaseTextEditor::copyLineDown(int count) { if (textEditorWidget()) for(int i=0; icopyLineDown(); } void BaseTextEditor::joinLines(int count) { if (textEditorWidget()) for(int i=0; ijoinLines(); } void BaseTextEditor::insertLineAbove(int count) { if (textEditorWidget()) for(int i=0; iinsertLineAbove(); } void BaseTextEditor::insertLineBelow(int count) { if (textEditorWidget()) for(int i=0; iinsertLineBelow(); } void BaseTextEditor::uppercaseSelection() { if (textEditorWidget()) textEditorWidget()->uppercaseSelection(); } void BaseTextEditor::lowercaseSelection() { if (textEditorWidget()) textEditorWidget()->lowercaseSelection(); } void BaseTextEditor::cleanWhitespace() { if (textEditorWidget()) textEditorWidget()->cleanWhitespace(); } void BaseTextEditor::insertText(const QString &text) { if (textEditorWidget()) textEditorWidget()->insertPlainText(text); } QString BaseTextEditor::selectedText() { if (textEditorWidget()) return textEditorWidget()->textCursor().selectedText(); return QString(); } QString BaseTextEditor::text() { if (textEditorWidget()) return textEditorWidget()->toPlainText(); return QString(); } /** * @brief Create a mark for the given line and column * * If line and column isn't provided, the currentline and column is used. */ Mark *BaseTextEditor::createMark(int line, int column) { if (line == -1) line = currentLine(); if (column == -1) column = currentColumn(); return Mark::create(this, line, column); } void BaseTextEditor::gotoMark(Mark * mark) { gotoLine(mark->line(), mark->column()); } /** * @brief Search for the given text * * In the case of search forward, the cursor will be placed after the match. * In the case of seatch backeard, the cursor will be place before the match. * @return true if a match is found */ bool BaseTextEditor::find(const QString &text, bool backward, bool caseSensitively, bool wholeWords) { if (!textEditorWidget()) return false; QTextDocument* doc = textEditorWidget()->document(); QTextCursor cursor = doc->find(text, nativePosition(), flags(backward,caseSensitively,wholeWords)); if (cursor.isNull()) return false; if (backward) setCursorPosition(cursor.anchor()); else setCursorPosition(cursor.position()); return true; } /** * @brief Search for the given regular expression. * * In the case of search forward, the cursor will be placed after the match. * In the case of search backeard, the cursor will be place before the match. * @return true if a match is found */ bool BaseTextEditor::findRegexp(const QString ®exp, bool backward, bool caseSensitively, bool wholeWords) { if (!textEditorWidget()) return false; QTextDocument* doc = textEditorWidget()->document(); QTextCursor cursor = doc->find(QRegExp(regexp), nativePosition(), flags(backward,caseSensitively,wholeWords)); if (cursor.isNull()) return false; if (backward) { // Unfortunately is the implementation of QTextDocument::find not greety, // we therefore need to run the search until it no longer matches. // Further the regexp object is copied in find(), so we will not be able to use it for capturing. // We might consider roling our own copy similar to QTextDocument::find const int endPoint = cursor.position(); int pos = cursor.anchor(); do { --pos; cursor = doc->find(QRegExp(regexp), pos, flags(false, caseSensitively, wholeWords)); } while ( !cursor.isNull() && cursor.position() == endPoint && cursor.anchor() == pos); setCursorPosition(pos+1); } else setCursorPosition(cursor.position()); return true; } 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); } void BaseTextEditor::indent() { if (textEditorWidget()) textEditorWidget()->format(); } QTextDocument::FindFlags BaseTextEditor::flags(bool backward, bool caseSensitively, bool wholeWords) const { QTextDocument::FindFlags result; if (backward) result |= QTextDocument::FindBackward; if (caseSensitively) result |= QTextDocument::FindCaseSensitively; if (wholeWords) result |= QTextDocument::FindWholeWords; return result; } ::TextEditor::BaseTextEditorWidget * BaseTextEditor::textEditorWidget() { ::TextEditor::BaseTextEditor *textEditor = qobject_cast< ::TextEditor::BaseTextEditor* >(editor()); if (textEditor) return textEditor->editorWidget(); return 0; }