Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtextoption.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
4#include "qtextoption.h"
6#include "qlist.h"
7
9
10QT_IMPL_METATYPE_EXTERN_TAGGED(QTextOption::Tab, QTextOption_Tab)
11
12struct QTextOptionPrivate
13{
14 QList<QTextOption::Tab> tabStops;
15};
16
17/*!
18 Constructs a text option with default properties for text.
19 The text alignment property is set to Qt::AlignLeft. The
20 word wrap property is set to QTextOption::WordWrap. The
21 using of design metrics flag is set to false.
22
23 \note Calling this constructor will set the default text direction to Qt::LayoutDirectionAuto.
24*/
25QTextOption::QTextOption()
26 : QTextOption(Qt::AlignLeft)
27{
28 direction = Qt::LayoutDirectionAuto;
29}
30
31/*!
32 Constructs a text option with the given \a alignment for text.
33 The word wrap property is set to QTextOption::WordWrap. The using
34 of design metrics flag is set to false.
35
36 \note Calling this constructor will set the default text direction to the current value of
37 QGuiApplication::layoutDirection() which depends on the system locale. For backwards
38 compatibility reasons, this does not match the default text direction set by the default
39 constructor.
40*/
41QTextOption::QTextOption(Qt::Alignment alignment)
42 : align(alignment),
43 wordWrap(QTextOption::WordWrap),
44 design(false),
45 unused(0),
46 f(0),
47 tab(-1),
48 d(nullptr)
49{
50 direction = QGuiApplication::layoutDirection();
51}
52
53/*!
54 Destroys the text option.
55*/
56QTextOption::~QTextOption()
57{
58 delete d;
59}
60
61/*!
62 \fn QTextOption::QTextOption(const QTextOption &other)
63
64 Construct a copy of the \a other text option.
65*/
66QTextOption::QTextOption(const QTextOption &o)
67 : align(o.align),
68 wordWrap(o.wordWrap),
69 design(o.design),
70 direction(o.direction),
71 unused(o.unused),
72 f(o.f),
73 tab(o.tab),
74 d(nullptr)
75{
76 if (o.d)
77 d = new QTextOptionPrivate(*o.d);
78}
79
80/*!
81 \fn QTextOption &QTextOption::operator=(const QTextOption &other)
82
83 Returns \c true if the text option is the same as the \a other text option;
84 otherwise returns \c false.
85*/
86QTextOption &QTextOption::operator=(const QTextOption &o)
87{
88 if (this == &o)
89 return *this;
90
91 QTextOptionPrivate* dNew = nullptr;
92 if (o.d)
93 dNew = new QTextOptionPrivate(*o.d);
94 delete d;
95 d = dNew;
96
97 align = o.align;
98 wordWrap = o.wordWrap;
99 design = o.design;
100 direction = o.direction;
101 unused = o.unused;
102 f = o.f;
103 tab = o.tab;
104 return *this;
105}
106
107/*!
108 Sets the tab positions for the text layout to those specified by
109 \a tabStops.
110
111 \sa tabArray(), setTabStopDistance(), setTabs()
112*/
113void QTextOption::setTabArray(const QList<qreal> &tabStops)
114{
115 if (!d)
116 d = new QTextOptionPrivate;
117 QList<QTextOption::Tab> tabs;
118 QTextOption::Tab tab;
119 tabs.reserve(tabStops.size());
120 for (qreal pos : tabStops) {
121 tab.position = pos;
122 tabs.append(tab);
123 }
124 d->tabStops = tabs;
125}
126
127/*!
128 \since 4.4
129 Sets the tab positions for the text layout to those specified by
130 \a tabStops.
131
132 \sa tabStopDistance()
133*/
134void QTextOption::setTabs(const QList<QTextOption::Tab> &tabStops)
135{
136 if (!d)
137 d = new QTextOptionPrivate;
138 d->tabStops = tabStops;
139}
140
141/*!
142 Returns a list of tab positions defined for the text layout.
143
144 \sa setTabArray(), tabStopDistance()
145*/
146QList<qreal> QTextOption::tabArray() const
147{
148 QList<qreal> answer;
149 if (!d)
150 return answer;
151
152 answer.reserve(d->tabStops.size());
153 QList<QTextOption::Tab>::ConstIterator iter = d->tabStops.constBegin();
154 while(iter != d->tabStops.constEnd()) {
155 answer.append( (*iter).position);
156 ++iter;
157 }
158 return answer;
159}
160
161
162QList<QTextOption::Tab> QTextOption::tabs() const
163{
164 if (!d)
165 return QList<QTextOption::Tab>();
166 return d->tabStops;
167}
168
169/*!
170 \class QTextOption
171 \reentrant
172
173 \brief The QTextOption class provides a description of general rich text
174 properties.
175 \inmodule QtGui
176
177 \ingroup richtext-processing
178
179 QTextOption is used to encapsulate common rich text properties in a single
180 object. It contains information about text alignment, layout direction,
181 word wrapping, and other standard properties associated with text rendering
182 and layout.
183
184 \sa QTextEdit, QTextDocument, QTextCursor
185*/
186
187/*!
188 \enum QTextOption::WrapMode
189
190 This enum describes how text is wrapped in a document.
191
192 \value NoWrap Text is not wrapped at all.
193 \value WordWrap Text is wrapped at word boundaries.
194 \value ManualWrap Same as QTextOption::NoWrap
195 \value WrapAnywhere Text can be wrapped at any point on a line, even if
196 it occurs in the middle of a word.
197 \value WrapAtWordBoundaryOrAnywhere If possible, wrapping occurs at a word
198 boundary; otherwise it will occur at the appropriate
199 point on the line, even in the middle of a word.
200*/
201
202/*!
203 \fn void QTextOption::setUseDesignMetrics(bool enable)
204
205 If \a enable is true then the layout will use design metrics;
206 otherwise it will use the metrics of the paint device (which is
207 the default behavior).
208
209 \sa useDesignMetrics()
210*/
211
212/*!
213 \fn bool QTextOption::useDesignMetrics() const
214
215 Returns \c true if the layout uses design rather than device metrics;
216 otherwise returns \c false.
217
218 \sa setUseDesignMetrics()
219*/
220
221/*!
222 \fn Qt::Alignment QTextOption::alignment() const
223
224 Returns the text alignment defined by the option.
225
226 \sa setAlignment()
227*/
228
229/*!
230 \fn void QTextOption::setAlignment(Qt::Alignment alignment);
231
232 Sets the option's text alignment to the specified \a alignment.
233
234 \sa alignment()
235*/
236
237/*!
238 \fn Qt::LayoutDirection QTextOption::textDirection() const
239
240 Returns the direction of the text layout defined by the option.
241
242 \note Due to backward compatibility reasons, the default value of the text direction differs
243 depending on which constructor is used. See the documentation for each constructor for details.
244
245 \sa setTextDirection()
246*/
247
248/*!
249 \fn void QTextOption::setTextDirection(Qt::LayoutDirection direction)
250
251 Sets the direction of the text layout defined by the option to the
252 given \a direction.
253
254 \sa textDirection()
255*/
256
257/*!
258 \fn WrapMode QTextOption::wrapMode() const
259
260 Returns the text wrap mode defined by the option.
261
262 \sa setWrapMode()
263*/
264
265/*!
266 \fn void QTextOption::setWrapMode(WrapMode mode)
267
268 Sets the option's text wrap mode to the given \a mode.
269*/
270
271/*!
272 \enum QTextOption::Flag
273
274 \value IncludeTrailingSpaces When this option is set, QTextLine::naturalTextWidth() and naturalTextRect() will
275 return a value that includes the width of trailing spaces in the text; otherwise
276 this width is excluded.
277 \value ShowTabsAndSpaces Visualize spaces with little dots, and tabs with little arrows. Non-breaking spaces are
278 shown differently to breaking spaces.
279 \value ShowLineAndParagraphSeparators Visualize line and paragraph separators with appropriate symbol characters.
280 \value [since 5.7] ShowDocumentTerminator Visualize the end of the document with a section sign.
281 \value [since 6.9] ShowDefaultIgnorables Render normally non-visual characters if supported by font.
282 \value AddSpaceForLineAndParagraphSeparators While determining the line-break positions take into account the
283 space added for drawing a separator character.
284 \value SuppressColors Suppress all color changes in the character formats (except the main selection).
285 \value [since 6.9] DisableEmojiParsing By default, Qt will detect emoji sequences in input strings
286 and prioritize using color fonts to display them. This extra step can be disabled by setting the
287 DisableEmojiParsing flag if it is known in advance that it will not be needed.
288*/
289
290/*!
291 \fn Flags QTextOption::flags() const
292
293 Returns the flags associated with the option.
294
295 \sa setFlags()
296*/
297
298/*!
299 \fn void QTextOption::setFlags(Flags flags)
300
301 Sets the flags associated with the option to the given \a flags.
302
303 \sa flags()
304*/
305
306/*!
307 \fn qreal QTextOption::tabStopDistance() const
308 \since 5.10
309
310 Returns the distance in device units between tab stops.
311
312 \sa setTabStopDistance(), tabArray(), setTabs(), tabs()
313*/
314
315/*!
316 \fn void QTextOption::setTabStopDistance(qreal tabStopDistance)
317 \since 5.10
318
319 Sets the default distance in device units between tab stops to the value specified
320 by \a tabStopDistance.
321
322 \sa tabStopDistance(), setTabArray(), setTabs(), tabs()
323*/
324
325/*!
326 \enum QTextOption::TabType
327 \since 4.4
328
329 This enum holds the different types of tabulator
330
331 \value LeftTab A left-tab
332 \value RightTab A right-tab
333 \value CenterTab A centered-tab
334 \value DelimiterTab A tab stopping at a certain delimiter-character
335*/
336
337/*!
338 \class QTextOption::Tab
339 \since 4.4
340 \inmodule QtGui
341 Each tab definition is represented by this struct.
342*/
343
344/*!
345 \variable QTextOption::Tab::position
346 Distance from the start of the paragraph.
347 The position of a tab is from the start of the paragraph which implies that when
348 the alignment of the paragraph is set to centered, the tab is interpreted to be
349 moved the same distance as the left edge of the paragraph does.
350 In case the paragraph is set to have a layoutDirection() RightToLeft the position
351 is interpreted to be from the right side of the paragraph with higher numbers moving
352 the tab to the left.
353*/
354
355/*!
356 \variable QTextOption::Tab::type
357 Determine which type is used.
358 In a paragraph that has layoutDirection() RightToLeft the type LeftTab will
359 be interpreted to be a RightTab and vice versa.
360*/
361
362/*!
363 \variable QTextOption::Tab::delimiter
364 If type is DelimitorTab; tab until this char is found in the text.
365*/
366
367/*!
368 \fn QTextOption::Tab::Tab()
369 Creates a default left tab with position 80.
370*/
371
372/*!
373 \fn QTextOption::Tab::Tab(qreal pos, TabType tabType, QChar delim = QChar())
374
375 Creates a tab with the given position, tab type, and delimiter
376 (\a pos, \a tabType, \a delim).
377
378 \note \a delim is only used when \a tabType is DelimiterTab.
379
380 \since 4.7
381*/
382
383/*!
384 \fn bool QTextOption::Tab::operator==(const QTextOption::Tab &other) const
385
386 Returns \c true if tab \a other is equal to this tab;
387 otherwise returns \c false.
388*/
389
390/*!
391 \fn bool QTextOption::Tab::operator!=(const QTextOption::Tab &other) const
392
393 Returns \c true if tab \a other is not equal to this tab;
394 otherwise returns \c false.
395*/
396
397/*!
398 \since 4.4
399 \fn QList<QTextOption::Tab> QTextOption::tabs() const
400 Returns a list of tab positions defined for the text layout.
401
402 \sa tabStopDistance(), setTabs(), setTabStopDistance()
403*/
404
405
406QT_END_NAMESPACE
Combined button and popup list for selecting options.