| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at http://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or later as published by the Free |
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
| 29 | ** the packaging of this file. Please review the following information to |
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be |
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
| 32 | ** |
| 33 | ** $QT_END_LICENSE$ |
| 34 | ** |
| 35 | ****************************************************************************/ |
| 36 | |
| 37 | #include "qquickstackview_p_p.h" |
| 38 | #include "qquickstackelement_p_p.h" |
| 39 | #include "qquickstacktransition_p_p.h" |
| 40 | |
| 41 | #include <QtQml/qqmlinfo.h> |
| 42 | #include <QtQml/qqmllist.h> |
| 43 | #include <QtQml/private/qv4qmlcontext_p.h> |
| 44 | #include <QtQml/private/qv4qobjectwrapper_p.h> |
| 45 | #include <QtQuick/private/qquickanimation_p.h> |
| 46 | #include <QtQuick/private/qquicktransition_p.h> |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE |
| 49 | |
| 50 | void QQuickStackViewPrivate::warn(const QString &error) |
| 51 | { |
| 52 | Q_Q(QQuickStackView); |
| 53 | if (operation.isEmpty()) |
| 54 | qmlWarning(me: q) << error; |
| 55 | else |
| 56 | qmlWarning(me: q) << operation << ": " << error; |
| 57 | } |
| 58 | |
| 59 | void QQuickStackViewPrivate::warnOfInterruption(const QString &attemptedOperation) |
| 60 | { |
| 61 | Q_Q(QQuickStackView); |
| 62 | qmlWarning(me: q) << "cannot " << attemptedOperation << " while already in the process of completing a " << operation; |
| 63 | } |
| 64 | |
| 65 | void QQuickStackViewPrivate::setCurrentItem(QQuickStackElement *element) |
| 66 | { |
| 67 | Q_Q(QQuickStackView); |
| 68 | QQuickItem *item = element ? element->item : nullptr; |
| 69 | if (currentItem == item) |
| 70 | return; |
| 71 | |
| 72 | currentItem = item; |
| 73 | if (element) |
| 74 | element->setVisible(true); |
| 75 | if (item) |
| 76 | item->setFocus(true); |
| 77 | emit q->currentItemChanged(); |
| 78 | } |
| 79 | |
| 80 | static bool initProperties(QQuickStackElement *element, const QV4::Value &props, QQmlV4Function *args) |
| 81 | { |
| 82 | if (props.isObject()) { |
| 83 | const QV4::QObjectWrapper *wrapper = props.as<QV4::QObjectWrapper>(); |
| 84 | if (!wrapper) { |
| 85 | QV4::ExecutionEngine *v4 = args->v4engine(); |
| 86 | element->properties.set(engine: v4, value: props); |
| 87 | element->qmlCallingContext.set(engine: v4, obj: v4->qmlContext()); |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | QList<QQuickStackElement *> QQuickStackViewPrivate::parseElements(int from, QQmlV4Function *args, QStringList *errors) |
| 95 | { |
| 96 | QV4::ExecutionEngine *v4 = args->v4engine(); |
| 97 | QQmlContextData *context = v4->callingQmlContext(); |
| 98 | QV4::Scope scope(v4); |
| 99 | |
| 100 | QList<QQuickStackElement *> elements; |
| 101 | |
| 102 | int argc = args->length(); |
| 103 | for (int i = from; i < argc; ++i) { |
| 104 | QV4::ScopedValue arg(scope, (*args)[i]); |
| 105 | if (QV4::ArrayObject *array = arg->as<QV4::ArrayObject>()) { |
| 106 | const uint len = uint(array->getLength()); |
| 107 | for (uint j = 0; j < len; ++j) { |
| 108 | QString error; |
| 109 | QV4::ScopedValue value(scope, array->get(idx: j)); |
| 110 | QQuickStackElement *element = createElement(value, context, error: &error); |
| 111 | if (element) { |
| 112 | if (j < len - 1) { |
| 113 | QV4::ScopedValue props(scope, array->get(idx: j + 1)); |
| 114 | if (initProperties(element, props, args)) |
| 115 | ++j; |
| 116 | } |
| 117 | elements += element; |
| 118 | } else if (!error.isEmpty()) { |
| 119 | *errors += error; |
| 120 | } |
| 121 | } |
| 122 | } else { |
| 123 | QString error; |
| 124 | QQuickStackElement *element = createElement(value: arg, context, error: &error); |
| 125 | if (element) { |
| 126 | if (i < argc - 1) { |
| 127 | QV4::ScopedValue props(scope, (*args)[i + 1]); |
| 128 | if (initProperties(element, props, args)) |
| 129 | ++i; |
| 130 | } |
| 131 | elements += element; |
| 132 | } else if (!error.isEmpty()) { |
| 133 | *errors += error; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | return elements; |
| 138 | } |
| 139 | |
| 140 | QQuickStackElement *QQuickStackViewPrivate::findElement(QQuickItem *item) const |
| 141 | { |
| 142 | if (item) { |
| 143 | for (QQuickStackElement *e : qAsConst(t: elements)) { |
| 144 | if (e->item == item) |
| 145 | return e; |
| 146 | } |
| 147 | } |
| 148 | return nullptr; |
| 149 | } |
| 150 | |
| 151 | QQuickStackElement *QQuickStackViewPrivate::findElement(const QV4::Value &value) const |
| 152 | { |
| 153 | if (const QV4::QObjectWrapper *o = value.as<QV4::QObjectWrapper>()) |
| 154 | return findElement(item: qobject_cast<QQuickItem *>(object: o->object())); |
| 155 | return nullptr; |
| 156 | } |
| 157 | |
| 158 | static QString resolvedUrl(const QString &str, QQmlContextData *context) |
| 159 | { |
| 160 | QUrl url(str); |
| 161 | if (url.isRelative()) |
| 162 | return context->resolvedUrl(url).toString(); |
| 163 | return str; |
| 164 | } |
| 165 | |
| 166 | QQuickStackElement *QQuickStackViewPrivate::createElement(const QV4::Value &value, QQmlContextData *context, QString *error) |
| 167 | { |
| 168 | Q_Q(QQuickStackView); |
| 169 | if (const QV4::String *s = value.as<QV4::String>()) |
| 170 | return QQuickStackElement::fromString(str: resolvedUrl(str: s->toQString(), context), view: q, error); |
| 171 | if (const QV4::QObjectWrapper *o = value.as<QV4::QObjectWrapper>()) |
| 172 | return QQuickStackElement::fromObject(object: o->object(), view: q, error); |
| 173 | return nullptr; |
| 174 | } |
| 175 | |
| 176 | bool QQuickStackViewPrivate::pushElements(const QList<QQuickStackElement *> &elems) |
| 177 | { |
| 178 | Q_Q(QQuickStackView); |
| 179 | if (!elems.isEmpty()) { |
| 180 | for (QQuickStackElement *e : elems) { |
| 181 | e->setIndex(elements.count()); |
| 182 | elements += e; |
| 183 | } |
| 184 | return elements.top()->load(parent: q); |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | bool QQuickStackViewPrivate::pushElement(QQuickStackElement *element) |
| 190 | { |
| 191 | if (element) |
| 192 | return pushElements(elems: QList<QQuickStackElement *>() << element); |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | bool QQuickStackViewPrivate::popElements(QQuickStackElement *element) |
| 197 | { |
| 198 | Q_Q(QQuickStackView); |
| 199 | while (elements.count() > 1 && elements.top() != element) { |
| 200 | delete elements.pop(); |
| 201 | if (!element) |
| 202 | break; |
| 203 | } |
| 204 | return elements.top()->load(parent: q); |
| 205 | } |
| 206 | |
| 207 | bool QQuickStackViewPrivate::replaceElements(QQuickStackElement *target, const QList<QQuickStackElement *> &elems) |
| 208 | { |
| 209 | if (target) { |
| 210 | while (!elements.isEmpty()) { |
| 211 | QQuickStackElement* top = elements.pop(); |
| 212 | delete top; |
| 213 | if (top == target) |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | return pushElements(elems); |
| 218 | } |
| 219 | |
| 220 | void QQuickStackViewPrivate::ensureTransitioner() |
| 221 | { |
| 222 | if (!transitioner) { |
| 223 | transitioner = new QQuickItemViewTransitioner; |
| 224 | transitioner->setChangeListener(this); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void QQuickStackViewPrivate::startTransition(const QQuickStackTransition &first, const QQuickStackTransition &second, bool immediate) |
| 229 | { |
| 230 | if (first.element) |
| 231 | first.element->transitionNextReposition(transitioner, type: first.type, asTarget: first.target); |
| 232 | if (second.element) |
| 233 | second.element->transitionNextReposition(transitioner, type: second.type, asTarget: second.target); |
| 234 | |
| 235 | if (first.element) { |
| 236 | if (immediate || !first.element->item || !first.element->prepareTransition(transitioner, viewBounds: first.viewBounds)) |
| 237 | completeTransition(element: first.element, transition: first.transition, status: first.status); |
| 238 | else |
| 239 | first.element->startTransition(transitioner, status: first.status); |
| 240 | } |
| 241 | if (second.element) { |
| 242 | if (immediate || !second.element->item || !second.element->prepareTransition(transitioner, viewBounds: second.viewBounds)) |
| 243 | completeTransition(element: second.element, transition: second.transition, status: second.status); |
| 244 | else |
| 245 | second.element->startTransition(transitioner, status: second.status); |
| 246 | } |
| 247 | |
| 248 | if (transitioner) { |
| 249 | setBusy(!transitioner->runningJobs.isEmpty()); |
| 250 | transitioner->resetTargetLists(); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void QQuickStackViewPrivate::completeTransition(QQuickStackElement *element, QQuickTransition *transition, QQuickStackView::Status status) |
| 255 | { |
| 256 | element->setStatus(status); |
| 257 | if (transition) { |
| 258 | // TODO: add a proper way to complete a transition |
| 259 | QQmlListProperty<QQuickAbstractAnimation> animations = transition->animations(); |
| 260 | int count = animations.count(&animations); |
| 261 | for (int i = 0; i < count; ++i) { |
| 262 | QQuickAbstractAnimation *anim = animations.at(&animations, i); |
| 263 | anim->complete(); |
| 264 | } |
| 265 | } |
| 266 | viewItemTransitionFinished(item: element); |
| 267 | } |
| 268 | |
| 269 | void QQuickStackViewPrivate::viewItemTransitionFinished(QQuickItemViewTransitionableItem *transitionable) |
| 270 | { |
| 271 | QQuickStackElement *element = static_cast<QQuickStackElement *>(transitionable); |
| 272 | if (element->status == QQuickStackView::Activating) { |
| 273 | element->setStatus(QQuickStackView::Active); |
| 274 | } else if (element->status == QQuickStackView::Deactivating) { |
| 275 | element->setStatus(QQuickStackView::Inactive); |
| 276 | QQuickStackElement *existingElement = element->item ? findElement(item: element->item) : nullptr; |
| 277 | // If a different element with the same item is found, |
| 278 | // do not call setVisible(false) since it needs to be visible. |
| 279 | if (!existingElement || element == existingElement) |
| 280 | element->setVisible(false); |
| 281 | if (element->removal || element->isPendingRemoval()) |
| 282 | removed += element; |
| 283 | } |
| 284 | |
| 285 | if (transitioner && transitioner->runningJobs.isEmpty()) { |
| 286 | // ~QQuickStackElement() emits QQuickStackViewAttached::removed(), which may be used |
| 287 | // to modify the stack. Set the status first and make a copy of the destroyable stack |
| 288 | // elements to exclude any modifications that may happen during qDeleteAll(). (QTBUG-62153) |
| 289 | setBusy(false); |
| 290 | QList<QQuickStackElement*> removedElements = removed; |
| 291 | removed.clear(); |
| 292 | |
| 293 | for (QQuickStackElement *removedElement : qAsConst(t&: removedElements)) { |
| 294 | // If an element with the same item is found in the active stack list, |
| 295 | // forget about the item so that we don't hide it. |
| 296 | if (removedElement->item && findElement(item: removedElement->item)) { |
| 297 | QQuickItemPrivate::get(item: removedElement->item)->removeItemChangeListener(removedElement, types: QQuickItemPrivate::Destroyed); |
| 298 | removedElement->item = nullptr; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | qDeleteAll(c: removedElements); |
| 303 | } |
| 304 | |
| 305 | removing.remove(value: element); |
| 306 | } |
| 307 | |
| 308 | void QQuickStackViewPrivate::setBusy(bool b) |
| 309 | { |
| 310 | Q_Q(QQuickStackView); |
| 311 | if (busy == b) |
| 312 | return; |
| 313 | |
| 314 | busy = b; |
| 315 | q->setFiltersChildMouseEvents(busy); |
| 316 | emit q->busyChanged(); |
| 317 | } |
| 318 | |
| 319 | void QQuickStackViewPrivate::depthChange(int newDepth, int oldDepth) |
| 320 | { |
| 321 | Q_Q(QQuickStackView); |
| 322 | if (newDepth == oldDepth) |
| 323 | return; |
| 324 | |
| 325 | emit q->depthChanged(); |
| 326 | if (newDepth == 0 || oldDepth == 0) |
| 327 | emit q->emptyChanged(); |
| 328 | } |
| 329 | |
| 330 | QT_END_NAMESPACE |
| 331 | |