forked from qt-creator/qt-creator
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:
@@ -26,6 +26,7 @@
|
|||||||
#include "texteditorwidget.h"
|
#include "texteditorwidget.h"
|
||||||
|
|
||||||
#include <texteditorview.h>
|
#include <texteditorview.h>
|
||||||
|
#include <rewriterview.h>
|
||||||
|
|
||||||
#include <theming.h>
|
#include <theming.h>
|
||||||
|
|
||||||
@@ -42,12 +43,20 @@ TextEditorWidget::TextEditorWidget(TextEditorView *textEditorView) : QWidget()
|
|||||||
QBoxLayout *layout = new QVBoxLayout(this);
|
QBoxLayout *layout = new QVBoxLayout(this);
|
||||||
layout->setMargin(0);
|
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")))));
|
setStyleSheet(Theming::replaceCssColors(QString::fromUtf8(Utils::FileReader::fetchQrc(QLatin1String(":/qmldesigner/scrollbar.css")))));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidget::setTextEditor(TextEditor::BaseTextEditor *textEditor) {
|
void TextEditorWidget::setTextEditor(TextEditor::BaseTextEditor *textEditor) {
|
||||||
m_textEditor.reset(textEditor);
|
m_textEditor.reset(textEditor);
|
||||||
layout()->addWidget(textEditor->editorWidget());
|
layout()->addWidget(textEditor->editorWidget());
|
||||||
|
|
||||||
|
|
||||||
|
connect(textEditor->editorWidget(), &QPlainTextEdit::cursorPositionChanged,
|
||||||
|
&m_updateSelectionTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
|
||||||
}
|
}
|
||||||
|
|
||||||
QString TextEditorWidget::contextHelpId() const
|
QString TextEditorWidget::contextHelpId() const
|
||||||
@@ -55,4 +64,20 @@ QString TextEditorWidget::contextHelpId() const
|
|||||||
return m_textEditorView->contextHelpId();
|
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
|
} // namespace QmlDesigner
|
||||||
|
@@ -27,6 +27,7 @@
|
|||||||
#include <texteditor/texteditor.h>
|
#include <texteditor/texteditor.h>
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
@@ -51,8 +52,11 @@ public:
|
|||||||
QString contextHelpId() const;
|
QString contextHelpId() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void updateSelectionByCursorPosition();
|
||||||
|
|
||||||
std::unique_ptr<TextEditor::BaseTextEditor> m_textEditor;
|
std::unique_ptr<TextEditor::BaseTextEditor> m_textEditor;
|
||||||
QPointer<TextEditorView> m_textEditorView;
|
QPointer<TextEditorView> m_textEditorView;
|
||||||
|
QTimer m_updateSelectionTimer;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QmlDesigner
|
} // namespace QmlDesigner
|
||||||
|
Reference in New Issue
Block a user