cpphoverhandler.cpp 4.33 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "cpphoverhandler.h"
31
#include "cppeditor.h"
32
#include "cppelementevaluator.h"
con's avatar
con committed
33

Leandro Melo's avatar
Leandro Melo committed
34
#include <coreplugin/editormanager/ieditor.h>
35
#include <coreplugin/editormanager/editormanager.h>
36
#include <coreplugin/helpmanager.h>
37
#include <cpptools/cppmodelmanagerinterface.h>
38
#include <extensionsystem/pluginmanager.h>
con's avatar
con committed
39
#include <texteditor/itexteditor.h>
Roberto Raggi's avatar
Roberto Raggi committed
40
#include <texteditor/basetexteditor.h>
41
#include <texteditor/helpitem.h>
con's avatar
con committed
42
43

#include <QtGui/QTextCursor>
44
#include <QtCore/QUrl>
con's avatar
con committed
45

46
using namespace CppEditor::Internal;
47
using namespace Core;
con's avatar
con committed
48

49
namespace {
50
51
52
53
54
55
56
    QString removeClassNameQualification(const QString &name) {
        const int index = name.lastIndexOf(QLatin1Char(':'));
        if (index == -1)
            return name;
        else
            return name.right(name.length() - index - 1);
    }
57
58
}

59
60
61
62
63
CppHoverHandler::CppHoverHandler(QObject *parent) : BaseHoverHandler(parent)
{}

CppHoverHandler::~CppHoverHandler()
{}
con's avatar
con committed
64

Leandro Melo's avatar
Leandro Melo committed
65
bool CppHoverHandler::acceptEditor(IEditor *editor)
con's avatar
con committed
66
{
67
    CPPEditorEditable *cppEditor = qobject_cast<CPPEditorEditable *>(editor);
Leandro Melo's avatar
Leandro Melo committed
68
69
70
    if (cppEditor)
        return true;
    return false;
con's avatar
con committed
71
72
}

73
void CppHoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
con's avatar
con committed
74
{
75
76
    CPPEditor *cppEditor = qobject_cast<CPPEditor *>(editor->widget());
    if (!cppEditor)
77
        return;
Roberto Raggi's avatar
Roberto Raggi committed
78

79
80
    if (!cppEditor->extraSelectionTooltip(pos).isEmpty()) {
        setToolTip(cppEditor->extraSelectionTooltip(pos));
81
    } else {
82
83
84
85
86
87
88
89
        QTextCursor tc(cppEditor->document());
        tc.setPosition(pos);

        CppElementEvaluator evaluator(cppEditor);
        evaluator.setTextCursor(tc);
        QSharedPointer<CppElement> cppElement = evaluator.identifyCppElement();
        if (!cppElement.isNull()) {
            setToolTip(cppElement->tooltip());
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
            foreach (QString helpId, cppElement->helpIdCandidates()) {
                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->helpCategory()));
                    break;
                }
105
            }
106
107
        }
    }
108
}
109

110
void CppHoverHandler::decorateToolTip()
111
{
112
113
    if (Qt::mightBeRichText(toolTip()))
        setToolTip(Qt::escape(toolTip()));
114
115
116

    const TextEditor::HelpItem &help = lastHelpItemIdentified();
    if (help.isValid()) {
117
        const QString &contents = help.extractContent(false);
118
        if (!contents.isEmpty()) {
119
120
            if (help.category() == TextEditor::HelpItem::ClassOrNamespace) {
                setToolTip(Qt::escape(toolTip()));
Leandro Melo's avatar
Leandro Melo committed
121
                appendToolTip(contents);
122
            } else {
Leandro Melo's avatar
Leandro Melo committed
123
                setToolTip(contents);
124
            }
125
        }
126
        addF1ToToolTip();
127
128
    }
}