diff options
| author | Mitch Curtis <mitch.curtis@qt.io> | 2022-11-18 15:15:16 +0800 |
|---|---|---|
| committer | Mitch Curtis <mitch.curtis@qt.io> | 2022-12-01 10:26:20 +0800 |
| commit | 4bd87b903b355b53e3105ba1ae7c154c4e55cdaf (patch) | |
| tree | cc2edb597f0d5871302eb86e9dda78217384a5aa /src/quicktemplates/qquickstackelement.cpp | |
| parent | 786e1748d4469c135a922a221024f3f9c421c0de (diff) | |
Remove "2" from Qt Quick Controls directories
Qt Quick Controls 2 was named that way because it was a follow-up to
Qt Quick Controls 1.x. Now that Qt Quick Controls 1 is no longer
supported, we don't need to have "2" in the name. Work on this was
already started for the documentation in
1abdfe5d5a052f2298b7bf657513dfa7e0c66a56.
By doing this renaming a few weeks before feature freeze, it won't
affect the release but still results in as little time possible spent
manually fixing conflicts in cherry-picks from non-LTS releases as a
result of the renaming.
This patch does the following:
- Renames directories.
- Adapts CMakeLists.txt and other files to account for the new paths.
A follow-up patch will handle documentation.
It does not touch library names or other user-facing stuff, as that
will have to be done in Qt 7.
Task-number: QTBUG-95413
Change-Id: I170d8db19033ee71e495ff0c5c1a517a41ed7634
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates/qquickstackelement.cpp')
| -rw-r--r-- | src/quicktemplates/qquickstackelement.cpp | 303 |
1 files changed, 303 insertions, 0 deletions
diff --git a/src/quicktemplates/qquickstackelement.cpp b/src/quicktemplates/qquickstackelement.cpp new file mode 100644 index 0000000000..f8001a2980 --- /dev/null +++ b/src/quicktemplates/qquickstackelement.cpp @@ -0,0 +1,303 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "qquickstackelement_p_p.h" +#include "qquickstackview_p_p.h" + +#include <QtQml/qqmlinfo.h> +#include <QtQml/qqmlengine.h> +#include <QtQml/qqmlcomponent.h> +#include <QtQml/qqmlincubator.h> +#include <QtQml/private/qv4qobjectwrapper_p.h> +#include <QtQml/private/qqmlcomponent_p.h> +#include <QtQml/private/qqmlengine_p.h> +#include <QtQml/private/qqmlincubator_p.h> + +QT_BEGIN_NAMESPACE + +static QQuickStackViewAttached *attachedStackObject(QQuickStackElement *element) +{ + QQuickStackViewAttached *attached = qobject_cast<QQuickStackViewAttached *>(qmlAttachedPropertiesObject<QQuickStackView>(element->item, false)); + if (attached) + QQuickStackViewAttachedPrivate::get(attached)->element = element; + return attached; +} + +class QQuickStackIncubator : public QQmlIncubator +{ +public: + QQuickStackIncubator(QQuickStackElement *element) + : QQmlIncubator(Synchronous), + element(element) + { + } + +protected: + void setInitialState(QObject *object) override + { + auto privIncubator = QQmlIncubatorPrivate::get(this); + element->incubate(object, privIncubator->requiredProperties()); + } + +private: + QQuickStackElement *element; +}; + +QQuickStackElement::QQuickStackElement() + : QQuickItemViewTransitionableItem(nullptr) +{ +} + +QQuickStackElement::~QQuickStackElement() +{ + if (item) + QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Destroyed); + + if (ownComponent) + delete component; + + QQuickStackViewAttached *attached = attachedStackObject(this); + if (item) { + if (ownItem) { + item->setParentItem(nullptr); + item->deleteLater(); + item = nullptr; + } else { + setVisible(false); + if (!widthValid) + item->resetWidth(); + if (!heightValid) + item->resetHeight(); + if (item->parentItem() != originalParent) { + item->setParentItem(originalParent); + } else { + if (attached) + QQuickStackViewAttachedPrivate::get(attached)->itemParentChanged(item, nullptr); + } + } + } + + if (attached) + emit attached->removed(); +} + +QQuickStackElement *QQuickStackElement::fromString(const QString &str, QQuickStackView *view, QString *error) +{ + QUrl url(str); + if (!url.isValid()) { + *error = QStringLiteral("invalid url: ") + str; + return nullptr; + } + + if (url.isRelative()) + url = qmlContext(view)->resolvedUrl(url); + + QQuickStackElement *element = new QQuickStackElement; + element->component = new QQmlComponent(qmlEngine(view), url, view); + element->ownComponent = true; + return element; +} + +QQuickStackElement *QQuickStackElement::fromObject(QObject *object, QQuickStackView *view, QString *error) +{ + Q_UNUSED(view); + QQmlComponent *component = qobject_cast<QQmlComponent *>(object); + QQuickItem *item = qobject_cast<QQuickItem *>(object); + if (!component && !item) { + *error = QQmlMetaType::prettyTypeName(object) + QStringLiteral(" is not supported. Must be Item or Component."); + return nullptr; + } + + QQuickStackElement *element = new QQuickStackElement; + element->component = qobject_cast<QQmlComponent *>(object); + element->item = qobject_cast<QQuickItem *>(object); + if (element->item) + element->originalParent = element->item->parentItem(); + return element; +} + +bool QQuickStackElement::load(QQuickStackView *parent) +{ + setView(parent); + if (!item) { + ownItem = true; + + if (component->isLoading()) { + QObject::connect(component, &QQmlComponent::statusChanged, [this](QQmlComponent::Status status) { + if (status == QQmlComponent::Ready) + load(view); + else if (status == QQmlComponent::Error) + QQuickStackViewPrivate::get(view)->warn(component->errorString().trimmed()); + }); + return true; + } + + QQmlContext *context = component->creationContext(); + if (!context) + context = qmlContext(parent); + + QQuickStackIncubator incubator(this); + component->create(incubator, context); + if (component->isError()) + QQuickStackViewPrivate::get(parent)->warn(component->errorString().trimmed()); + } else { + initialize(/*required properties=*/nullptr); + } + return item; +} + +void QQuickStackElement::incubate(QObject *object, RequiredProperties *requiredProperties) +{ + item = qmlobject_cast<QQuickItem *>(object); + if (item) { + QQmlEngine::setObjectOwnership(item, QQmlEngine::CppOwnership); + item->setParent(view); + initialize(requiredProperties); + } +} + +void QQuickStackElement::initialize(RequiredProperties *requiredProperties) +{ + if (!item || init) + return; + + QQuickItemPrivate *p = QQuickItemPrivate::get(item); + if (!(widthValid = p->widthValid())) + item->setWidth(view->width()); + if (!(heightValid = p->heightValid())) + item->setHeight(view->height()); + item->setParentItem(view); + + if (!properties.isUndefined()) { + QQmlEngine *engine = qmlEngine(view); + Q_ASSERT(engine); + QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine); + Q_ASSERT(v4); + QV4::Scope scope(v4); + QV4::ScopedValue ipv(scope, properties.value()); + QV4::Scoped<QV4::QmlContext> qmlContext(scope, qmlCallingContext.value()); + QV4::ScopedValue qmlObject(scope, QV4::QObjectWrapper::wrap(v4, item)); + QQmlComponentPrivate::setInitialProperties(v4, qmlContext, qmlObject, ipv, requiredProperties, item); + properties.clear(); + } + + if (requiredProperties && !requiredProperties->empty()) { + QString error; + for (const auto &property: *requiredProperties) { + error += QLatin1String("Property %1 was marked as required but not set.\n") + .arg(property.propertyName); + } + QQuickStackViewPrivate::get(view)->warn(error); + item = nullptr; + } else { + p->addItemChangeListener(this, QQuickItemPrivate::Destroyed); + } + + init = true; +} + +void QQuickStackElement::setIndex(int value) +{ + if (index == value) + return; + + index = value; + QQuickStackViewAttached *attached = attachedStackObject(this); + if (attached) + emit attached->indexChanged(); +} + +void QQuickStackElement::setView(QQuickStackView *value) +{ + if (view == value) + return; + + view = value; + QQuickStackViewAttached *attached = attachedStackObject(this); + if (attached) + emit attached->viewChanged(); +} + +void QQuickStackElement::setStatus(QQuickStackView::Status value) +{ + if (status == value) + return; + + status = value; + QQuickStackViewAttached *attached = attachedStackObject(this); + if (!attached) + return; + + switch (value) { + case QQuickStackView::Inactive: + emit attached->deactivated(); + break; + case QQuickStackView::Deactivating: + emit attached->deactivating(); + break; + case QQuickStackView::Activating: + emit attached->activating(); + break; + case QQuickStackView::Active: + emit attached->activated(); + break; + default: + Q_UNREACHABLE(); + break; + } + + emit attached->statusChanged(); +} + +void QQuickStackElement::setVisible(bool visible) +{ + QQuickStackViewAttached *attached = attachedStackObject(this); + if (!item || (attached && QQuickStackViewAttachedPrivate::get(attached)->explicitVisible)) + return; + + item->setVisible(visible); +} + +void QQuickStackElement::transitionNextReposition(QQuickItemViewTransitioner *transitioner, QQuickItemViewTransitioner::TransitionType type, bool asTarget) +{ + if (transitioner) + transitioner->transitionNextReposition(this, type, asTarget); +} + +bool QQuickStackElement::prepareTransition(QQuickItemViewTransitioner *transitioner, const QRectF &viewBounds) +{ + if (transitioner) { + if (item) { + QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors; + // TODO: expose QQuickAnchorLine so we can test for other conflicting anchors + if (anchors && (anchors->fill() || anchors->centerIn())) + qmlWarning(item) << "StackView has detected conflicting anchors. Transitions may not execute properly."; + } + + // TODO: add force argument to QQuickItemViewTransitionableItem::prepareTransition()? + nextTransitionToSet = true; + nextTransitionFromSet = true; + nextTransitionFrom += QPointF(1, 1); + return QQuickItemViewTransitionableItem::prepareTransition(transitioner, index, viewBounds); + } + return false; +} + +void QQuickStackElement::startTransition(QQuickItemViewTransitioner *transitioner, QQuickStackView::Status status) +{ + setStatus(status); + if (transitioner) + QQuickItemViewTransitionableItem::startTransition(transitioner, index); +} + +void QQuickStackElement::completeTransition(QQuickTransition *quickTransition) +{ + QQuickItemViewTransitionableItem::completeTransition(quickTransition); +} + +void QQuickStackElement::itemDestroyed(QQuickItem *) +{ + item = nullptr; +} + +QT_END_NAMESPACE |
