summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <[email protected]>2017-12-12 08:08:40 +0100
committerMontel Laurent <[email protected]>2017-12-12 08:08:40 +0100
commitfe535a76683afe92e64f3e5ee304b389b962888e (patch)
tree20e9efcfac879ce3b0598ac8f4a0cf3a98110ba3
parent56d04e10d7ffc986e1683feda2c55e8af52b51c5 (diff)
Add moveup/movedown
-rw-r--r--src/texteditor/plaintexteditor/plaintexteditor.cpp59
-rw-r--r--src/texteditor/plaintexteditor/plaintexteditor.h2
2 files changed, 52 insertions, 9 deletions
diff --git a/src/texteditor/plaintexteditor/plaintexteditor.cpp b/src/texteditor/plaintexteditor/plaintexteditor.cpp
index 5541f21..b1cdfb5 100644
--- a/src/texteditor/plaintexteditor/plaintexteditor.cpp
+++ b/src/texteditor/plaintexteditor/plaintexteditor.cpp
@@ -636,16 +636,59 @@ void PlainTextEditor::deleteEndOfLine()
setTextCursor(cursor);
}
-void PlainTextEditor::moveUpDownText(bool moveUp)
+void PlainTextEditor::moveLineUpDown(bool moveUp)
{
- //TODO select all block text
+ qDebug() << " void PlainTextEditor::moveUpDownText(bool moveUp)" << moveUp;
QTextCursor cursor = textCursor();
- cursor.beginEditBlock();
- cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
- cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
- //TODO more
- cursor.endEditBlock();
- setTextCursor(cursor);
+ 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(move.atBlockStart() ? QTextCursor::Left: QTextCursor::EndOfBlock,
+ QTextCursor::KeepAnchor);
+ } else {
+ move.movePosition(QTextCursor::StartOfBlock);
+ move.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
+ }
+ QString text = move.selectedText();
+
+ move.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
+ move.removeSelectedText();
+
+ if (moveUp) {
+ move.movePosition(QTextCursor::PreviousBlock);
+ move.insertBlock();
+ move.movePosition(QTextCursor::Left);
+ } else {
+ move.movePosition(QTextCursor::EndOfBlock);
+ if (move.atBlockStart()) { // empty block
+ 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();
+
+ if (hasSelection) {
+ move.setPosition(end);
+ move.setPosition(start, QTextCursor::KeepAnchor);
+ } else {
+ move.setPosition(start);
+ }
+ move.endEditBlock();
+
+ setTextCursor(move);
}
void PlainTextEditor::wheelEvent(QWheelEvent *event)
diff --git a/src/texteditor/plaintexteditor/plaintexteditor.h b/src/texteditor/plaintexteditor/plaintexteditor.h
index 527b7c8..d4cdfd5 100644
--- a/src/texteditor/plaintexteditor/plaintexteditor.h
+++ b/src/texteditor/plaintexteditor/plaintexteditor.h
@@ -122,7 +122,7 @@ private:
void deleteWordForward();
void highlightWord(int length, int pos);
void deleteEndOfLine();
- void moveUpDownText(bool moveUp);
+ void moveLineUpDown(bool moveUp);
class PlainTextEditorPrivate;
PlainTextEditorPrivate *const d;
};