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 <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2019-01-25 15:04:50 +01:00
parent c04c5c1575
commit 418dcfbcbb
17 changed files with 67 additions and 58 deletions

View File

@@ -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));
}
//

View File

@@ -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;
};

View File

@@ -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<QString, QUrl> helpLinks = retrieveHelpLinks();
QMap<QString, QUrl> 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<QString, QUrl> HelpItem::retrieveHelpLinks() const
QMap<QString, QUrl> HelpItem::links() const
{
if (m_helpLinks.isEmpty())
m_helpLinks = Core::HelpManager::linksForIdentifier(m_helpId);

View File

@@ -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<QString, QUrl> retrieveHelpLinks() const;
QMap<QString, QUrl> links() const;
private:
QString m_helpId;

View File

@@ -25,8 +25,9 @@
#pragma once
#include <coreplugin/core_global.h>
#include <coreplugin/id.h>
#include "core_global.h"
#include "helpitem.h"
#include "id.h"
#include <QList>
#include <QObject>
@@ -73,17 +74,17 @@ public:
virtual Context context() const { return m_context; }
virtual QWidget *widget() const { return m_widget; }
using HelpIdCallback = std::function<void(const QString &id)>;
virtual void contextHelpId(const HelpIdCallback &callback) const { callback(m_contextHelpId); }
using HelpIdCallback = std::function<void(const HelpItem &id)>;
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<QWidget> m_widget;
QString m_contextHelpId;
HelpItem m_contextHelp;
};
} // namespace Core

View File

@@ -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());

View File

@@ -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

View File

@@ -56,7 +56,7 @@ public:
setIcon(QIcon());
setPriority(0);
setId("HelloWorld.HelloWorldMode");
setContextHelpId(QString());
setContextHelp(QString());
}
};

View File

@@ -65,6 +65,7 @@
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/findplaceholder.h>
#include <coreplugin/helpitem.h>
#include <coreplugin/icore.h>
#include <coreplugin/minisplitter.h>
#include <coreplugin/modemanager.h>
@@ -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<QString, QUrl> links = Core::HelpManager::linksForIdentifier(contextHelpId);
QMap<QString, QUrl> 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 {

View File

@@ -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");
}
};

View File

@@ -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());
}

View File

@@ -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<DesignModeWidget *>(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<FormEditorWidget *>(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<NavigatorWidget *>(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<TextEditorWidget *>(m_widget)->contextHelpId(callback);
}

View File

@@ -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;
};
}

View File

@@ -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)

View File

@@ -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

View File

@@ -33,6 +33,7 @@
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/editormanager/ieditorfactory.h>
#include <coreplugin/helpitem.h>
#include <utils/link.h>
#include <utils/uncommentselection.h>
@@ -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();

View File

@@ -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();