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 QtQuick 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 | #ifndef QQUICKTEXT_P_H |
41 | #define QQUICKTEXT_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include "qquickimplicitsizeitem_p.h" |
55 | #include <private/qtquickglobal_p.h> |
56 | #include <QtGui/qtextoption.h> |
57 | |
58 | QT_BEGIN_NAMESPACE |
59 | |
60 | class QQuickTextPrivate; |
61 | class QQuickTextLine; |
62 | class Q_QUICK_PRIVATE_EXPORT QQuickText : public QQuickImplicitSizeItem |
63 | { |
64 | Q_OBJECT |
65 | |
66 | Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) |
67 | Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) |
68 | Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) |
69 | Q_PROPERTY(QColor linkColor READ linkColor WRITE setLinkColor NOTIFY linkColorChanged) |
70 | Q_PROPERTY(TextStyle style READ style WRITE setStyle NOTIFY styleChanged) |
71 | Q_PROPERTY(QColor styleColor READ styleColor WRITE setStyleColor NOTIFY styleColorChanged) |
72 | Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged) |
73 | Q_PROPERTY(HAlignment effectiveHorizontalAlignment READ effectiveHAlign NOTIFY effectiveHorizontalAlignmentChanged) |
74 | Q_PROPERTY(VAlignment verticalAlignment READ vAlign WRITE setVAlign NOTIFY verticalAlignmentChanged) |
75 | Q_PROPERTY(WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged) |
76 | Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged) |
77 | Q_PROPERTY(bool truncated READ truncated NOTIFY truncatedChanged) |
78 | Q_PROPERTY(int maximumLineCount READ maximumLineCount WRITE setMaximumLineCount NOTIFY maximumLineCountChanged RESET resetMaximumLineCount) |
79 | |
80 | Q_PROPERTY(TextFormat textFormat READ textFormat WRITE setTextFormat NOTIFY textFormatChanged) |
81 | Q_PROPERTY(TextElideMode elide READ elideMode WRITE setElideMode NOTIFY elideModeChanged) //### elideMode? |
82 | Q_PROPERTY(qreal contentWidth READ contentWidth NOTIFY contentWidthChanged) |
83 | Q_PROPERTY(qreal contentHeight READ contentHeight NOTIFY contentHeightChanged) |
84 | Q_PROPERTY(qreal paintedWidth READ contentWidth NOTIFY contentWidthChanged) // Compatibility |
85 | Q_PROPERTY(qreal paintedHeight READ contentHeight NOTIFY contentHeightChanged) |
86 | Q_PROPERTY(qreal lineHeight READ lineHeight WRITE setLineHeight NOTIFY lineHeightChanged) |
87 | Q_PROPERTY(LineHeightMode lineHeightMode READ lineHeightMode WRITE setLineHeightMode NOTIFY lineHeightModeChanged) |
88 | Q_PROPERTY(QUrl baseUrl READ baseUrl WRITE setBaseUrl RESET resetBaseUrl NOTIFY baseUrlChanged) |
89 | Q_PROPERTY(int minimumPixelSize READ minimumPixelSize WRITE setMinimumPixelSize NOTIFY minimumPixelSizeChanged) |
90 | Q_PROPERTY(int minimumPointSize READ minimumPointSize WRITE setMinimumPointSize NOTIFY minimumPointSizeChanged) |
91 | Q_PROPERTY(FontSizeMode fontSizeMode READ fontSizeMode WRITE setFontSizeMode NOTIFY fontSizeModeChanged) |
92 | Q_PROPERTY(RenderType renderType READ renderType WRITE setRenderType NOTIFY renderTypeChanged) |
93 | Q_PROPERTY(QString hoveredLink READ hoveredLink NOTIFY linkHovered REVISION 2) |
94 | |
95 | Q_PROPERTY(qreal padding READ padding WRITE setPadding RESET resetPadding NOTIFY paddingChanged REVISION 6) |
96 | Q_PROPERTY(qreal topPadding READ topPadding WRITE setTopPadding RESET resetTopPadding NOTIFY topPaddingChanged REVISION 6) |
97 | Q_PROPERTY(qreal leftPadding READ leftPadding WRITE setLeftPadding RESET resetLeftPadding NOTIFY leftPaddingChanged REVISION 6) |
98 | Q_PROPERTY(qreal rightPadding READ rightPadding WRITE setRightPadding RESET resetRightPadding NOTIFY rightPaddingChanged REVISION 6) |
99 | Q_PROPERTY(qreal bottomPadding READ bottomPadding WRITE setBottomPadding RESET resetBottomPadding NOTIFY bottomPaddingChanged REVISION 6) |
100 | |
101 | Q_PROPERTY(QJSValue fontInfo READ fontInfo NOTIFY fontInfoChanged REVISION 9) |
102 | Q_PROPERTY(QSizeF advance READ advance NOTIFY contentSizeChanged REVISION 10) |
103 | |
104 | public: |
105 | QQuickText(QQuickItem *parent=nullptr); |
106 | ~QQuickText() override; |
107 | |
108 | enum HAlignment { AlignLeft = Qt::AlignLeft, |
109 | AlignRight = Qt::AlignRight, |
110 | AlignHCenter = Qt::AlignHCenter, |
111 | AlignJustify = Qt::AlignJustify }; |
112 | Q_ENUM(HAlignment) |
113 | enum VAlignment { AlignTop = Qt::AlignTop, |
114 | AlignBottom = Qt::AlignBottom, |
115 | AlignVCenter = Qt::AlignVCenter }; |
116 | Q_ENUM(VAlignment) |
117 | enum TextStyle { Normal, |
118 | Outline, |
119 | Raised, |
120 | Sunken }; |
121 | Q_ENUM(TextStyle) |
122 | enum TextFormat { PlainText = Qt::PlainText, |
123 | RichText = Qt::RichText, |
124 | MarkdownText = Qt::MarkdownText, |
125 | AutoText = Qt::AutoText, |
126 | StyledText = 4 }; |
127 | Q_ENUM(TextFormat) |
128 | enum TextElideMode { ElideLeft = Qt::ElideLeft, |
129 | ElideRight = Qt::ElideRight, |
130 | ElideMiddle = Qt::ElideMiddle, |
131 | ElideNone = Qt::ElideNone }; |
132 | Q_ENUM(TextElideMode) |
133 | |
134 | enum WrapMode { NoWrap = QTextOption::NoWrap, |
135 | WordWrap = QTextOption::WordWrap, |
136 | WrapAnywhere = QTextOption::WrapAnywhere, |
137 | WrapAtWordBoundaryOrAnywhere = QTextOption::WrapAtWordBoundaryOrAnywhere, // COMPAT |
138 | Wrap = QTextOption::WrapAtWordBoundaryOrAnywhere |
139 | }; |
140 | Q_ENUM(WrapMode) |
141 | |
142 | enum RenderType { QtRendering, |
143 | NativeRendering |
144 | }; |
145 | Q_ENUM(RenderType) |
146 | |
147 | enum LineHeightMode { ProportionalHeight, FixedHeight }; |
148 | Q_ENUM(LineHeightMode) |
149 | |
150 | enum FontSizeMode { FixedSize = 0x0, HorizontalFit = 0x01, VerticalFit = 0x02, |
151 | Fit = HorizontalFit | VerticalFit }; |
152 | Q_ENUM(FontSizeMode) |
153 | |
154 | QString text() const; |
155 | void setText(const QString &); |
156 | |
157 | QFont font() const; |
158 | void setFont(const QFont &font); |
159 | |
160 | QColor color() const; |
161 | void setColor(const QColor &c); |
162 | |
163 | QColor linkColor() const; |
164 | void setLinkColor(const QColor &color); |
165 | |
166 | TextStyle style() const; |
167 | void setStyle(TextStyle style); |
168 | |
169 | QColor styleColor() const; |
170 | void setStyleColor(const QColor &c); |
171 | |
172 | HAlignment hAlign() const; |
173 | void setHAlign(HAlignment align); |
174 | void resetHAlign(); |
175 | HAlignment effectiveHAlign() const; |
176 | |
177 | VAlignment vAlign() const; |
178 | void setVAlign(VAlignment align); |
179 | |
180 | WrapMode wrapMode() const; |
181 | void setWrapMode(WrapMode w); |
182 | |
183 | int lineCount() const; |
184 | bool truncated() const; |
185 | |
186 | int maximumLineCount() const; |
187 | void setMaximumLineCount(int lines); |
188 | void resetMaximumLineCount(); |
189 | |
190 | TextFormat textFormat() const; |
191 | void setTextFormat(TextFormat format); |
192 | |
193 | TextElideMode elideMode() const; |
194 | void setElideMode(TextElideMode); |
195 | |
196 | qreal lineHeight() const; |
197 | void setLineHeight(qreal lineHeight); |
198 | |
199 | LineHeightMode lineHeightMode() const; |
200 | void setLineHeightMode(LineHeightMode); |
201 | |
202 | |
203 | QUrl baseUrl() const; |
204 | void setBaseUrl(const QUrl &url); |
205 | void resetBaseUrl(); |
206 | |
207 | int minimumPixelSize() const; |
208 | void setMinimumPixelSize(int size); |
209 | |
210 | int minimumPointSize() const; |
211 | void setMinimumPointSize(int size); |
212 | |
213 | FontSizeMode fontSizeMode() const; |
214 | void setFontSizeMode(FontSizeMode mode); |
215 | |
216 | void componentComplete() override; |
217 | |
218 | int resourcesLoading() const; // mainly for testing |
219 | |
220 | qreal contentWidth() const; |
221 | qreal contentHeight() const; |
222 | |
223 | QRectF boundingRect() const override; |
224 | QRectF clipRect() const override; |
225 | Q_INVOKABLE void doLayout(); // ### Qt 6: remove |
226 | Q_REVISION(9) Q_INVOKABLE void forceLayout(); |
227 | |
228 | RenderType renderType() const; |
229 | void setRenderType(RenderType renderType); |
230 | |
231 | QString hoveredLink() const; |
232 | |
233 | Q_REVISION(3) Q_INVOKABLE QString linkAt(qreal x, qreal y) const; |
234 | |
235 | qreal padding() const; |
236 | void setPadding(qreal padding); |
237 | void resetPadding(); |
238 | |
239 | qreal topPadding() const; |
240 | void setTopPadding(qreal padding); |
241 | void resetTopPadding(); |
242 | |
243 | qreal leftPadding() const; |
244 | void setLeftPadding(qreal padding); |
245 | void resetLeftPadding(); |
246 | |
247 | qreal rightPadding() const; |
248 | void setRightPadding(qreal padding); |
249 | void resetRightPadding(); |
250 | |
251 | qreal bottomPadding() const; |
252 | void setBottomPadding(qreal padding); |
253 | void resetBottomPadding(); |
254 | |
255 | QJSValue fontInfo() const; |
256 | QSizeF advance() const; |
257 | |
258 | Q_SIGNALS: |
259 | void textChanged(const QString &text); |
260 | void linkActivated(const QString &link); |
261 | Q_REVISION(2) void linkHovered(const QString &link); |
262 | void fontChanged(const QFont &font); |
263 | void colorChanged(); |
264 | void linkColorChanged(); |
265 | void styleChanged(QQuickText::TextStyle style); |
266 | void styleColorChanged(); |
267 | void horizontalAlignmentChanged(QQuickText::HAlignment alignment); |
268 | void verticalAlignmentChanged(QQuickText::VAlignment alignment); |
269 | void wrapModeChanged(); |
270 | void lineCountChanged(); |
271 | void truncatedChanged(); |
272 | void maximumLineCountChanged(); |
273 | void textFormatChanged(QQuickText::TextFormat textFormat); |
274 | void elideModeChanged(QQuickText::TextElideMode mode); |
275 | void contentSizeChanged(); |
276 | // The next two signals should be marked as Q_REVISION(12). See QTBUG-71247 |
277 | void contentWidthChanged(qreal contentWidth); |
278 | void contentHeightChanged(qreal contentHeight); |
279 | |
280 | void lineHeightChanged(qreal lineHeight); |
281 | void lineHeightModeChanged(LineHeightMode mode); |
282 | void fontSizeModeChanged(); |
283 | void minimumPixelSizeChanged(); |
284 | void minimumPointSizeChanged(); |
285 | void effectiveHorizontalAlignmentChanged(); |
286 | void lineLaidOut(QQuickTextLine *line); |
287 | void baseUrlChanged(); |
288 | void renderTypeChanged(); |
289 | Q_REVISION(6) void paddingChanged(); |
290 | Q_REVISION(6) void topPaddingChanged(); |
291 | Q_REVISION(6) void leftPaddingChanged(); |
292 | Q_REVISION(6) void rightPaddingChanged(); |
293 | Q_REVISION(6) void bottomPaddingChanged(); |
294 | Q_REVISION(9) void fontInfoChanged(); |
295 | |
296 | protected: |
297 | QQuickText(QQuickTextPrivate &dd, QQuickItem *parent = nullptr); |
298 | |
299 | void mousePressEvent(QMouseEvent *event) override; |
300 | void mouseReleaseEvent(QMouseEvent *event) override; |
301 | void itemChange(ItemChange change, const ItemChangeData &value) override; |
302 | void geometryChanged(const QRectF &newGeometry, |
303 | const QRectF &oldGeometry) override; |
304 | QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override; |
305 | |
306 | void updatePolish() override; |
307 | |
308 | void hoverEnterEvent(QHoverEvent *event) override; |
309 | void hoverMoveEvent(QHoverEvent *event) override; |
310 | void hoverLeaveEvent(QHoverEvent *event) override; |
311 | void invalidateFontCaches(); |
312 | |
313 | private Q_SLOTS: |
314 | void q_updateLayout(); |
315 | void triggerPreprocess(); |
316 | void imageDownloadFinished(); |
317 | |
318 | private: |
319 | Q_DISABLE_COPY(QQuickText) |
320 | Q_DECLARE_PRIVATE(QQuickText) |
321 | }; |
322 | |
323 | class QTextLine; |
324 | class QQuickTextLine : public QObject |
325 | { |
326 | Q_OBJECT |
327 | Q_PROPERTY(int number READ number) |
328 | Q_PROPERTY(qreal width READ width WRITE setWidth) |
329 | Q_PROPERTY(qreal height READ height WRITE setHeight) |
330 | Q_PROPERTY(qreal x READ x WRITE setX) |
331 | Q_PROPERTY(qreal y READ y WRITE setY) |
332 | |
333 | public: |
334 | QQuickTextLine(); |
335 | |
336 | void setLine(QTextLine* line); |
337 | void setLineOffset(int offset); |
338 | int number() const; |
339 | |
340 | qreal width() const; |
341 | void setWidth(qreal width); |
342 | |
343 | qreal height() const; |
344 | void setHeight(qreal height); |
345 | |
346 | qreal x() const; |
347 | void setX(qreal x); |
348 | |
349 | qreal y() const; |
350 | void setY(qreal y); |
351 | |
352 | private: |
353 | QTextLine *m_line; |
354 | qreal m_height; |
355 | int m_lineOffset; |
356 | }; |
357 | |
358 | QT_END_NAMESPACE |
359 | |
360 | QML_DECLARE_TYPE(QQuickText) |
361 | QML_DECLARE_TYPE(QQuickTextLine) |
362 | |
363 | #endif // QQUICKTEXT_P_H |
364 | |