basehoverhandler.cpp 4.78 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Leandro Melo's avatar
Leandro Melo committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
Leandro Melo's avatar
Leandro Melo committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Leandro Melo's avatar
Leandro Melo committed
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
Leandro Melo's avatar
Leandro Melo committed
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Leandro Melo's avatar
Leandro Melo committed
30
31

#include "basehoverhandler.h"
32
#include "texteditor.h"
Leandro Melo's avatar
Leandro Melo committed
33
34

#include <coreplugin/icore.h>
35
#include <utils/tooltip/tooltip.h>
Leandro Melo's avatar
Leandro Melo committed
36

37
#include <QPoint>
Leandro Melo's avatar
Leandro Melo committed
38
39
40

using namespace Core;

hjk's avatar
hjk committed
41
42
namespace TextEditor {

43
BaseHoverHandler::BaseHoverHandler() : m_diagnosticTooltip(false), m_priority(-1)
Leandro Melo's avatar
Leandro Melo committed
44
45
46
{
}

47
48
49
BaseHoverHandler::~BaseHoverHandler()
{}

50
void BaseHoverHandler::showToolTip(TextEditorWidget *widget, const QPoint &point, int pos)
Leandro Melo's avatar
Leandro Melo committed
51
{
52
    widget->setContextHelpId(QString());
Leandro Melo's avatar
Leandro Melo committed
53

54
55
    process(widget, pos);
    operateTooltip(widget, point);
Leandro Melo's avatar
Leandro Melo committed
56
57
}

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
int BaseHoverHandler::checkToolTip(TextEditorWidget *widget, int pos)
{
    widget->setContextHelpId(QString());

    process(widget, pos);

    return priority();
}

int BaseHoverHandler::priority() const
{
    if (m_priority >= 0)
        return m_priority;

    if (isDiagnosticTooltip())
        return Priority_Diagnostic;

    if (lastHelpItemIdentified().isValid())
        return Priority_Help;

    if (!toolTip().isEmpty())
        return Priority_Tooltip;

    return Priority_None;
}

void BaseHoverHandler::setPriority(int priority)
{
    m_priority = priority;
}

89
QString BaseHoverHandler::contextHelpId(TextEditorWidget *widget, int pos)
Leandro Melo's avatar
Leandro Melo committed
90
91
92
{
    // 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.
hjk's avatar
hjk committed
93
    if (!Utils::ToolTip::isVisible() || !lastHelpItemIdentified().isValid())
94
        process(widget, pos);
Leandro Melo's avatar
Leandro Melo committed
95

96
    if (lastHelpItemIdentified().isValid())
97
98
        return lastHelpItemIdentified().helpId();
    return QString();
Leandro Melo's avatar
Leandro Melo committed
99
100
101
}

void BaseHoverHandler::setToolTip(const QString &tooltip)
hjk's avatar
hjk committed
102
103
104
{
    m_toolTip = tooltip;
}
Leandro Melo's avatar
Leandro Melo committed
105
106

const QString &BaseHoverHandler::toolTip() const
hjk's avatar
hjk committed
107
108
109
{
    return m_toolTip;
}
Leandro Melo's avatar
Leandro Melo committed
110
111

void BaseHoverHandler::appendToolTip(const QString &extension)
hjk's avatar
hjk committed
112
113
114
{
    m_toolTip.append(extension);
}
Leandro Melo's avatar
Leandro Melo committed
115

116
117
118
119
120
121
122
123
124
125
void BaseHoverHandler::setIsDiagnosticTooltip(bool isDiagnosticTooltip)
{
    m_diagnosticTooltip = isDiagnosticTooltip;
}

bool BaseHoverHandler::isDiagnosticTooltip() const
{
    return m_diagnosticTooltip;
}

126
void BaseHoverHandler::setLastHelpItemIdentified(const HelpItem &help)
hjk's avatar
hjk committed
127
128
129
{
    m_lastHelpItemIdentified = help;
}
130
131

const HelpItem &BaseHoverHandler::lastHelpItemIdentified() const
hjk's avatar
hjk committed
132
133
134
{
    return m_lastHelpItemIdentified;
}
135
136

void BaseHoverHandler::clear()
Leandro Melo's avatar
Leandro Melo committed
137
{
138
    m_diagnosticTooltip = false;
Leandro Melo's avatar
Leandro Melo committed
139
    m_toolTip.clear();
140
    m_priority = -1;
141
    m_lastHelpItemIdentified = HelpItem();
Leandro Melo's avatar
Leandro Melo committed
142
143
}

144
void BaseHoverHandler::process(TextEditorWidget *widget, int pos)
Leandro Melo's avatar
Leandro Melo committed
145
{
146
    clear();
147
    identifyMatch(widget, pos);
148
    decorateToolTip();
Leandro Melo's avatar
Leandro Melo committed
149
150
}

151
void BaseHoverHandler::decorateToolTip()
Leandro Melo's avatar
Leandro Melo committed
152
{
153
    if (Qt::mightBeRichText(toolTip()))
154
        setToolTip(toolTip().toHtmlEscaped());
155

156
    if (!isDiagnosticTooltip() && lastHelpItemIdentified().isValid()) {
157
        const QString &contents = lastHelpItemIdentified().extractContent(false);
158
        if (!contents.isEmpty()) {
159
            setToolTip(toolTip().toHtmlEscaped());
160
            appendToolTip(contents);
161
        }
Leandro Melo's avatar
Leandro Melo committed
162
163
164
    }
}

165
void BaseHoverHandler::operateTooltip(TextEditorWidget *editorWidget, const QPoint &point)
Leandro Melo's avatar
Leandro Melo committed
166
{
167
    if (m_toolTip.isEmpty())
hjk's avatar
hjk committed
168
        Utils::ToolTip::hide();
Leandro Melo's avatar
Leandro Melo committed
169
    else
170
171
172
        Utils::ToolTip::show(point, m_toolTip, editorWidget, m_lastHelpItemIdentified.isValid()
                             ? m_lastHelpItemIdentified.helpId()
                             : QString());
Leandro Melo's avatar
Leandro Melo committed
173
174
}

hjk's avatar
hjk committed
175
} // namespace TextEditor