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

source code of qtbase/src/gui/text/qtextoption.cpp