Files
qt-creator/src/plugins/qmljseditor/qmljshoverhandler.cpp

215 lines
7.1 KiB
C++
Raw Normal View History

2009-05-06 15:45:19 +02:00
/**************************************************************************
**
** This file is part of Qt Creator
**
2010-03-05 11:25:49 +01:00
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
2009-05-06 15:45:19 +02:00
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2009-05-06 15:45:19 +02:00
**
** 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
2009-08-14 09:30:56 +02:00
** contact the sales department at http://qt.nokia.com/contact.
2009-05-06 15:45:19 +02:00
**
**************************************************************************/
#include "qmljseditor.h"
2009-10-08 13:48:39 +02:00
#include "qmlexpressionundercursor.h"
2010-02-15 13:41:51 +01:00
#include "qmljshoverhandler.h"
2009-05-06 15:45:19 +02:00
#include <coreplugin/icore.h>
#include <coreplugin/helpmanager.h>
2009-05-06 15:45:19 +02:00
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <debugger/debuggerconstants.h>
2009-05-06 15:45:19 +02:00
#include <extensionsystem/pluginmanager.h>
2010-07-08 14:38:47 +02:00
#include <qmljs/qmljslookupcontext.h>
#include <qmljs/qmljsinterpreter.h>
#include <qmljs/parser/qmljsast_p.h>
2009-05-06 15:45:19 +02:00
#include <texteditor/itexteditor.h>
#include <texteditor/basetexteditor.h>
2009-10-08 13:51:00 +02:00
#include <QtCore/QDebug>
2009-05-06 15:45:19 +02:00
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QSettings>
#include <QtGui/QToolTip>
#include <QtGui/QTextCursor>
#include <QtGui/QTextBlock>
using namespace Core;
using namespace QmlJS;
using namespace QmlJSEditor;
using namespace QmlJSEditor::Internal;
2009-05-06 15:45:19 +02:00
2010-02-15 13:41:51 +01:00
HoverHandler::HoverHandler(QObject *parent)
2009-05-06 15:45:19 +02:00
: QObject(parent)
{
2010-06-09 15:56:03 +02:00
m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<QmlJS::ModelManagerInterface>();
2009-10-08 13:48:39 +02:00
2009-05-06 15:45:19 +02:00
// Listen for editor opened events in order to connect to tooltip/helpid requests
connect(ICore::instance()->editorManager(), SIGNAL(editorOpened(Core::IEditor *)),
2009-05-06 15:45:19 +02:00
this, SLOT(editorOpened(Core::IEditor *)));
}
2010-02-15 13:41:51 +01:00
void HoverHandler::editorOpened(IEditor *editor)
2009-05-06 15:45:19 +02:00
{
QmlJSEditorEditable *qmlEditor = qobject_cast<QmlJSEditorEditable *>(editor);
2009-09-30 17:43:21 +02:00
if (!qmlEditor)
2009-05-06 15:45:19 +02:00
return;
2009-09-30 17:43:21 +02:00
connect(qmlEditor, SIGNAL(tooltipRequested(TextEditor::ITextEditor*, QPoint, int)),
2009-05-06 15:45:19 +02:00
this, SLOT(showToolTip(TextEditor::ITextEditor*, QPoint, int)));
2009-09-30 17:43:21 +02:00
connect(qmlEditor, SIGNAL(contextHelpIdRequested(TextEditor::ITextEditor*, int)),
2009-05-06 15:45:19 +02:00
this, SLOT(updateContextHelpId(TextEditor::ITextEditor*, int)));
}
2010-02-15 13:41:51 +01:00
void HoverHandler::showToolTip(TextEditor::ITextEditor *editor, const QPoint &point, int pos)
2009-05-06 15:45:19 +02:00
{
if (! editor)
return;
ICore *core = ICore::instance();
const int dbgcontext = core->uniqueIDManager()->uniqueIdentifier(Debugger::Constants::C_DEBUGMODE);
2009-05-06 15:45:19 +02:00
if (core->hasContext(dbgcontext))
return;
2009-10-08 13:48:39 +02:00
updateHelpIdAndTooltip(editor, pos);
2009-05-06 15:45:19 +02:00
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);
}
}
2010-02-15 13:41:51 +01:00
void HoverHandler::updateContextHelpId(TextEditor::ITextEditor *editor, int pos)
2009-10-08 13:48:39 +02:00
{
updateHelpIdAndTooltip(editor, pos);
}
2010-02-15 13:41:51 +01:00
void HoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, int pos)
2009-10-08 13:48:39 +02:00
{
m_helpId.clear();
m_toolTip.clear();
if (!m_modelManager)
return;
QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(editor->widget());
if (!edit)
2009-10-08 13:48:39 +02:00
return;
const SemanticInfo semanticInfo = edit->semanticInfo();
if (semanticInfo.revision() != edit->editorRevision())
return;
2009-10-08 13:48:39 +02:00
const Snapshot snapshot = semanticInfo.snapshot;
const Document::Ptr qmlDocument = semanticInfo.document;
2009-10-08 13:48:39 +02:00
// We only want to show F1 if the tooltip matches the help id
bool showF1 = true;
foreach (const QTextEdit::ExtraSelection &sel, edit->extraSelections(TextEditor::BaseTextEditor::CodeWarningsSelection)) {
if (pos >= sel.cursor.selectionStart() && pos <= sel.cursor.selectionEnd()) {
2009-10-08 13:48:39 +02:00
showF1 = false;
m_toolTip = sel.format.toolTip();
2009-10-08 13:48:39 +02:00
}
}
QString symbolName = QLatin1String("<unknown>");
if (m_helpId.isEmpty() && m_toolTip.isEmpty()) {
AST::Node *node = semanticInfo.nodeUnderCursor(pos);
if (node && !(AST::cast<AST::StringLiteral *>(node) != 0 || AST::cast<AST::NumericLiteral *>(node) != 0)) {
QList<AST::Node *> astPath = semanticInfo.astPath(pos);
2010-07-08 14:38:47 +02:00
LookupContext::Ptr lookupContext = LookupContext::create(qmlDocument, snapshot, astPath);
const Interpreter::Value *value = lookupContext->evaluate(node);
QStringList baseClasses;
2010-07-08 14:38:47 +02:00
m_toolTip = prettyPrint(value, lookupContext->context(), &baseClasses);
foreach (const QString &baseClass, baseClasses) {
QString helpId = QLatin1String("QML.");
helpId += baseClass;
2009-10-08 13:48:39 +02:00
if (!Core::HelpManager::instance()->linksForIdentifier(helpId).isEmpty()) {
m_helpId = helpId;
break;
}
}
2009-10-08 13:48:39 +02:00
}
}
if (!m_toolTip.isEmpty())
m_toolTip = Qt::escape(m_toolTip);
if (!m_helpId.isEmpty()) {
2009-10-08 13:48:39 +02:00
if (showF1) {
m_toolTip = QString::fromUtf8("<table><tr><td valign=middle><nobr>%1</td>"
2010-03-05 14:30:08 +01:00
"<td><img src=\":/cppeditor/images/f1.png\"></td></tr></table>")
2009-10-08 13:48:39 +02:00
.arg(m_toolTip);
}
editor->setContextHelpId(m_helpId);
} else if (!m_toolTip.isEmpty()) {
m_toolTip = QString::fromUtf8("<nobr>%1").arg(m_toolTip);
2009-10-08 13:48:39 +02:00
} else if (!m_helpId.isEmpty()) {
m_toolTip = QString::fromUtf8("<nobr>No help available for \"%1\"").arg(symbolName);
2009-10-08 13:48:39 +02:00
}
}
2010-02-15 13:41:51 +01:00
QString HoverHandler::prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Context *context,
2010-07-08 14:38:47 +02:00
QStringList *baseClasses) const
{
if (! value)
return QString();
if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
do {
const QString className = objectValue->className();
if (! className.isEmpty())
baseClasses->append(className);
objectValue = objectValue->prototype(context);
} while (objectValue);
if (! baseClasses->isEmpty())
return baseClasses->first();
} else if (const Interpreter::QmlEnumValue *enumValue = dynamic_cast<const Interpreter::QmlEnumValue *>(value)) {
return enumValue->name();
}
QString typeId = context->engine()->typeId(value);
if (typeId == QLatin1String("undefined"))
typeId.clear();
return typeId;
}