1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qtextoption.h"
41 #include "qguiapplication.h"
42 #include "qlist.h"
43 
44 QT_BEGIN_NAMESPACE
45 
46 struct QTextOptionPrivate
47 {
48     QList<QTextOption::Tab> tabStops;
49 };
50 
51 /*!
52     Constructs a text option with default properties for text.
53     The text alignment property is set to Qt::AlignLeft. The
54     word wrap property is set to QTextOption::WordWrap. The
55     using of design metrics flag is set to false.
56 */
QTextOption()57 QTextOption::QTextOption()
58     : align(Qt::AlignLeft),
59       wordWrap(QTextOption::WordWrap),
60       design(false),
61       unused(0),
62       unused2(0),
63       f(0),
64       tab(-1),
65       d(nullptr)
66 {
67     direction = Qt::LayoutDirectionAuto;
68 }
69 
70 /*!
71     Constructs a text option with the given \a alignment for text.
72     The word wrap property is set to QTextOption::WordWrap. The using
73     of design metrics flag is set to false.
74 */
QTextOption(Qt::Alignment alignment)75 QTextOption::QTextOption(Qt::Alignment alignment)
76     : align(alignment),
77       wordWrap(QTextOption::WordWrap),
78       design(false),
79       unused(0),
80       unused2(0),
81       f(0),
82       tab(-1),
83       d(nullptr)
84 {
85     direction = QGuiApplication::layoutDirection();
86 }
87 
88 /*!
89     Destroys the text option.
90 */
~QTextOption()91 QTextOption::~QTextOption()
92 {
93     delete d;
94 }
95 
96 /*!
97     \fn QTextOption::QTextOption(const QTextOption &other)
98 
99     Construct a copy of the \a other text option.
100 */
QTextOption(const QTextOption & o)101 QTextOption::QTextOption(const QTextOption &o)
102     : align(o.align),
103       wordWrap(o.wordWrap),
104       design(o.design),
105       direction(o.direction),
106       unused(o.unused),
107       unused2(o.unused2),
108       f(o.f),
109       tab(o.tab),
110       d(nullptr)
111 {
112     if (o.d)
113         d = new QTextOptionPrivate(*o.d);
114 }
115 
116 /*!
117     \fn QTextOption &QTextOption::operator=(const QTextOption &other)
118 
119     Returns \c true if the text option is the same as the \a other text option;
120     otherwise returns \c false.
121 */
operator =(const QTextOption & o)122 QTextOption &QTextOption::operator=(const QTextOption &o)
123 {
124     if (this == &o)
125         return *this;
126 
127     QTextOptionPrivate* dNew = nullptr;
128     if (o.d)
129         dNew = new QTextOptionPrivate(*o.d);
130     delete d;
131     d = dNew;
132 
133     align = o.align;
134     wordWrap = o.wordWrap;
135     design = o.design;
136     direction = o.direction;
137     unused = o.unused;
138     f = o.f;
139     tab = o.tab;
140     return *this;
141 }
142 
143 /*!
144     Sets the tab positions for the text layout to those specified by
145     \a tabStops.
146 
147     \sa tabArray(), setTabStopDistance(), setTabs()
148 */
setTabArray(const QList<qreal> & tabStops)149 void QTextOption::setTabArray(const QList<qreal> &tabStops)
150 {
151     if (!d)
152         d = new QTextOptionPrivate;
153     QList<QTextOption::Tab> tabs;
154     QTextOption::Tab tab;
155     tabs.reserve(tabStops.count());
156     for (qreal pos : tabStops) {
157         tab.position = pos;
158         tabs.append(tab);
159     }
160     d->tabStops = tabs;
161 }
162 
163 /*!
164     \since 4.4
165     Sets the tab positions for the text layout to those specified by
166     \a tabStops.
167 
168     \sa tabStop()
169 */
setTabs(const QList<QTextOption::Tab> & tabStops)170 void QTextOption::setTabs(const QList<QTextOption::Tab> &tabStops)
171 {
172     if (!d)
173         d = new QTextOptionPrivate;
174     d->tabStops = tabStops;
175 }
176 
177 /*!
178     Returns a list of tab positions defined for the text layout.
179 
180     \sa setTabArray(), tabStop()
181 */
tabArray() const182 QList<qreal> QTextOption::tabArray() const
183 {
184     QList<qreal> answer;
185     if (!d)
186         return answer;
187 
188     answer.reserve(d->tabStops.count());
189     QList<QTextOption::Tab>::ConstIterator iter = d->tabStops.constBegin();
190     while(iter != d->tabStops.constEnd()) {
191         answer.append( (*iter).position);
192         ++iter;
193     }
194     return answer;
195 }
196 
197 
tabs() const198 QList<QTextOption::Tab> QTextOption::tabs() const
199 {
200     if (!d)
201         return QList<QTextOption::Tab>();
202     return d->tabStops;
203 }
204 
205 /*!
206     \class QTextOption
207     \reentrant
208 
209     \brief The QTextOption class provides a description of general rich text
210     properties.
211     \inmodule QtGui
212 
213     \ingroup richtext-processing
214 
215     QTextOption is used to encapsulate common rich text properties in a single
216     object. It contains information about text alignment, layout direction,
217     word wrapping, and other standard properties associated with text rendering
218     and layout.
219 
220     \sa QTextEdit, QTextDocument, QTextCursor
221 */
222 
223 /*!
224     \enum QTextOption::WrapMode
225 
226     This enum describes how text is wrapped in a document.
227 
228     \value NoWrap       Text is not wrapped at all.
229     \value WordWrap     Text is wrapped at word boundaries.
230     \value ManualWrap   Same as QTextOption::NoWrap
231     \value WrapAnywhere Text can be wrapped at any point on a line, even if
232                         it occurs in the middle of a word.
233     \value WrapAtWordBoundaryOrAnywhere If possible, wrapping occurs at a word
234                         boundary; otherwise it will occur at the appropriate
235                         point on the line, even in the middle of a word.
236 */
237 
238 /*!
239   \fn void QTextOption::setUseDesignMetrics(bool enable)
240 
241     If \a enable is true then the layout will use design metrics;
242     otherwise it will use the metrics of the paint device (which is
243     the default behavior).
244 
245     \sa useDesignMetrics()
246 */
247 
248 /*!
249   \fn bool QTextOption::useDesignMetrics() const
250 
251     Returns \c true if the layout uses design rather than device metrics;
252     otherwise returns \c false.
253 
254     \sa setUseDesignMetrics()
255 */
256 
257 /*!
258   \fn Qt::Alignment QTextOption::alignment() const
259 
260   Returns the text alignment defined by the option.
261 
262   \sa setAlignment()
263 */
264 
265 /*!
266   \fn void QTextOption::setAlignment(Qt::Alignment alignment);
267 
268   Sets the option's text alignment to the specified \a alignment.
269 
270   \sa alignment()
271 */
272 
273 /*!
274   \fn Qt::LayoutDirection QTextOption::textDirection() const
275 
276   Returns the direction of the text layout defined by the option.
277 
278   \sa setTextDirection()
279 */
280 
281 /*!
282   \fn void QTextOption::setTextDirection(Qt::LayoutDirection direction)
283 
284   Sets the direction of the text layout defined by the option to the
285   given \a direction.
286 
287   \sa textDirection()
288 */
289 
290 /*!
291   \fn WrapMode QTextOption::wrapMode() const
292 
293   Returns the text wrap mode defined by the option.
294 
295   \sa setWrapMode()
296 */
297 
298 /*!
299   \fn void QTextOption::setWrapMode(WrapMode mode)
300 
301   Sets the option's text wrap mode to the given \a mode.
302 */
303 
304 /*!
305   \enum QTextOption::Flag
306 
307   \value IncludeTrailingSpaces When this option is set, QTextLine::naturalTextWidth() and naturalTextRect() will
308                                return a value that includes the width of trailing spaces in the text; otherwise
309                                this width is excluded.
310   \value ShowTabsAndSpaces Visualize spaces with little dots, and tabs with little arrows. Non-breaking spaces are
311             shown differently to breaking spaces.
312   \value ShowLineAndParagraphSeparators Visualize line and paragraph separators with appropriate symbol characters.
313   \value ShowDocumentTerminator Visualize the end of the document with a section sign. This enum value was added
314             in Qt 5.7.
315   \value AddSpaceForLineAndParagraphSeparators While determining the line-break positions take into account the
316             space added for drawing a separator character.
317   \value SuppressColors Suppress all color changes in the character formats (except the main selection).
318 */
319 
320 /*!
321   \fn Flags QTextOption::flags() const
322 
323   Returns the flags associated with the option.
324 
325   \sa setFlags()
326 */
327 
328 /*!
329   \fn void QTextOption::setFlags(Flags flags)
330 
331   Sets the flags associated with the option to the given \a flags.
332 
333   \sa flags()
334 */
335 
336 #if QT_DEPRECATED_SINCE(5, 10)
337 /*!
338   \fn qreal QTextOption::tabStop() const
339   \deprecated in Qt 5.10. Use tabStopDistance() instead.
340 
341   Returns the distance in device units between tab stops.
342   Convenient function for the above method
343 
344   \sa setTabStopDistance(), tabArray(), setTabs(), tabs()
345 */
346 
347 /*!
348   \fn void QTextOption::setTabStop(qreal tabStop)
349   \deprecated in Qt 5.10. Use setTabStopDistance() instead.
350 
351   Sets the default distance in device units between tab stops to the value specified
352   by \a tabStop.
353 
354   \sa tabStopDistance(), setTabArray(), setTabs(), tabs()
355 */
356 #endif
357 
358 /*!
359   \fn qreal QTextOption::tabStopDistance() const
360   \since 5.10
361 
362   Returns the distance in device units between tab stops.
363 
364   \sa setTabStopDistance(), tabArray(), setTabs(), tabs()
365 */
366 
367 /*!
368   \fn void QTextOption::setTabStopDistance(qreal tabStopDistance)
369   \since 5.10
370 
371   Sets the default distance in device units between tab stops to the value specified
372   by \a tabStopDistance.
373 
374   \sa tabStopDistance(), setTabArray(), setTabs(), tabs()
375 */
376 
377 /*!
378     \enum QTextOption::TabType
379     \since 4.4
380 
381     This enum holds the different types of tabulator
382 
383     \value LeftTab      A left-tab
384     \value RightTab     A right-tab
385     \value CenterTab    A centered-tab
386     \value DelimiterTab A tab stopping at a certain delimiter-character
387 */
388 
389 /*!
390     \class QTextOption::Tab
391     \since 4.4
392     \inmodule QtGui
393     Each tab definition is represented by this struct.
394 */
395 
396 /*!
397     \variable Tab::position
398     Distance from the start of the paragraph.
399     The position of a tab is from the start of the paragraph which implies that when
400     the alignment of the paragraph is set to centered, the tab is interpreted to be
401     moved the same distance as the left ege of the paragraph does.
402     In case the paragraph is set to have a layoutDirection() RightToLeft the position
403     is interpreted to be from the right side of the paragraph with higher numbers moving
404     the tab to the left.
405 */
406 
407 /*!
408     \variable QTextOption::Tab::type
409     Determine which type is used.
410     In a paragraph that has layoutDirection() RightToLeft the type LeftTab will
411     be interpreted to be a RightTab and vice versa.
412 */
413 
414 /*!
415     \variable QTextOption::Tab::delimiter
416     If type is DelimitorTab; tab until this char is found in the text.
417 */
418 
419 /*!
420     \fn QTextOption::Tab::Tab()
421     Creates a default left tab with position 80.
422 */
423 
424 /*!
425     \fn QTextOption::Tab::Tab(qreal pos, TabType tabType, QChar delim = QChar())
426 
427     Creates a tab with the given position, tab type, and delimiter
428     (\a pos, \a tabType, \a delim).
429 
430     \note \a delim is only used when \a tabType is DelimiterTab.
431 
432     \since 4.7
433 */
434 
435 /*!
436     \fn bool QTextOption::Tab::operator==(const QTextOption::Tab &other) const
437 
438     Returns \c true if tab \a other is equal to this tab;
439     otherwise returns \c false.
440 */
441 
442 /*!
443     \fn bool QTextOption::Tab::operator!=(const QTextOption::Tab &other) const
444 
445     Returns \c true if tab \a other is not equal to this tab;
446     otherwise returns \c false.
447 */
448 
449 /*!
450   \since 4.4
451   \fn QList<QTextOption::Tab> QTextOption::tabs() const
452   Returns a list of tab positions defined for the text layout.
453 
454   \sa tabStopDistance(), setTabs(), setTabStop()
455 */
456 
457 
458 QT_END_NAMESPACE
459