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

253 lines
8.4 KiB
C++
Raw Normal View History

2009-05-06 15:45:19 +02:00
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** 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"
#include "qmlhoverhandler.h"
2009-05-06 15:45:19 +02:00
#include <coreplugin/icore.h>
#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>
#include <qmljs/qmljsbind.h>
#include <qmljs/qmljscheck.h>
#include <qmljs/qmljsinterpreter.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>
#include <QtHelp/QHelpEngineCore>
using namespace Core;
using namespace QmlJS;
using namespace QmlJSEditor;
using namespace QmlJSEditor::Internal;
2009-05-06 15:45:19 +02:00
2009-09-30 17:43:21 +02:00
QmlHoverHandler::QmlHoverHandler(QObject *parent)
2009-05-06 15:45:19 +02:00
: QObject(parent)
2009-10-08 13:48:39 +02:00
, m_helpEngineNeedsSetup(false)
2009-05-06 15:45:19 +02:00
{
2009-10-08 13:48:39 +02:00
m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<QmlModelManagerInterface>();
2009-05-06 15:45:19 +02:00
ICore *core = ICore::instance();
2009-10-08 13:48:39 +02:00
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;
2009-05-06 15:45:19 +02:00
// 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 *)));
}
2009-09-30 17:43:21 +02:00
void QmlHoverHandler::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)));
}
2009-09-30 17:43:21 +02:00
void QmlHoverHandler::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_GDBDEBUGGER);
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);
}
}
2009-10-08 13:48:39 +02:00
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;
QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(editor->widget());
if (!edit)
2009-10-08 13:48:39 +02:00
return;
const Snapshot snapshot = m_modelManager->snapshot();
SemanticInfo semanticInfo = edit->semanticInfo();
Document::Ptr qmlDocument = semanticInfo.document;
if (qmlDocument.isNull())
return;
2009-10-08 13:48:39 +02:00
if (m_helpEngineNeedsSetup && m_helpEngine->registeredDocumentations().count() > 0) {
m_helpEngine->setupData();
m_helpEngineNeedsSetup = false;
}
QTextCursor tc(edit->document());
2009-10-08 13:48:39 +02:00
tc.setPosition(pos);
// 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>");
2009-10-08 13:48:39 +02:00
if (m_helpId.isEmpty()) {
// Move to the end of a qualified name
bool stop = false;
while (!stop) {
const QChar ch = editor->characterAt(tc.position());
if (ch.isLetterOrNumber() || ch == QLatin1Char('_')) {
2009-10-08 13:48:39 +02:00
tc.setPosition(tc.position() + 1);
} else {
stop = true;
}
}
// Fetch the expression's code
QmlExpressionUnderCursor expressionUnderCursor;
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;
}
}
2009-10-08 13:48:39 +02:00
Interpreter::Engine interp;
Interpreter::ObjectValue *scope = Bind::scopeChainAt(qmlDocument, snapshot, &interp, declaringMember);
Check check(&interp);
const Interpreter::Value *value = check(expression, scope);
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;
}
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>"
"<td><img src=\":/cppeditor/images/f1.svg\"></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
}
}
QString QmlHoverHandler::prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Engine *interp,
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();
} while (objectValue);
if (! baseClasses->isEmpty())
return baseClasses->first();
}
QString typeId = interp->typeId(value);
if (typeId.isEmpty() || typeId == QLatin1String("undefined"))
typeId = QLatin1String("Unknown");
return typeId;
}