From 18b675d32f286efa793fb7f878a3c01d5ce48151 Mon Sep 17 00:00:00 2001 From: Yuchen Deng Date: Sat, 29 Oct 2011 13:09:33 +0800 Subject: [PATCH] Double-click to select current block This is a Eclipse-like feature, for select enclosing element. e.g. int test() {| // Here is the cursor position ... } When Double-click, we can select the block: {...} I think this is a useful feature. Change-Id: I4ca7ed04056176195d1622714effda9079ae0e44 Reviewed-by: Leandro Melo --- .../texteditor/basetextdocumentlayout.cpp | 4 ++- .../texteditor/basetextdocumentlayout.h | 3 ++- src/plugins/texteditor/basetexteditor.cpp | 26 ++++++++++++++----- src/plugins/texteditor/basetexteditor.h | 5 ++-- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/plugins/texteditor/basetextdocumentlayout.cpp b/src/plugins/texteditor/basetextdocumentlayout.cpp index d0d6fe04ba3..455779f795f 100644 --- a/src/plugins/texteditor/basetextdocumentlayout.cpp +++ b/src/plugins/texteditor/basetextdocumentlayout.cpp @@ -201,7 +201,7 @@ TextBlockUserData::MatchType TextBlockUserData::checkClosedParenthesis(QTextCurs } } -bool TextBlockUserData::findPreviousOpenParenthesis(QTextCursor *cursor, bool select) +bool TextBlockUserData::findPreviousOpenParenthesis(QTextCursor *cursor, bool select, bool onlyInCurrentBlock) { QTextBlock block = cursor->block(); int position = cursor->position(); @@ -224,6 +224,8 @@ bool TextBlockUserData::findPreviousOpenParenthesis(QTextCursor *cursor, bool se } } } + if (onlyInCurrentBlock) + return false; block = block.previous(); } return false; diff --git a/src/plugins/texteditor/basetextdocumentlayout.h b/src/plugins/texteditor/basetextdocumentlayout.h index 6930e5d0a78..8331f79f804 100644 --- a/src/plugins/texteditor/basetextdocumentlayout.h +++ b/src/plugins/texteditor/basetextdocumentlayout.h @@ -104,7 +104,8 @@ public: static MatchType checkClosedParenthesis(QTextCursor *cursor, QChar c); static MatchType matchCursorBackward(QTextCursor *cursor); static MatchType matchCursorForward(QTextCursor *cursor); - static bool findPreviousOpenParenthesis(QTextCursor *cursor, bool select = false); + static bool findPreviousOpenParenthesis(QTextCursor *cursor, bool select = false, + bool onlyInCurrentBlock = false); static bool findNextClosingParenthesis(QTextCursor *cursor, bool select = false); static bool findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition = false); diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index f53131e7670..25772c208c3 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -865,7 +865,7 @@ static QTextCursor flippedCursor(const QTextCursor &cursor) return flipped; } -void BaseTextEditorWidget::selectBlockUp() +bool BaseTextEditorWidget::selectBlockUp() { QTextCursor cursor = textCursor(); if (!cursor.hasSelection()) @@ -873,22 +873,23 @@ void BaseTextEditorWidget::selectBlockUp() else cursor.setPosition(cursor.selectionStart()); - if (!TextBlockUserData::findPreviousOpenParenthesis(&cursor, false)) - return; + return false; if (!TextBlockUserData::findNextClosingParenthesis(&cursor, true)) - return; + return false; + setTextCursor(flippedCursor(cursor)); _q_matchParentheses(); + return true; } -void BaseTextEditorWidget::selectBlockDown() +bool BaseTextEditorWidget::selectBlockDown() { QTextCursor tc = textCursor(); QTextCursor cursor = d->m_selectBlockAnchor; if (!tc.hasSelection() || cursor.isNull()) - return; + return false; tc.setPosition(tc.selectionStart()); forever { @@ -904,6 +905,7 @@ void BaseTextEditorWidget::selectBlockDown() setTextCursor(flippedCursor(cursor)); _q_matchParentheses(); + return true; } void BaseTextEditorWidget::copyLineUp() @@ -4214,6 +4216,18 @@ void BaseTextEditorWidget::mouseReleaseEvent(QMouseEvent *e) QPlainTextEdit::mouseReleaseEvent(e); } +void BaseTextEditorWidget::mouseDoubleClickEvent(QMouseEvent *e) +{ + QTextCursor cursor = textCursor(); + const int position = cursor.position(); + if (TextBlockUserData::findPreviousOpenParenthesis(&cursor, false, true)) { + if (position - cursor.position() == 1 && selectBlockUp()) + return; + } + + QPlainTextEdit::mouseDoubleClickEvent(e); +} + void BaseTextEditorWidget::leaveEvent(QEvent *e) { // Clear link emulation when the mouse leaves the editor diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index bb5a9eff97e..48ceca3c7ba 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -298,8 +298,8 @@ public slots: void gotoNextWordCamelCase(); void gotoNextWordCamelCaseWithSelection(); - void selectBlockUp(); - void selectBlockDown(); + bool selectBlockUp(); + bool selectBlockDown(); void moveLineUp(); void moveLineDown(); @@ -454,6 +454,7 @@ protected: void mouseMoveEvent(QMouseEvent *); void mousePressEvent(QMouseEvent *); void mouseReleaseEvent(QMouseEvent *); + void mouseDoubleClickEvent(QMouseEvent *); void leaveEvent(QEvent *); void keyReleaseEvent(QKeyEvent *);