aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicklayouts/qquicklayoutitemproxy.cpp
blob: f690a2eae85e9f0d66e8ad979afc4c1d85d4e436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Copyright (C) 2023 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 "qquicklayoutitemproxy_p.h"
#include "qquicklayout_p.h"

/*!
    \qmltype LayoutItemProxy
    \instantiates QQuickLayoutItemProxy
    \inherits Item
    \inqmlmodule QtQuick.Layouts
    \ingroup layouts
    \since QtQuick.Layouts 6.6
    \brief The LayoutItemProxy class provides a placeholder for \l{QQuickItem}s
    in layouts.

    Some responsive layouts require different layout hierarchies for different
    screen sizes, but the layout hierarchy is the same as the QML structure and
    can therefore not be changed at runtime. LayoutItemProxy overcomes this
    limitation by representing a \l{target} item within the layout. The
    \l{target} item itself can be defined anywhere in the QML hierarchy. This
    allows declaration of multiple layouts with the same content items. The
    layouts can be shown and hidden to switch between them.

    The LayoutItemProxy will try to take control of the \l{target} item if it
    is \l [QML] {Item::}{visible}. Taking control will position and resize the
    \l{target} item to match the position and size of the LayoutItemProxy.
    Further, the LayoutItemProxy will set itself as the parent of the
    \l{target} (to ensure event delivery and useful drawing order) and set the
    visibility to \c true. Multiple LayoutItemProxies can \l{target} the same
    item, but only one LayoutItemProxy can control an item at a time. Therefore
    only one of the proxies targeting the same item should be visible at a
    time. If multiple proxies target the same item but \e visible is set to
    false for each proxy, the item will also be invisible.

    All \l{Layout} attached properties of the \l {target}, as well as the
    \l{QQuickItem::implicitWidth} and \l{QQuickItem::implicitHeight} of the
    \l{target} are forwarded by the LayoutItemProxy. The LayoutItemProxy will
    mimic the \l{target} as closely as possible in terms of \l{Layout}
    properties and size. \l{Layout} attached properties can also be set
    explicitly on the LayoutItemProxy which will stop the forwarding of the
    \l {target} properties.

    \section1 Example Usage

    This is a minimalistic example, changing between two layouts using proxies
    to use the same items in both layouts. The items that populate the layouts
    can be defined at an arbitrary point in the QML structure.

    \snippet layouts/simpleProxy.qml item definition

    Then we can define the Layouts with LayoutItemProxys

    \snippet layouts/simpleProxy.qml layout definition

    We can switch now between the layouts, depending on a criterion of our
    choice by toggling the visibility of the layouts on and off.

    \snippet layouts/simpleProxy.qml layout choice

    The two resulting layouts look like this:

    \div {class="float-right"}
    \inlineimage simpleProxy.png
    \enddiv

    The LayoutItemProxy can also be used without layouts, e.g. by anchoring it
    to different items. A mix of real \l {Item}{Items} and proxy items is
    equally possible, as well as nested structures of layouts and items.

    \warning The LayoutItemProxy will set the parent of its target to itself.
    Keep this in mind when referring to the parent of the target item.

    \sa Item, GridLayout, RowLayout, ColumnLayout
*/

Q_LOGGING_CATEGORY(lcLayouts, "qt.quick.layouts")


QQuickLayoutItemProxy::QQuickLayoutItemProxy(QQuickItem *parent)
    : QQuickItem(*new QQuickLayoutItemProxyPrivate, parent)
{

}

QQuickLayoutItemProxy::~QQuickLayoutItemProxy()
{
    Q_D(QQuickLayoutItemProxy);

    if (!d->target)
        return;

    QQuickLayoutItemProxyAttachedData * attachedData = d->target->property("QQuickLayoutItemProxyAttachedData").value<QQuickLayoutItemProxyAttachedData*>();
    // De-register this proxy from the proxies controlling the target
    if (attachedData) {
        if (attachedData->getControllingProxy() == this) {
            attachedData->releaseControl(this);
            d->target->setParentItem(nullptr);
        }
        attachedData->releaseProxy(this);
    }
    // The target item still has a QObject parent that takes care of its destrctuion.
    // No need to invoke destruction of the target tiem from here.
}

/*! \internal
    \brief QQuickLayoutItemProxy::geometryChange Reimplementation of
    QQuickItem::geometryChange to update the target geometry too.
*/
void QQuickLayoutItemProxy::geometryChange(const QRectF &newGeom, const QRectF &oldGeom)
{
    QQuickItem::geometryChange(newGeom, oldGeom);
    if (!isVisible())
        return;

    const QSizeF sz = newGeom.size();
    QPointF pos(0., 0.);

    if (QQuickItem *t = effectiveTarget()) {
        if (QQuickLayoutItemProxyAttachedData * attachedData = target()->property("QQuickLayoutItemProxyAttachedData").value<QQuickLayoutItemProxyAttachedData*>()) {
            if (attachedData->getControllingProxy() != this)
                return;
        }

        // Should normally not be the case, except the user resets the parent
        // This is a failsave for this case and positions the item correctly
        if (t->parentItem() != this)
            pos = t->parentItem()->mapFromGlobal(mapToGlobal(0, 0));

        if (t->size() == sz && t->position() == pos && newGeom == oldGeom)
            return;

        t->setSize(sz);
        t->setPosition(pos);
    }
}

/*! \internal
    \brief QQuickLayoutItemProxy::itemChange is a reimplementation of
    QQuickItem::itemChange to react to changes in visibility.
*/
void QQuickLayoutItemProxy::itemChange(ItemChange c, const ItemChangeData &d)
{
    if (c == QQuickItem::ItemVisibleHasChanged)
    {
        maybeTakeControl();
    }
    QQuickItem::itemChange(c, d);
}

// Implementation of the slots to react to changes of the Layout attached properties.
// If the target Layout propertie change, we change the proxy Layout properties accordingly
// If the proxy Layout properties have been changed externally, we want to remove this binding.
// The member variables m_expectProxy##Property##Change help us keep track about who invokes
// the change of the parameter. If it is invoked by the target we expect a proxy property
// change and will not remove the connection.
#define propertyForwarding(property, Property) \
    void QQuickLayoutItemProxy::target##Property##Changed() { \
        Q_D(QQuickLayoutItemProxy); \
        QQuickLayoutAttached *attTarget = attachedLayoutObject(target(), false); \
        QQuickLayoutAttached *attProxy = attachedLayoutObject(this, false); \
        if (!attTarget) return; \
        if (attProxy->property() == attTarget->property()) \
            return; \
        d->m_expectProxy##Property##Change = true; \
        attProxy->set##Property(attTarget->property()); \
    } \
    void QQuickLayoutItemProxy::proxy##Property##Changed() { \
        Q_D(QQuickLayoutItemProxy); \
        if (d->m_expectProxy##Property##Change) { \
            d->m_expectProxy##Property##Change = false; \
            return; \
        } \
        QQuickLayoutAttached *attTarget = attachedLayoutObject(target(), false); \
        if (!attTarget) return;  \
        disconnect(attTarget, &QQuickLayoutAttached::property##Changed, this, &QQuickLayoutItemProxy::target##Property##Changed); \
    }

propertyForwarding(minimumWidth, MinimumWidth)
propertyForwarding(minimumHeight, MinimumHeight)
propertyForwarding(preferredWidth, PreferredWidth)
propertyForwarding(preferredHeight, PreferredHeight)
propertyForwarding(maximumWidth, MaximumWidth)
propertyForwarding(maximumHeight, MaximumHeight)
propertyForwarding(fillWidth, FillWidth)
propertyForwarding(fillHeight, FillHeight)
propertyForwarding(alignment, Alignment)
propertyForwarding(horizontalStretchFactor, HorizontalStretchFactor)
propertyForwarding(verticalStretchFactor, VerticalStretchFactor)
propertyForwarding(margins, Margins)
propertyForwarding(leftMargin, LeftMargin)
propertyForwarding(topMargin, TopMargin)
propertyForwarding(rightMargin, RightMargin)
propertyForwarding(bottomMargin, BottomMargin)

#undef propertyForwarding

/*!
    \qmlproperty Item LayoutItemProxy::target

    This property holds the \l Item that the proxy should represent in a
    \l {Layout} hierarchy.
*/

/*! \internal
    \brief QQuickLayoutItemProxy::target
    \return The target item of the proxy
*/
QQuickItem *QQuickLayoutItemProxy::target() const
{
    Q_D(const QQuickLayoutItemProxy);
    return d->target;
}

/*! \internal
    \brief QQuickLayoutItemProxy::setTarget sets the target
    \param newTarget The item that the proxy stands in place for.

    All layout properties of the target are connected to the layout properties
    of the LayoutItemProxy. It the LayoutItemProxy is visible, it will try to
    take control of the target.
*/
void QQuickLayoutItemProxy::setTarget(QQuickItem *newTarget)
{
    Q_D(QQuickLayoutItemProxy);

    if (newTarget == d->target)
        return;

    d->target = newTarget;

    if (newTarget) {

        QQuickLayoutItemProxyAttachedData *attachedData;
        if (newTarget->property("QQuickLayoutItemProxyAttachedData").isValid()) {
            attachedData = newTarget->property("QQuickLayoutItemProxyAttachedData").value<QQuickLayoutItemProxyAttachedData*>();
        } else {
            attachedData = new QQuickLayoutItemProxyAttachedData(newTarget);
            QVariant v;
            v.setValue(attachedData);
            newTarget->setProperty("QQuickLayoutItemProxyAttachedData", v);
        }
        attachedData->registerProxy(this);

        // If there is no other controlling proxy, we will hide the target
        if (!attachedData->proxyHasControl())
            newTarget->setVisible(false);
        // We are calling maybeTakeControl at the end to eventually take
        // responsibility of showing the target.

        if (QQuickLayoutAttached *attTarget = attachedLayoutObject(newTarget)) {
            QQuickLayoutAttached *attProxy = attachedLayoutObject(this, true);

            disconnect(attTarget, nullptr, attProxy, nullptr);

            // bind item-specific layout properties:

#define connectPropertyForwarding(property, Property) \
            if (!attProxy->is##Property##Set()) { \
                connect(attTarget, &QQuickLayoutAttached::property##Changed, this, &QQuickLayoutItemProxy::target##Property##Changed); \
                connect(attProxy, &QQuickLayoutAttached::property##Changed, this, &QQuickLayoutItemProxy::proxy##Property##Changed); \
                target##Property##Changed(); \
            }
            connectPropertyForwarding(minimumWidth, MinimumWidth)
            connectPropertyForwarding(minimumHeight, MinimumHeight)
            connectPropertyForwarding(preferredWidth, PreferredWidth)
            connectPropertyForwarding(preferredHeight, PreferredHeight)
            connectPropertyForwarding(maximumWidth, MaximumWidth)
            connectPropertyForwarding(maximumHeight, MaximumHeight)
            connectPropertyForwarding(fillWidth, FillWidth)
            connectPropertyForwarding(fillHeight, FillHeight)
            connectPropertyForwarding(alignment, Alignment)
            connectPropertyForwarding(horizontalStretchFactor, HorizontalStretchFactor)
            connectPropertyForwarding(verticalStretchFactor, VerticalStretchFactor)
            connectPropertyForwarding(margins, Margins)
            connectPropertyForwarding(leftMargin, LeftMargin)
            connectPropertyForwarding(topMargin, TopMargin)
            connectPropertyForwarding(rightMargin, RightMargin)
            connectPropertyForwarding(bottomMargin, BottomMargin)
#undef connectPropertyForwarding

            // proxy.implicitWidth: target.implicitWidth
            auto fnBindImplW = [newTarget, this](){ this->setImplicitWidth(newTarget->implicitWidth()); };
            fnBindImplW();
            connect(newTarget, &QQuickItem::implicitWidthChanged, fnBindImplW);

            // proxy.implicitHeight: target.implicitHeight
            auto fnBindImplH = [newTarget, this](){ this->setImplicitHeight(newTarget->implicitHeight()); };
            fnBindImplH();
            connect(newTarget, &QQuickItem::implicitHeightChanged, fnBindImplH);
        }
    }

    if (isVisible())
        maybeTakeControl();

    emit targetChanged();
}

/*! \internal
    \brief QQuickLayoutItemProxy::effectiveTarget
    \return The target item of the proxy if it is in control, \c null otherwise.
*/
QQuickItem *QQuickLayoutItemProxy::effectiveTarget() const
{
    if (target() == nullptr)
        return nullptr;

    QQuickLayoutItemProxyAttachedData * attachedData = target()->property("QQuickLayoutItemProxyAttachedData").value<QQuickLayoutItemProxyAttachedData*>();
    return (attachedData->getControllingProxy() == this) ? target() : nullptr;
}

/*! \internal
    \brief QQuickLayoutItemProxy::clearTarget sets the target to null.

    This function is called if the target is destroyed to make sure we do not
    try to access a non-existing object.
*/
void QQuickLayoutItemProxy::clearTarget()
{
    setTarget(nullptr);
}

/*! \internal
    \brief QQuickLayoutItemProxy::maybeTakeControl checks and takes over control
    of the item.

    If the proxy is visible it will try to take control over the target and set
    its visibility to true. If the proxy is hidden it will also hide the target
    and another LayoutItemProxy has to set the visibility to \c true or the
    target will stay invisible.
*/
void QQuickLayoutItemProxy::maybeTakeControl()
{
    Q_D(QQuickLayoutItemProxy);
    if (!d->target)
        return;

    QQuickLayoutItemProxyAttachedData * attachedData = d->target->property("QQuickLayoutItemProxyAttachedData").value<QQuickLayoutItemProxyAttachedData*>();
    if (isVisible() && attachedData->getControllingProxy() != this) {
        if (attachedData->takeControl(this)) {
            d->target->setVisible(true);
            d->target->setParentItem(this);
            updatePos();
        }
    }
    if (!isVisible() && attachedData->getControllingProxy() == this){
        if (d->target->parentItem() == this) {
            d->target->setParentItem(nullptr);
        } else
            qCDebug(lcLayouts) << "Parent was changed to" << d->target->parentItem() << "while an ItemProxy had control";
        d->target->setVisible(false);
        attachedData->releaseControl(this);
    }
}

/*! \internal
    \brief QQuickLayoutItemProxy::updatePos sets the geometry of the target to
    the geometry of the proxy
*/
void QQuickLayoutItemProxy::updatePos()
{
    if (!isVisible())
        return;
    if (target()) {
        if (QQuickLayoutItemProxyAttachedData * attachedData = target()->property("QQuickLayoutItemProxyAttachedData").value<QQuickLayoutItemProxyAttachedData*>()) {
            if (attachedData->getControllingProxy() == this)
                geometryChange(boundingRect(), boundingRect());
        }
    }
}

QQuickLayoutItemProxyPrivate::QQuickLayoutItemProxyPrivate()
    : QQuickItemPrivate(),
      m_expectProxyMinimumWidthChange(false),
      m_expectProxyMinimumHeightChange(false),
      m_expectProxyPreferredWidthChange(false),
      m_expectProxyPreferredHeightChange(false),
      m_expectProxyMaximumWidthChange(false),
      m_expectProxyMaximumHeightChange(false),
      m_expectProxyFillWidthChange(false),
      m_expectProxyFillHeightChange(false),
      m_expectProxyAlignmentChange(false),
      m_expectProxyHorizontalStretchFactorChange(false),
      m_expectProxyVerticalStretchFactorChange(false),
      m_expectProxyMarginsChange(false),
      m_expectProxyLeftMarginChange(false),
      m_expectProxyTopMarginChange(false),
      m_expectProxyRightMarginChange(false),
      m_expectProxyBottomMarginChange(false)
{

}

/*! \internal
    \class QQuickLayoutItemProxyAttachedData
    \brief Provides attached properties for items that are managed by one or
    more LayoutItemProxy.

    It stores all proxies that target the item, and will emit signals when the
    proxies or the controlling proxy changes. Proxies can listen to the signal
    and pick up control if they wish to.
*/
QQuickLayoutItemProxyAttachedData::QQuickLayoutItemProxyAttachedData(QObject *parent)
    : QObject(parent), controllingProxy(nullptr)
{

}

QQuickLayoutItemProxyAttachedData::~QQuickLayoutItemProxyAttachedData()
{
    // If this is destroyed, so is the target. Clear the target from the
    // proxies so they do not try to access a destroyed object
    for (auto &proxy: std::as_const(proxies))
        proxy->clearTarget();
}

/*! \internal
    \brief QQuickLayoutItemProxyAttachedData::registerProxy registers a proxy
    that manages the item this data is attached to.

    This is required to easily notify proxies when the target is destroyed or
    when it is free to take over control.
*/
void QQuickLayoutItemProxyAttachedData::registerProxy(QQuickLayoutItemProxy *proxy)
{
    if (proxies.contains(proxy))
        return;

    proxies.append(proxy);
    emit proxiesChanged();
}

/*! \internal
    \brief QQuickLayoutItemProxyAttachedData::releaseProxy removes a proxy from
    a list of known proxies that manage the item this data is attached to.
*/
void QQuickLayoutItemProxyAttachedData::releaseProxy(QQuickLayoutItemProxy *proxy)
{
    if (proxy == controllingProxy)
        releaseControl(proxy);

    proxies.removeAll(proxy);

    if (proxies.isEmpty())
        deleteLater();

    emit proxiesChanged();
}

/*! \internal
    \brief QQuickLayoutItemProxyAttachedData::takeControl is called by
    LayoutItemProxies when they try to take control over the item this data is
    attached to.
    \return \c true if no other proxy controls the item and if control is
    granted to the proxy, \c false otherwise.

    \param proxy The proxy that tries to take control.
*/
bool QQuickLayoutItemProxyAttachedData::takeControl(QQuickLayoutItemProxy *proxy)
{
    if (controllingProxy || !proxies.contains(proxy))
        return false;

    qCDebug(lcLayouts) << proxy
                       << "takes control of"
                       << parent();

    controllingProxy = proxy;
    emit controlTaken();
    emit controllingProxyChanged();
    return true;
}

/*! \internal
    \brief QQuickLayoutItemProxyAttachedData::releaseControl is called by
    LayoutItemProxies when they try no longer control the item

    \param proxy The proxy that gives up control.
*/
void QQuickLayoutItemProxyAttachedData::releaseControl(QQuickLayoutItemProxy *proxy)
{
    if (controllingProxy != proxy)
        return;

    qCDebug(lcLayouts) << proxy
                       << "no longer controls"
                       << parent();

    controllingProxy = nullptr;
    emit controlReleased();
    emit controllingProxyChanged();

    for (auto &otherProxy: std::as_const(proxies)) {
        if (proxy != otherProxy)
            otherProxy->maybeTakeControl();
    }
}

/*! \internal
    \brief QQuickLayoutItemProxyAttachedData::getControllingProxy
    \return the proxy that currently controls the item this data is attached to.
    Returns \c null if no proxy controls the item.
*/
QQuickLayoutItemProxy *QQuickLayoutItemProxyAttachedData::getControllingProxy() const
{
    return controllingProxy;
}

/*! \internal
    \brief QQuickLayoutItemProxyAttachedData::getProxies
    \return a list of all proxies that target the item this data is attached to.
*/
QQmlListProperty<QQuickLayoutItemProxy> QQuickLayoutItemProxyAttachedData::getProxies()
{
    using Type = QQuickLayoutItemProxy;
    using Property = QQmlListProperty<Type>;

    return Property(
        this, &proxies,
        [](Property *p) { return static_cast<QList<Type *> *>(p->data)->size(); },
        [](Property *p, qsizetype i) { return static_cast<QList<Type *> *>(p->data)->at(i); }
    );
}

/*! \internal
    \brief QQuickLayoutItemProxyAttachedData::proxyHasControl
    \return \c true if a proxy is controlling the item, \c false otherwise.
*/
bool QQuickLayoutItemProxyAttachedData::proxyHasControl() const
{
    return controllingProxy != nullptr;
}