Refactor how base hover handler manipulates the help items (qt docs interaction).

This commit is contained in:
Leandro Melo
2010-08-27 12:11:55 +02:00
parent db7bf9a806
commit ba12a3390e
11 changed files with 335 additions and 256 deletions

View File

@@ -199,16 +199,16 @@ void CppElementEvaluator::handleLookupItemMatch(const Snapshot &snapshot,
} }
// CppElement // CppElement
CppElement::CppElement() : m_helpCategory(CppHoverHandler::HelpCandidate::Unknown) CppElement::CppElement() : m_helpCategory(TextEditor::HelpItem::Unknown)
{} {}
CppElement::~CppElement() CppElement::~CppElement()
{} {}
void CppElement::setHelpCategory(const CppHoverHandler::HelpCandidate::Category &cat) void CppElement::setHelpCategory(const TextEditor::HelpItem::Category &cat)
{ m_helpCategory = cat; } { m_helpCategory = cat; }
const CppHoverHandler::HelpCandidate::Category &CppElement::helpCategory() const const TextEditor::HelpItem::Category &CppElement::helpCategory() const
{ return m_helpCategory; } { return m_helpCategory; }
void CppElement::setHelpIdCandidates(const QStringList &candidates) void CppElement::setHelpIdCandidates(const QStringList &candidates)
@@ -273,7 +273,7 @@ CppInclude::CppInclude(const Document::Include &includeFile) :
m_path(QDir::toNativeSeparators(includeFile.fileName())), m_path(QDir::toNativeSeparators(includeFile.fileName())),
m_fileName(QFileInfo(includeFile.fileName()).fileName()) m_fileName(QFileInfo(includeFile.fileName()).fileName())
{ {
setHelpCategory(CppHoverHandler::HelpCandidate::Brief); setHelpCategory(TextEditor::HelpItem::Brief);
setHelpIdCandidates(QStringList(m_fileName)); setHelpIdCandidates(QStringList(m_fileName));
setHelpMark(m_fileName); setHelpMark(m_fileName);
setLink(CPPEditor::Link(m_path)); setLink(CPPEditor::Link(m_path));
@@ -289,7 +289,7 @@ const QString &CppInclude::fileName() const
// CppMacro // CppMacro
CppMacro::CppMacro(const Macro &macro) : CppElement() CppMacro::CppMacro(const Macro &macro) : CppElement()
{ {
setHelpCategory(CppHoverHandler::HelpCandidate::Macro); setHelpCategory(TextEditor::HelpItem::Macro);
setHelpIdCandidates(QStringList(macro.name())); setHelpIdCandidates(QStringList(macro.name()));
setHelpMark(macro.name()); setHelpMark(macro.name());
setLink(CPPEditor::Link(macro.fileName(), macro.line())); setLink(CPPEditor::Link(macro.fileName(), macro.line()));
@@ -366,7 +366,7 @@ const QIcon &CppDeclarableElement::icon() const
// CppNamespace // CppNamespace
CppNamespace::CppNamespace(Symbol *declaration) : CppDeclarableElement(declaration) CppNamespace::CppNamespace(Symbol *declaration) : CppDeclarableElement(declaration)
{ {
setHelpCategory(CppHoverHandler::HelpCandidate::ClassOrNamespace); setHelpCategory(TextEditor::HelpItem::ClassOrNamespace);
} }
CppNamespace::~CppNamespace() CppNamespace::~CppNamespace()
@@ -375,7 +375,7 @@ CppNamespace::~CppNamespace()
// CppClass // CppClass
CppClass::CppClass(Symbol *declaration) : CppDeclarableElement(declaration) CppClass::CppClass(Symbol *declaration) : CppDeclarableElement(declaration)
{ {
setHelpCategory(CppHoverHandler::HelpCandidate::ClassOrNamespace); setHelpCategory(TextEditor::HelpItem::ClassOrNamespace);
} }
CppClass::~CppClass() CppClass::~CppClass()
@@ -418,7 +418,7 @@ const QList<CppClass> &CppClass::bases() const
// CppFunction // CppFunction
CppFunction::CppFunction(Symbol *declaration) : CppDeclarableElement(declaration) CppFunction::CppFunction(Symbol *declaration) : CppDeclarableElement(declaration)
{ {
setHelpCategory(CppHoverHandler::HelpCandidate::Function); setHelpCategory(TextEditor::HelpItem::Function);
const FullySpecifiedType &type = declaration->type(); const FullySpecifiedType &type = declaration->type();
@@ -438,7 +438,7 @@ CppFunction::~CppFunction()
// CppEnum // CppEnum
CppEnum::CppEnum(Symbol *declaration) : CppDeclarableElement(declaration) CppEnum::CppEnum(Symbol *declaration) : CppDeclarableElement(declaration)
{ {
setHelpCategory(CppHoverHandler::HelpCandidate::Enum); setHelpCategory(TextEditor::HelpItem::Enum);
if (declaration->enclosingScope()->isEnum()) { if (declaration->enclosingScope()->isEnum()) {
Symbol *enumSymbol = declaration->enclosingScope()->asEnum(); Symbol *enumSymbol = declaration->enclosingScope()->asEnum();
@@ -455,7 +455,7 @@ CppEnum::~CppEnum()
CppTypedef::CppTypedef(Symbol *declaration) : CppTypedef::CppTypedef(Symbol *declaration) :
CppDeclarableElement(declaration) CppDeclarableElement(declaration)
{ {
setHelpCategory(CppHoverHandler::HelpCandidate::Typedef); setHelpCategory(TextEditor::HelpItem::Typedef);
} }
CppTypedef::~CppTypedef() CppTypedef::~CppTypedef()
@@ -488,7 +488,7 @@ CppVariable::CppVariable(Symbol *declaration, const LookupContext &context, Scop
const QString &name = const QString &name =
overview.prettyName(LookupContext::fullyQualifiedName(symbol)); overview.prettyName(LookupContext::fullyQualifiedName(symbol));
setTooltip(name); setTooltip(name);
setHelpCategory(CppHoverHandler::HelpCandidate::ClassOrNamespace); setHelpCategory(TextEditor::HelpItem::ClassOrNamespace);
setHelpMark(name); setHelpMark(name);
setHelpIdCandidates(QStringList(name)); setHelpIdCandidates(QStringList(name));
} }

View File

@@ -31,7 +31,8 @@
#define CPPHIGHLEVELMODEL_H #define CPPHIGHLEVELMODEL_H
#include "cppeditor.h" #include "cppeditor.h"
#include "cpphoverhandler.h"
#include <texteditor/helpitem.h>
#include <cplusplus/CppDocument.h> #include <cplusplus/CppDocument.h>
#include <cplusplus/Overview.h> #include <cplusplus/Overview.h>
@@ -88,7 +89,7 @@ class CppElement
public: public:
virtual ~CppElement(); virtual ~CppElement();
const CppHoverHandler::HelpCandidate::Category &helpCategory() const; const TextEditor::HelpItem::Category &helpCategory() const;
const QStringList &helpIdCandidates() const; const QStringList &helpIdCandidates() const;
const QString &helpMark() const; const QString &helpMark() const;
const CPPEditor::Link &link() const; const CPPEditor::Link &link() const;
@@ -97,7 +98,7 @@ public:
protected: protected:
CppElement(); CppElement();
void setHelpCategory(const CppHoverHandler::HelpCandidate::Category &category); void setHelpCategory(const TextEditor::HelpItem::Category &category);
void setLink(const CPPEditor::Link &link); void setLink(const CPPEditor::Link &link);
void setTooltip(const QString &tooltip); void setTooltip(const QString &tooltip);
void setHelpIdCandidates(const QStringList &candidates); void setHelpIdCandidates(const QStringList &candidates);
@@ -105,7 +106,7 @@ protected:
void setHelpMark(const QString &mark); void setHelpMark(const QString &mark);
private: private:
CppHoverHandler::HelpCandidate::Category m_helpCategory; TextEditor::HelpItem::Category m_helpCategory;
QStringList m_helpIdCandidates; QStringList m_helpIdCandidates;
QString m_helpMark; QString m_helpMark;
CPPEditor::Link m_link; CPPEditor::Link m_link;

View File

@@ -33,10 +33,12 @@
#include <coreplugin/editormanager/ieditor.h> #include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/helpmanager.h>
#include <cpptools/cppmodelmanagerinterface.h> #include <cpptools/cppmodelmanagerinterface.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <texteditor/itexteditor.h> #include <texteditor/itexteditor.h>
#include <texteditor/basetexteditor.h> #include <texteditor/basetexteditor.h>
#include <texteditor/helpitem.h>
#include <QtGui/QTextCursor> #include <QtGui/QTextCursor>
@@ -84,39 +86,37 @@ void CppHoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
QSharedPointer<CppElement> cppElement = evaluator.identifyCppElement(); QSharedPointer<CppElement> cppElement = evaluator.identifyCppElement();
if (!cppElement.isNull()) { if (!cppElement.isNull()) {
setToolTip(cppElement->tooltip()); setToolTip(cppElement->tooltip());
foreach (const QString &helpId, cppElement->helpIdCandidates()) foreach (QString helpId, cppElement->helpIdCandidates()) {
addHelpCandidate(HelpCandidate(helpId, bool found = false;
if (!Core::HelpManager::instance()->linksForIdentifier(helpId).isEmpty()) {
found = true;
} else {
helpId = removeClassNameQualification(helpId);
if (!Core::HelpManager::instance()->linksForIdentifier(helpId).isEmpty())
found = true;
}
if (found) {
setLastHelpItemIdentified(TextEditor::HelpItem(helpId,
cppElement->helpMark(), cppElement->helpMark(),
cppElement->helpCategory())); cppElement->helpCategory()));
break;
} }
} }
}
void CppHoverHandler::evaluateHelpCandidates()
{
for (int i = 0; i < helpCandidates().size() && matchingHelpCandidate() == -1; ++i) {
if (helpIdExists(helpCandidates().at(i).m_helpId)) {
setMatchingHelpCandidate(i);
} else {
// There are class help ids with and without qualification.
HelpCandidate candidate = helpCandidates().at(i);
const QString &helpId = removeClassNameQualification(candidate.m_helpId);
if (helpIdExists(helpId)) {
candidate.m_helpId = helpId;
setHelpCandidate(candidate, i);
setMatchingHelpCandidate(i);
}
} }
} }
} }
void CppHoverHandler::decorateToolTip(TextEditor::ITextEditor *editor) void CppHoverHandler::decorateToolTip(TextEditor::ITextEditor *editor)
{ {
if (matchingHelpCandidate() != -1) { CPPEditor *cppEditor = qobject_cast<CPPEditor *>(editor->widget());
const QString &contents = getDocContents(extendToolTips(editor)); if (!cppEditor)
return;
const TextEditor::HelpItem &help = lastHelpItemIdentified();
if (help.isValid()) {
const QString &contents = help.extractContent(extendToolTips(editor));
if (!contents.isEmpty()) { if (!contents.isEmpty()) {
HelpCandidate::Category cat = helpCandidate(matchingHelpCandidate()).m_category; if (help.category() == TextEditor::HelpItem::ClassOrNamespace)
if (cat == HelpCandidate::ClassOrNamespace)
appendToolTip(contents); appendToolTip(contents);
else else
setToolTip(contents); setToolTip(contents);
@@ -126,5 +126,6 @@ void CppHoverHandler::decorateToolTip(TextEditor::ITextEditor *editor)
tip.append(QLatin1String("</nobr>")); tip.append(QLatin1String("</nobr>"));
setToolTip(tip); setToolTip(tip);
} }
addF1ToToolTip();
} }
} }

View File

@@ -55,7 +55,6 @@ public:
private: private:
virtual bool acceptEditor(Core::IEditor *editor); virtual bool acceptEditor(Core::IEditor *editor);
virtual void identifyMatch(TextEditor::ITextEditor *editor, int pos); virtual void identifyMatch(TextEditor::ITextEditor *editor, int pos);
virtual void evaluateHelpCandidates();
virtual void decorateToolTip(TextEditor::ITextEditor *editor); virtual void decorateToolTip(TextEditor::ITextEditor *editor);
}; };

View File

@@ -33,6 +33,7 @@
#include <coreplugin/editormanager/ieditor.h> #include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/helpmanager.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <qmljs/qmljsinterpreter.h> #include <qmljs/qmljsinterpreter.h>
#include <qmljs/parser/qmljsast_p.h> #include <qmljs/parser/qmljsast_p.h>
@@ -40,9 +41,12 @@
#include <qmljs/qmljscheck.h> #include <qmljs/qmljscheck.h>
#include <texteditor/itexteditor.h> #include <texteditor/itexteditor.h>
#include <texteditor/basetexteditor.h> #include <texteditor/basetexteditor.h>
#include <texteditor/helpitem.h>
#include <texteditor/tooltip/tooltip.h> #include <texteditor/tooltip/tooltip.h>
#include <texteditor/tooltip/tipcontents.h> #include <texteditor/tooltip/tipcontents.h>
#include <QtCore/QList>
using namespace Core; using namespace Core;
using namespace QmlJS; using namespace QmlJS;
using namespace QmlJSEditor; using namespace QmlJSEditor;
@@ -95,6 +99,8 @@ bool HoverHandler::acceptEditor(IEditor *editor)
void HoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos) void HoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
{ {
reset();
if (!m_modelManager) if (!m_modelManager)
return; return;
@@ -114,8 +120,12 @@ void HoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
const Document::Ptr qmlDocument = semanticInfo.document; const Document::Ptr qmlDocument = semanticInfo.document;
LookupContext::Ptr lookupContext = semanticInfo.lookupContext(astPath); LookupContext::Ptr lookupContext = semanticInfo.lookupContext(astPath);
if (!matchColorItem(lookupContext, qmlDocument, astPath, pos)) if (!matchColorItem(lookupContext, qmlDocument, astPath, pos)) {
handleOrdinaryMatch(lookupContext, semanticInfo.nodeUnderCursor(pos)); handleOrdinaryMatch(lookupContext, semanticInfo.nodeUnderCursor(pos));
const QString &helpId = qmlHelpId(toolTip());
if (!helpId.isEmpty())
setLastHelpItemIdentified(TextEditor::HelpItem(helpId, TextEditor::HelpItem::QML));
}
} }
} }
@@ -192,43 +202,15 @@ void HoverHandler::handleOrdinaryMatch(const LookupContext::Ptr &lookupContext,
if (node && !(AST::cast<AST::StringLiteral *>(node) != 0 || if (node && !(AST::cast<AST::StringLiteral *>(node) != 0 ||
AST::cast<AST::NumericLiteral *>(node) != 0)) { AST::cast<AST::NumericLiteral *>(node) != 0)) {
const Interpreter::Value *value = lookupContext->evaluate(node); const Interpreter::Value *value = lookupContext->evaluate(node);
setToolTip(prettyPrint(value, lookupContext->context())); prettyPrintTooltip(value, lookupContext->context());
} }
} }
void HoverHandler::resetExtras() void HoverHandler::reset()
{ {
m_colorTip = QColor(); m_colorTip = QColor();
} }
void HoverHandler::evaluateHelpCandidates()
{
for (int i = 0; i < helpCandidates().size(); ++i) {
HelpCandidate helpCandidate = helpCandidates().at(i);
helpCandidate.m_helpId.prepend(QLatin1String("QML."));
if (helpIdExists(helpCandidate.m_helpId)) {
setMatchingHelpCandidate(i);
setHelpCandidate(helpCandidate, i);
break;
}
}
}
void HoverHandler::decorateToolTip(TextEditor::ITextEditor *editor)
{
if (matchingHelpCandidate() != -1) {
const QString &contents = getDocContents(extendToolTips(editor));
if (!contents.isEmpty()) {
appendToolTip(contents);
} else {
QString tip = Qt::escape(toolTip());
tip.prepend(QLatin1String("<nobr>"));
tip.append(QLatin1String("</nobr>"));
setToolTip(tip);
}
}
}
void HoverHandler::operateTooltip(TextEditor::ITextEditor *editor, const QPoint &point) void HoverHandler::operateTooltip(TextEditor::ITextEditor *editor, const QPoint &point)
{ {
if (toolTip().isEmpty()) if (toolTip().isEmpty())
@@ -239,9 +221,6 @@ void HoverHandler::operateTooltip(TextEditor::ITextEditor *editor, const QPoint
TextEditor::ColorContent(m_colorTip), TextEditor::ColorContent(m_colorTip),
editor->widget()); editor->widget());
} else { } else {
if (matchingHelpCandidate() != -1)
addF1ToToolTip();
TextEditor::ToolTip::instance()->show(point, TextEditor::ToolTip::instance()->show(point,
TextEditor::TextContent(toolTip()), TextEditor::TextContent(toolTip()),
editor->widget()); editor->widget());
@@ -249,33 +228,41 @@ void HoverHandler::operateTooltip(TextEditor::ITextEditor *editor, const QPoint
} }
} }
QString HoverHandler::prettyPrint(const QmlJS::Interpreter::Value *value, void HoverHandler::prettyPrintTooltip(const QmlJS::Interpreter::Value *value,
const QmlJS::Interpreter::Context *context) const QmlJS::Interpreter::Context *context)
{ {
if (! value) if (! value)
return QString(); return;
if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) { if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
bool found = false;
do { do {
const QString className = objectValue->className(); const QString className = objectValue->className();
if (! className.isEmpty()) if (! className.isEmpty()) {
addHelpCandidate(HelpCandidate(className, HelpCandidate::QML)); found = !qmlHelpId(className).isEmpty();
if (toolTip().isEmpty() || found)
objectValue = objectValue->prototype(context); setToolTip(className);
} while (objectValue);
if (! helpCandidates().isEmpty())
return helpCandidates().first().m_helpId;
} else if (const Interpreter::QmlEnumValue *enumValue =
dynamic_cast<const Interpreter::QmlEnumValue *>(value)) {
return enumValue->name();
} }
objectValue = objectValue->prototype(context);
} while (objectValue && !found);
} else if (const Interpreter::QmlEnumValue *enumValue =
dynamic_cast<const Interpreter::QmlEnumValue *>(value)) {
setToolTip(enumValue->name());
}
if (toolTip().isEmpty()) {
QString typeId = context->engine()->typeId(value); QString typeId = context->engine()->typeId(value);
if (typeId != QLatin1String("undefined"))
if (typeId == QLatin1String("undefined")) setToolTip(typeId);
typeId.clear(); }
}
return typeId;
QString HoverHandler::qmlHelpId(const QString &itemName) const
{
QString helpId(QLatin1String("QML.") + itemName);
if (!Core::HelpManager::instance()->linksForIdentifier(helpId).isEmpty())
return helpId;
return QString();
} }

View File

@@ -34,10 +34,12 @@
#include <qmljs/qmljslookupcontext.h> #include <qmljs/qmljslookupcontext.h>
#include <texteditor/basehoverhandler.h> #include <texteditor/basehoverhandler.h>
#include <QtCore/QObject>
#include <QtCore/QStringList>
#include <QtGui/QColor> #include <QtGui/QColor>
QT_BEGIN_NAMESPACE
template <class> class QList;
QT_END_NAMESPACE
namespace Core { namespace Core {
class IEditor; class IEditor;
} }
@@ -59,11 +61,10 @@ public:
HoverHandler(QObject *parent = 0); HoverHandler(QObject *parent = 0);
private: private:
void reset();
virtual bool acceptEditor(Core::IEditor *editor); virtual bool acceptEditor(Core::IEditor *editor);
virtual void identifyMatch(TextEditor::ITextEditor *editor, int pos); virtual void identifyMatch(TextEditor::ITextEditor *editor, int pos);
virtual void resetExtras();
virtual void evaluateHelpCandidates();
virtual void decorateToolTip(TextEditor::ITextEditor *editor);
virtual void operateTooltip(TextEditor::ITextEditor *editor, const QPoint &point); virtual void operateTooltip(TextEditor::ITextEditor *editor, const QPoint &point);
bool matchDiagnosticMessage(QmlJSTextEditor *qmlEditor, int pos); bool matchDiagnosticMessage(QmlJSTextEditor *qmlEditor, int pos);
@@ -74,9 +75,11 @@ private:
void handleOrdinaryMatch(const QmlJS::LookupContext::Ptr &lookupContext, void handleOrdinaryMatch(const QmlJS::LookupContext::Ptr &lookupContext,
QmlJS::AST::Node *node); QmlJS::AST::Node *node);
QString prettyPrint(const QmlJS::Interpreter::Value *value, void prettyPrintTooltip(const QmlJS::Interpreter::Value *value,
const QmlJS::Interpreter::Context *context); const QmlJS::Interpreter::Context *context);
QString qmlHelpId(const QString &itemName) const;
QmlJS::ModelManagerInterface *m_modelManager; QmlJS::ModelManagerInterface *m_modelManager;
QColor m_colorTip; QColor m_colorTip;
}; };

View File

@@ -45,8 +45,7 @@
using namespace TextEditor; using namespace TextEditor;
using namespace Core; using namespace Core;
BaseHoverHandler::BaseHoverHandler(QObject *parent) : BaseHoverHandler::BaseHoverHandler(QObject *parent) : QObject(parent)
QObject(parent), m_matchingHelpCandidate(-1)
{ {
// Listen for editor opened events in order to connect to tooltip/helpid requests // Listen for editor opened events in order to connect to tooltip/helpid requests
connect(ICore::instance()->editorManager(), SIGNAL(editorOpened(Core::IEditor *)), connect(ICore::instance()->editorManager(), SIGNAL(editorOpened(Core::IEditor *)),
@@ -102,13 +101,13 @@ void BaseHoverHandler::updateContextHelpId(TextEditor::ITextEditor *editor, int
// If the tooltip is visible and there is a help match, this match is used to update // If the tooltip is visible and there is a help match, this match is used to update
// the help id. Otherwise, let the identification process happen. // the help id. Otherwise, let the identification process happen.
if (!ToolTip::instance()->isVisible() || m_matchingHelpCandidate == -1) if (!ToolTip::instance()->isVisible() || !lastHelpItemIdentified().isValid())
process(editor, pos); process(editor, pos);
if (m_matchingHelpCandidate != -1) if (lastHelpItemIdentified().isValid())
editor->setContextHelpId(m_helpCandidates.at(m_matchingHelpCandidate).m_helpId); editor->setContextHelpId(lastHelpItemIdentified().helpId());
else else
editor->setContextHelpId(QString()); editor->setContextHelpId(QString()); // Make sure it's an empty string.
} }
void BaseHoverHandler::setToolTip(const QString &tooltip) void BaseHoverHandler::setToolTip(const QString &tooltip)
@@ -127,126 +126,51 @@ void BaseHoverHandler::addF1ToToolTip()
"</tr></table>")).arg(m_toolTip); "</tr></table>")).arg(m_toolTip);
} }
void BaseHoverHandler::reset() void BaseHoverHandler::setLastHelpItemIdentified(const HelpItem &help)
{ { m_lastHelpItemIdentified = help; }
m_matchingHelpCandidate = -1;
m_helpCandidates.clear();
m_toolTip.clear();
resetExtras(); const HelpItem &BaseHoverHandler::lastHelpItemIdentified() const
{ return m_lastHelpItemIdentified; }
void BaseHoverHandler::clear()
{
m_toolTip.clear();
m_lastHelpItemIdentified = HelpItem();
} }
void BaseHoverHandler::process(ITextEditor *editor, int pos) void BaseHoverHandler::process(ITextEditor *editor, int pos)
{ {
reset(); clear();
identifyMatch(editor, pos); identifyMatch(editor, pos);
evaluateHelpCandidates();
decorateToolTip(editor); decorateToolTip(editor);
} }
void BaseHoverHandler::resetExtras() void BaseHoverHandler::decorateToolTip(ITextEditor *editor)
{}
void BaseHoverHandler::evaluateHelpCandidates()
{ {
for (int i = 0; i < m_helpCandidates.size(); ++i) { BaseTextEditor *baseEditor = baseTextEditor(editor);
if (helpIdExists(m_helpCandidates.at(i).m_helpId)) { if (!baseEditor)
m_matchingHelpCandidate = i;
return; return;
if (lastHelpItemIdentified().isValid()) {
const QString &contents = lastHelpItemIdentified().extractContent(extendToolTips(editor));
if (!contents.isEmpty()) {
appendToolTip(contents);
} else {
QString tip = Qt::escape(toolTip());
tip.prepend(QLatin1String("<nobr>"));
tip.append(QLatin1String("</nobr>"));
setToolTip(tip);
} }
addF1ToToolTip();
} }
} }
void BaseHoverHandler::decorateToolTip(ITextEditor *)
{}
void BaseHoverHandler::operateTooltip(ITextEditor *editor, const QPoint &point) void BaseHoverHandler::operateTooltip(ITextEditor *editor, const QPoint &point)
{ {
if (m_toolTip.isEmpty()) { if (m_toolTip.isEmpty())
TextEditor::ToolTip::instance()->hide(); ToolTip::instance()->hide();
} else {
if (m_matchingHelpCandidate != -1)
addF1ToToolTip();
ToolTip::instance()->show(point, TextContent(m_toolTip), editor->widget());
}
}
bool BaseHoverHandler::helpIdExists(const QString &helpId) const
{
if (!Core::HelpManager::instance()->linksForIdentifier(helpId).isEmpty())
return true;
return false;
}
void BaseHoverHandler::addHelpCandidate(const HelpCandidate &helpCandidate)
{ m_helpCandidates.append(helpCandidate); }
void BaseHoverHandler::setHelpCandidate(const HelpCandidate &helpCandidate, int index)
{ m_helpCandidates[index] = helpCandidate; }
const QList<BaseHoverHandler::HelpCandidate> &BaseHoverHandler::helpCandidates() const
{ return m_helpCandidates; }
const BaseHoverHandler::HelpCandidate &BaseHoverHandler::helpCandidate(int index) const
{ return m_helpCandidates.at(index); }
void BaseHoverHandler::setMatchingHelpCandidate(int index)
{ m_matchingHelpCandidate = index; }
int BaseHoverHandler::matchingHelpCandidate() const
{ return m_matchingHelpCandidate; }
QString BaseHoverHandler::getDocContents(const bool extended)
{
Q_ASSERT(m_matchingHelpCandidate >= 0);
return getDocContents(m_helpCandidates.at(m_matchingHelpCandidate), extended);
}
QString BaseHoverHandler::getDocContents(const HelpCandidate &help, const bool extended)
{
if (extended)
m_htmlDocExtractor.extractExtendedContents(1500, true);
else else
m_htmlDocExtractor.extractFirstParagraphOnly(); ToolTip::instance()->show(point, TextContent(m_toolTip), editor->widget());
QString contents;
QMap<QString, QUrl> helpLinks =
Core::HelpManager::instance()->linksForIdentifier(help.m_helpId);
foreach (const QUrl &url, helpLinks) {
const QByteArray &html = Core::HelpManager::instance()->fileData(url);
switch (help.m_category) {
case HelpCandidate::Brief:
contents = m_htmlDocExtractor.getClassOrNamespaceBrief(html, help.m_docMark);
break;
case HelpCandidate::ClassOrNamespace:
contents = m_htmlDocExtractor.getClassOrNamespaceDescription(html, help.m_docMark);
break;
case HelpCandidate::Function:
contents = m_htmlDocExtractor.getFunctionDescription(html, help.m_docMark);
break;
case HelpCandidate::Enum:
contents = m_htmlDocExtractor.getEnumDescription(html, help.m_docMark);
break;
case HelpCandidate::Typedef:
contents = m_htmlDocExtractor.getTypedefDescription(html, help.m_docMark);
break;
case HelpCandidate::Macro:
contents = m_htmlDocExtractor.getMacroDescription(html, help.m_docMark);
break;
case HelpCandidate::QML:
contents = m_htmlDocExtractor.getQMLItemDescription(html, help.m_docMark);
break;
default:
break;
}
if (!contents.isEmpty())
break;
}
return contents;
} }
BaseTextEditor *BaseHoverHandler::baseTextEditor(ITextEditor *editor) BaseTextEditor *BaseHoverHandler::baseTextEditor(ITextEditor *editor)

View File

@@ -31,12 +31,10 @@
#define BASEHOVERHANDLER_H #define BASEHOVERHANDLER_H
#include "texteditor_global.h" #include "texteditor_global.h"
#include "helpitem.h"
#include <utils/htmldocextractor.h>
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QString> #include <QtCore/QString>
#include <QtCore/QList>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QPoint; class QPoint;
@@ -57,30 +55,6 @@ class TEXTEDITOR_EXPORT BaseHoverHandler : public QObject
public: public:
BaseHoverHandler(QObject *parent = 0); BaseHoverHandler(QObject *parent = 0);
struct HelpCandidate
{
enum Category {
ClassOrNamespace,
Enum,
Typedef,
Macro,
Brief,
Function,
QML,
Unknown
};
HelpCandidate(const QString &helpId, Category category) :
m_helpId(helpId), m_docMark(helpId), m_category(category)
{}
HelpCandidate(const QString &helpId, const QString &docMark, Category category) :
m_helpId(helpId), m_docMark(docMark), m_category(category)
{}
QString m_helpId;
QString m_docMark;
Category m_category;
};
private slots: private slots:
void editorOpened(Core::IEditor *editor); void editorOpened(Core::IEditor *editor);
@@ -90,42 +64,28 @@ public slots:
protected: protected:
void setToolTip(const QString &tooltip); void setToolTip(const QString &tooltip);
const QString &toolTip() const;
void appendToolTip(const QString &extension); void appendToolTip(const QString &extension);
const QString &toolTip() const;
void addF1ToToolTip(); void addF1ToToolTip();
void addHelpCandidate(const HelpCandidate &helpCandidate); void setLastHelpItemIdentified(const HelpItem &help);
void setHelpCandidate(const HelpCandidate &helpCandidate, int index); const HelpItem &lastHelpItemIdentified() const;
const QList<HelpCandidate> &helpCandidates() const;
const HelpCandidate &helpCandidate(int index) const;
void setMatchingHelpCandidate(int index);
int matchingHelpCandidate() const;
bool helpIdExists(const QString &helpId) const;
QString getDocContents(const bool extended);
QString getDocContents(const HelpCandidate &help, const bool extended);
static BaseTextEditor *baseTextEditor(ITextEditor *editor); static BaseTextEditor *baseTextEditor(ITextEditor *editor);
static bool extendToolTips(ITextEditor *editor); static bool extendToolTips(ITextEditor *editor);
private: private:
void reset(); void clear();
void process(ITextEditor *editor, int pos); void process(ITextEditor *editor, int pos);
virtual bool acceptEditor(Core::IEditor *editor) = 0; virtual bool acceptEditor(Core::IEditor *editor) = 0;
virtual void identifyMatch(ITextEditor *editor, int pos) = 0; virtual void identifyMatch(ITextEditor *editor, int pos) = 0;
virtual void resetExtras();
virtual void evaluateHelpCandidates();
virtual void decorateToolTip(ITextEditor *editor); virtual void decorateToolTip(ITextEditor *editor);
virtual void operateTooltip(ITextEditor *editor, const QPoint &point); virtual void operateTooltip(ITextEditor *editor, const QPoint &point);
int m_matchingHelpCandidate;
QList<HelpCandidate> m_helpCandidates;
QString m_toolTip; QString m_toolTip;
Utils::HtmlDocExtractor m_htmlDocExtractor; HelpItem m_lastHelpItemIdentified;
}; };
} // namespace TextEditor } // namespace TextEditor

View File

@@ -0,0 +1,123 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "helpitem.h"
#include <coreplugin/helpmanager.h>
#include <utils/htmldocextractor.h>
#include <QtCore/QUrl>
#include <QtCore/QByteArray>
#include <QtCore/QMap>
using namespace TextEditor;
HelpItem::HelpItem()
{}
HelpItem::HelpItem(const QString &helpId, Category category) :
m_helpId(helpId), m_docMark(helpId), m_category(category)
{}
HelpItem::HelpItem(const QString &helpId, const QString &docMark, Category category) :
m_helpId(helpId), m_docMark(docMark), m_category(category)
{}
HelpItem::~HelpItem()
{}
void HelpItem::setHelpId(const QString &id)
{ m_helpId = id; }
const QString &HelpItem::helpId() const
{ return m_helpId; }
void HelpItem::setDocMark(const QString &mark)
{ m_docMark = mark; }
const QString &HelpItem::docMark() const
{ return m_docMark; }
void HelpItem::setCategory(Category cat)
{ m_category = cat; }
HelpItem::Category HelpItem::category() const
{ return m_category; }
bool HelpItem::isValid() const
{
if (!Core::HelpManager::instance()->linksForIdentifier(m_helpId).isEmpty())
return true;
return false;
}
QString HelpItem::extractContent(bool extended) const
{
Utils::HtmlDocExtractor htmlExtractor;
if (extended)
htmlExtractor.extractExtendedContents(1500, true);
else
htmlExtractor.extractFirstParagraphOnly();
QString contents;
QMap<QString, QUrl> helpLinks = Core::HelpManager::instance()->linksForIdentifier(m_helpId);
foreach (const QUrl &url, helpLinks) {
const QByteArray &html = Core::HelpManager::instance()->fileData(url);
switch (m_category) {
case Brief:
contents = htmlExtractor.getClassOrNamespaceBrief(html, m_docMark);
break;
case ClassOrNamespace:
contents = htmlExtractor.getClassOrNamespaceDescription(html, m_docMark);
break;
case Function:
contents = htmlExtractor.getFunctionDescription(html, m_docMark);
break;
case Enum:
contents = htmlExtractor.getEnumDescription(html, m_docMark);
break;
case Typedef:
contents = htmlExtractor.getTypedefDescription(html, m_docMark);
break;
case Macro:
contents = htmlExtractor.getMacroDescription(html, m_docMark);
break;
case QML:
contents = htmlExtractor.getQMLItemDescription(html, m_docMark);
break;
default:
break;
}
if (!contents.isEmpty())
break;
}
return contents;
}

View File

@@ -0,0 +1,79 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef TEXTEDITORHELPITEM_H
#define TEXTEDITORHELPITEM_H
#include "texteditor_global.h"
#include <QtCore/QString>
namespace TextEditor {
class TEXTEDITOR_EXPORT HelpItem
{
public:
enum Category {
ClassOrNamespace,
Enum,
Typedef,
Macro,
Brief,
Function,
QML,
Unknown
};
HelpItem();
HelpItem(const QString &helpId, Category category);
HelpItem(const QString &helpId, const QString &docMark, Category category);
~HelpItem();
void setHelpId(const QString &id);
const QString &helpId() const;
void setDocMark(const QString &mark);
const QString &docMark() const;
void setCategory(Category cat);
Category category() const;
bool isValid() const;
QString extractContent(bool extended) const;
private:
QString m_helpId;
QString m_docMark;
Category m_category;
};
} // namespace TextEditor
#endif // TEXTEDITORHELPITEM_H

View File

@@ -68,7 +68,8 @@ SOURCES += texteditorplugin.cpp \
tooltip/tips.cpp \ tooltip/tips.cpp \
tooltip/tipcontents.cpp \ tooltip/tipcontents.cpp \
basehoverhandler.cpp \ basehoverhandler.cpp \
tooltip/tipfactory.cpp tooltip/tipfactory.cpp \
helpitem.cpp
HEADERS += texteditorplugin.h \ HEADERS += texteditorplugin.h \
textfilewizard.h \ textfilewizard.h \
@@ -139,7 +140,8 @@ HEADERS += texteditorplugin.h \
tooltip/tipcontents.h \ tooltip/tipcontents.h \
basehoverhandler.h \ basehoverhandler.h \
tooltip/effects.h \ tooltip/effects.h \
tooltip/tipfactory.h tooltip/tipfactory.h \
helpitem.h
FORMS += behaviorsettingspage.ui \ FORMS += behaviorsettingspage.ui \
displaysettingspage.ui \ displaysettingspage.ui \