From 51cbd5288c85cb4de382cb23d6f5559c2b626126 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Fri, 8 Nov 2019 14:48:23 +0100 Subject: Fix rendering of markdown in QLabel Since 65314b6ce88cdbb28a22be0cab9856ec9bc9604b 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 --- src/widgets/widgets/qlabel.cpp | 33 ++++++++++++++++++++++----------- src/widgets/widgets/qlabel_p.h | 6 +++--- 2 files changed, 25 insertions(+), 14 deletions(-) (limited to 'src/widgets/widgets') diff --git a/src/widgets/widgets/qlabel.cpp b/src/widgets/widgets/qlabel.cpp index a840bf4ee6..77d117775a 100644 --- a/src/widgets/widgets/qlabel.cpp +++ b/src/widgets/widgets/qlabel.cpp @@ -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); } diff --git a/src/widgets/widgets/qlabel_p.h b/src/widgets/widgets/qlabel_p.h index 59188563a9..6b3fbc5f0c 100644 --- a/src/widgets/widgets/qlabel_p.h +++ b/src/widgets/widgets/qlabel_p.h @@ -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 -- cgit v1.2.3