OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cppeditor / cpphoverhandler.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "cpphoverhandler.h"
34 #include "cppeditor.h"
35 #include "cppelementevaluator.h"
36
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>
45
46 #include <QtGui/QTextCursor>
47 #include <QtCore/QUrl>
48
49 using namespace CppEditor::Internal;
50 using namespace Core;
51
52 CppHoverHandler::CppHoverHandler(QObject *parent) : BaseHoverHandler(parent)
53 {}
54
55 CppHoverHandler::~CppHoverHandler()
56 {}
57
58 bool CppHoverHandler::acceptEditor(IEditor *editor)
59 {
60     CPPEditor *cppEditor = qobject_cast<CPPEditor *>(editor);
61     if (cppEditor)
62         return true;
63     return false;
64 }
65
66 void CppHoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
67 {
68     CPPEditorWidget *cppEditor = qobject_cast<CPPEditorWidget *>(editor->widget());
69     if (!cppEditor)
70         return;
71
72     if (!cppEditor->extraSelectionTooltip(pos).isEmpty()) {
73         setToolTip(cppEditor->extraSelectionTooltip(pos));
74     } else {
75         QTextCursor tc(cppEditor->document());
76         tc.setPosition(pos);
77
78         CppElementEvaluator evaluator(cppEditor);
79         evaluator.setTextCursor(tc);
80         evaluator.execute();
81         if (evaluator.hasDiagnosis()) {
82             setToolTip(evaluator.diagnosis());
83             setIsDiagnosticTooltip(true);
84         }
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()));
94                     break;
95                 }
96             }
97         }
98     }
99 }
100
101 void CppHoverHandler::decorateToolTip()
102 {
103     if (Qt::mightBeRichText(toolTip()))
104         setToolTip(Qt::escape(toolTip()));
105
106     if (isDiagnosticTooltip())
107         return;
108
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);
118             else
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.
125             QString prefix;
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());
131         }
132         addF1ToToolTip();
133     }
134 }