2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-08-03 15:42:14 +02:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2010-08-03 15:42:14 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-08-03 15:42:14 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-08-03 15:42:14 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-08-03 15:42:14 +02:00
|
|
|
|
|
|
|
|
#include "basehoverhandler.h"
|
|
|
|
|
#include "basetexteditor.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
2013-01-10 15:07:17 +01:00
|
|
|
#include <utils/tooltip/tooltip.h>
|
|
|
|
|
#include <utils/tooltip/tipcontents.h>
|
2010-08-03 15:42:14 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QPoint>
|
2010-08-03 15:42:14 +02:00
|
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
|
|
2013-10-11 13:17:38 +02:00
|
|
|
namespace TextEditor {
|
|
|
|
|
|
2014-07-23 19:10:38 +02:00
|
|
|
static BaseTextEditorWidget *baseTextEditor(BaseTextEditor *editor)
|
2013-10-11 13:17:38 +02:00
|
|
|
{
|
|
|
|
|
if (!editor)
|
|
|
|
|
return 0;
|
|
|
|
|
return qobject_cast<BaseTextEditorWidget *>(editor->widget());
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-17 16:29:57 +01:00
|
|
|
BaseHoverHandler::BaseHoverHandler(QObject *parent) : QObject(parent), m_diagnosticTooltip(false)
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
|
|
|
|
// Listen for editor opened events in order to connect to tooltip/helpid requests
|
2013-08-29 17:17:24 +02:00
|
|
|
connect(Core::EditorManager::instance(), SIGNAL(editorOpened(Core::IEditor*)),
|
2012-03-05 22:30:59 +01:00
|
|
|
this, SLOT(editorOpened(Core::IEditor*)));
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-24 20:17:40 +02:00
|
|
|
BaseHoverHandler::~BaseHoverHandler()
|
|
|
|
|
{}
|
|
|
|
|
|
2010-08-03 15:42:14 +02:00
|
|
|
void BaseHoverHandler::editorOpened(Core::IEditor *editor)
|
|
|
|
|
{
|
|
|
|
|
if (acceptEditor(editor)) {
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditor *textEditor = qobject_cast<BaseTextEditor *>(editor);
|
2010-08-03 15:42:14 +02:00
|
|
|
if (textEditor) {
|
2014-07-23 19:10:38 +02:00
|
|
|
connect(textEditor, SIGNAL(tooltipRequested(TextEditor::BaseTextEditor*,QPoint,int)),
|
|
|
|
|
this, SLOT(showToolTip(TextEditor::BaseTextEditor*,QPoint,int)));
|
2010-08-03 15:42:14 +02:00
|
|
|
|
2014-07-23 19:10:38 +02:00
|
|
|
connect(textEditor, SIGNAL(contextHelpIdRequested(TextEditor::BaseTextEditor*,int)),
|
|
|
|
|
this, SLOT(updateContextHelpId(TextEditor::BaseTextEditor*,int)));
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 19:10:38 +02:00
|
|
|
void BaseHoverHandler::showToolTip(TextEditor::BaseTextEditor *editor, const QPoint &point, int pos)
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget *baseEditor = baseTextEditor(editor);
|
2010-08-03 15:42:14 +02:00
|
|
|
if (!baseEditor)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
editor->setContextHelpId(QString());
|
|
|
|
|
|
|
|
|
|
process(editor, pos);
|
2011-02-21 16:45:07 +01:00
|
|
|
operateTooltip(editor, point);
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-23 19:10:38 +02:00
|
|
|
void BaseHoverHandler::updateContextHelpId(TextEditor::BaseTextEditor *editor, int pos)
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget *baseEditor = baseTextEditor(editor);
|
2010-08-13 16:38:45 +02:00
|
|
|
if (!baseEditor)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-08-03 15:42:14 +02:00
|
|
|
// 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.
|
2013-09-11 17:11:15 +02:00
|
|
|
if (!Utils::ToolTip::isVisible() || !lastHelpItemIdentified().isValid())
|
2010-08-03 15:42:14 +02:00
|
|
|
process(editor, pos);
|
|
|
|
|
|
2010-08-27 12:11:55 +02:00
|
|
|
if (lastHelpItemIdentified().isValid())
|
|
|
|
|
editor->setContextHelpId(lastHelpItemIdentified().helpId());
|
2010-08-03 15:42:14 +02:00
|
|
|
else
|
2010-08-27 12:11:55 +02:00
|
|
|
editor->setContextHelpId(QString()); // Make sure it's an empty string.
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseHoverHandler::setToolTip(const QString &tooltip)
|
2013-10-11 13:17:38 +02:00
|
|
|
{
|
|
|
|
|
m_toolTip = tooltip;
|
|
|
|
|
}
|
2010-08-03 15:42:14 +02:00
|
|
|
|
|
|
|
|
const QString &BaseHoverHandler::toolTip() const
|
2013-10-11 13:17:38 +02:00
|
|
|
{
|
|
|
|
|
return m_toolTip;
|
|
|
|
|
}
|
2010-08-03 15:42:14 +02:00
|
|
|
|
|
|
|
|
void BaseHoverHandler::appendToolTip(const QString &extension)
|
2013-10-11 13:17:38 +02:00
|
|
|
{
|
|
|
|
|
m_toolTip.append(extension);
|
|
|
|
|
}
|
2010-08-03 15:42:14 +02:00
|
|
|
|
|
|
|
|
void BaseHoverHandler::addF1ToToolTip()
|
|
|
|
|
{
|
2014-05-08 22:12:53 +02:00
|
|
|
m_toolTip = QString::fromLatin1("<table><tr><td valign=middle>%1</td><td> "
|
|
|
|
|
"<img src=\":/texteditor/images/f1.png\"></td>"
|
|
|
|
|
"</tr></table>").arg(m_toolTip);
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
2011-01-17 16:29:57 +01:00
|
|
|
void BaseHoverHandler::setIsDiagnosticTooltip(bool isDiagnosticTooltip)
|
|
|
|
|
{
|
|
|
|
|
m_diagnosticTooltip = isDiagnosticTooltip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BaseHoverHandler::isDiagnosticTooltip() const
|
|
|
|
|
{
|
|
|
|
|
return m_diagnosticTooltip;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-27 12:11:55 +02:00
|
|
|
void BaseHoverHandler::setLastHelpItemIdentified(const HelpItem &help)
|
2013-10-11 13:17:38 +02:00
|
|
|
{
|
|
|
|
|
m_lastHelpItemIdentified = help;
|
|
|
|
|
}
|
2010-08-27 12:11:55 +02:00
|
|
|
|
|
|
|
|
const HelpItem &BaseHoverHandler::lastHelpItemIdentified() const
|
2013-10-11 13:17:38 +02:00
|
|
|
{
|
|
|
|
|
return m_lastHelpItemIdentified;
|
|
|
|
|
}
|
2010-08-27 12:11:55 +02:00
|
|
|
|
|
|
|
|
void BaseHoverHandler::clear()
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
2011-01-17 16:29:57 +01:00
|
|
|
m_diagnosticTooltip = false;
|
2010-08-03 15:42:14 +02:00
|
|
|
m_toolTip.clear();
|
2010-08-27 12:11:55 +02:00
|
|
|
m_lastHelpItemIdentified = HelpItem();
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-23 19:10:38 +02:00
|
|
|
void BaseHoverHandler::process(BaseTextEditor *editor, int pos)
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
2010-08-27 12:11:55 +02:00
|
|
|
clear();
|
2010-08-03 15:42:14 +02:00
|
|
|
identifyMatch(editor, pos);
|
2010-09-01 12:08:38 +02:00
|
|
|
decorateToolTip();
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-01 12:08:38 +02:00
|
|
|
void BaseHoverHandler::decorateToolTip()
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
2010-09-01 12:08:38 +02:00
|
|
|
if (Qt::mightBeRichText(toolTip()))
|
2014-08-28 17:33:47 +02:00
|
|
|
setToolTip(toolTip().toHtmlEscaped());
|
2010-08-27 12:11:55 +02:00
|
|
|
|
2011-01-17 16:29:57 +01:00
|
|
|
if (!isDiagnosticTooltip() && lastHelpItemIdentified().isValid()) {
|
2010-08-31 13:40:13 +02:00
|
|
|
const QString &contents = lastHelpItemIdentified().extractContent(false);
|
2010-09-23 10:29:13 +02:00
|
|
|
if (!contents.isEmpty()) {
|
2014-08-28 17:33:47 +02:00
|
|
|
setToolTip(toolTip().toHtmlEscaped());
|
2010-08-27 12:11:55 +02:00
|
|
|
appendToolTip(contents);
|
2011-11-10 11:55:05 +01:00
|
|
|
addF1ToToolTip();
|
2010-09-23 10:29:13 +02:00
|
|
|
}
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 19:10:38 +02:00
|
|
|
void BaseHoverHandler::operateTooltip(BaseTextEditor *editor, const QPoint &point)
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
2010-08-27 12:11:55 +02:00
|
|
|
if (m_toolTip.isEmpty())
|
2013-09-11 17:11:15 +02:00
|
|
|
Utils::ToolTip::hide();
|
2010-08-03 15:42:14 +02:00
|
|
|
else
|
2013-09-11 17:11:15 +02:00
|
|
|
Utils::ToolTip::show(point, Utils::TextContent(m_toolTip), editor->widget());
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-11 13:17:38 +02:00
|
|
|
} // namespace TextEditor
|