source: trunk/src/gui/text/qtextoption.cpp@ 352

Last change on this file since 352 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 10.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qtextoption.h"
43#include "qapplication.h"
44#include "qlist.h"
45
46QT_BEGIN_NAMESPACE
47
48struct QTextOptionPrivate
49{
50 QList<QTextOption::Tab> tabStops;
51};
52
53/*!
54 Constructs a text option with default properties for text.
55*/
56QTextOption::QTextOption()
57 : align(Qt::AlignLeft),
58 wordWrap(QTextOption::WordWrap),
59 design(false),
60 unused(0),
61 f(0),
62 tab(-1),
63 d(0)
64{
65 direction = QApplication::layoutDirection();
66}
67
68/*!
69 Constructs a text option with the given \a alignment for text.
70*/
71QTextOption::QTextOption(Qt::Alignment alignment)
72 : align(alignment),
73 wordWrap(QTextOption::WordWrap),
74 design(false),
75 unused(0),
76 f(0),
77 tab(-1),
78 d(0)
79{
80 direction = QApplication::layoutDirection();
81}
82
83/*!
84 Destroys the text option.
85*/
86QTextOption::~QTextOption()
87{
88 delete d;
89}
90
91/*!
92 \fn QTextOption::QTextOption(const QTextOption &other)
93
94 Construct a copy of the \a other text option.
95*/
96QTextOption::QTextOption(const QTextOption &o)
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 d(0)
105{
106 if (o.d)
107 d = new QTextOptionPrivate(*o.d);
108}
109
110/*!
111 \fn QTextOption &QTextOption::operator=(const QTextOption &other)
112
113 Returns true if the text option is the same as the \a other text option;
114 otherwise returns false.
115*/
116QTextOption &QTextOption::operator=(const QTextOption &o)
117{
118 if (this == &o)
119 return *this;
120 delete d; d = 0;
121 align = o.align;
122 wordWrap = o.wordWrap;
123 design = o.design;
124 direction = o.direction;
125 unused = o.unused;
126 f = o.f;
127 tab = o.tab;
128 if (o.d)
129 d = new QTextOptionPrivate(*o.d);
130 return *this;
131}
132
133/*!
134 Sets the tab positions for the text layout to those specified by
135 \a tabStops.
136
137 \sa tabArray(), setTabStop(), setTabs()
138*/
139void QTextOption::setTabArray(QList<qreal> tabStops)
140{
141 if (!d)
142 d = new QTextOptionPrivate;
143 QList<QTextOption::Tab> tabs;
144 QTextOption::Tab tab;
145 foreach (qreal pos, tabStops) {
146 tab.position = pos;
147 tabs.append(tab);
148 }
149 d->tabStops = tabs;
150}
151
152/*!
153 \since 4.4
154 Sets the tab positions for the text layout to those specified by
155 \a tabStops.
156
157 \sa tabStops()
158*/
159void QTextOption::setTabs(QList<QTextOption::Tab> tabStops)
160{
161 if (!d)
162 d = new QTextOptionPrivate;
163 d->tabStops = tabStops;
164}
165
166/*!
167 Returns a list of tab positions defined for the text layout.
168
169 \sa setTabArray(), tabStop()
170*/
171QList<qreal> QTextOption::tabArray() const
172{
173 if (!d)
174 return QList<qreal>();
175
176 QList<qreal> answer;
177 QList<QTextOption::Tab>::ConstIterator iter = d->tabStops.constBegin();
178 while(iter != d->tabStops.constEnd()) {
179 answer.append( (*iter).position);
180 ++iter;
181 }
182 return answer;
183}
184
185
186QList<QTextOption::Tab> QTextOption::tabs() const
187{
188 if (!d)
189 return QList<QTextOption::Tab>();
190 return d->tabStops;
191}
192
193/*!
194 \class QTextOption
195 \reentrant
196
197 \brief The QTextOption class provides a description of general rich text
198 properties.
199
200 \ingroup text
201
202 QTextOption is used to encapsulate common rich text properties in a single
203 object. It contains information about text alignment, layout direction,
204 word wrapping, and other standard properties associated with text rendering
205 and layout.
206
207 \sa QTextEdit, QTextDocument, QTextCursor
208*/
209
210/*!
211 \enum QTextOption::WrapMode
212
213 This enum describes how text is wrapped in a document.
214
215 \value NoWrap Text is not wrapped at all.
216 \value WordWrap Text is wrapped at word boundaries.
217 \value ManualWrap Same as QTextOption::NoWrap
218 \value WrapAnywhere Text can be wrapped at any point on a line, even if
219 it occurs in the middle of a word.
220 \value WrapAtWordBoundaryOrAnywhere If possible, wrapping occurs at a word
221 boundary; otherwise it will occur at the appropriate
222 point on the line, even in the middle of a word.
223*/
224
225/*!
226 \fn void QTextOption::setUseDesignMetrics(bool enable)
227
228 If \a enable is true then the layout will use design metrics;
229 otherwise it will use the metrics of the paint device (which is
230 the default behavior).
231
232 \sa useDesignMetrics()