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