Fix rendering of markdown in QLabel

Since 65314b6ce8 there is a TextFormat
for MarkdownText, and QWidgetTextControl supports that, but QLabel
does it in its own way and sets plain text or rich text on the text
document itself. Add a code path for MarkdownText there.

[ChangeLog][QtWidgets][QLabel] Markdown is now a supported textFormat
for QLabel.

Fixes: QTBUG-79766
Change-Id: Ib9370ef300089af2c4d6070e545c5470f32833a8
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Eike Ziller 2019-11-08 14:48:23 +01:00 committed by Shawn Rutledge
parent 2735c5bf06
commit 51cbd5288c
2 changed files with 25 additions and 14 deletions

View File

@ -85,6 +85,7 @@ QLabelPrivate::QLabelPrivate()
shortcutId(0),
#endif
textformat(Qt::AutoText),
effectiveTextFormat(Qt::PlainText),
textInteractionFlags(Qt::LinksAccessibleByMouse),
sizePolicy(),
margin(0),
@ -94,7 +95,6 @@ QLabelPrivate::QLabelPrivate()
scaledcontents(false),
textLayoutDirty(false),
textDirty(false),
isRichText(false),
isTextLabel(false),
hasShortcut(/*???*/),
#ifndef QT_NO_CURSOR
@ -294,8 +294,14 @@ void QLabel::setText(const QString &text)
d->text = text;
d->isTextLabel = true;
d->textDirty = true;
d->isRichText = d->textformat == Qt::RichText
|| (d->textformat == Qt::AutoText && Qt::mightBeRichText(d->text));
if (d->textformat == Qt::AutoText) {
if (Qt::mightBeRichText(d->text))
d->effectiveTextFormat = Qt::RichText;
else
d->effectiveTextFormat = Qt::PlainText;
} else {
d->effectiveTextFormat = d->textformat;
}
d->control = oldControl;
@ -306,7 +312,7 @@ void QLabel::setText(const QString &text)
d->control = nullptr;
}
if (d->isRichText) {
if (d->effectiveTextFormat != Qt::PlainText) {
setMouseTracking(true);
} else {
// Note: mouse tracking not disabled intentionally
@ -1478,14 +1484,19 @@ void QLabelPrivate::ensureTextPopulated() const
if (control) {
QTextDocument *doc = control->document();
if (textDirty) {
#ifndef QT_NO_TEXTHTMLPARSER
if (isRichText)
doc->setHtml(text);
else
if (effectiveTextFormat == Qt::PlainText) {
doc->setPlainText(text);
#else
doc->setPlainText(text);
#if QT_CONFIG(texthtmlparser)
} else if (effectiveTextFormat == Qt::RichText) {
doc->setHtml(text);
#endif
#if QT_CONFIG(textmarkdownreader)
} else if (effectiveTextFormat == Qt::MarkdownText) {
doc->setMarkdown(text);
#endif
} else {
doc->setPlainText(text);
}
doc->setUndoRedoEnabled(false);
#ifndef QT_NO_SHORTCUT
@ -1623,7 +1634,7 @@ QMenu *QLabelPrivate::createStandardContextMenu(const QPoint &pos)
{
QString linkToCopy;
QPoint p;
if (control && isRichText) {
if (control && effectiveTextFormat != Qt::PlainText) {
p = layoutPoint(pos);
linkToCopy = control->document()->documentLayout()->anchorAt(p);
}

View File

@ -93,8 +93,8 @@ public:
#endif
inline bool needTextControl() const {
return isTextLabel
&& (isRichText
|| (!isRichText && (textInteractionFlags & (Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard))));
&& (effectiveTextFormat != Qt::PlainText
|| (textInteractionFlags & (Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard)));
}
void ensureTextPopulated() const;
@ -134,6 +134,7 @@ public:
int shortcutId;
#endif
Qt::TextFormat textformat;
Qt::TextFormat effectiveTextFormat;
Qt::TextInteractionFlags textInteractionFlags;
mutable QSizePolicy sizePolicy;
int margin;
@ -143,7 +144,6 @@ public:
uint scaledcontents : 1;
mutable uint textLayoutDirty : 1;
mutable uint textDirty : 1;
mutable uint isRichText : 1;
mutable uint isTextLabel : 1;
mutable uint hasShortcut : 1;
#ifndef QT_NO_CURSOR