From 2dd046640d5f2498f625c1c6dfa90565c1463293 Mon Sep 17 00:00:00 2001
From: Serge Ratke <dev.serge.ratke@gmx.de>
Date: Fri, 15 May 2009 23:17:11 +0200
Subject: [PATCH] implementation of copy lines up/down functionality

---
 src/plugins/texteditor/basetexteditor.cpp     | 60 +++++++++++++++++++
 src/plugins/texteditor/basetexteditor.h       |  4 ++
 .../texteditor/texteditoractionhandler.cpp    | 14 +++++
 .../texteditor/texteditoractionhandler.h      |  4 ++
 src/plugins/texteditor/texteditorconstants.h  |  2 +
 5 files changed, 84 insertions(+)

diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 8af95a69825..4c78513cd45 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -694,6 +694,66 @@ void BaseTextEditor::selectBlockDown()
     _q_matchParentheses();
 }
 
+void BaseTextEditor::copyLineUp()
+{
+    copyLineUpDown(true);
+}
+
+void BaseTextEditor::copyLineDown()
+{
+    copyLineUpDown(false);
+}
+
+void BaseTextEditor::copyLineUpDown(bool up)
+{
+    QTextCursor cursor = textCursor();
+    QTextCursor move = cursor;
+    move.beginEditBlock();
+
+    bool hasSelection = cursor.hasSelection();
+
+    if (hasSelection) {
+        move.setPosition(cursor.selectionStart());
+        move.movePosition(QTextCursor::StartOfBlock);
+        move.setPosition(cursor.selectionEnd(), QTextCursor::KeepAnchor);
+        move.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
+    } else {
+        move.movePosition(QTextCursor::StartOfBlock);
+        move.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
+    }
+
+    QString text = move.selectedText();
+
+    if (up) {
+        move.setPosition(cursor.selectionStart());
+        move.movePosition(QTextCursor::StartOfBlock);
+        move.insertBlock();
+        move.movePosition(QTextCursor::Left);
+    } else {
+        move.movePosition(QTextCursor::EndOfBlock);
+        if (move.atBlockStart()) {
+            move.movePosition(QTextCursor::NextBlock);
+            move.insertBlock();
+            move.movePosition(QTextCursor::Left);
+        } else {
+            move.insertBlock();
+        }
+    }
+
+    int start = move.position();
+    move.clearSelection();
+    move.insertText(text);
+    int end = move.position();
+
+    move.setPosition(start);
+    move.setPosition(end, QTextCursor::KeepAnchor);
+
+    indent(document(), move, QChar::Null);
+    move.endEditBlock();
+
+    setTextCursor(move);
+}
+
 void BaseTextEditor::moveLineUp()
 {
     moveLineUpDown(true);
diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h
index c93cd86123b..f81b7852835 100644
--- a/src/plugins/texteditor/basetexteditor.h
+++ b/src/plugins/texteditor/basetexteditor.h
@@ -387,6 +387,9 @@ public slots:
     void moveLineUp();
     void moveLineDown();
 
+    void copyLineUp();
+    void copyLineDown();
+
     void cleanWhitespace();
 
 signals:
@@ -509,6 +512,7 @@ private:
     void handleHomeKey(bool anchor);
     void handleBackspaceKey();
     void moveLineUpDown(bool up);
+    void copyLineUpDown(bool up);
     void saveCurrentCursorPositionForNavigation();
 
     void drawFoldingMarker(QPainter *painter, const QPalette &pal,
diff --git a/src/plugins/texteditor/texteditoractionhandler.cpp b/src/plugins/texteditor/texteditoractionhandler.cpp
index 0c4821992cc..af26d648efd 100644
--- a/src/plugins/texteditor/texteditoractionhandler.cpp
+++ b/src/plugins/texteditor/texteditoractionhandler.cpp
@@ -84,6 +84,8 @@ TextEditorActionHandler::TextEditorActionHandler(const QString &context,
     m_selectBlockDownAction = 0;
     m_moveLineUpAction = 0;
     m_moveLineDownAction = 0;
+    m_copyLineUpAction = 0;
+    m_copyLineDownAction = 0;
 
     m_contextId << Core::UniqueIDManager::instance()->uniqueIdentifier(context);
 
@@ -246,6 +248,16 @@ void TextEditorActionHandler::createActions()
     command = am->registerAction(m_moveLineDownAction, Constants::MOVE_LINE_DOWN, m_contextId);
     command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+Down")));
     connect(m_moveLineDownAction, SIGNAL(triggered()), this, SLOT(moveLineDown()));
+
+    m_copyLineUpAction= new QAction(tr("Copy Line Up"), this);
+    command = am->registerAction(m_copyLineUpAction, Constants::COPY_LINE_UP, m_contextId);
+    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+Up")));
+    connect(m_copyLineUpAction, SIGNAL(triggered()), this, SLOT(copyLineUp()));
+
+    m_copyLineDownAction= new QAction(tr("Copy Line Down"), this);
+    command = am->registerAction(m_copyLineDownAction, Constants::COPY_LINE_DOWN, m_contextId);
+    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+Down")));
+    connect(m_copyLineDownAction, SIGNAL(triggered()), this, SLOT(copyLineDown()));
 }
 
 bool TextEditorActionHandler::supportsAction(const QString & /*id */) const
@@ -400,6 +412,8 @@ FUNCTION(selectBlockUp)
 FUNCTION(selectBlockDown)
 FUNCTION(moveLineUp)
 FUNCTION(moveLineDown)
+FUNCTION(copyLineUp)
+FUNCTION(copyLineDown)
 
 void TextEditorActionHandler::updateCurrentEditor(Core::IEditor *editor)
 {
diff --git a/src/plugins/texteditor/texteditoractionhandler.h b/src/plugins/texteditor/texteditoractionhandler.h
index 4b216a5290b..2cd9bf52d3e 100644
--- a/src/plugins/texteditor/texteditoractionhandler.h
+++ b/src/plugins/texteditor/texteditoractionhandler.h
@@ -110,6 +110,8 @@ private slots:
     void selectBlockDown();
     void moveLineUp();
     void moveLineDown();
+    void copyLineUp();
+    void copyLineDown();
     void updateCurrentEditor(Core::IEditor *editor);
 
 private:
@@ -141,6 +143,8 @@ private:
     QAction *m_selectBlockDownAction;
     QAction *m_moveLineUpAction;
     QAction *m_moveLineDownAction;
+    QAction *m_copyLineUpAction;
+    QAction *m_copyLineDownAction;
 
     uint m_optionalActions;
     QPointer<BaseTextEditor> m_currentEditor;
diff --git a/src/plugins/texteditor/texteditorconstants.h b/src/plugins/texteditor/texteditorconstants.h
index a8dfac2b5c0..8ff86a98fac 100644
--- a/src/plugins/texteditor/texteditorconstants.h
+++ b/src/plugins/texteditor/texteditorconstants.h
@@ -53,6 +53,8 @@ const char * const SELECT_BLOCK_UP       = "TextEditor.SelectBlockUp";
 const char * const SELECT_BLOCK_DOWN     = "TextEditor.SelectBlockDown";
 const char * const MOVE_LINE_UP       = "TextEditor.MoveLineUp";
 const char * const MOVE_LINE_DOWN     = "TextEditor.MoveLineDown";
+const char * const COPY_LINE_UP        = "TextEditor.CopyLineUp";
+const char * const COPY_LINE_DOWN      = "TextEditor.CopyLineDown";
 const char * const DELETE_LINE           = "TextEditor.DeleteLine";
 const char * const DELETE_WORD           = "TextEditor.DeleteWord";
 const char * const SELECT_ENCODING       = "TextEditor.SelectEncoding";
-- 
GitLab