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 "qquicktextutil_p.h" |
5 | |
6 | #include <QtQml/qqmlinfo.h> |
7 | |
8 | #include <private/qqmlglobal_p.h> |
9 | #include <private/qquickitem_p.h> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | QQuickItem *QQuickTextUtil::createCursor( |
14 | QQmlComponent *component, QQuickItem *parent, const QRectF &rectangle, const char *className) |
15 | { |
16 | QQuickItem *item = nullptr; |
17 | if (component->isReady()) { |
18 | QQmlContext *creationContext = component->creationContext(); |
19 | |
20 | if (QObject *object = component->beginCreate(creationContext |
21 | ? creationContext |
22 | : qmlContext(parent))) { |
23 | if ((item = qobject_cast<QQuickItem *>(o: object))) { |
24 | QQml_setParent_noEvent(object: item, parent); |
25 | item->setParentItem(parent); |
26 | item->setPosition(rectangle.topLeft()); |
27 | item->setHeight(rectangle.height()); |
28 | } else { |
29 | qmlWarning(me: parent) << tr(s: "%1 does not support loading non-visual cursor delegates." ) |
30 | .arg(a: QString::fromUtf8(utf8: className)); |
31 | } |
32 | component->completeCreate(); |
33 | if (parent->clip()) |
34 | QQuickItemPrivate::get(item: parent)->dirty(QQuickItemPrivate::Size); |
35 | return item; |
36 | } |
37 | } else if (component->isLoading()) { |
38 | QObject::connect(sender: component, SIGNAL(statusChanged(QQmlComponent::Status)), |
39 | receiver: parent, SLOT(createCursor()), Qt::UniqueConnection); |
40 | return item; |
41 | } |
42 | qmlWarning(me: parent, errors: component->errors()) << tr(s: "Could not load cursor delegate" ); |
43 | return item; |
44 | } |
45 | |
46 | qreal QQuickTextUtil::alignedX(const qreal textWidth, const qreal itemWidth, int alignment) |
47 | { |
48 | qreal x = 0; |
49 | switch (alignment) { |
50 | case Qt::AlignLeft: |
51 | case Qt::AlignJustify: |
52 | break; |
53 | case Qt::AlignRight: |
54 | x = itemWidth - textWidth; |
55 | break; |
56 | case Qt::AlignHCenter: |
57 | x = (itemWidth - textWidth) / 2; |
58 | break; |
59 | } |
60 | return x; |
61 | } |
62 | |
63 | qreal QQuickTextUtil::alignedY(const qreal textHeight, const qreal itemHeight, int alignment) |
64 | { |
65 | qreal y = 0; |
66 | switch (alignment) { |
67 | case Qt::AlignTop: |
68 | break; |
69 | case Qt::AlignBottom: |
70 | y = itemHeight - textHeight; |
71 | break; |
72 | case Qt::AlignVCenter: |
73 | y = (itemHeight - textHeight) / 2; |
74 | break; |
75 | } |
76 | return y; |
77 | } |
78 | |
79 | QT_END_NAMESPACE |
80 | |
81 | #include "moc_qquicktextutil_p.cpp" |
82 | |