From 418dcfbcbbd15a63ed261b22da3391e43e7ab8aa Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Fri, 25 Jan 2019 15:04:50 +0100 Subject: [PATCH] Help: Avoid multiple lookups of help id for context help Context help would first query the database with potential IDs, and afterwards the help plugin would look up the links for the resulting ID again. Pass the HelpItem (which potentially contains the cached links) directly to context help. Change-Id: I73bddcd3cd4eacaea412b98d53c5e5354a31f3d5 Reviewed-by: David Schulz --- .../cmakeprojectmanager/cmakeeditor.cpp | 4 ++-- src/plugins/cmakeprojectmanager/cmakeeditor.h | 2 +- src/plugins/coreplugin/helpitem.cpp | 14 ++++++++--- src/plugins/coreplugin/helpitem.h | 5 ++-- src/plugins/coreplugin/icontext.h | 13 +++++----- src/plugins/designer/designercontext.cpp | 2 +- src/plugins/designer/designercontext.h | 2 +- src/plugins/helloworld/helloworldplugin.cpp | 2 +- src/plugins/help/helpplugin.cpp | 15 ++++++------ .../projectexplorer/projectexplorer.cpp | 2 +- .../components/texteditor/texteditorview.cpp | 2 +- src/plugins/qmldesigner/designmodecontext.cpp | 8 +++---- src/plugins/qmldesigner/designmodecontext.h | 8 +++---- src/plugins/texteditor/basehoverhandler.cpp | 11 ++++----- src/plugins/texteditor/texteditor.cpp | 24 +++++++++---------- src/plugins/texteditor/texteditor.h | 9 +++---- src/plugins/welcome/welcomeplugin.cpp | 2 +- 17 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp index 63425353617..36b9ddddec2 100644 --- a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp @@ -60,7 +60,7 @@ namespace Internal { // CMakeEditor // -void CMakeEditor::contextHelpId(const HelpIdCallback &callback) const +void CMakeEditor::contextHelp(const HelpIdCallback &callback) const { int pos = position(); @@ -102,7 +102,7 @@ void CMakeEditor::contextHelpId(const HelpIdCallback &callback) const } QString command = textAt(begin, end - begin).toLower(); - callback(QLatin1String("command/") + command); + callback(QString("command/" + command)); } // diff --git a/src/plugins/cmakeprojectmanager/cmakeeditor.h b/src/plugins/cmakeprojectmanager/cmakeeditor.h index 364fb48c026..5e5a88fcaec 100644 --- a/src/plugins/cmakeprojectmanager/cmakeeditor.h +++ b/src/plugins/cmakeprojectmanager/cmakeeditor.h @@ -38,7 +38,7 @@ class CMakeEditor : public TextEditor::BaseTextEditor Q_OBJECT public: - void contextHelpId(const HelpIdCallback &callback) const override; + void contextHelp(const HelpIdCallback &callback) const override; friend class CMakeEditorWidget; }; diff --git a/src/plugins/coreplugin/helpitem.cpp b/src/plugins/coreplugin/helpitem.cpp index dfb152ed056..a5025b1b6e6 100644 --- a/src/plugins/coreplugin/helpitem.cpp +++ b/src/plugins/coreplugin/helpitem.cpp @@ -32,6 +32,14 @@ using namespace Core; HelpItem::HelpItem() = default; +HelpItem::HelpItem(const char *helpId) + : m_helpId(QString::fromUtf8(helpId)) +{} + +HelpItem::HelpItem(const QString &helpId) + : m_helpId(helpId) +{} + HelpItem::HelpItem(const QString &helpId, Category category) : m_helpId(helpId), m_docMark(helpId), m_category(category) {} @@ -70,7 +78,7 @@ bool HelpItem::isValid() const { if (m_helpId.isEmpty()) return false; - if (!retrieveHelpLinks().isEmpty()) + if (!links().isEmpty()) return true; if (QUrl(m_helpId).isValid()) return true; @@ -86,7 +94,7 @@ QString HelpItem::extractContent(bool extended) const htmlExtractor.setMode(Utils::HtmlDocExtractor::FirstParagraph); QString contents; - QMap helpLinks = retrieveHelpLinks(); + QMap helpLinks = links(); if (helpLinks.isEmpty()) { // Maybe this is already an URL... QUrl url(m_helpId); @@ -134,7 +142,7 @@ QString HelpItem::extractContent(bool extended) const return contents; } -QMap HelpItem::retrieveHelpLinks() const +QMap HelpItem::links() const { if (m_helpLinks.isEmpty()) m_helpLinks = Core::HelpManager::linksForIdentifier(m_helpId); diff --git a/src/plugins/coreplugin/helpitem.h b/src/plugins/coreplugin/helpitem.h index f79836ce98d..496e32ffe7d 100644 --- a/src/plugins/coreplugin/helpitem.h +++ b/src/plugins/coreplugin/helpitem.h @@ -50,6 +50,8 @@ public: }; HelpItem(); + HelpItem(const char *helpId); + HelpItem(const QString &helpId); HelpItem(const QString &helpId, Category category); HelpItem(const QString &helpId, const QString &docMark, Category category); HelpItem(const QString &helpId, const QString &docMark, Category category, @@ -69,8 +71,7 @@ public: QString extractContent(bool extended) const; -private: - QMap retrieveHelpLinks() const; + QMap links() const; private: QString m_helpId; diff --git a/src/plugins/coreplugin/icontext.h b/src/plugins/coreplugin/icontext.h index d7bb022c5b6..f69dfc2bad8 100644 --- a/src/plugins/coreplugin/icontext.h +++ b/src/plugins/coreplugin/icontext.h @@ -25,8 +25,9 @@ #pragma once -#include -#include +#include "core_global.h" +#include "helpitem.h" +#include "id.h" #include #include @@ -73,17 +74,17 @@ public: virtual Context context() const { return m_context; } virtual QWidget *widget() const { return m_widget; } - using HelpIdCallback = std::function; - virtual void contextHelpId(const HelpIdCallback &callback) const { callback(m_contextHelpId); } + using HelpIdCallback = std::function; + virtual void contextHelp(const HelpIdCallback &callback) const { callback(m_contextHelp); } virtual void setContext(const Context &context) { m_context = context; } virtual void setWidget(QWidget *widget) { m_widget = widget; } - virtual void setContextHelpId(const QString &id) { m_contextHelpId = id; } + virtual void setContextHelp(const HelpItem &id) { m_contextHelp = id; } protected: Context m_context; QPointer m_widget; - QString m_contextHelpId; + HelpItem m_contextHelp; }; } // namespace Core diff --git a/src/plugins/designer/designercontext.cpp b/src/plugins/designer/designercontext.cpp index bff77559b15..edc7c098296 100644 --- a/src/plugins/designer/designercontext.cpp +++ b/src/plugins/designer/designercontext.cpp @@ -45,7 +45,7 @@ DesignerContext::DesignerContext(const Core::Context &context, setWidget(widget); } -void DesignerContext::contextHelpId(const HelpIdCallback &callback) const +void DesignerContext::contextHelp(const HelpIdCallback &callback) const { const QDesignerFormEditorInterface *core = FormEditorW::designerEditor(); callback(core->integration()->contextHelpId()); diff --git a/src/plugins/designer/designercontext.h b/src/plugins/designer/designercontext.h index 3259951e7d3..b9a892097f6 100644 --- a/src/plugins/designer/designercontext.h +++ b/src/plugins/designer/designercontext.h @@ -37,7 +37,7 @@ public: QWidget *widget, QObject *parent = nullptr); - void contextHelpId(const HelpIdCallback &callback) const override; + void contextHelp(const HelpIdCallback &callback) const override; }; } // namespace Internal diff --git a/src/plugins/helloworld/helloworldplugin.cpp b/src/plugins/helloworld/helloworldplugin.cpp index c2e431508b5..8d9309fd030 100644 --- a/src/plugins/helloworld/helloworldplugin.cpp +++ b/src/plugins/helloworld/helloworldplugin.cpp @@ -56,7 +56,7 @@ public: setIcon(QIcon()); setPriority(0); setId("HelloWorld.HelloWorldMode"); - setContextHelpId(QString()); + setContextHelp(QString()); } }; diff --git a/src/plugins/help/helpplugin.cpp b/src/plugins/help/helpplugin.cpp index 20dc5f13928..c7ac66436b4 100644 --- a/src/plugins/help/helpplugin.cpp +++ b/src/plugins/help/helpplugin.cpp @@ -65,6 +65,7 @@ #include #include #include +#include #include #include #include @@ -119,7 +120,7 @@ public: void modeChanged(Core::Id mode, Core::Id old); void requestContextHelp(); - void showContextHelp(const QString &contextHelpId); + void showContextHelp(const HelpItem &contextHelp); void activateIndex(); void activateContents(); @@ -649,17 +650,17 @@ void HelpPluginPrivate::requestContextHelp() QString contextHelpId = Utils::ToolTip::contextHelpId(); IContext *context = ICore::currentContextObject(); if (contextHelpId.isEmpty() && context) - context->contextHelpId([this](const QString &id) { showContextHelp(id); }); + context->contextHelp([this](const HelpItem &item) { showContextHelp(item); }); else showContextHelp(contextHelpId); } -void HelpPluginPrivate::showContextHelp(const QString &contextHelpId) +void HelpPluginPrivate::showContextHelp(const HelpItem &contextHelp) { - QMap links = Core::HelpManager::linksForIdentifier(contextHelpId); + QMap links = contextHelp.links(); // Maybe the id is already an URL - if (links.isEmpty() && LocalHelpManager::isValidUrl(contextHelpId)) - links.insert(contextHelpId, contextHelpId); + if (links.isEmpty() && LocalHelpManager::isValidUrl(contextHelp.helpId())) + links.insert(contextHelp.helpId(), contextHelp.helpId()); QUrl source = findBestLink(links); if (!source.isValid()) { @@ -675,7 +676,7 @@ void HelpPluginPrivate::showContextHelp(const QString &contextHelpId) .arg(HelpPlugin::tr("No Documentation")) .arg(creatorTheme()->color(Theme::BackgroundColorNormal).name()) .arg(creatorTheme()->color(Theme::TextColorNormal).name()) - .arg(contextHelpId) + .arg(contextHelp.helpId()) .arg(HelpPlugin::tr("No documentation available."))); } } else { diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 75882cff8c7..82d458ffa08 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -305,7 +305,7 @@ public: Icons::MODE_PROJECT_FLAT, Icons::MODE_PROJECT_FLAT_ACTIVE)); setPriority(Constants::P_MODE_SESSION); setId(Constants::MODE_SESSION); - setContextHelpId(QLatin1String("Managing Projects")); + setContextHelp("Managing Projects"); } }; diff --git a/src/plugins/qmldesigner/components/texteditor/texteditorview.cpp b/src/plugins/qmldesigner/components/texteditor/texteditorview.cpp index 462a51dbe75..737faf77ac4 100644 --- a/src/plugins/qmldesigner/components/texteditor/texteditorview.cpp +++ b/src/plugins/qmldesigner/components/texteditor/texteditorview.cpp @@ -160,7 +160,7 @@ void TextEditorView::contextHelpId(const Core::IContext::HelpIdCallback &callbac void TextEditorView::qmlJSEditorHelpId(const Core::IContext::HelpIdCallback &callback) const { if (m_widget->textEditor()) - m_widget->textEditor()->contextHelpId(callback); + m_widget->textEditor()->contextHelp(callback); else callback(QString()); } diff --git a/src/plugins/qmldesigner/designmodecontext.cpp b/src/plugins/qmldesigner/designmodecontext.cpp index 42d0f04bf66..ae8d2e2548c 100644 --- a/src/plugins/qmldesigner/designmodecontext.cpp +++ b/src/plugins/qmldesigner/designmodecontext.cpp @@ -40,7 +40,7 @@ DesignModeContext::DesignModeContext(QWidget *widget) setContext(Core::Context(Constants::C_QMLDESIGNER, Constants::C_QT_QUICK_TOOLS_MENU)); } -void DesignModeContext::contextHelpId(const HelpIdCallback &callback) const +void DesignModeContext::contextHelp(const HelpIdCallback &callback) const { qobject_cast(m_widget)->contextHelpId(callback); } @@ -52,7 +52,7 @@ FormEditorContext::FormEditorContext(QWidget *widget) setContext(Core::Context(Constants::C_QMLFORMEDITOR, Constants::C_QT_QUICK_TOOLS_MENU)); } -void FormEditorContext::contextHelpId(const HelpIdCallback &callback) const +void FormEditorContext::contextHelp(const HelpIdCallback &callback) const { qobject_cast(m_widget)->contextHelpId(callback); } @@ -64,7 +64,7 @@ NavigatorContext::NavigatorContext(QWidget *widget) setContext(Core::Context(Constants::C_QMLNAVIGATOR, Constants::C_QT_QUICK_TOOLS_MENU)); } -void NavigatorContext::contextHelpId(const HelpIdCallback &callback) const +void NavigatorContext::contextHelp(const HelpIdCallback &callback) const { qobject_cast(m_widget)->contextHelpId(callback); } @@ -76,7 +76,7 @@ TextEditorContext::TextEditorContext(QWidget *widget) setContext(Core::Context(Constants::C_QMLTEXTEDITOR, Constants::C_QT_QUICK_TOOLS_MENU)); } -void TextEditorContext::contextHelpId(const HelpIdCallback &callback) const +void TextEditorContext::contextHelp(const HelpIdCallback &callback) const { qobject_cast(m_widget)->contextHelpId(callback); } diff --git a/src/plugins/qmldesigner/designmodecontext.h b/src/plugins/qmldesigner/designmodecontext.h index e6772330756..ccea1d79d9b 100644 --- a/src/plugins/qmldesigner/designmodecontext.h +++ b/src/plugins/qmldesigner/designmodecontext.h @@ -39,7 +39,7 @@ class DesignModeContext : public Core::IContext public: DesignModeContext(QWidget *widget); - void contextHelpId(const Core::IContext::HelpIdCallback &callback) const override; + void contextHelp(const Core::IContext::HelpIdCallback &callback) const override; }; class FormEditorContext : public Core::IContext @@ -48,7 +48,7 @@ class FormEditorContext : public Core::IContext public: FormEditorContext(QWidget *widget); - void contextHelpId(const Core::IContext::HelpIdCallback &callback) const override; + void contextHelp(const Core::IContext::HelpIdCallback &callback) const override; }; class NavigatorContext : public Core::IContext @@ -57,7 +57,7 @@ class NavigatorContext : public Core::IContext public: NavigatorContext(QWidget *widget); - void contextHelpId(const Core::IContext::HelpIdCallback &callback) const override; + void contextHelp(const Core::IContext::HelpIdCallback &callback) const override; }; class TextEditorContext : public Core::IContext @@ -66,7 +66,7 @@ class TextEditorContext : public Core::IContext public: TextEditorContext(QWidget *widget); - void contextHelpId(const Core::IContext::HelpIdCallback &callback) const override; + void contextHelp(const Core::IContext::HelpIdCallback &callback) const override; }; } diff --git a/src/plugins/texteditor/basehoverhandler.cpp b/src/plugins/texteditor/basehoverhandler.cpp index 19ab9073e9c..6529ab9d39b 100644 --- a/src/plugins/texteditor/basehoverhandler.cpp +++ b/src/plugins/texteditor/basehoverhandler.cpp @@ -45,7 +45,7 @@ void BaseHoverHandler::checkPriority(TextEditorWidget *widget, int pos, ReportPriority report) { - widget->setContextHelpId(QString()); + widget->setContextHelpItem({}); process(widget, pos, report); } @@ -117,12 +117,9 @@ bool BaseHoverHandler::isContextHelpRequest() const void BaseHoverHandler::propagateHelpId(TextEditorWidget *widget, const Core::IContext::HelpIdCallback &callback) { - QString id; - if (lastHelpItemIdentified().isValid()) - id = lastHelpItemIdentified().helpId(); - - widget->setContextHelpId(id); - callback(id); + const Core::HelpItem contextHelp = lastHelpItemIdentified(); + widget->setContextHelpItem(contextHelp); + callback(contextHelp); } void BaseHoverHandler::process(TextEditorWidget *widget, int pos, ReportPriority report) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index aed04a3ba77..c42358a8d53 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -663,7 +663,7 @@ public: QRectF getLastLineLineRect(const QTextBlock &block); RefactorOverlay *m_refactorOverlay = nullptr; - QString m_contextHelpId; + HelpItem m_contextHelpItem; QBasicTimer foldedBlockTimer; int visibleFoldedBlockNumber = -1; @@ -8000,36 +8000,36 @@ void BaseTextEditor::select(int toPos) void TextEditorWidgetPrivate::updateCursorPosition() { - m_contextHelpId.clear(); + m_contextHelpItem = HelpItem(); if (!q->textCursor().block().isVisible()) q->ensureCursorVisible(); } -void BaseTextEditor::contextHelpId(const HelpIdCallback &callback) const +void BaseTextEditor::contextHelp(const HelpIdCallback &callback) const { - editorWidget()->contextHelpId(callback); + editorWidget()->contextHelpItem(callback); } -void BaseTextEditor::setContextHelpId(const QString &id) +void BaseTextEditor::setContextHelp(const HelpItem &item) { - IEditor::setContextHelpId(id); - editorWidget()->setContextHelpId(id); + IEditor::setContextHelp(item); + editorWidget()->setContextHelpItem(item); } -void TextEditorWidget::contextHelpId(const IContext::HelpIdCallback &callback) +void TextEditorWidget::contextHelpItem(const IContext::HelpIdCallback &callback) { - if (d->m_contextHelpId.isEmpty() && !d->m_hoverHandlers.isEmpty()) { + if (!d->m_contextHelpItem.isValid() && !d->m_hoverHandlers.isEmpty()) { d->m_hoverHandlers.first()->contextHelpId(this, Text::wordStartCursor(textCursor()).position(), callback); } else { - callback(d->m_contextHelpId); + callback(d->m_contextHelpItem); } } -void TextEditorWidget::setContextHelpId(const QString &id) +void TextEditorWidget::setContextHelpItem(const HelpItem &item) { - d->m_contextHelpId = id; + d->m_contextHelpItem = item; } RefactorMarkers TextEditorWidget::refactorMarkers() const diff --git a/src/plugins/texteditor/texteditor.h b/src/plugins/texteditor/texteditor.h index f1d1b193535..b2c840e6c87 100644 --- a/src/plugins/texteditor/texteditor.h +++ b/src/plugins/texteditor/texteditor.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -130,8 +131,8 @@ public: bool restoreState(const QByteArray &state) override; QWidget *toolBar() override; - void contextHelpId(const HelpIdCallback &callback) const override; // from IContext - void setContextHelpId(const QString &id) override; + void contextHelp(const HelpIdCallback &callback) const override; // from IContext + void setContextHelp(const Core::HelpItem &item) override; int currentLine() const override; int currentColumn() const override; @@ -542,8 +543,8 @@ public: QChar characterAt(int pos) const; QString textAt(int from, int to) const; - void contextHelpId(const Core::IContext::HelpIdCallback &callback); - void setContextHelpId(const QString &id); + void contextHelpItem(const Core::IContext::HelpIdCallback &callback); + void setContextHelpItem(const Core::HelpItem &item); static TextEditorWidget *currentTextEditorWidget(); diff --git a/src/plugins/welcome/welcomeplugin.cpp b/src/plugins/welcome/welcomeplugin.cpp index b70639fb4e2..dea242a8108 100644 --- a/src/plugins/welcome/welcomeplugin.cpp +++ b/src/plugins/welcome/welcomeplugin.cpp @@ -309,7 +309,7 @@ WelcomeMode::WelcomeMode() setPriority(Constants::P_MODE_WELCOME); setId(Constants::MODE_WELCOME); - setContextHelpId("Qt Creator Manual"); + setContextHelp("Qt Creator Manual"); setContext(Context(Constants::C_WELCOME_MODE)); QPalette palette = creatorTheme()->palette();