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 QQUICKTEXTUTIL_P_H |
41 | #define QQUICKTEXTUTIL_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 <QtQml/qqml.h> |
55 | #include <QtQml/qqmlincubator.h> |
56 | #include <QtQuick/qquickitem.h> |
57 | #include <QtQuick/qquickwindow.h> |
58 | |
59 | QT_BEGIN_NAMESPACE |
60 | |
61 | class QQuickTextUtil : public QObject // For the benefit of translations. |
62 | { |
63 | Q_OBJECT |
64 | public: |
65 | template <typename Private> static void setCursorDelegate(Private *d, QQmlComponent *delegate); |
66 | template <typename Private> static void createCursor(Private *d); |
67 | |
68 | template <typename T> static typename T::RenderType textRenderType(); |
69 | |
70 | static qreal alignedX(qreal textWidth, qreal itemWidth, int alignment); |
71 | static qreal alignedY(qreal textHeight, qreal itemHeight, int alignment); |
72 | |
73 | private: |
74 | static QQuickItem *createCursor( |
75 | QQmlComponent *component, |
76 | QQuickItem *parent, |
77 | const QRectF &cursorRectangle, |
78 | const char *className); |
79 | |
80 | }; |
81 | |
82 | template <typename Private> |
83 | void QQuickTextUtil::setCursorDelegate(Private *d, QQmlComponent *delegate) |
84 | { |
85 | if (d->cursorComponent == delegate) |
86 | return; |
87 | |
88 | typename Private::Public *parent = d->q_func(); |
89 | |
90 | if (d->cursorComponent) { |
91 | disconnect(d->cursorComponent, SIGNAL(statusChanged(QQmlComponent::Status)), |
92 | parent, SLOT(createCursor())); |
93 | } |
94 | |
95 | delete d->cursorItem; |
96 | d->cursorItem = 0; |
97 | d->cursorPending = true; |
98 | |
99 | d->cursorComponent = delegate; |
100 | |
101 | if (parent->isCursorVisible() && parent->isComponentComplete()) |
102 | createCursor(d); |
103 | |
104 | Q_EMIT parent->cursorDelegateChanged(); |
105 | } |
106 | |
107 | template <typename Private> |
108 | void QQuickTextUtil::createCursor(Private *d) |
109 | { |
110 | if (!d->cursorPending) |
111 | return; |
112 | |
113 | d->cursorPending = false; |
114 | |
115 | typename Private::Public *parent = d->q_func(); |
116 | if (d->cursorComponent) { |
117 | d->cursorItem = createCursor( |
118 | d->cursorComponent, |
119 | parent, |
120 | parent->cursorRectangle(), |
121 | Private::Public::staticMetaObject.className()); |
122 | } |
123 | |
124 | d->setNativeCursorEnabled(!d->cursorItem); |
125 | d->updateType = Private::UpdatePaintNode; |
126 | parent->update(); |
127 | } |
128 | |
129 | template <typename T> |
130 | typename T::RenderType QQuickTextUtil::textRenderType() |
131 | { |
132 | switch (QQuickWindow::textRenderType()) { |
133 | case QQuickWindow::QtTextRendering: |
134 | return T::QtRendering; |
135 | case QQuickWindow::NativeTextRendering: |
136 | return T::NativeRendering; |
137 | } |
138 | |
139 | Q_UNREACHABLE(); |
140 | return T::QtRendering; |
141 | } |
142 | |
143 | QT_END_NAMESPACE |
144 | |
145 | #endif |
146 | |