/src/qtbase/src/gui/text/qtextoption.cpp
Line | Count | Source |
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" |
5 | | #include "qguiapplication.h" |
6 | | #include "qlist.h" |
7 | | |
8 | | QT_BEGIN_NAMESPACE |
9 | | |
10 | | QT_IMPL_METATYPE_EXTERN_TAGGED(QTextOption::Tab, QTextOption_Tab) |
11 | | |
12 | | struct 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 | | */ |
25 | | QTextOption::QTextOption() |
26 | 433k | : QTextOption(Qt::AlignLeft) |
27 | 433k | { |
28 | 433k | direction = Qt::LayoutDirectionAuto; |
29 | 433k | } |
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 | | */ |
41 | | QTextOption::QTextOption(Qt::Alignment alignment) |
42 | 452k | : align(alignment), |
43 | 452k | wordWrap(QTextOption::WordWrap), |
44 | 452k | design(false), |
45 | 452k | unused(0), |
46 | 452k | f(0), |
47 | 452k | tab(-1), |
48 | 452k | d(nullptr) |
49 | 452k | { |
50 | 452k | direction = QGuiApplication::layoutDirection(); |
51 | 452k | } |
52 | | |
53 | | /*! |
54 | | Destroys the text option. |
55 | | */ |
56 | | QTextOption::~QTextOption() |
57 | 1.25M | { |
58 | 1.25M | delete d; |
59 | 1.25M | } |
60 | | |
61 | | /*! |
62 | | \fn QTextOption::QTextOption(const QTextOption &other) |
63 | | |
64 | | Construct a copy of the \a other text option. |
65 | | */ |
66 | | QTextOption::QTextOption(const QTextOption &o) |
67 | 805k | : align(o.align), |
68 | 805k | wordWrap(o.wordWrap), |
69 | 805k | design(o.design), |
70 | 805k | direction(o.direction), |
71 | 805k | unused(o.unused), |
72 | 805k | f(o.f), |
73 | 805k | tab(o.tab), |
74 | 805k | d(nullptr) |
75 | 805k | { |
76 | 805k | if (o.d) |
77 | 0 | d = new QTextOptionPrivate(*o.d); |
78 | 805k | } |
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 | | */ |
86 | | QTextOption &QTextOption::operator=(const QTextOption &o) |
87 | 1.03M | { |
88 | 1.03M | if (this == &o) |
89 | 0 | return *this; |
90 | | |
91 | 1.03M | QTextOptionPrivate* dNew = nullptr; |
92 | 1.03M | if (o.d) |
93 | 1.01M | dNew = new QTextOptionPrivate(*o.d); |
94 | 1.03M | delete d; |
95 | 1.03M | d = dNew; |
96 | | |
97 | 1.03M | align = o.align; |
98 | 1.03M | wordWrap = o.wordWrap; |
99 | 1.03M | design = o.design; |
100 | 1.03M | direction = o.direction; |
101 | 1.03M | unused = o.unused; |
102 | 1.03M | f = o.f; |
103 | 1.03M | tab = o.tab; |
104 | 1.03M | return *this; |
105 | 1.03M | } |
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 | | */ |
113 | | void QTextOption::setTabArray(const QList<qreal> &tabStops) |
114 | 0 | { |
115 | 0 | if (!d) |
116 | 0 | d = new QTextOptionPrivate; |
117 | 0 | QList<QTextOption::Tab> tabs; |
118 | 0 | QTextOption::Tab tab; |
119 | 0 | tabs.reserve(tabStops.size()); |
120 | 0 | for (qreal pos : tabStops) { |
121 | 0 | tab.position = pos; |
122 | 0 | tabs.append(tab); |
123 | 0 | } |
124 | 0 | d->tabStops = tabs; |
125 | 0 | } |
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 | | */ |
134 | | void QTextOption::setTabs(const QList<QTextOption::Tab> &tabStops) |
135 | 805k | { |
136 | 805k | if (!d) |
137 | 805k | d = new QTextOptionPrivate; |
138 | 805k | d->tabStops = tabStops; |
139 | 805k | } |
140 | | |
141 | | /*! |
142 | | Returns a list of tab positions defined for the text layout. |
143 | | |
144 | | \sa setTabArray(), tabStopDistance() |
145 | | */ |
146 | | QList<qreal> QTextOption::tabArray() const |
147 | 0 | { |
148 | 0 | QList<qreal> answer; |
149 | 0 | if (!d) |
150 | 0 | return answer; |
151 | | |
152 | 0 | answer.reserve(d->tabStops.size()); |
153 | 0 | QList<QTextOption::Tab>::ConstIterator iter = d->tabStops.constBegin(); |
154 | 0 | while(iter != d->tabStops.constEnd()) { |
155 | 0 | answer.append( (*iter).position); |
156 | 0 | ++iter; |
157 | 0 | } |
158 | 0 | return answer; |
159 | 0 | } |
160 | | |
161 | | |
162 | | QList<QTextOption::Tab> QTextOption::tabs() const |
163 | 696k | { |
164 | 696k | if (!d) |
165 | 0 | return QList<QTextOption::Tab>(); |
166 | 696k | return d->tabStops; |
167 | 696k | } |
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 | | |
406 | | QT_END_NAMESPACE |