QmlDesigner: Synchronize selection with cursor position

We always the select the item that contains the cursor,
so the user can use the property editor.
This even works for non visible items.

Change-Id: I5573e6d9be4b478f764ab6960a3b5742136dcee9
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Thomas Hartmann
2017-01-04 13:43:36 +01:00
parent 1d3292e3aa
commit e540f2eda5
2 changed files with 29 additions and 0 deletions

View File

@@ -26,6 +26,7 @@
#include "texteditorwidget.h"
#include <texteditorview.h>
#include <rewriterview.h>
#include <theming.h>
@@ -42,12 +43,20 @@ TextEditorWidget::TextEditorWidget(TextEditorView *textEditorView) : QWidget()
QBoxLayout *layout = new QVBoxLayout(this);
layout->setMargin(0);
m_updateSelectionTimer.setSingleShot(true);
m_updateSelectionTimer.setInterval(200);
connect(&m_updateSelectionTimer, &QTimer::timeout, this, &TextEditorWidget::updateSelectionByCursorPosition);
setStyleSheet(Theming::replaceCssColors(QString::fromUtf8(Utils::FileReader::fetchQrc(QLatin1String(":/qmldesigner/scrollbar.css")))));
}
void TextEditorWidget::setTextEditor(TextEditor::BaseTextEditor *textEditor) {
m_textEditor.reset(textEditor);
layout()->addWidget(textEditor->editorWidget());
connect(textEditor->editorWidget(), &QPlainTextEdit::cursorPositionChanged,
&m_updateSelectionTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
}
QString TextEditorWidget::contextHelpId() const
@@ -55,4 +64,20 @@ QString TextEditorWidget::contextHelpId() const
return m_textEditorView->contextHelpId();
}
void TextEditorWidget::updateSelectionByCursorPosition()
{
/* Because of the timer we have to be careful. */
if (!m_textEditorView->model())
return;
const int cursorPosition = m_textEditor->editorWidget()->textCursor().position();
RewriterView *rewriterView = m_textEditorView->model()->rewriterView();
if (rewriterView) {
ModelNode modelNode = rewriterView->nodeAtTextCursorPosition(cursorPosition);
if (modelNode.isValid())
m_textEditorView->setSelectedModelNode(modelNode);
}
}
} // namespace QmlDesigner

View File

@@ -27,6 +27,7 @@
#include <texteditor/texteditor.h>
#include <QWidget>
#include <QTimer>
#include <memory>
@@ -51,8 +52,11 @@ public:
QString contextHelpId() const;
private:
void updateSelectionByCursorPosition();
std::unique_ptr<TextEditor::BaseTextEditor> m_textEditor;
QPointer<TextEditorView> m_textEditorView;
QTimer m_updateSelectionTimer;
};
} // namespace QmlDesigner