1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "cpphoverhandler.h"
34 #include "cppeditor.h"
35 #include "cppelementevaluator.h"
37 #include <coreplugin/editormanager/ieditor.h>
38 #include <coreplugin/editormanager/editormanager.h>
39 #include <coreplugin/helpmanager.h>
40 #include <cplusplus/ModelManagerInterface.h>
41 #include <extensionsystem/pluginmanager.h>
42 #include <texteditor/itexteditor.h>
43 #include <texteditor/basetexteditor.h>
44 #include <texteditor/helpitem.h>
46 #include <QtGui/QTextCursor>
47 #include <QtCore/QUrl>
49 using namespace CppEditor::Internal;
52 CppHoverHandler::CppHoverHandler(QObject *parent) : BaseHoverHandler(parent)
55 CppHoverHandler::~CppHoverHandler()
58 bool CppHoverHandler::acceptEditor(IEditor *editor)
60 CPPEditor *cppEditor = qobject_cast<CPPEditor *>(editor);
66 void CppHoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
68 CPPEditorWidget *cppEditor = qobject_cast<CPPEditorWidget *>(editor->widget());
72 if (!cppEditor->extraSelectionTooltip(pos).isEmpty()) {
73 setToolTip(cppEditor->extraSelectionTooltip(pos));
75 QTextCursor tc(cppEditor->document());
78 CppElementEvaluator evaluator(cppEditor);
79 evaluator.setTextCursor(tc);
81 if (evaluator.hasDiagnosis()) {
82 setToolTip(evaluator.diagnosis());
83 setIsDiagnosticTooltip(true);
85 if (evaluator.identifiedCppElement()) {
86 const QSharedPointer<CppElement> &cppElement = evaluator.cppElement();
87 if (!isDiagnosticTooltip())
88 setToolTip(cppElement->tooltip());
89 foreach (const QString &helpId, cppElement->helpIdCandidates()) {
90 if (!Core::HelpManager::instance()->linksForIdentifier(helpId).isEmpty()) {
91 setLastHelpItemIdentified(TextEditor::HelpItem(helpId,
92 cppElement->helpMark(),
93 cppElement->helpCategory()));
101 void CppHoverHandler::decorateToolTip()
103 if (Qt::mightBeRichText(toolTip()))
104 setToolTip(Qt::escape(toolTip()));
106 if (isDiagnosticTooltip())
109 const TextEditor::HelpItem &help = lastHelpItemIdentified();
110 if (help.isValid()) {
111 // If Qt is built with a namespace, we still show the tip without it, as
112 // it is in the docs and for consistency with the doc extraction mechanism.
113 const TextEditor::HelpItem::Category category = help.category();
114 const QString &contents = help.extractContent(false);
115 if (!contents.isEmpty()) {
116 if (category == TextEditor::HelpItem::ClassOrNamespace)
117 setToolTip(help.helpId() + contents);
119 setToolTip(contents);
120 } else if (category == TextEditor::HelpItem::Typedef ||
121 category == TextEditor::HelpItem::Enum ||
122 category == TextEditor::HelpItem::ClassOrNamespace) {
123 // This approach is a bit limited since it cannot be used for functions
124 // because the help id doesn't really help in that case.
126 if (category == TextEditor::HelpItem::Typedef)
127 prefix = QLatin1String("typedef ");
128 else if (category == TextEditor::HelpItem::Enum)
129 prefix = QLatin1String("enum ");
130 setToolTip(prefix + help.helpId());