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

226 lines
7.7 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"
#include "qmllookupcontext.h"
#include "qmlresolveexpression.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/qmljssymbol.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);
}
static QString buildHelpId(Symbol *symbol)
2009-05-06 15:45:19 +02:00
{
2009-10-08 13:48:39 +02:00
if (!symbol)
return QString();
2009-10-09 11:04:42 +02:00
const QString idTemplate(QLatin1String("QML.%1"));
2009-10-08 13:48:39 +02:00
return idTemplate.arg(symbol->name());
2009-05-06 15:45:19 +02:00
}
2009-10-08 13:48:39 +02:00
void QmlHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, int pos)
{
m_helpId.clear();
m_toolTip.clear();
if (!m_modelManager)
return;
QmlJSTextEditor *scriptEditor = qobject_cast<QmlJSTextEditor *>(editor->widget());
2009-10-08 13:48:39 +02:00
if (!scriptEditor)
return;
const Snapshot documents = m_modelManager->snapshot();
const QString fileName = editor->file()->fileName();
Document::Ptr doc = documents.value(fileName);
2009-10-08 13:48:39 +02:00
if (!doc)
return; // nothing to do
QTextCursor tc(scriptEditor->document());
tc.setPosition(pos);
// We only want to show F1 if the tooltip matches the help id
bool showF1 = true;
foreach (const QTextEdit::ExtraSelection &sel, scriptEditor->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('_') || ch == QLatin1Char('.')) {
tc.setPosition(tc.position() + 1);
} else {
stop = true;
}
}
// Fetch the expression's code
QmlExpressionUnderCursor expressionUnderCursor;
expressionUnderCursor(tc, doc);
QmlJS::TypeSystem *typeSystem = ExtensionSystem::PluginManager::instance()->getObject<QmlJS::TypeSystem>();
QmlLookupContext context(expressionUnderCursor.expressionScopes(), doc, m_modelManager->snapshot(), typeSystem);
2009-10-08 13:48:39 +02:00
QmlResolveExpression resolver(context);
Symbol *resolvedSymbol = resolver.typeOf(expressionUnderCursor.expressionNode());
2009-10-08 13:48:39 +02:00
if (resolvedSymbol) {
symbolName = resolvedSymbol->name();
if (resolvedSymbol->isBuildInSymbol())
m_helpId = buildHelpId(resolvedSymbol);
else if (SymbolFromFile *symbolFromFile = resolvedSymbol->asSymbolFromFile())
m_toolTip = symbolFromFile->fileName();
2009-10-08 13:48:39 +02:00
}
}
if (m_helpEngineNeedsSetup && m_helpEngine->registeredDocumentations().count() > 0) {
m_helpEngine->setupData();
m_helpEngineNeedsSetup = false;
}
if (!m_toolTip.isEmpty())
m_toolTip = Qt::escape(m_toolTip);
if (!m_helpId.isEmpty() && !m_helpEngine->linksForIdentifier(m_helpId).isEmpty()) {
if (showF1) {
m_toolTip = QString(QLatin1String("<table><tr><td valign=middle><nobr>%1</td>"
"<td><img src=\":/cppeditor/images/f1.svg\"></td></tr></table>"))
.arg(m_toolTip);
}
editor->setContextHelpId(m_helpId);
} else if (!m_toolTip.isEmpty()) {
m_toolTip = QString(QLatin1String("<nobr>%1")).arg(m_toolTip);
} else if (!m_helpId.isEmpty()) {
m_toolTip = QString(QLatin1String("<nobr>No help available for \"%1\"")).arg(symbolName);
2009-10-08 13:48:39 +02:00
}
}