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