From 9eb826f3931ad9ed32582c709cee06d3fc7843d0 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Tue, 11 Apr 2023 23:12:36 +0200 Subject: [PATCH] JavaScriptFilter: Use Acceptor for LocatorFilterEntry Change-Id: Icceb830a391809cc760adf106e6c9e8b71dcb176 Reviewed-by: Qt CI Bot Reviewed-by: Eike Ziller Reviewed-by: --- .../coreplugin/locator/javascriptfilter.cpp | 59 ++++++++----------- .../coreplugin/locator/javascriptfilter.h | 3 - 2 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/plugins/coreplugin/locator/javascriptfilter.cpp b/src/plugins/coreplugin/locator/javascriptfilter.cpp index 37803abbaaa..a0fb181b56d 100644 --- a/src/plugins/coreplugin/locator/javascriptfilter.cpp +++ b/src/plugins/coreplugin/locator/javascriptfilter.cpp @@ -12,8 +12,6 @@ namespace Core { namespace Internal { -enum class EngineAction { Reset = 1, Abort }; - JavaScriptFilter::JavaScriptFilter() { setId("JavaScriptFilter"); @@ -52,53 +50,46 @@ QList JavaScriptFilter::matchesFor( QList entries; if (entry.trimmed().isEmpty()) { - LocatorFilterEntry entry{this, Tr::tr("Reset Engine")}; - entry.internalData = QVariant::fromValue(EngineAction::Reset); + LocatorFilterEntry entry; + entry.displayName = Tr::tr("Reset Engine"); + entry.acceptor = [this] { + m_engine.reset(); + return AcceptResult(); + }; entries.append(entry); } else { + // Note, that evaluate may be interrupted from caller thread. + // In this case m_aborted is set to true. const QString result = m_engine->evaluate(entry).toString(); if (m_aborted) { const QString message = entry + " = " + Tr::tr("Engine aborted after timeout."); - LocatorFilterEntry entry(this, message); - entry.internalData = QVariant::fromValue(EngineAction::Abort); + LocatorFilterEntry entry; + entry.displayName = message; + entry.acceptor = [] { return AcceptResult(); }; entries.append(entry); } else { + const auto acceptor = [](const QString &clipboardContents) { + return [clipboardContents] { + QGuiApplication::clipboard()->setText(clipboardContents); + return AcceptResult(); + }; + }; const QString expression = entry + " = " + result; entries.append({this, expression}); - LocatorFilterEntry resultEntry(this, Tr::tr("Copy to clipboard: %1").arg(result)); - resultEntry.internalData = result; + LocatorFilterEntry resultEntry; + resultEntry.displayName = Tr::tr("Copy to clipboard: %1").arg(result); + resultEntry.acceptor = acceptor(result); entries.append(resultEntry); - LocatorFilterEntry expressionEntry(this, Tr::tr("Copy to clipboard: %1").arg(expression)); - resultEntry.internalData = expression; + + LocatorFilterEntry expressionEntry; + expressionEntry.displayName = Tr::tr("Copy to clipboard: %1").arg(expression); + expressionEntry.acceptor = acceptor(expression); entries.append(expressionEntry); } } - return entries; } -void JavaScriptFilter::accept(const LocatorFilterEntry &selection, QString *newText, - int *selectionStart, int *selectionLength) const -{ - Q_UNUSED(newText) - Q_UNUSED(selectionStart) - Q_UNUSED(selectionLength) - - if (selection.internalData.isNull()) - return; - - const EngineAction action = selection.internalData.value(); - if (action == EngineAction::Reset) { - m_engine.reset(); - return; - } - if (action == EngineAction::Abort) - return; - - QClipboard *clipboard = QGuiApplication::clipboard(); - clipboard->setText(selection.internalData.toString()); -} - void JavaScriptFilter::setupEngine() { m_engine.reset(new QJSEngine); @@ -130,5 +121,3 @@ void JavaScriptFilter::setupEngine() } // namespace Internal } // namespace Core - -Q_DECLARE_METATYPE(Core::Internal::EngineAction) diff --git a/src/plugins/coreplugin/locator/javascriptfilter.h b/src/plugins/coreplugin/locator/javascriptfilter.h index 5153e0b7b56..b65abf53439 100644 --- a/src/plugins/coreplugin/locator/javascriptfilter.h +++ b/src/plugins/coreplugin/locator/javascriptfilter.h @@ -27,9 +27,6 @@ public: void prepareSearch(const QString &entry) override; QList matchesFor(QFutureInterface &future, const QString &entry) override; - void accept(const Core::LocatorFilterEntry &selection, QString *newText, - int *selectionStart, int *selectionLength) const override; - private: void setupEngine();