Newer
Older
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** 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
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "basehoverhandler.h"
#include "itexteditor.h"
#include "basetexteditor.h"
#include "displaysettings.h"
#include "tooltip.h"
#include "tipcontents.h"
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/helpmanager.h>
#include <coreplugin/uniqueidmanager.h>
#include <debugger/debuggerconstants.h>
#include <QtCore/QPoint>
using namespace TextEditor;
using namespace Core;

Leandro Melo
committed
BaseHoverHandler::BaseHoverHandler(QObject *parent) : QObject(parent)
{
// Listen for editor opened events in order to connect to tooltip/helpid requests
connect(ICore::instance()->editorManager(), SIGNAL(editorOpened(Core::IEditor *)),
this, SLOT(editorOpened(Core::IEditor *)));
}
BaseHoverHandler::~BaseHoverHandler()
{}
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
89
90
91
92
93
94
95
96
97
98
99
100
void BaseHoverHandler::editorOpened(Core::IEditor *editor)
{
if (acceptEditor(editor)) {
BaseTextEditorEditable *textEditor = qobject_cast<BaseTextEditorEditable *>(editor);
if (textEditor) {
connect(textEditor, SIGNAL(tooltipRequested(TextEditor::ITextEditor*, QPoint, int)),
this, SLOT(showToolTip(TextEditor::ITextEditor*, QPoint, int)));
connect(textEditor, SIGNAL(contextHelpIdRequested(TextEditor::ITextEditor*, int)),
this, SLOT(updateContextHelpId(TextEditor::ITextEditor*, int)));
}
}
}
void BaseHoverHandler::showToolTip(TextEditor::ITextEditor *editor, const QPoint &point, int pos)
{
BaseTextEditor *baseEditor = baseTextEditor(editor);
if (!baseEditor)
return;
editor->setContextHelpId(QString());
ICore *core = ICore::instance();
const int dbgContext =
core->uniqueIDManager()->uniqueIdentifier(Debugger::Constants::C_DEBUGMODE);
if (core->hasContext(dbgContext))
return;
process(editor, pos);
const QPoint &actualPoint = point - QPoint(0,
#ifdef Q_WS_WIN
24
#else
16
#endif
);
operateTooltip(editor, actualPoint);
}
void BaseHoverHandler::updateContextHelpId(TextEditor::ITextEditor *editor, int pos)
{

Leandro Melo
committed
BaseTextEditor *baseEditor = baseTextEditor(editor);
if (!baseEditor)
return;
// 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.

Leandro Melo
committed
if (!ToolTip::instance()->isVisible() || !lastHelpItemIdentified().isValid())

Leandro Melo
committed
if (lastHelpItemIdentified().isValid())
editor->setContextHelpId(lastHelpItemIdentified().helpId());

Leandro Melo
committed
editor->setContextHelpId(QString()); // Make sure it's an empty string.
}
void BaseHoverHandler::setToolTip(const QString &tooltip)
{ m_toolTip = tooltip; }
const QString &BaseHoverHandler::toolTip() const
{ return m_toolTip; }
void BaseHoverHandler::appendToolTip(const QString &extension)
{ m_toolTip.append(extension); }
void BaseHoverHandler::addF1ToToolTip()
{
m_toolTip = QString(QLatin1String("<table><tr><td valign=middle>%1</td><td> "
"<img src=\":/cppeditor/images/f1.png\"></td>"
"</tr></table>")).arg(m_toolTip);
}

Leandro Melo
committed
void BaseHoverHandler::setLastHelpItemIdentified(const HelpItem &help)
{ m_lastHelpItemIdentified = help; }
const HelpItem &BaseHoverHandler::lastHelpItemIdentified() const
{ return m_lastHelpItemIdentified; }
void BaseHoverHandler::clear()

Leandro Melo
committed
m_lastHelpItemIdentified = HelpItem();
}
void BaseHoverHandler::process(ITextEditor *editor, int pos)
{

Leandro Melo
committed
clear();
decorateToolTip();
void BaseHoverHandler::decorateToolTip()
if (Qt::mightBeRichText(toolTip()))
setToolTip(Qt::escape(toolTip()));

Leandro Melo
committed
if (lastHelpItemIdentified().isValid()) {
const QString &contents = lastHelpItemIdentified().extractContent(false);
if (!contents.isEmpty()) {
setToolTip(Qt::escape(toolTip()));

Leandro Melo
committed
appendToolTip(contents);
}

Leandro Melo
committed
addF1ToToolTip();
}
}
void BaseHoverHandler::operateTooltip(ITextEditor *editor, const QPoint &point)
{

Leandro Melo
committed
if (m_toolTip.isEmpty())
ToolTip::instance()->hide();

Leandro Melo
committed
ToolTip::instance()->show(point, TextContent(m_toolTip), editor->widget());
}
BaseTextEditor *BaseHoverHandler::baseTextEditor(ITextEditor *editor)
{
if (!editor)
return 0;
return qobject_cast<BaseTextEditor *>(editor->widget());
}