2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2010-08-03 15:42:14 +02:00
|
|
|
|
|
|
|
|
#include "basehoverhandler.h"
|
2014-09-26 09:14:03 +02:00
|
|
|
#include "texteditor.h"
|
2010-08-03 15:42:14 +02:00
|
|
|
|
2017-06-26 11:15:19 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2013-01-10 15:07:17 +01:00
|
|
|
#include <utils/tooltip/tooltip.h>
|
2010-08-03 15:42:14 +02:00
|
|
|
|
2023-06-03 13:30:22 +02:00
|
|
|
#include <QScopeGuard>
|
2019-10-25 15:16:47 +02:00
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
2013-10-11 13:17:38 +02:00
|
|
|
namespace TextEditor {
|
|
|
|
|
|
2018-11-25 18:52:41 +01:00
|
|
|
BaseHoverHandler::~BaseHoverHandler() = default;
|
2010-09-24 20:17:40 +02:00
|
|
|
|
2019-10-25 15:16:47 +02:00
|
|
|
void BaseHoverHandler::showToolTip(TextEditorWidget *widget, const QPoint &point)
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
2014-09-30 13:08:05 +02:00
|
|
|
operateTooltip(widget, point);
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
2022-12-06 15:28:39 +03:00
|
|
|
bool BaseHoverHandler::lastHelpItemAppliesTo(const TextEditorWidget *widget) const
|
|
|
|
|
{
|
|
|
|
|
return m_lastWidget == widget;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 11:15:19 +02:00
|
|
|
void BaseHoverHandler::checkPriority(TextEditorWidget *widget,
|
|
|
|
|
int pos,
|
|
|
|
|
ReportPriority report)
|
2016-01-04 09:17:59 +11:00
|
|
|
{
|
2019-01-25 15:04:50 +01:00
|
|
|
widget->setContextHelpItem({});
|
2016-01-04 09:17:59 +11:00
|
|
|
|
2017-06-26 11:15:19 +02:00
|
|
|
process(widget, pos, report);
|
|
|
|
|
}
|
2016-01-04 09:17:59 +11:00
|
|
|
|
|
|
|
|
int BaseHoverHandler::priority() const
|
|
|
|
|
{
|
|
|
|
|
if (m_priority >= 0)
|
|
|
|
|
return m_priority;
|
|
|
|
|
|
|
|
|
|
if (lastHelpItemIdentified().isValid())
|
|
|
|
|
return Priority_Help;
|
|
|
|
|
|
|
|
|
|
if (!toolTip().isEmpty())
|
|
|
|
|
return Priority_Tooltip;
|
|
|
|
|
|
|
|
|
|
return Priority_None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseHoverHandler::setPriority(int priority)
|
|
|
|
|
{
|
|
|
|
|
m_priority = priority;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:58:22 +01:00
|
|
|
void BaseHoverHandler::contextHelpId(TextEditorWidget *widget,
|
|
|
|
|
int pos,
|
2019-01-28 11:47:50 +01:00
|
|
|
const Core::IContext::HelpCallback &callback)
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
2019-01-07 09:56:52 +01:00
|
|
|
m_isContextHelpRequest = true;
|
|
|
|
|
|
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.
|
2018-11-23 10:02:29 +01:00
|
|
|
if (!Utils::ToolTip::isVisible() || !lastHelpItemIdentified().isValid()) {
|
|
|
|
|
process(widget, pos, [this, widget = QPointer<TextEditorWidget>(widget), callback](int) {
|
|
|
|
|
if (widget)
|
|
|
|
|
propagateHelpId(widget, callback);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2018-01-18 11:58:22 +01:00
|
|
|
propagateHelpId(widget, callback);
|
2018-11-23 10:02:29 +01:00
|
|
|
}
|
2019-01-07 09:56:52 +01:00
|
|
|
|
|
|
|
|
m_isContextHelpRequest = false;
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-04 17:14:25 +01:00
|
|
|
void BaseHoverHandler::setToolTip(const QString &tooltip, Qt::TextFormat format)
|
2013-10-11 13:17:38 +02:00
|
|
|
{
|
|
|
|
|
m_toolTip = tooltip;
|
2019-11-04 17:14:25 +01:00
|
|
|
m_textFormat = format;
|
2013-10-11 13:17:38 +02:00
|
|
|
}
|
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
|
|
|
|
2019-01-24 11:30:58 +01:00
|
|
|
void BaseHoverHandler::setLastHelpItemIdentified(const Core::HelpItem &help)
|
2013-10-11 13:17:38 +02:00
|
|
|
{
|
|
|
|
|
m_lastHelpItemIdentified = help;
|
|
|
|
|
}
|
2010-08-27 12:11:55 +02:00
|
|
|
|
2019-01-24 11:30:58 +01:00
|
|
|
const Core::HelpItem &BaseHoverHandler::lastHelpItemIdentified() const
|
2013-10-11 13:17:38 +02:00
|
|
|
{
|
|
|
|
|
return m_lastHelpItemIdentified;
|
|
|
|
|
}
|
2010-08-27 12:11:55 +02:00
|
|
|
|
2019-01-07 09:56:52 +01:00
|
|
|
bool BaseHoverHandler::isContextHelpRequest() const
|
|
|
|
|
{
|
|
|
|
|
return m_isContextHelpRequest;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 11:58:22 +01:00
|
|
|
void BaseHoverHandler::propagateHelpId(TextEditorWidget *widget,
|
2019-01-28 11:47:50 +01:00
|
|
|
const Core::IContext::HelpCallback &callback)
|
2018-01-18 11:58:22 +01:00
|
|
|
{
|
2019-01-25 15:04:50 +01:00
|
|
|
const Core::HelpItem contextHelp = lastHelpItemIdentified();
|
|
|
|
|
widget->setContextHelpItem(contextHelp);
|
|
|
|
|
callback(contextHelp);
|
2018-01-18 11:58:22 +01:00
|
|
|
}
|
|
|
|
|
|
2017-06-26 11:15:19 +02:00
|
|
|
void BaseHoverHandler::process(TextEditorWidget *widget, int pos, ReportPriority report)
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
|
|
|
|
m_toolTip.clear();
|
2016-01-04 09:17:59 +11:00
|
|
|
m_priority = -1;
|
2019-01-24 11:30:58 +01:00
|
|
|
m_lastHelpItemIdentified = Core::HelpItem();
|
2022-12-06 15:28:39 +03:00
|
|
|
m_lastWidget = nullptr;
|
2010-08-03 15:42:14 +02:00
|
|
|
|
2022-12-06 15:28:39 +03:00
|
|
|
identifyMatch(widget, pos, [this, widget, report](int priority) {
|
|
|
|
|
m_lastWidget = widget;
|
|
|
|
|
report(priority);
|
|
|
|
|
});
|
2017-06-26 11:15:19 +02:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 10:47:45 +01:00
|
|
|
void BaseHoverHandler::identifyMatch(TextEditorWidget *editorWidget, int pos, ReportPriority report)
|
2017-06-26 11:15:19 +02:00
|
|
|
{
|
2023-06-03 13:30:22 +02:00
|
|
|
const auto cleanup = qScopeGuard([this, report] { report(priority()); });
|
2010-08-03 15:42:14 +02:00
|
|
|
|
2016-01-22 11:48:22 +01:00
|
|
|
QString tooltip = editorWidget->extraSelectionTooltip(pos);
|
|
|
|
|
if (!tooltip.isEmpty())
|
|
|
|
|
setToolTip(tooltip);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
void BaseHoverHandler::operateTooltip(TextEditorWidget *editorWidget, const QPoint &point)
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
2020-10-23 09:55:01 +02:00
|
|
|
const QVariant helpItem = m_lastHelpItemIdentified.isValid()
|
|
|
|
|
? QVariant::fromValue(m_lastHelpItemIdentified)
|
|
|
|
|
: QVariant();
|
2019-10-25 15:16:47 +02:00
|
|
|
const bool extractHelp = m_lastHelpItemIdentified.isValid()
|
|
|
|
|
&& !m_lastHelpItemIdentified.isFuzzyMatch();
|
|
|
|
|
const QString helpContents = extractHelp ? m_lastHelpItemIdentified.firstParagraph()
|
|
|
|
|
: QString();
|
|
|
|
|
if (m_toolTip.isEmpty() && helpContents.isEmpty()) {
|
2013-09-11 17:11:15 +02:00
|
|
|
Utils::ToolTip::hide();
|
2019-10-25 15:16:47 +02:00
|
|
|
} else {
|
|
|
|
|
if (helpContents.isEmpty()) {
|
2019-11-04 17:14:25 +01:00
|
|
|
Utils::ToolTip::show(point, m_toolTip, m_textFormat, editorWidget, helpItem);
|
2019-10-25 15:16:47 +02:00
|
|
|
} else if (m_toolTip.isEmpty()) {
|
2019-11-04 17:14:25 +01:00
|
|
|
Utils::ToolTip::show(point, helpContents, Qt::RichText, editorWidget, helpItem);
|
2019-10-25 15:16:47 +02:00
|
|
|
} else {
|
|
|
|
|
// separate labels for tool tip text and help,
|
|
|
|
|
// so the text format (plain, rich, markdown) can be handled differently
|
|
|
|
|
auto layout = new QVBoxLayout;
|
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
2019-11-04 17:14:25 +01:00
|
|
|
auto label = new QLabel;
|
2020-04-16 23:51:26 +02:00
|
|
|
label->setObjectName("qcWidgetTipTopLabel");
|
2019-11-04 17:14:25 +01:00
|
|
|
label->setTextFormat(m_textFormat);
|
|
|
|
|
label->setText(m_toolTip);
|
|
|
|
|
layout->addWidget(label);
|
2020-04-16 23:51:26 +02:00
|
|
|
auto helpContentLabel = new QLabel("<hr/>" + helpContents);
|
|
|
|
|
helpContentLabel->setObjectName("qcWidgetTipHelpLabel");
|
|
|
|
|
layout->addWidget(helpContentLabel);
|
2019-10-25 15:16:47 +02:00
|
|
|
Utils::ToolTip::show(point, layout, editorWidget, helpItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-11 13:17:38 +02:00
|
|
|
} // namespace TextEditor
|