QmlDesigner: Enable searching in the text editor

Also hide the bottom status bar when it is empty.

Task-number: QDS-6187
Change-Id: I053771243c268ea20df8e127831b789b854021aa
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Mahmoud Badri
2022-02-14 18:13:06 +02:00
parent c755de0823
commit c8481710c9
3 changed files with 40 additions and 33 deletions

View File

@@ -81,7 +81,7 @@ TextEditorView::TextEditorView(QObject *parent)
Core::Command *command = Core::ActionManager::registerAction(completionAction, TextEditor::Constants::COMPLETE_THIS, context); Core::Command *command = Core::ActionManager::registerAction(completionAction, TextEditor::Constants::COMPLETE_THIS, context);
command->setDefaultKeySequence(QKeySequence(Core::useMacShortcuts ? tr("Meta+Space") : tr("Ctrl+Space"))); command->setDefaultKeySequence(QKeySequence(Core::useMacShortcuts ? tr("Meta+Space") : tr("Ctrl+Space")));
connect(completionAction, &QAction::triggered, [this]() { connect(completionAction, &QAction::triggered, this, [this] {
if (m_widget->textEditor()) if (m_widget->textEditor())
m_widget->textEditor()->editorWidget()->invokeAssist(TextEditor::Completion); m_widget->textEditor()->editorWidget()->invokeAssist(TextEditor::Completion);
}); });
@@ -102,13 +102,9 @@ void TextEditorView::modelAttached(Model *model)
auto textEditor = qobject_cast<TextEditor::BaseTextEditor *>( auto textEditor = qobject_cast<TextEditor::BaseTextEditor *>(
QmlDesignerPlugin::instance()->currentDesignDocument()->textEditor()->duplicate()); QmlDesignerPlugin::instance()->currentDesignDocument()->textEditor()->duplicate());
// Set the context of the text editor, but we add another special context to override shortcuts.
Core::Context context = textEditor->context(); Core::Context context = textEditor->context();
context.prepend(TEXTEDITOR_CONTEXT_ID); context.prepend(TEXTEDITOR_CONTEXT_ID);
/*
* Set the context of the text editor, but we add another special context to override shortcuts.
*/
m_textEditorContext->setContext(context); m_textEditorContext->setContext(context);
m_widget->setTextEditor(textEditor); m_widget->setTextEditor(textEditor);
@@ -272,9 +268,8 @@ void TextEditorView::reformatFile()
auto document = auto document =
qobject_cast<QmlJSEditor::QmlJSEditorDocument *>(Core::EditorManager::currentDocument()); qobject_cast<QmlJSEditor::QmlJSEditorDocument *>(Core::EditorManager::currentDocument());
/* Reformat document if we have a .ui.qml file */ // Reformat document if we have a .ui.qml file
if (document if (document && document->filePath().toString().endsWith(".ui.qml")
&& document->filePath().toString().endsWith(".ui.qml")
&& DesignerSettings::getValue(DesignerSettingsKey::REFORMAT_UI_QML_FILES).toBool()) { && DesignerSettings::getValue(DesignerSettingsKey::REFORMAT_UI_QML_FILES).toBool()) {
QmlJS::Document::Ptr currentDocument(document->semanticInfo().document); QmlJS::Document::Ptr currentDocument(document->semanticInfo().document);
@@ -315,4 +310,3 @@ void TextEditorView::instancePropertyChanged(const QList<QPair<ModelNode, Proper
} }
} // namespace QmlDesigner } // namespace QmlDesigner

View File

@@ -27,7 +27,7 @@
#include <texteditorstatusbar.h> #include <texteditorstatusbar.h>
#include <texteditorview.h> #include <texteditorview.h>
#include <coreplugin/findplaceholder.h>
#include <rewriterview.h> #include <rewriterview.h>
#include <qmldesignerplugin.h> #include <qmldesignerplugin.h>
@@ -52,13 +52,17 @@ TextEditorWidget::TextEditorWidget(TextEditorView *textEditorView)
: QWidget() : QWidget()
, m_textEditorView(textEditorView) , m_textEditorView(textEditorView)
, m_statusBar(new TextEditorStatusBar(this)) , m_statusBar(new TextEditorStatusBar(this))
, m_findToolBar(new Core::FindToolBarPlaceHolder(this))
, m_layout(new QVBoxLayout(this))
{ {
setAcceptDrops(true); setAcceptDrops(true);
QBoxLayout *layout = new QVBoxLayout(this); m_statusBar->hide();
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0); m_layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_statusBar); m_layout->setSpacing(0);
m_layout->addWidget(m_statusBar);
m_layout->addWidget(m_findToolBar);
m_updateSelectionTimer.setSingleShot(true); m_updateSelectionTimer.setSingleShot(true);
m_updateSelectionTimer.setInterval(200); m_updateSelectionTimer.setInterval(200);
@@ -72,24 +76,22 @@ void TextEditorWidget::setTextEditor(TextEditor::BaseTextEditor *textEditor)
m_textEditor.reset(textEditor); m_textEditor.reset(textEditor);
if (textEditor) { if (textEditor) {
layout()->removeWidget(m_statusBar); m_layout->insertWidget(0, textEditor->editorWidget());
layout()->addWidget(textEditor->editorWidget());
layout()->addWidget(m_statusBar);
setFocusProxy(textEditor->editorWidget()); setFocusProxy(textEditor->editorWidget());
QmlDesignerPlugin::instance()->emitCurrentTextEditorChanged(textEditor); QmlDesignerPlugin::instance()->emitCurrentTextEditorChanged(textEditor);
connect(textEditor->editorWidget(), &QPlainTextEdit::cursorPositionChanged, connect(textEditor->editorWidget(), &QPlainTextEdit::cursorPositionChanged, this, [this] {
this, [this]() { // Cursor position is changed by rewriter
/* Cursor position is changed by rewriter */
if (!m_blockCursorSelectionSynchronisation) if (!m_blockCursorSelectionSynchronisation)
m_updateSelectionTimer.start(); m_updateSelectionTimer.start();
}); });
textEditor->editorWidget()->installEventFilter(this); textEditor->editorWidget()->installEventFilter(this);
static QString styleSheet = Theme::replaceCssColors( static QString styleSheet = Theme::replaceCssColors(
QString::fromUtf8(Utils::FileReader::fetchQrc( QString::fromUtf8(Utils::FileReader::fetchQrc(":/qmldesigner/scrollbar.css")));
":/qmldesigner/scrollbar.css")));
textEditor->editorWidget()->verticalScrollBar()->setStyleSheet(styleSheet); textEditor->editorWidget()->verticalScrollBar()->setStyleSheet(styleSheet);
textEditor->editorWidget()->horizontalScrollBar()->setStyleSheet(styleSheet); textEditor->editorWidget()->horizontalScrollBar()->setStyleSheet(styleSheet);
} }
@@ -105,7 +107,6 @@ void TextEditorWidget::contextHelp(const Core::IContext::HelpCallback &callback)
void TextEditorWidget::updateSelectionByCursorPosition() void TextEditorWidget::updateSelectionByCursorPosition()
{ {
/* Because of the timer we have to be careful. */
if (!m_textEditorView->model()) if (!m_textEditorView->model())
return; return;
@@ -162,11 +163,13 @@ void TextEditorWidget::gotoCursorPosition(int line, int column)
void TextEditorWidget::setStatusText(const QString &text) void TextEditorWidget::setStatusText(const QString &text)
{ {
m_statusBar->setText(text); m_statusBar->setText(text);
m_statusBar->setVisible(!text.isEmpty());
} }
void TextEditorWidget::clearStatusBar() void TextEditorWidget::clearStatusBar()
{ {
m_statusBar->clearText(); m_statusBar->clearText();
m_statusBar->hide();
} }
int TextEditorWidget::currentLine() const int TextEditorWidget::currentLine() const
@@ -199,6 +202,9 @@ bool TextEditorWidget::eventFilter(QObject *, QEvent *event)
auto keyEvent = static_cast<QKeyEvent *>(event); auto keyEvent = static_cast<QKeyEvent *>(event);
if (std::find(overrideKeys.begin(), overrideKeys.end(), keyEvent->key()) != overrideKeys.end()) { if (std::find(overrideKeys.begin(), overrideKeys.end(), keyEvent->key()) != overrideKeys.end()) {
if (keyEvent->key() == Qt::Key_Escape)
m_findToolBar->hide();
keyEvent->accept(); keyEvent->accept();
return true; return true;
} }
@@ -208,13 +214,14 @@ bool TextEditorWidget::eventFilter(QObject *, QEvent *event)
| Qt::AltModifier | Qt::AltModifier
| Qt::MetaModifier; | Qt::MetaModifier;
QKeySequence keySqeuence(keyEvent->key() | (keyEvent->modifiers() & relevantModifiers)); const QKeySequence keySqeuence(keyEvent->key() | (keyEvent->modifiers() & relevantModifiers));
for (const QKeySequence &overrideSequence : overrideSequences) for (const QKeySequence &overrideSequence : overrideSequences) {
if (keySqeuence.matches(overrideSequence)) { if (keySqeuence.matches(overrideSequence)) {
keyEvent->accept(); keyEvent->accept();
return true; return true;
} }
} }
}
return false; return false;
} }

View File

@@ -26,11 +26,15 @@
#include <texteditor/texteditor.h> #include <texteditor/texteditor.h>
#include <QWidget>
#include <QTimer> #include <QTimer>
#include <QVBoxLayout>
#include <QWidget>
#include <memory> #include <memory>
namespace Core {
class FindToolBarPlaceHolder;
}
namespace QmlDesigner { namespace QmlDesigner {
class TextEditorView; class TextEditorView;
@@ -72,7 +76,9 @@ private:
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; QTimer m_updateSelectionTimer;
TextEditorStatusBar *m_statusBar; TextEditorStatusBar *m_statusBar = nullptr;
Core::FindToolBarPlaceHolder *m_findToolBar = nullptr;
QVBoxLayout *m_layout = nullptr;
bool m_blockCursorSelectionSynchronisation = false; bool m_blockCursorSelectionSynchronisation = false;
bool m_blockRoundTrip = false; bool m_blockRoundTrip = false;
}; };