aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquicktextcontrol.cpp
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2017-12-19 21:45:21 +0100
committerShawn Rutledge <shawn.rutledge@qt.io>2019-05-29 13:46:44 +0200
commit8f62f07bbc59ee3d97cd3d7d54b60b47c979a3cc (patch)
tree191b8c84f38fd25bad90103716a5d6c06445ba5c /src/quick/items/qquicktextcontrol.cpp
parentc8827b444c23656f67feee2e3ebd4f2868ab3db7 (diff)
Add Markdown support to TextEdit
- textFormat can be set to MarkdownText, and markdown can be loaded into the text property - markdown text can be pulled out of the text property - if there are lists with checkboxes, you can click them to change the checkbox state Change-Id: I450115c732d737446ae71806e4abb18e8cc639f3 Reviewed-by: Gatis Paeglis <gatis.paeglis@qt.io>
Diffstat (limited to 'src/quick/items/qquicktextcontrol.cpp')
-rw-r--r--src/quick/items/qquicktextcontrol.cpp44
1 files changed, 43 insertions, 1 deletions
diff --git a/src/quick/items/qquicktextcontrol.cpp b/src/quick/items/qquicktextcontrol.cpp
index ac8093e10b..5c4ecd60aa 100644
--- a/src/quick/items/qquicktextcontrol.cpp
+++ b/src/quick/items/qquicktextcontrol.cpp
@@ -113,6 +113,7 @@ QQuickTextControlPrivate::QQuickTextControlPrivate()
wordSelectionEnabled(false),
hasImState(false),
cursorRectangleChanged(false),
+ hoveredMarker(false),
lastSelectionStart(-1),
lastSelectionEnd(-1)
{}
@@ -321,6 +322,9 @@ void QQuickTextControlPrivate::setContent(Qt::TextFormat format, const QString &
formatCursor.select(QTextCursor::Document);
formatCursor.setCharFormat(charFormatForInsertion);
formatCursor.endEditBlock();
+ } else if (format == Qt::MarkdownText) {
+ doc->setBaseUrl(doc->baseUrl().adjusted(QUrl::RemoveFilename));
+ doc->setMarkdown(text);
} else {
#if QT_CONFIG(texthtmlparser)
doc->setHtml(text);
@@ -801,6 +805,12 @@ void QQuickTextControl::setPlainText(const QString &text)
d->setContent(Qt::PlainText, text);
}
+void QQuickTextControl::setMarkdownText(const QString &text)
+{
+ Q_D(QQuickTextControl);
+ d->setContent(Qt::MarkdownText, text);
+}
+
void QQuickTextControl::setHtml(const QString &text)
{
Q_D(QQuickTextControl);
@@ -1027,6 +1037,8 @@ void QQuickTextControlPrivate::mousePressEvent(QMouseEvent *e, const QPointF &po
cursor.clearSelection();
}
}
+ if (interactionFlags & Qt::TextEditable)
+ blockWithMarkerUnderMousePress = q->blockWithMarkerAt(pos);
if (e->button() & Qt::MiddleButton) {
return;
} else if (!(e->button() & Qt::LeftButton)) {
@@ -1198,6 +1210,16 @@ void QQuickTextControlPrivate::mouseReleaseEvent(QMouseEvent *e, const QPointF &
q->updateCursorRectangle(true);
}
+ if ((interactionFlags & Qt::TextEditable) && (e->button() & Qt::LeftButton) && blockWithMarkerUnderMousePress.isValid()) {
+ QTextBlock block = q->blockWithMarkerAt(pos);
+ if (block == blockWithMarkerUnderMousePress) {
+ auto fmt = block.blockFormat();
+ fmt.setMarker(fmt.marker() == QTextBlockFormat::Unchecked ?
+ QTextBlockFormat::Checked : QTextBlockFormat::Unchecked);
+ cursor.setBlockFormat(fmt);
+ }
+ }
+
if (interactionFlags & Qt::LinksAccessibleByMouse) {
if (!(e->button() & Qt::LeftButton))
return;
@@ -1480,8 +1502,15 @@ void QQuickTextControlPrivate::hoverEvent(QHoverEvent *e, const QPointF &pos)
if (hoveredLink != link) {
hoveredLink = link;
emit q->linkHovered(link);
+ qCDebug(DBG_HOVER_TRACE) << q << e->type() << pos << "hoveredLink" << hoveredLink;
+ } else {
+ QTextBlock block = q->blockWithMarkerAt(pos);
+ if (block.isValid() != hoveredMarker)
+ emit q->markerHovered(block.isValid());
+ hoveredMarker = block.isValid();
+ if (hoveredMarker)
+ qCDebug(DBG_HOVER_TRACE) << q << e->type() << pos << "hovered marker" << block.blockFormat().marker() << block.text();
}
- qCDebug(DBG_HOVER_TRACE) << q << e->type() << pos << "hoveredLink" << hoveredLink;
}
bool QQuickTextControl::hasImState() const
@@ -1557,6 +1586,12 @@ QString QQuickTextControl::anchorAt(const QPointF &pos) const
return d->doc->documentLayout()->anchorAt(pos);
}
+QTextBlock QQuickTextControl::blockWithMarkerAt(const QPointF &pos) const
+{
+ Q_D(const QQuickTextControl);
+ return d->doc->documentLayout()->blockWithMarkerAt(pos);
+}
+
void QQuickTextControl::setAcceptRichText(bool accept)
{
Q_D(QQuickTextControl);
@@ -1786,6 +1821,13 @@ QString QQuickTextControl::toHtml() const
}
#endif
+#if QT_CONFIG(textmarkdownwriter)
+QString QQuickTextControl::toMarkdown() const
+{
+ return document()->toMarkdown();
+}
+#endif
+
bool QQuickTextControl::cursorOn() const
{
Q_D(const QQuickTextControl);