From c27d7c00d616f53448e450472746fdb4f56ecb7b Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Wed, 4 Jan 2017 14:19:52 +0100 Subject: [PATCH] QmlDesigner: Move cursor to selected item When an item is selected in the navigator we move the cursor. Change-Id: Ia6d41e4d7bdf38688cf0cd73f134c349126eb68e Reviewed-by: Tim Jenssen --- .../components/texteditor/texteditorview.cpp | 1 + .../texteditor/texteditorwidget.cpp | 24 +++++++++++++++++++ .../components/texteditor/texteditorwidget.h | 1 + 3 files changed, 26 insertions(+) diff --git a/src/plugins/qmldesigner/components/texteditor/texteditorview.cpp b/src/plugins/qmldesigner/components/texteditor/texteditorview.cpp index 8d6d80dfc08..2c40d5ea278 100644 --- a/src/plugins/qmldesigner/components/texteditor/texteditorview.cpp +++ b/src/plugins/qmldesigner/components/texteditor/texteditorview.cpp @@ -116,6 +116,7 @@ void TextEditorView::nodeIdChanged(const ModelNode& /*node*/, const QString &/*n void TextEditorView::selectedNodesChanged(const QList &/*selectedNodeList*/, const QList &/*lastSelectedNodeList*/) { + m_widget->jumpTextCursorToSelectedModelNode(); } void TextEditorView::customNotification(const AbstractView * /*view*/, const QString &/*identifier*/, const QList &/*nodeList*/, const QList &/*data*/) diff --git a/src/plugins/qmldesigner/components/texteditor/texteditorwidget.cpp b/src/plugins/qmldesigner/components/texteditor/texteditorwidget.cpp index ee1a749a492..91c99606a8f 100644 --- a/src/plugins/qmldesigner/components/texteditor/texteditorwidget.cpp +++ b/src/plugins/qmldesigner/components/texteditor/texteditorwidget.cpp @@ -80,4 +80,28 @@ void TextEditorWidget::updateSelectionByCursorPosition() } } +void TextEditorWidget::jumpTextCursorToSelectedModelNode() +{ + ModelNode selectedNode; + + if (!m_textEditorView->selectedModelNodes().isEmpty()) + selectedNode = m_textEditorView->selectedModelNodes().first(); + + if (selectedNode.isValid()) { + RewriterView *rewriterView = m_textEditorView->model()->rewriterView(); + + const int nodeOffset = rewriterView->nodeOffset(selectedNode); + if (nodeOffset > 0) { + const ModelNode currentSelectedNode = rewriterView-> + nodeAtTextCursorPosition(m_textEditor->editorWidget()->textCursor().position()); + + if (currentSelectedNode != selectedNode) { + int line, column; + m_textEditor->editorWidget()->convertPosition(nodeOffset, &line, &column); + m_textEditor->editorWidget()->gotoLine(line, column); + } + } + } +} + } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/texteditor/texteditorwidget.h b/src/plugins/qmldesigner/components/texteditor/texteditorwidget.h index 84cd7297f92..26eca933c9e 100644 --- a/src/plugins/qmldesigner/components/texteditor/texteditorwidget.h +++ b/src/plugins/qmldesigner/components/texteditor/texteditorwidget.h @@ -50,6 +50,7 @@ public: } QString contextHelpId() const; + void jumpTextCursorToSelectedModelNode(); private: void updateSelectionByCursorPosition();