summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2022-05-20 09:23:28 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2022-06-24 22:42:28 +0200
commit56f0ebfe860e440dcbba8997f44836debc901119 (patch)
tree87cce44b0d2cf34a3dca418d4c126874300be5bb /src
parentec0ecedf2ae6f467674bd4e8bc38680669012a08 (diff)
Support markdown in QTextEditMimeData; fix pasting trailing newlines
- Since 4edcea762d9ce334c4c1a78234c90c118b81da87 the dropsite example shows markdown if available; and now it shows that when we do DnD of a selection from the richtext example, text/markdown is available. - If we artificially make html unavailable, copying and pasting between widget-based rich text editors uses markdown. In case markdown writer output contains unnecessary backticks due to monospace fonts getting used, the workaround from 1ad456c908467212bc30223a69eb7524b64b86e1 is applied. Pick-to: 6.4 Task-number: QTBUG-76105 Task-number: QTBUG-103484 Change-Id: Ie6ca4dbb450dbc36b3d09fd0df1ae5909aaebca7 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/widgets/widgets/qwidgettextcontrol.cpp23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/widgets/widgets/qwidgettextcontrol.cpp b/src/widgets/widgets/qwidgettextcontrol.cpp
index b97d0fa3c2..cec18dbe69 100644
--- a/src/widgets/widgets/qwidgettextcontrol.cpp
+++ b/src/widgets/widgets/qwidgettextcontrol.cpp
@@ -24,6 +24,7 @@
#endif
#include "qtextdocument.h"
#include "private/qtextdocument_p.h"
+#include "private/qtextdocumentfragment_p.h"
#include "qtextlist.h"
#include "private/qwidgettextcontrol_p.h"
#if QT_CONFIG(style_stylesheet)
@@ -2697,6 +2698,13 @@ void QWidgetTextControl::insertFromMimeData(const QMimeData *source)
bool hasData = false;
QTextDocumentFragment fragment;
+#if QT_CONFIG(textmarkdownreader)
+ if (source->formats().first() == "text/markdown"_L1) {
+ auto s = QString::fromUtf8(source->data("text/markdown"_L1));
+ fragment = QTextDocumentFragment::fromMarkdown(s);
+ hasData = true;
+ } else
+#endif
#ifndef QT_NO_TEXTHTMLPARSER
if (source->hasFormat("application/x-qrichtext"_L1) && d->acceptRichText) {
// x-qrichtext is always UTF-8 (taken from Qt3 since we don't use it anymore).
@@ -2707,16 +2715,15 @@ void QWidgetTextControl::insertFromMimeData(const QMimeData *source)
} else if (source->hasHtml() && d->acceptRichText) {
fragment = QTextDocumentFragment::fromHtml(source->html(), d->doc);
hasData = true;
- } else {
- QString text = source->text();
+ }
+#endif // QT_NO_TEXTHTMLPARSER
+ if (!hasData) {
+ const QString text = source->text();
if (!text.isNull()) {
fragment = QTextDocumentFragment::fromPlainText(text);
hasData = true;
}
}
-#else
- fragment = QTextDocumentFragment::fromPlainText(source->text());
-#endif // QT_NO_TEXTHTMLPARSER
if (hasData)
d->cursor.insertFragment(fragment);
@@ -3434,6 +3441,9 @@ QStringList QTextEditMimeData::formats() const
{
if (!fragment.isEmpty())
return QStringList() << u"text/plain"_s << u"text/html"_s
+#if QT_CONFIG(textmarkdownwriter)
+ << u"text/markdown"_s
+#endif
#ifndef QT_NO_TEXTODFWRITER
<< u"application/vnd.oasis.opendocument.text"_s
#endif
@@ -3455,6 +3465,9 @@ void QTextEditMimeData::setup() const
#ifndef QT_NO_TEXTHTMLPARSER
that->setData("text/html"_L1, fragment.toHtml().toUtf8());
#endif
+#if QT_CONFIG(textmarkdownwriter)
+ that->setData("text/markdown"_L1, fragment.toMarkdown().toUtf8());
+#endif
#ifndef QT_NO_TEXTODFWRITER
{
QBuffer buffer;