| 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 | QTextOption::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 | */ |
| 34 | QTextOption::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 | */ |
| 49 | QTextOption::~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 | */ |
| 59 | QTextOption::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 | */ |
| 79 | QTextOption &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 | */ |
| 106 | void 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 | */ |
| 127 | void 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 | */ |
| 139 | QList<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 | |
| 155 | QList<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 [since 5.7] ShowDocumentTerminator Visualize the end of the document with a section sign. |
| 271 | \value AddSpaceForLineAndParagraphSeparators While determining the line-break positions take into account the |
| 272 | space added for drawing a separator character. |
| 273 | \value SuppressColors Suppress all color changes in the character formats (except the main selection). |
| 274 | */ |
| 275 | |
| 276 | /*! |
| 277 | \fn Flags QTextOption::flags() const |
| 278 | |
| 279 | Returns the flags associated with the option. |
| 280 | |
| 281 | \sa setFlags() |
| 282 | */ |
| 283 | |
| 284 | /*! |
| 285 | \fn void QTextOption::setFlags(Flags flags) |
| 286 | |
| 287 | Sets the flags associated with the option to the given \a flags. |
| 288 | |
| 289 | \sa flags() |
| 290 | */ |
| 291 | |
| 292 | /*! |
| 293 | \fn qreal QTextOption::tabStopDistance() const |
| 294 | \since 5.10 |
| 295 | |
| 296 | Returns the distance in device units between tab stops. |
| 297 | |
| 298 | \sa setTabStopDistance(), tabArray(), setTabs(), tabs() |
| 299 | */ |
| 300 | |
| 301 | /*! |
| 302 | \fn void QTextOption::setTabStopDistance(qreal tabStopDistance) |
| 303 | \since 5.10 |
| 304 | |
| 305 | Sets the default distance in device units between tab stops to the value specified |
| 306 | by \a tabStopDistance. |
| 307 | |
| 308 | \sa tabStopDistance(), setTabArray(), setTabs(), tabs() |
| 309 | */ |
| 310 | |
| 311 | /*! |
| 312 | \enum QTextOption::TabType |
| 313 | \since 4.4 |
| 314 | |
| 315 | This enum holds the different types of tabulator |
| 316 | |
| 317 | \value LeftTab A left-tab |
| 318 | \value RightTab A right-tab |
| 319 | \value CenterTab A centered-tab |
| 320 | \value DelimiterTab A tab stopping at a certain delimiter-character |
| 321 | */ |
| 322 | |
| 323 | /*! |
| 324 | \class QTextOption::Tab |
| 325 | \since 4.4 |
| 326 | \inmodule QtGui |
| 327 | Each tab definition is represented by this struct. |
| 328 | */ |
| 329 | |
| 330 | /*! |
| 331 | \variable QTextOption::Tab::position |
| 332 | Distance from the start of the paragraph. |
| 333 | The position of a tab is from the start of the paragraph which implies that when |
| 334 | the alignment of the paragraph is set to centered, the tab is interpreted to be |
| 335 | moved the same distance as the left edge of the paragraph does. |
| 336 | In case the paragraph is set to have a layoutDirection() RightToLeft the position |
| 337 | is interpreted to be from the right side of the paragraph with higher numbers moving |
| 338 | the tab to the left. |
| 339 | */ |
| 340 | |
| 341 | /*! |
| 342 | \variable QTextOption::Tab::type |
| 343 | Determine which type is used. |
| 344 | In a paragraph that has layoutDirection() RightToLeft the type LeftTab will |
| 345 | be interpreted to be a RightTab and vice versa. |
| 346 | */ |
| 347 | |
| 348 | /*! |
| 349 | \variable QTextOption::Tab::delimiter |
| 350 | If type is DelimitorTab; tab until this char is found in the text. |
| 351 | */ |
| 352 | |
| 353 | /*! |
| 354 | \fn QTextOption::Tab::Tab() |
| 355 | Creates a default left tab with position 80. |
| 356 | */ |
| 357 | |
| 358 | /*! |
| 359 | \fn QTextOption::Tab::Tab(qreal pos, TabType tabType, QChar delim = QChar()) |
| 360 | |
| 361 | Creates a tab with the given position, tab type, and delimiter |
| 362 | (\a pos, \a tabType, \a delim). |
| 363 | |
| 364 | \note \a delim is only used when \a tabType is DelimiterTab. |
| 365 | |
| 366 | \since 4.7 |
| 367 | */ |
| 368 | |
| 369 | /*! |
| 370 | \fn bool QTextOption::Tab::operator==(const QTextOption::Tab &other) const |
| 371 | |
| 372 | Returns \c true if tab \a other is equal to this tab; |
| 373 | otherwise returns \c false. |
| 374 | */ |
| 375 | |
| 376 | /*! |
| 377 | \fn bool QTextOption::Tab::operator!=(const QTextOption::Tab &other) const |
| 378 | |
| 379 | Returns \c true if tab \a other is not equal to this tab; |
| 380 | otherwise returns \c false. |
| 381 | */ |
| 382 | |
| 383 | /*! |
| 384 | \since 4.4 |
| 385 | \fn QList<QTextOption::Tab> QTextOption::tabs() const |
| 386 | Returns a list of tab positions defined for the text layout. |
| 387 | |
| 388 | \sa tabStopDistance(), setTabs(), setTabStopDistance() |
| 389 | */ |
| 390 | |
| 391 | |
| 392 | QT_END_NAMESPACE |
| 393 | |