Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquicktextselection.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QFont>
7#include <QTextOption>
8#include <QtQuick/private/qquicktextcontrol_p.h>
9#include <QtQuick/private/qquicktextcontrol_p_p.h>
10#include <QtQuick/private/qquicktextedit_p_p.h>
11
13
41 : QObject(parent)
42{
43 // When QQuickTextEdit creates its cursorSelection, it passes itself as the parent
44 if (auto *textEdit = qmlobject_cast<QQuickTextEdit *>(parent)) {
45 m_control = QQuickTextEditPrivate::get(textEdit)->control;
47 this, &QQuickTextSelection::updateFromCharFormat);
49 this, &QQuickTextSelection::updateFromBlockFormat);
50 }
51}
52
61{
62 return cursor().selectedText();
63}
64
66{
67 auto cur = cursor();
68 if (cur.selectedText() == text)
69 return;
70
71 cur.insertText(text);
73}
74
83{
84 return cursor().charFormat().font();
85}
86
88{
89 auto cur = cursor();
90 if (cur.selection().isEmpty())
92
93 if (font == cur.charFormat().font())
94 return;
95
98 cur.mergeCharFormat(fmt);
100}
101
110{
111 return cursor().charFormat().foreground().color();
112}
113
115{
116 auto cur = cursor();
117 if (cur.selection().isEmpty())
119
120 if (color == cur.charFormat().foreground().color())
121 return;
122
125 cur.mergeCharFormat(fmt);
127}
128
137{
138 return cursor().blockFormat().alignment();
139}
140
141void QQuickTextSelection::setAlignment(Qt::Alignment align)
142{
143 if (align == alignment())
144 return;
145
147 format.setAlignment(align);
148 cursor().mergeBlockFormat(format);
150}
151
158QTextCursor QQuickTextSelection::cursor() const
159{
160 if (m_control)
161 return m_control->textCursor();
162 return m_cursor;
163}
164
165inline void QQuickTextSelection::updateFromCharFormat(const QTextCharFormat &fmt)
166{
167 if (fmt.font() != m_charFormat.font())
169 if (fmt.foreground().color() != m_charFormat.foreground().color())
171
172 m_charFormat = fmt;
173}
174
175inline void QQuickTextSelection::updateFromBlockFormat()
176{
177 QTextBlockFormat fmt = cursor().blockFormat();
178
179 if (fmt.alignment() != m_blockFormat.alignment())
181
182 m_blockFormat = fmt;
183}
184
186
187#include "moc_qquicktextselection_p.cpp"
const QColor & color() const
Returns the brush color.
Definition qbrush.h:121
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
\reentrant
Definition qfont.h:22
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void currentCharFormatChanged(const QTextCharFormat &format)
void cursorPositionChanged()
QTextCursor textCursor() const
static QQuickTextEditPrivate * get(QQuickTextEdit *item)
void setColor(QColor color)
void setFont(const QFont &font)
QQuickTextSelection(QObject *parent=nullptr)
\qmltype TextSelection \instantiates QQuickTextSelection \inqmlmodule QtQuick
void setText(const QString &text)
void setAlignment(Qt::Alignment align)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Qt::Alignment alignment() const
Returns the paragraph's alignment.
void setAlignment(Qt::Alignment alignment)
Sets the paragraph's alignment.
QFont font() const
Returns the font for this character format.
void setFont(const QFont &font, FontPropertiesInheritanceBehavior behavior=FontPropertiesAll)
\reentrant \inmodule QtGui
Definition qtextcursor.h:30
QString selectedText() const
Returns the current selection's text (which may be empty).
QTextBlockFormat blockFormat() const
Returns the block format of the block the cursor is in.
QTextCharFormat charFormat() const
Returns the format of the character immediately before the cursor position().
void mergeBlockFormat(const QTextBlockFormat &modifier)
Modifies the block format of the current block (or all blocks that are contained in the selection) wi...
void setForeground(const QBrush &brush)
Sets the foreground brush to the specified brush.
QBrush foreground() const
Returns the brush used to render foreground details, such as text, frame outlines,...
QString text
Combined button and popup list for selecting options.
GLuint color
[2]
GLint GLsizei GLsizei GLenum format
#define emit
QVideoFrameFormat::PixelFormat fmt
QGraphicsWidget * textEdit