qmlhoverhandler.cpp 8.35 KB
Newer Older
1
2
3
4
5
6
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
**
** 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
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27
28
29
**
**************************************************************************/

30
#include "qmljseditor.h"
31
32
#include "qmlexpressionundercursor.h"
#include "qmlhoverhandler.h"
33
34
35
36

#include <coreplugin/icore.h>
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/editormanager/editormanager.h>
37
#include <debugger/debuggerconstants.h>
38
#include <extensionsystem/pluginmanager.h>
39
40
41
#include <qmljs/qmljsbind.h>
#include <qmljs/qmljscheck.h>
#include <qmljs/qmljsinterpreter.h>
42
43
44
#include <texteditor/itexteditor.h>
#include <texteditor/basetexteditor.h>

Erik Verbruggen's avatar
Erik Verbruggen committed
45
#include <QtCore/QDebug>
46
47
48
49
50
51
52
53
54
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QSettings>
#include <QtGui/QToolTip>
#include <QtGui/QTextCursor>
#include <QtGui/QTextBlock>
#include <QtHelp/QHelpEngineCore>

using namespace Core;
55
using namespace QmlJS;
56
57
using namespace QmlJSEditor;
using namespace QmlJSEditor::Internal;
58

59
QmlHoverHandler::QmlHoverHandler(QObject *parent)
60
    : QObject(parent)
61
    , m_helpEngineNeedsSetup(false)
62
{
63
64
    m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<QmlModelManagerInterface>();

65
    ICore *core = ICore::instance();
66
67
68
69
70
71
72
73
74
75
76
77
78
    QFileInfo fi(core->settings()->fileName());
    // FIXME shouldn't the help engine create the directory if it doesn't exist?
    QDir directory(fi.absolutePath()+"/qtcreator");
    if (!directory.exists())
        directory.mkpath(directory.absolutePath());

    m_helpEngine = new QHelpEngineCore(directory.absolutePath()
                                       + QLatin1String("/helpcollection.qhc"), this);
    //m_helpEngine->setAutoSaveFilter(false);
    if (!m_helpEngine->setupData())
        qWarning() << "Could not initialize help engine:" << m_helpEngine->error();
    m_helpEngine->setCurrentFilter(tr("Unfiltered"));
    m_helpEngineNeedsSetup = m_helpEngine->registeredDocumentations().count() == 0;
79
80
81
82
83
84

    // Listen for editor opened events in order to connect to tooltip/helpid requests
    connect(core->editorManager(), SIGNAL(editorOpened(Core::IEditor *)),
            this, SLOT(editorOpened(Core::IEditor *)));
}

85
void QmlHoverHandler::editorOpened(IEditor *editor)
86
{
87
    QmlJSEditorEditable *qmlEditor = qobject_cast<QmlJSEditorEditable *>(editor);
88
    if (!qmlEditor)
89
90
        return;

91
    connect(qmlEditor, SIGNAL(tooltipRequested(TextEditor::ITextEditor*, QPoint, int)),
92
93
            this, SLOT(showToolTip(TextEditor::ITextEditor*, QPoint, int)));

94
    connect(qmlEditor, SIGNAL(contextHelpIdRequested(TextEditor::ITextEditor*, int)),
95
96
97
            this, SLOT(updateContextHelpId(TextEditor::ITextEditor*, int)));
}

98
void QmlHoverHandler::showToolTip(TextEditor::ITextEditor *editor, const QPoint &point, int pos)
99
100
101
102
103
104
105
106
107
108
{
    if (! editor)
        return;

    ICore *core = ICore::instance();
    const int dbgcontext = core->uniqueIDManager()->uniqueIdentifier(Debugger::Constants::C_GDBDEBUGGER);

    if (core->hasContext(dbgcontext))
        return;

109
    updateHelpIdAndTooltip(editor, pos);
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

    if (m_toolTip.isEmpty())
        QToolTip::hideText();
    else {
        const QPoint pnt = point - QPoint(0,
#ifdef Q_WS_WIN
        24
#else
        16
#endif
        );

        QToolTip::showText(pnt, m_toolTip);
    }
}

126
127
128
129
130
131
132
133
134
135
136
137
138
void QmlHoverHandler::updateContextHelpId(TextEditor::ITextEditor *editor, int pos)
{
    updateHelpIdAndTooltip(editor, pos);
}

void QmlHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, int pos)
{
    m_helpId.clear();
    m_toolTip.clear();

    if (!m_modelManager)
        return;

139
140
    QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(editor->widget());
    if (!edit)
141
142
        return;

143
    const Snapshot snapshot = m_modelManager->snapshot();
144
145
146
147
    SemanticInfo semanticInfo = edit->semanticInfo();
    Document::Ptr qmlDocument = semanticInfo.document;
    if (qmlDocument.isNull())
        return;
148

149
150
151
152
153
    if (m_helpEngineNeedsSetup && m_helpEngine->registeredDocumentations().count() > 0) {
        m_helpEngine->setupData();
        m_helpEngineNeedsSetup = false;
    }

154
    QTextCursor tc(edit->document());
155
156
157
158
159
    tc.setPosition(pos);

    // We only want to show F1 if the tooltip matches the help id
    bool showF1 = true;

160
    foreach (const QTextEdit::ExtraSelection &sel, edit->extraSelections(TextEditor::BaseTextEditor::CodeWarningsSelection)) {
161
        if (pos >= sel.cursor.selectionStart() && pos <= sel.cursor.selectionEnd()) {
162
            showF1 = false;
163
            m_toolTip = sel.format.toolTip();
164
165
166
        }
    }

167
    QString symbolName = QLatin1String("<unknown>");
168
169
170
171
172
    if (m_helpId.isEmpty()) {
        // Move to the end of a qualified name
        bool stop = false;
        while (!stop) {
            const QChar ch = editor->characterAt(tc.position());
173
            if (ch.isLetterOrNumber() || ch == QLatin1Char('_')) {
174
175
176
177
178
179
180
181
                tc.setPosition(tc.position() + 1);
            } else {
                stop = true;
            }
        }

        // Fetch the expression's code
        QmlExpressionUnderCursor expressionUnderCursor;
182
183
184
185
186
187
188
189
190
        QmlJS::AST::ExpressionNode *expression = expressionUnderCursor(tc);

        AST::UiObjectMember *declaringMember = 0;

        foreach (const Range &range, semanticInfo.ranges) {
            if (pos >= range.begin.position() && pos <= range.end.position()) {
                declaringMember = range.ast;
            }
        }
191

192
        Interpreter::Engine interp;
193
        Interpreter::ObjectValue *scope = Bind::scopeChainAt(qmlDocument, snapshot, &interp, declaringMember);
194
195
        Check check(&interp);
        const Interpreter::Value *value = check(expression, scope);
196
197
198
199
200
201
202
203
204
205
        QStringList baseClasses;
        m_toolTip = prettyPrint(value, &interp, &baseClasses);
        foreach (const QString &baseClass, baseClasses) {
            QString helpId = QLatin1String("QML.");
            helpId += baseClass;

            if (! m_helpEngine->linksForIdentifier(helpId).isEmpty()) {
                m_helpId = helpId;
                break;
            }
206
207
208
209
210
211
        }
    }

    if (!m_toolTip.isEmpty())
        m_toolTip = Qt::escape(m_toolTip);

212
    if (!m_helpId.isEmpty()) {
213
        if (showF1) {
214
215
            m_toolTip = QString::fromUtf8("<table><tr><td valign=middle><nobr>%1</td>"
                                            "<td><img src=\":/cppeditor/images/f1.svg\"></td></tr></table>")
216
217
218
219
                    .arg(m_toolTip);
        }
        editor->setContextHelpId(m_helpId);
    } else if (!m_toolTip.isEmpty()) {
220
        m_toolTip = QString::fromUtf8("<nobr>%1").arg(m_toolTip);
221
    } else if (!m_helpId.isEmpty()) {
222
        m_toolTip = QString::fromUtf8("<nobr>No help available for \"%1\"").arg(symbolName);
223
224
    }
}
225

226
227
QString QmlHoverHandler::prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Engine *interp,
                                     QStringList *baseClasses) const
228
229
230
231
232
{
    if (!value)
        return QString();

    if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
233
234
235
236
237
        do {
            const QString className = objectValue->className();

            if (! className.isEmpty())
                baseClasses->append(className);
238
239

            objectValue = objectValue->prototype();
240
        } while (objectValue);
241

242
243
        if (! baseClasses->isEmpty())
            return baseClasses->first();
244
245
    }

246
247
    QString typeId = interp->typeId(value);

248
249
    if (typeId == QLatin1String("undefined"))
        typeId.clear();
250
251

    return typeId;
252
}