TextEditor: Allow explicitly passing the text format for tool tips

Change-Id: I6e592a73fa6a3229cda9e76a4ab33f2c0ca330c5
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2019-11-04 17:14:25 +01:00
parent 177f14b115
commit 8e4fb05532
6 changed files with 63 additions and 11 deletions

View File

@@ -159,7 +159,14 @@ static bool likelyContainsLink(const QString &s)
void TextTip::setContent(const QVariant &content)
{
if (content.canConvert<QString>()) {
m_text = content.toString();
} else if (content.canConvert<TextItem>()) {
auto item = content.value<TextItem>();
m_text = item.first;
m_format = item.second;
}
bool containsLink = likelyContainsLink(m_text);
setOpenExternalLinks(containsLink);
}
@@ -171,6 +178,7 @@ bool TextTip::isInteractive() const
void TextTip::configure(const QPoint &pos, QWidget *w)
{
setTextFormat(m_format);
setText(m_text);
// Make it look good with the default ToolTip font on Mac, which has a small descent.
@@ -205,7 +213,9 @@ int TextTip::showTime() const
bool TextTip::equals(int typeId, const QVariant &other, const QVariant &otherContextHelp) const
{
return typeId == ToolTip::TextContent && otherContextHelp == contextHelp()
&& other.toString() == m_text;
&& ((other.canConvert<QString>() && other.toString() == m_text)
|| (other.canConvert<TextItem>()
&& other.value<TextItem>() == TextItem(m_text, m_format)));
}
void TextTip::paintEvent(QPaintEvent *event)

View File

@@ -60,6 +60,8 @@ private:
QVariant m_contextHelp;
};
using TextItem = std::pair<QString, Qt::TextFormat>;
class TextTip : public TipLabel
{
public:
@@ -76,6 +78,7 @@ public:
private:
QString m_text;
Qt::TextFormat m_format = Qt::AutoText;
};
class ColorTip : public TipLabel
@@ -117,3 +120,5 @@ private:
} // namespace Internal
} // namespace Utils
Q_DECLARE_METATYPE(Utils::Internal::TextItem)

View File

@@ -75,18 +75,41 @@ static QWidget *createF1Icon()
\a contextHelp of the current shown tool tip can be retrieved via ToolTip::contextHelp().
*/
void ToolTip::show(const QPoint &pos, const QString &content, QWidget *w, const QVariant &contextHelp, const QRect &rect)
{
show(pos, content, Qt::AutoText, w, contextHelp, rect);
}
/*!
Shows a tool tip with the text \a content with a specific text \a format.
If \a contextHelp is given, a context help icon is shown as well.
\a contextHelp of the current shown tool tip can be retrieved via ToolTip::contextHelp().
*/
void ToolTip::show(const QPoint &pos,
const QString &content,
Qt::TextFormat format,
QWidget *w,
const QVariant &contextHelp,
const QRect &rect)
{
if (content.isEmpty()) {
instance()->hideTipWithDelay();
} else {
if (contextHelp.isNull()) {
instance()->showInternal(pos, QVariant(content), TextContent, w, contextHelp, rect);
instance()->showInternal(pos,
qVariantFromValue(TextItem(content, format)),
TextContent,
w,
contextHelp,
rect);
} else {
auto tooltipWidget = new FakeToolTip;
auto layout = new QHBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
tooltipWidget->setLayout(layout);
layout->addWidget(new QLabel(content));
auto label = new QLabel;
label->setTextFormat(format);
label->setText(content);
layout->addWidget(label);
layout->addWidget(createF1Icon());
instance()->showInternal(pos,
QVariant::fromValue(tooltipWidget),

View File

@@ -74,8 +74,17 @@ public:
static void show(const QPoint &pos, const QString &content, QWidget *w = nullptr,
const QVariant &contextHelp = {}, const QRect &rect = QRect());
static void show(const QPoint &pos, const QColor &color, QWidget *w = nullptr,
const QVariant &contextHelp = {}, const QRect &rect = QRect());
static void show(const QPoint &pos,
const QString &content,
Qt::TextFormat format,
QWidget *w = nullptr,
const QVariant &contextHelp = {},
const QRect &rect = QRect());
static void show(const QPoint &pos,
const QColor &color,
QWidget *w = nullptr,
const QVariant &contextHelp = {},
const QRect &rect = QRect());
static void show(const QPoint &pos, QWidget *content, QWidget *w = nullptr,
const QVariant &contextHelp = {}, const QRect &rect = QRect());
static void show(const QPoint &pos, QLayout *content, QWidget *w = nullptr,

View File

@@ -89,9 +89,10 @@ void BaseHoverHandler::contextHelpId(TextEditorWidget *widget,
m_isContextHelpRequest = false;
}
void BaseHoverHandler::setToolTip(const QString &tooltip)
void BaseHoverHandler::setToolTip(const QString &tooltip, Qt::TextFormat format)
{
m_toolTip = tooltip;
m_textFormat = format;
}
const QString &BaseHoverHandler::toolTip() const
@@ -153,15 +154,18 @@ void BaseHoverHandler::operateTooltip(TextEditorWidget *editorWidget, const QPoi
Utils::ToolTip::hide();
} else {
if (helpContents.isEmpty()) {
Utils::ToolTip::show(point, m_toolTip, editorWidget, helpItem);
Utils::ToolTip::show(point, m_toolTip, m_textFormat, editorWidget, helpItem);
} else if (m_toolTip.isEmpty()) {
Utils::ToolTip::show(point, helpContents, editorWidget, helpItem);
Utils::ToolTip::show(point, helpContents, Qt::RichText, editorWidget, helpItem);
} 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);
layout->addWidget(new QLabel(m_toolTip));
auto label = new QLabel;
label->setTextFormat(m_textFormat);
label->setText(m_toolTip);
layout->addWidget(label);
layout->addWidget(new QLabel("<hr/>" + helpContents));
Utils::ToolTip::show(point, layout, editorWidget, helpItem);
}

View File

@@ -65,7 +65,7 @@ protected:
void setPriority(int priority);
int priority() const;
void setToolTip(const QString &tooltip);
void setToolTip(const QString &tooltip, Qt::TextFormat format = Qt::PlainText);
const QString &toolTip() const;
void setLastHelpItemIdentified(const Core::HelpItem &help);
@@ -86,6 +86,7 @@ private:
void process(TextEditorWidget *widget, int pos, ReportPriority report);
QString m_toolTip;
Qt::TextFormat m_textFormat = Qt::PlainText;
Core::HelpItem m_lastHelpItemIdentified;
int m_priority = -1;
bool m_isContextHelpRequest = false;