diff options
| author | Shawn Rutledge <shawn.rutledge@qt.io> | 2023-11-02 20:10:40 -0700 |
|---|---|---|
| committer | Oliver Eftevaag <oliver.eftevaag@qt.io> | 2023-12-08 20:11:28 +0100 |
| commit | 045f9ce192d841f3cc36d514b5f238b46488b41e (patch) | |
| tree | 2dddf5bea40d39f4e197c51f203887638ac4c08a /src/quick/util/qquicktextselection.cpp | |
| parent | 0a1313d8e75e805f6dff86d8b682cb6f9bf3a7bb (diff) | |
Add TextSelection (Tech Preview)
In the Controls text editor example, DocumentHandler always sounded like
a hack, just by its name.
We don't expect to be able to handle multiple selections anytime soon;
but if we realistically expect to have multi-seat support in Qt some
day, then probably the multi-user experience should include support for
multiple text cursors and selections. So we shouldn't paint ourselves
into a corner. QQuickTextControl works with only one QTextCursor most
of the time (but it's private, thus modifiable); and TextEdit has
properties like selectionStart, selectionEnd, selectedText, etc. which
seem to assume that there is only one selection. So probably if we
needed to support multiple selections, we could add
Q_PROPERTY(QQmlListProperty<QQuickTextSelection> selections ...),
document that those legacy properties just work with the first
selection, and/or deprecate them.
So with that in mind, let's get started with a QQuickTextSelection
object. We add TextEdit.cursorSelection which holds the single selection
near the text cursor. It provides API needed for tracking and
manipulating often-used properties of selected rich text (such as
QTextCharFormat properties) so that DocumentHandler can be removed.
The example now uses TextArea.cursorSelection to manipulate the selected
text's format. It's not possible to be fully declarative with this API
though; we need to call setFont (by assigning a font), but QFont is a
value type, and is not as mergeable as QTextCharFormat is, for example.
If we used a binding rather than Action.onTriggered, it would trigger
reading the font for an entire span of selected text (which may have had
multiple fonts), setting one attribute (like bold), then applying the
font to the whole span. What we do now is almost like that; but instead
of reading the font first, we start with a default-constructed QFont,
set one attribute, and call QTextCursor::mergeCharFormat(), in the hope
that it can merge only the features of QFont that have actually been
set. Unfortunately this is not quite true either: if you toggle the
bold button, it might change the font size too, and so on; so maybe we
really need QTextCharFormat in QML (as a value type, presumably) to
implement those feature-toggling toolbar buttons correctly.
This API is in tech preview, because of such issues as described above;
because we're just scratching the surface of what might be possible;
because we should perhaps compare popular JavaScript text-editing APIs
that might be found elsewhere, in the meantime get feedback from users
during the tech preview phase, and keep iterating.
[ChangeLog][QtQuick][TextEdit] TextEdit.cursorSelection is a
TextSelection object, which provides properties to inspect and modify
the formatting of the single selection that is currently supported.
This API is in Tech Preview.
[ChangeLog][Controls][TextArea] TextArea.cursorSelection is a
TextSelection object, which provides properties to inspect and modify
the formatting of the single selection that is currently supported.
This API is in Tech Preview.
Task-number: QTBUG-36521
Task-number: QTBUG-38830
Task-number: QTBUG-81022
Change-Id: Icea99f633694aa712d0b4730b77369077288540f
Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
Diffstat (limited to 'src/quick/util/qquicktextselection.cpp')
| -rw-r--r-- | src/quick/util/qquicktextselection.cpp | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/src/quick/util/qquicktextselection.cpp b/src/quick/util/qquicktextselection.cpp new file mode 100644 index 0000000000..2f1a7dbc6d --- /dev/null +++ b/src/quick/util/qquicktextselection.cpp @@ -0,0 +1,188 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "qquicktextselection_p.h" + +#include <QFont> +#include <QTextOption> +#include <QtQuick/private/qquicktextcontrol_p.h> +#include <QtQuick/private/qquicktextcontrol_p_p.h> +#include <QtQuick/private/qquicktextedit_p_p.h> + +QT_BEGIN_NAMESPACE + +/*! + \qmltype TextSelection + \instantiates QQuickTextSelection + \inqmlmodule QtQuick + \ingroup qtquick-visual + \ingroup qtquick-input + \brief Represents a contiguous selection of text and its properties. + \since 6.7 + + \l {QtQuick::TextEdit::cursorSelection}{TextEdit.cursorSelection} + represents the range of text that is currently selected (for example by + dragging the mouse). It can be used to query and modify the selected text, + as well as properties in the \l {QTextCharFormat}{character} and + \l {QTextBlockFormat}{block} formats. + + \note This API is considered tech preview and may change or be removed in + future versions of Qt. + + \sa TextEdit, QTextCursor +*/ + +/*! \internal + QQuickTextSelection provides QML API using QTextCursor. + QQuickTextControl owns a text cursor, and one instance of + QQuickTextSelection represents it and delegates all operations to it. +*/ +QQuickTextSelection::QQuickTextSelection(QObject *parent) + : QObject(parent) +{ + // When QQuickTextEdit creates its cursorSelection, it passes itself as the parent + if (auto *textEdit = qmlobject_cast<QQuickTextEdit *>(parent)) { + m_doc = textEdit->textDocument(); + m_control = QQuickTextEditPrivate::get(textEdit)->control; + connect(m_control, &QQuickTextControl::currentCharFormatChanged, + this, &QQuickTextSelection::updateFromCharFormat); + connect(m_control, &QQuickTextControl::cursorPositionChanged, + this, &QQuickTextSelection::updateFromBlockFormat); + } +} + +/*! + \qmlproperty string QtQuick::TextSelection::text + + The selected text, without any rich text markup. + + Setting this property replaces the selected text with the given string. +*/ +QString QQuickTextSelection::text() const +{ + return cursor().selectedText(); +} + +void QQuickTextSelection::setText(const QString &text) +{ + auto cur = cursor(); + if (cur.selectedText() == text) + return; + + cur.insertText(text); + emit textChanged(); +} + +/*! + \qmlproperty color QtQuick::TextSelection::font + + The font of the selected text. + + \sa QTextCharFormat::font() +*/ +QFont QQuickTextSelection::font() const +{ + return cursor().charFormat().font(); +} + +void QQuickTextSelection::setFont(const QFont &font) +{ + auto cur = cursor(); + if (cur.selection().isEmpty()) + cur.select(QTextCursor::WordUnderCursor); + + if (font == cur.charFormat().font()) + return; + + QTextCharFormat fmt; + fmt.setFont(font); + cur.mergeCharFormat(fmt); + emit fontChanged(); +} + +/*! + \qmlproperty color QtQuick::TextSelection::color + + The foreground color of the selected text. + + \sa QTextCharFormat::foreground() +*/ +QColor QQuickTextSelection::color() const +{ + return cursor().charFormat().foreground().color(); +} + +void QQuickTextSelection::setColor(QColor color) +{ + auto cur = cursor(); + if (cur.selection().isEmpty()) + cur.select(QTextCursor::WordUnderCursor); + + if (color == cur.charFormat().foreground().color()) + return; + + QTextCharFormat fmt; + fmt.setForeground(color); + cur.mergeCharFormat(fmt); + emit colorChanged(); +} + +/*! + \qmlproperty enumeration QtQuick::TextSelection::alignment + + The alignment of the block containing the selected text. + + \sa QTextBlockFormat::alignment() +*/ +Qt::Alignment QQuickTextSelection::alignment() const +{ + return cursor().blockFormat().alignment(); +} + +void QQuickTextSelection::setAlignment(Qt::Alignment align) +{ + if (align == alignment()) + return; + + QTextBlockFormat format; + format.setAlignment(align); + cursor().mergeBlockFormat(format); + emit alignmentChanged(); +} + +/*! \internal + Return the cursor, which is either the graphically-manipulable cursor from + QQuickTextControl if that is set, or else the internally-stored cursor + with which the user is trying to mutate and/or monitor the underlying document, + in the case that TextSelection is declared in QML. +*/ +QTextCursor QQuickTextSelection::cursor() const +{ + if (m_control) + return m_control->textCursor(); + return m_cursor; +} + +inline void QQuickTextSelection::updateFromCharFormat(const QTextCharFormat &fmt) +{ + if (fmt.font() != m_charFormat.font()) + emit fontChanged(); + if (fmt.foreground().color() != m_charFormat.foreground().color()) + emit colorChanged(); + + m_charFormat = fmt; +} + +inline void QQuickTextSelection::updateFromBlockFormat() +{ + QTextBlockFormat fmt = cursor().blockFormat(); + + if (fmt.alignment() != m_blockFormat.alignment()) + emit alignmentChanged(); + + m_blockFormat = fmt; +} + +QT_END_NAMESPACE + +#include "moc_qquicktextselection_p.cpp" |
