9#include <QtQuick/private/qquicktextcontrol_p.h>
10#include <QtQuick/private/qquicktextcontrol_p_p.h>
11#include <QtQuick/private/qquicktextedit_p_p.h>
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
53
54
55
56
57
58
59
60
61QQuickTextSelection::QQuickTextSelection(QObject *parent)
65 if (
auto *textEdit = qmlobject_cast<QQuickTextEdit *>(parent)) {
66 m_doc = textEdit->textDocument();
67 m_control = QQuickTextEditPrivate::get(textEdit)->control;
68 connect(m_control, &QQuickTextControl::currentCharFormatChanged,
69 this, &QQuickTextSelection::updateFromCharFormat);
70 connect(m_control, &QQuickTextControl::cursorPositionChanged,
71 this, &QQuickTextSelection::updateFromBlockFormat);
75void QQuickTextSelection::componentComplete()
81 if (
auto *textEdit = qmlobject_cast<QQuickTextEdit *>(parent())) {
82 m_doc = textEdit->textDocument();
83 m_cursor = QTextCursor(m_doc->textDocument());
89
90
91
92
93
94
95
96QQuickTextDocument *QQuickTextSelection::document()
const
101void QQuickTextSelection::setDocument(QQuickTextDocument *doc)
107 m_cursor = QTextCursor(m_doc->textDocument());
108 emit documentChanged();
112
113
114
115
116
117
118
119int QQuickTextSelection::selectionStart()
const
121 return cursor().selectionStart();
124void QQuickTextSelection::setSelectionStart(
int start)
127 if (start == cur.selectionStart())
131 cur.setPosition(start, QTextCursor::MoveAnchor);
132 m_control->setTextCursor(cur);
134 m_cursor.setPosition(start, QTextCursor::MoveAnchor);
136 emit selectionStartChanged();
140
141
142
143
144
145
146
147int QQuickTextSelection::selectionEnd()
const
149 return cursor().selectionEnd();
152void QQuickTextSelection::setSelectionEnd(
int end)
155 if (end == cur.selectionEnd())
159 cur.setPosition(end, QTextCursor::KeepAnchor);
160 m_control->setTextCursor(cur);
162 m_cursor.setPosition(end, QTextCursor::KeepAnchor);
164 emit selectionEndChanged();
168
169
170
171
172
173
174QString QQuickTextSelection::text()
const
176 return cursor().selectedText();
179void QQuickTextSelection::setText(
const QString &text)
182 if (cur.selectedText() == text)
185 cur.insertText(text);
190
191
192
193
194
195
196QFont QQuickTextSelection::font()
const
198 return cursor().charFormat().font();
201void QQuickTextSelection::setFont(
const QFont &font)
204 if (cur.selection().isEmpty())
205 cur.select(QTextCursor::WordUnderCursor);
207 if (font == cur.charFormat().font())
212 cur.mergeCharFormat(fmt);
217
218
219
220
221
222
223QColor QQuickTextSelection::color()
const
225 return cursor().charFormat().foreground().color();
228void QQuickTextSelection::setColor(QColor color)
231 if (cur.selection().isEmpty())
232 cur.select(QTextCursor::WordUnderCursor);
234 if (color == cur.charFormat().foreground().color())
238 fmt.setForeground(color);
239 cur.mergeCharFormat(fmt);
244
245
246
247
248
249
250Qt::Alignment QQuickTextSelection::alignment()
const
252 return cursor().blockFormat().alignment();
255void QQuickTextSelection::setAlignment(Qt::Alignment align)
257 if (align == alignment())
260 QTextBlockFormat format;
261 format.setAlignment(align);
262 cursor().mergeBlockFormat(format);
263 emit alignmentChanged();
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311bool QQuickTextSelection::moveSelectionStart(MoveOperation op,
int n)
313 const QTextCursor::MoveOperation qop =
static_cast<QTextCursor::MoveOperation>(op);
316 if (cur.movePosition(qop, QTextCursor::MoveAnchor, n)) {
317 m_control->setTextCursor(cur);
321 return m_cursor.movePosition(qop, QTextCursor::MoveAnchor, n);
328
329
330
331
332
333
334
335
336
337
338
339
340bool QQuickTextSelection::moveSelectionEnd(MoveOperation op,
int n)
342 const QTextCursor::MoveOperation qop =
static_cast<QTextCursor::MoveOperation>(op);
345 if (cur.movePosition(qop, QTextCursor::KeepAnchor, n)) {
346 m_control->setTextCursor(cur);
350 return m_cursor.movePosition(qop, QTextCursor::KeepAnchor, n);
357
358
359
360
361
362
363
364
365
366
367void QQuickTextSelection::duplicate()
369 auto sel = cursor().selection();
370 const auto start = selectionEnd();
371 setSelectionStart(start);
372 cursor().insertFragment(sel);
375 const auto end = selectionEnd();
376 setSelectionStart(start);
377 setSelectionEnd(end);
381
382
383
384
385
386
387
388void QQuickTextSelection::linkTo(
const QUrl &destination)
391 if (cur.selection().isEmpty())
392 cur.select(QTextCursor::WordUnderCursor);
393 cur.beginEditBlock();
394 QTextCharFormat fmt = cur.charFormat();
395 fmt.setForeground(QPalette().link());
397 fmt.setAnchorHref(destination.toString());
398 cur.setCharFormat(fmt);
403
404
405
406
407
408QTextCursor QQuickTextSelection::cursor()
const
411 return m_control->textCursor();
415inline void QQuickTextSelection::updateFromCharFormat(
const QTextCharFormat &fmt)
417 if (fmt.font() != m_charFormat.font())
419 if (fmt.foreground().color() != m_charFormat.foreground().color())
425inline void QQuickTextSelection::updateFromBlockFormat()
427 QTextBlockFormat fmt = cursor().blockFormat();
429 if (fmt.alignment() != m_blockFormat.alignment())
430 emit alignmentChanged();
437#include "moc_qquicktextselection_p.cpp"