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#ifndef QQUICKTEXTUTIL_P_H
5#define QQUICKTEXTUTIL_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtQml/qqml.h>
19#include <QtQml/qqmlincubator.h>
20#include <QtQuick/qquickitem.h>
21#include <QtQuick/qquickwindow.h>
22#include <QtCore/private/qglobal_p.h>
23
24QT_BEGIN_NAMESPACE
25
26class QQuickTextUtil : public QObject // For the benefit of translations.
27{
28 Q_OBJECT
29public:
30 template <typename Private> static void setCursorDelegate(Private *d, QQmlComponent *delegate);
31 template <typename Private> static void createCursor(Private *d);
32
33 template <typename T> static typename T::RenderType textRenderType();
34
35 static qreal alignedX(qreal textWidth, qreal itemWidth, int alignment);
36 static qreal alignedY(qreal textHeight, qreal itemHeight, int alignment);
37
38private:
39 static QQuickItem *createCursor(
40 QQmlComponent *component,
41 QQuickItem *parent,
42 const QRectF &cursorRectangle,
43 const char *className);
44
45};
46
47template <typename Private>
48void QQuickTextUtil::setCursorDelegate(Private *d, QQmlComponent *delegate)
49{
50 if (d->cursorComponent == delegate)
51 return;
52
53 typename Private::Public *parent = d->q_func();
54
55 if (d->cursorComponent) {
56 disconnect(d->cursorComponent, SIGNAL(statusChanged(QQmlComponent::Status)),
57 parent, SLOT(createCursor()));
58 }
59
60 delete d->cursorItem;
61 d->cursorItem = 0;
62 d->cursorPending = true;
63
64 d->cursorComponent = delegate;
65
66 if (parent->isCursorVisible() && parent->isComponentComplete())
67 createCursor(d);
68
69 Q_EMIT parent->cursorDelegateChanged();
70}
71
72template <typename Private>
73void QQuickTextUtil::createCursor(Private *d)
74{
75 if (!d->cursorPending)
76 return;
77
78 d->cursorPending = false;
79
80 typename Private::Public *parent = d->q_func();
81 if (d->cursorComponent) {
82 d->cursorItem = createCursor(
83 d->cursorComponent,
84 parent,
85 parent->cursorRectangle(),
86 Private::Public::staticMetaObject.className());
87 }
88
89 d->setNativeCursorEnabled(!d->cursorItem);
90 d->updateType = Private::UpdatePaintNode;
91 parent->update();
92}
93
94template <typename T>
95typename T::RenderType QQuickTextUtil::textRenderType()
96{
97 switch (QQuickWindow::textRenderType()) {
98 case QQuickWindow::QtTextRendering:
99 return T::QtRendering;
100 case QQuickWindow::NativeTextRendering:
101 return T::NativeRendering;
102 }
103
104 Q_UNREACHABLE_RETURN(T::QtRendering);
105}
106
107QT_END_NAMESPACE
108
109#endif
110

source code of qtdeclarative/src/quick/items/qquicktextutil_p.h