diff options
Diffstat (limited to 'src/quick/items/qquickanchors.cpp')
-rw-r--r-- | src/quick/items/qquickanchors.cpp | 522 |
1 files changed, 308 insertions, 214 deletions
diff --git a/src/quick/items/qquickanchors.cpp b/src/quick/items/qquickanchors.cpp index d87669df56..4cbd41106e 100644 --- a/src/quick/items/qquickanchors.cpp +++ b/src/quick/items/qquickanchors.cpp @@ -39,19 +39,48 @@ #include "qquickanchors_p_p.h" -#include "qquickitem.h" #include "qquickitem_p.h" #include <qqmlinfo.h> QT_BEGIN_NAMESPACE +static Q_ALWAYS_INLINE QQuickItem *readParentItem(const QQuickItem *item) +{ + return QQuickItemPrivate::get(item)->parentItem; +} + +static Q_ALWAYS_INLINE qreal readX(const QQuickItem *item) +{ + return QQuickItemPrivate::get(item)->x; +} + +static Q_ALWAYS_INLINE qreal readY(const QQuickItem *item) +{ + return QQuickItemPrivate::get(item)->y; +} + +static Q_ALWAYS_INLINE qreal readWidth(const QQuickItem *item) +{ + return QQuickItemPrivate::get(item)->width; +} + +static Q_ALWAYS_INLINE qreal readHeight(const QQuickItem *item) +{ + return QQuickItemPrivate::get(item)->height; +} + +static Q_ALWAYS_INLINE qreal readBaselineOffset(const QQuickItem *item) +{ + return QQuickItemPrivate::get(item)->baselineOffset; +} + //TODO: should we cache relationships, so we don't have to check each time (parent-child or sibling)? //TODO: support non-parent, non-sibling (need to find lowest common ancestor) -static inline qreal hcenter(QQuickItem const *item) +static inline qreal hcenter(const QQuickItem *item) { - qreal width = item->width(); + qreal width = readWidth(item); if (QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors) { if (!QQuickAnchorsPrivate::get(anchors)->centerAligned) return width / 2; @@ -63,9 +92,9 @@ static inline qreal hcenter(QQuickItem const *item) return width / 2; } -static inline qreal vcenter(QQuickItem const *item) +static inline qreal vcenter(const QQuickItem *item) { - qreal height = item->height(); + qreal height = readHeight(item); if (QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors) { if (!QQuickAnchorsPrivate::get(anchors)->centerAligned) return height / 2; @@ -78,30 +107,30 @@ static inline qreal vcenter(QQuickItem const *item) } //local position -static qreal position(QQuickItem const *item, QQuickAnchorLine::AnchorLine anchorLine) +static inline qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine) { qreal ret = 0.0; switch (anchorLine) { - case QQuickAnchorLine::Left: - ret = item->x(); + case QQuickAnchors::LeftAnchor: + ret = readX(item); break; - case QQuickAnchorLine::Right: - ret = item->x() + item->width(); + case QQuickAnchors::RightAnchor: + ret = readX(item) + readWidth(item); break; - case QQuickAnchorLine::Top: - ret = item->y(); + case QQuickAnchors::TopAnchor: + ret = readY(item); break; - case QQuickAnchorLine::Bottom: - ret = item->y() + item->height(); + case QQuickAnchors::BottomAnchor: + ret = readY(item) + readHeight(item); break; - case QQuickAnchorLine::HCenter: - ret = item->x() + hcenter(item); + case QQuickAnchors::HCenterAnchor: + ret = readX(item) + hcenter(item); break; - case QQuickAnchorLine::VCenter: - ret = item->y() + vcenter(item); + case QQuickAnchors::VCenterAnchor: + ret = readY(item) + vcenter(item); break; - case QQuickAnchorLine::Baseline: - ret = item->y() + item->baselineOffset(); + case QQuickAnchors::BaselineAnchor: + ret = readY(item) + readBaselineOffset(item); break; default: break; @@ -111,30 +140,30 @@ static qreal position(QQuickItem const *item, QQuickAnchorLine::AnchorLine ancho } //position when origin is 0,0 -static qreal adjustedPosition(QQuickItem *item, QQuickAnchorLine::AnchorLine anchorLine) +static inline qreal adjustedPosition(QQuickItem *item, QQuickAnchors::Anchor anchorLine) { qreal ret = 0.0; switch (anchorLine) { - case QQuickAnchorLine::Left: + case QQuickAnchors::LeftAnchor: ret = 0.0; break; - case QQuickAnchorLine::Right: - ret = item->width(); + case QQuickAnchors::RightAnchor: + ret = readWidth(item); break; - case QQuickAnchorLine::Top: + case QQuickAnchors::TopAnchor: ret = 0.0; break; - case QQuickAnchorLine::Bottom: - ret = item->height(); + case QQuickAnchors::BottomAnchor: + ret = readHeight(item); break; - case QQuickAnchorLine::HCenter: + case QQuickAnchors::HCenterAnchor: ret = hcenter(item); break; - case QQuickAnchorLine::VCenter: + case QQuickAnchors::VCenterAnchor: ret = vcenter(item); break; - case QQuickAnchorLine::Baseline: - ret = item->baselineOffset(); + case QQuickAnchors::BaselineAnchor: + ret = readBaselineOffset(item); break; default: break; @@ -154,13 +183,13 @@ QQuickAnchors::~QQuickAnchors() d->inDestructor = true; d->remDepend(d->fill); d->remDepend(d->centerIn); - d->remDepend(d->left.item); - d->remDepend(d->right.item); - d->remDepend(d->top.item); - d->remDepend(d->bottom.item); - d->remDepend(d->vCenter.item); - d->remDepend(d->hCenter.item); - d->remDepend(d->baseline.item); + d->remDepend(d->leftAnchorItem); + d->remDepend(d->rightAnchorItem); + d->remDepend(d->topAnchorItem); + d->remDepend(d->bottomAnchorItem); + d->remDepend(d->vCenterAnchorItem); + d->remDepend(d->hCenterAnchorItem); + d->remDepend(d->baselineAnchorItem); } void QQuickAnchorsPrivate::fillChanged() @@ -174,12 +203,13 @@ void QQuickAnchorsPrivate::fillChanged() qreal horizontalMargin = q->mirrored() ? rightMargin : leftMargin; - if (fill == item->parentItem()) { //child-parent + if (fill == readParentItem(item)) { //child-parent setItemPos(QPointF(horizontalMargin, topMargin)); - } else if (fill->parentItem() == item->parentItem()) { //siblings - setItemPos(QPointF(fill->x()+horizontalMargin, fill->y()+topMargin)); + } else if (readParentItem(fill) == readParentItem(item)) { //siblings + setItemPos(QPointF(readX(fill)+horizontalMargin, readY(fill) + topMargin)); } - setItemSize(QSizeF(fill->width()-leftMargin-rightMargin, fill->height()-topMargin-bottomMargin)); + setItemSize(QSizeF(readWidth(fill) - leftMargin - rightMargin, + readHeight(fill) - topMargin - bottomMargin)); --updatingFill; } else { @@ -199,12 +229,12 @@ void QQuickAnchorsPrivate::centerInChanged() ++updatingCenterIn; qreal effectiveHCenterOffset = q->mirrored() ? -hCenterOffset : hCenterOffset; - if (centerIn == item->parentItem()) { - QPointF p(hcenter(item->parentItem()) - hcenter(item) + effectiveHCenterOffset, - vcenter(item->parentItem()) - vcenter(item) + vCenterOffset); + if (centerIn == readParentItem(item)) { + QPointF p(hcenter(readParentItem(item)) - hcenter(item) + effectiveHCenterOffset, + vcenter(readParentItem(item)) - vcenter(item) + vCenterOffset); setItemPos(p); - } else if (centerIn->parentItem() == item->parentItem()) { + } else if (readParentItem(centerIn) == readParentItem(item)) { QPointF p(centerIn->x() + hcenter(centerIn) - hcenter(item) + effectiveHCenterOffset, centerIn->y() + vcenter(centerIn) - vcenter(item) + vCenterOffset); setItemPos(p); @@ -225,32 +255,32 @@ void QQuickAnchorsPrivate::clearItem(QQuickItem *item) fill = 0; if (centerIn == item) centerIn = 0; - if (left.item == item) { - left.item = 0; + if (leftAnchorItem == item) { + leftAnchorItem = 0; usedAnchors &= ~QQuickAnchors::LeftAnchor; } - if (right.item == item) { - right.item = 0; + if (rightAnchorItem == item) { + rightAnchorItem = 0; usedAnchors &= ~QQuickAnchors::RightAnchor; } - if (top.item == item) { - top.item = 0; + if (topAnchorItem == item) { + topAnchorItem = 0; usedAnchors &= ~QQuickAnchors::TopAnchor; } - if (bottom.item == item) { - bottom.item = 0; + if (bottomAnchorItem == item) { + bottomAnchorItem = 0; usedAnchors &= ~QQuickAnchors::BottomAnchor; } - if (vCenter.item == item) { - vCenter.item = 0; + if (vCenterAnchorItem == item) { + vCenterAnchorItem = 0; usedAnchors &= ~QQuickAnchors::VCenterAnchor; } - if (hCenter.item == item) { - hCenter.item = 0; + if (hCenterAnchorItem == item) { + hCenterAnchorItem = 0; usedAnchors &= ~QQuickAnchors::HCenterAnchor; } - if (baseline.item == item) { - baseline.item = 0; + if (baselineAnchorItem == item) { + baselineAnchorItem = 0; usedAnchors &= ~QQuickAnchors::BaselineAnchor; } } @@ -263,7 +293,7 @@ int QQuickAnchorsPrivate::calculateDependency(QQuickItem *controlItem) return dependency; if (fill == controlItem) { - if (controlItem == item->parentItem()) + if (controlItem == readParentItem(item)) dependency |= QQuickItemPrivate::SizeChange; else //sibling dependency |= QQuickItemPrivate::GeometryChange; @@ -271,27 +301,27 @@ int QQuickAnchorsPrivate::calculateDependency(QQuickItem *controlItem) } if (centerIn == controlItem) { - if (controlItem == item->parentItem()) + if (controlItem == readParentItem(item)) dependency |= QQuickItemPrivate::SizeChange; else //sibling dependency |= QQuickItemPrivate::GeometryChange; return dependency; //exit early } - if ((usedAnchors & QQuickAnchors::LeftAnchor && left.item == controlItem) || - (usedAnchors & QQuickAnchors::RightAnchor && right.item == controlItem) || - (usedAnchors & QQuickAnchors::HCenterAnchor && hCenter.item == controlItem)) { - if (controlItem == item->parentItem()) + if ((usedAnchors & QQuickAnchors::LeftAnchor && leftAnchorItem == controlItem) || + (usedAnchors & QQuickAnchors::RightAnchor && rightAnchorItem == controlItem) || + (usedAnchors & QQuickAnchors::HCenterAnchor && hCenterAnchorItem == controlItem)) { + if (controlItem == readParentItem(item)) dependency |= QQuickItemPrivate::WidthChange; else //sibling dependency |= QFlags<QQuickItemPrivate::GeometryChangeType>(QQuickItemPrivate::XChange | QQuickItemPrivate::WidthChange); } - if ((usedAnchors & QQuickAnchors::TopAnchor && top.item == controlItem) || - (usedAnchors & QQuickAnchors::BottomAnchor && bottom.item == controlItem) || - (usedAnchors & QQuickAnchors::VCenterAnchor && vCenter.item == controlItem) || - (usedAnchors & QQuickAnchors::BaselineAnchor && baseline.item == controlItem)) { - if (controlItem == item->parentItem()) + if ((usedAnchors & QQuickAnchors::TopAnchor && topAnchorItem == controlItem) || + (usedAnchors & QQuickAnchors::BottomAnchor && bottomAnchorItem == controlItem) || + (usedAnchors & QQuickAnchors::VCenterAnchor && vCenterAnchorItem == controlItem) || + (usedAnchors & QQuickAnchors::BaselineAnchor && baselineAnchorItem == controlItem)) { + if (controlItem == readParentItem(item)) dependency |= QQuickItemPrivate::HeightChange; else //sibling dependency |= QFlags<QQuickItemPrivate::GeometryChangeType>(QQuickItemPrivate::YChange | QQuickItemPrivate::HeightChange); @@ -422,13 +452,13 @@ void QQuickAnchorsPrivate::updateOnComplete() QQuickItem *dependencies[9]; dependencies[0] = fill; dependencies[1] = centerIn; - dependencies[2] = left.item; - dependencies[3] = right.item; - dependencies[4] = hCenter.item; - dependencies[5] = top.item; - dependencies[6] = bottom.item; - dependencies[7] = vCenter.item; - dependencies[8] = baseline.item; + dependencies[2] = leftAnchorItem; + dependencies[3] = rightAnchorItem; + dependencies[4] = hCenterAnchorItem; + dependencies[5] = topAnchorItem; + dependencies[6] = bottomAnchorItem; + dependencies[7] = vCenterAnchorItem; + dependencies[8] = baselineAnchorItem; std::sort(dependencies, dependencies + 9); @@ -447,22 +477,38 @@ void QQuickAnchorsPrivate::updateOnComplete() void QQuickAnchorsPrivate::update() { - fillChanged(); - centerInChanged(); - if (usedAnchors & QQuickAnchorLine::Horizontal_Mask) - updateHorizontalAnchors(); - if (usedAnchors & QQuickAnchorLine::Vertical_Mask) - updateVerticalAnchors(); + if (!isItemComplete()) + return; + + if (fill) { + fillChanged(); + } else if (centerIn) { + centerInChanged(); + } else { + if (usedAnchors & QQuickAnchors::Horizontal_Mask) + updateHorizontalAnchors(); + if (usedAnchors & QQuickAnchors::Vertical_Mask) + updateVerticalAnchors(); + } } void QQuickAnchorsPrivate::itemGeometryChanged(QQuickItem *, const QRectF &newG, const QRectF &oldG) { - fillChanged(); - centerInChanged(); - if ((usedAnchors & QQuickAnchorLine::Horizontal_Mask) && (newG.x() != oldG.x() || newG.width() != oldG.width())) - updateHorizontalAnchors(); - if ((usedAnchors & QQuickAnchorLine::Vertical_Mask) && (newG.y() != oldG.y() || newG.height() != oldG.height())) - updateVerticalAnchors(); + if (!isItemComplete()) + return; + + if (fill) { + fillChanged(); + } else if (centerIn) { + centerInChanged(); + } else { + if ((usedAnchors & QQuickAnchors::Horizontal_Mask) + && (newG.x() != oldG.x() || newG.width() != oldG.width())) + updateHorizontalAnchors(); + if ((usedAnchors & QQuickAnchors::Vertical_Mask) + && (newG.y() != oldG.y() || newG.height() != oldG.height())) + updateVerticalAnchors(); + } } QQuickItem *QQuickAnchors::fill() const @@ -484,7 +530,7 @@ void QQuickAnchors::setFill(QQuickItem *f) emit fillChanged(); return; } - if (f != d->item->parentItem() && f->parentItem() != d->item->parentItem()){ + if (f != readParentItem(d->item) && readParentItem(f) != readParentItem(d->item)){ qmlInfo(d->item) << tr("Cannot anchor to an item that isn't a parent or sibling."); return; } @@ -520,7 +566,7 @@ void QQuickAnchors::setCenterIn(QQuickItem* c) emit centerInChanged(); return; } - if (c != d->item->parentItem() && c->parentItem() != d->item->parentItem()){ + if (c != readParentItem(d->item) && readParentItem(c) != readParentItem(d->item)){ qmlInfo(d->item) << tr("Cannot anchor to an item that isn't a parent or sibling."); return; } @@ -537,29 +583,31 @@ void QQuickAnchors::resetCenterIn() setCenterIn(0); } -bool QQuickAnchorsPrivate::calcStretch(const QQuickAnchorLine &edge1, - const QQuickAnchorLine &edge2, - qreal offset1, - qreal offset2, - QQuickAnchorLine::AnchorLine line, - qreal &stretch) +bool QQuickAnchorsPrivate::calcStretch(QQuickItem *edge1Item, + QQuickAnchors::Anchor edge1Line, + QQuickItem *edge2Item, + QQuickAnchors::Anchor edge2Line, + qreal offset1, + qreal offset2, + QQuickAnchors::Anchor line, + qreal &stretch) { - bool edge1IsParent = (edge1.item == item->parentItem()); - bool edge2IsParent = (edge2.item == item->parentItem()); - bool edge1IsSibling = (edge1.item->parentItem() == item->parentItem()); - bool edge2IsSibling = (edge2.item->parentItem() == item->parentItem()); + bool edge1IsParent = (edge1Item == readParentItem(item)); + bool edge2IsParent = (edge2Item == readParentItem(item)); + bool edge1IsSibling = (readParentItem(edge1Item) == readParentItem(item)); + bool edge2IsSibling = (readParentItem(edge2Item) == readParentItem(item)); bool invalid = false; if ((edge2IsParent && edge1IsParent) || (edge2IsSibling && edge1IsSibling)) { - stretch = (position(edge2.item, edge2.anchorLine) + offset2) - - (position(edge1.item, edge1.anchorLine) + offset1); + stretch = (position(edge2Item, edge2Line) + offset2) + - (position(edge1Item, edge1Line) + offset1); } else if (edge2IsParent && edge1IsSibling) { - stretch = (position(edge2.item, edge2.anchorLine) + offset2) - - (position(item->parentItem(), line) - + position(edge1.item, edge1.anchorLine) + offset1); + stretch = (position(edge2Item, edge2Line) + offset2) + - (position(readParentItem(item), line) + + position(edge1Item, edge1Line) + offset1); } else if (edge2IsSibling && edge1IsParent) { - stretch = (position(item->parentItem(), line) + position(edge2.item, edge2.anchorLine) + offset2) - - (position(edge1.item, edge1.anchorLine) + offset1); + stretch = (position(readParentItem(item), line) + position(edge2Item, edge2Line) + offset2) + - (position(edge1Item, edge1Line) + offset1); } else invalid = true; @@ -578,52 +626,60 @@ void QQuickAnchorsPrivate::updateVerticalAnchors() bool invalid = true; qreal height = 0.0; if (usedAnchors & QQuickAnchors::BottomAnchor) { - invalid = calcStretch(top, bottom, topMargin, -bottomMargin, QQuickAnchorLine::Top, height); + invalid = calcStretch(topAnchorItem, topAnchorLine, + bottomAnchorItem, bottomAnchorLine, + topMargin, -bottomMargin, QQuickAnchors::TopAnchor, height); } else if (usedAnchors & QQuickAnchors::VCenterAnchor) { - invalid = calcStretch(top, vCenter, topMargin, vCenterOffset, QQuickAnchorLine::Top, height); + invalid = calcStretch(topAnchorItem, topAnchorLine, + vCenterAnchorItem, vCenterAnchorLine, + topMargin, vCenterOffset, QQuickAnchors::TopAnchor, height); height *= 2; } if (!invalid) setItemHeight(height); //Handle top - if (top.item == item->parentItem()) { - setItemY(adjustedPosition(top.item, top.anchorLine) + topMargin); - } else if (top.item->parentItem() == item->parentItem()) { - setItemY(position(top.item, top.anchorLine) + topMargin); + if (topAnchorItem == readParentItem(item)) { + setItemY(adjustedPosition(topAnchorItem, topAnchorLine) + topMargin); + } else if (readParentItem(topAnchorItem) == readParentItem(item)) { + setItemY(position(topAnchorItem, topAnchorLine) + topMargin); } } else if (usedAnchors & QQuickAnchors::BottomAnchor) { //Handle stretching (top + bottom case is handled above) if (usedAnchors & QQuickAnchors::VCenterAnchor) { qreal height = 0.0; - bool invalid = calcStretch(vCenter, bottom, vCenterOffset, -bottomMargin, - QQuickAnchorLine::Top, height); + bool invalid = calcStretch(vCenterAnchorItem, vCenterAnchorLine, + bottomAnchorItem, bottomAnchorLine, + vCenterOffset, -bottomMargin, QQuickAnchors::TopAnchor, + height); if (!invalid) setItemHeight(height*2); } //Handle bottom - if (bottom.item == item->parentItem()) { - setItemY(adjustedPosition(bottom.item, bottom.anchorLine) - item->height() - bottomMargin); - } else if (bottom.item->parentItem() == item->parentItem()) { - setItemY(position(bottom.item, bottom.anchorLine) - item->height() - bottomMargin); + if (bottomAnchorItem == readParentItem(item)) { + setItemY(adjustedPosition(bottomAnchorItem, bottomAnchorLine) - readHeight(item) - bottomMargin); + } else if (readParentItem(bottomAnchorItem) == readParentItem(item)) { + setItemY(position(bottomAnchorItem, bottomAnchorLine) - readHeight(item) - bottomMargin); } } else if (usedAnchors & QQuickAnchors::VCenterAnchor) { //(stetching handled above) //Handle vCenter - if (vCenter.item == item->parentItem()) { - setItemY(adjustedPosition(vCenter.item, vCenter.anchorLine) + if (vCenterAnchorItem == readParentItem(item)) { + setItemY(adjustedPosition(vCenterAnchorItem, vCenterAnchorLine) - vcenter(item) + vCenterOffset); - } else if (vCenter.item->parentItem() == item->parentItem()) { - setItemY(position(vCenter.item, vCenter.anchorLine) - vcenter(item) + vCenterOffset); + } else if (readParentItem(vCenterAnchorItem) == readParentItem(item)) { + setItemY(position(vCenterAnchorItem, vCenterAnchorLine) - vcenter(item) + vCenterOffset); } } else if (usedAnchors & QQuickAnchors::BaselineAnchor) { //Handle baseline - if (baseline.item == item->parentItem()) { - setItemY(adjustedPosition(baseline.item, baseline.anchorLine) - item->baselineOffset() + baselineOffset); - } else if (baseline.item->parentItem() == item->parentItem()) { - setItemY(position(baseline.item, baseline.anchorLine) - item->baselineOffset() + baselineOffset); + if (baselineAnchorItem == readParentItem(item)) { + setItemY(adjustedPosition(baselineAnchorItem, baselineAnchorLine) + - readBaselineOffset(item) + baselineOffset); + } else if (readParentItem(baselineAnchorItem) == readParentItem(item)) { + setItemY(position(baselineAnchorItem, baselineAnchorLine) + - readBaselineOffset(item) + baselineOffset); } } --updatingVerticalAnchor; @@ -633,12 +689,12 @@ void QQuickAnchorsPrivate::updateVerticalAnchors() } } -inline QQuickAnchorLine::AnchorLine reverseAnchorLine(QQuickAnchorLine::AnchorLine anchorLine) +static inline QQuickAnchors::Anchor reverseAnchorLine(QQuickAnchors::Anchor anchorLine) { - if (anchorLine == QQuickAnchorLine::Left) { - return QQuickAnchorLine::Right; - } else if (anchorLine == QQuickAnchorLine::Right) { - return QQuickAnchorLine::Left; + if (anchorLine == QQuickAnchors::LeftAnchor) { + return QQuickAnchors::RightAnchor; + } else if (anchorLine == QQuickAnchors::RightAnchor) { + return QQuickAnchors::LeftAnchor; } else { return anchorLine; } @@ -653,26 +709,30 @@ void QQuickAnchorsPrivate::updateHorizontalAnchors() if (updatingHorizontalAnchor < 3) { ++updatingHorizontalAnchor; qreal effectiveRightMargin, effectiveLeftMargin, effectiveHorizontalCenterOffset; - QQuickAnchorLine effectiveLeft, effectiveRight, effectiveHorizontalCenter; + QQuickItem *effectiveLeftItem, *effectiveRightItem, *effectiveHorizontalCenterItem; + QQuickAnchors::Anchor effectiveLeftLine, effectiveRightLine, effectiveHorizontalCenterLine; QQuickAnchors::Anchor effectiveLeftAnchor, effectiveRightAnchor; if (q->mirrored()) { effectiveLeftAnchor = QQuickAnchors::RightAnchor; effectiveRightAnchor = QQuickAnchors::LeftAnchor; - effectiveLeft.item = right.item; - effectiveLeft.anchorLine = reverseAnchorLine(right.anchorLine); - effectiveRight.item = left.item; - effectiveRight.anchorLine = reverseAnchorLine(left.anchorLine); - effectiveHorizontalCenter.item = hCenter.item; - effectiveHorizontalCenter.anchorLine = reverseAnchorLine(hCenter.anchorLine); + effectiveLeftItem = rightAnchorItem; + effectiveLeftLine = reverseAnchorLine(rightAnchorLine); + effectiveRightItem = leftAnchorItem; + effectiveRightLine = reverseAnchorLine(leftAnchorLine); + effectiveHorizontalCenterItem = hCenterAnchorItem; + effectiveHorizontalCenterLine = reverseAnchorLine(hCenterAnchorLine); effectiveLeftMargin = rightMargin; effectiveRightMargin = leftMargin; effectiveHorizontalCenterOffset = -hCenterOffset; } else { effectiveLeftAnchor = QQuickAnchors::LeftAnchor; effectiveRightAnchor = QQuickAnchors::RightAnchor; - effectiveLeft = left; - effectiveRight = right; - effectiveHorizontalCenter = hCenter; + effectiveLeftItem = leftAnchorItem; + effectiveLeftLine = leftAnchorLine; + effectiveRightItem = rightAnchorItem; + effectiveRightLine = rightAnchorLine; + effectiveHorizontalCenterItem = hCenterAnchorItem; + effectiveHorizontalCenterLine = hCenterAnchorLine; effectiveLeftMargin = leftMargin; effectiveRightMargin = rightMargin; effectiveHorizontalCenterOffset = hCenterOffset; @@ -683,42 +743,53 @@ void QQuickAnchorsPrivate::updateHorizontalAnchors() bool invalid = true; qreal width = 0.0; if (usedAnchors & effectiveRightAnchor) { - invalid = calcStretch(effectiveLeft, effectiveRight, effectiveLeftMargin, -effectiveRightMargin, QQuickAnchorLine::Left, width); + invalid = calcStretch(effectiveLeftItem, effectiveLeftLine, + effectiveRightItem, effectiveRightLine, + effectiveLeftMargin, -effectiveRightMargin, + QQuickAnchors::LeftAnchor, width); } else if (usedAnchors & QQuickAnchors::HCenterAnchor) { - invalid = calcStretch(effectiveLeft, effectiveHorizontalCenter, effectiveLeftMargin, effectiveHorizontalCenterOffset, QQuickAnchorLine::Left, width); + invalid = calcStretch(effectiveLeftItem, effectiveLeftLine, + effectiveHorizontalCenterItem, effectiveHorizontalCenterLine, + effectiveLeftMargin, effectiveHorizontalCenterOffset, + QQuickAnchors::LeftAnchor, width); width *= 2; } if (!invalid) setItemWidth(width); //Handle left - if (effectiveLeft.item == item->parentItem()) { - setItemX(adjustedPosition(effectiveLeft.item, effectiveLeft.anchorLine) + effectiveLeftMargin); - } else if (effectiveLeft.item->parentItem() == item->parentItem()) { - setItemX(position(effectiveLeft.item, effectiveLeft.anchorLine) + effectiveLeftMargin); + if (effectiveLeftItem == readParentItem(item)) { + setItemX(adjustedPosition(effectiveLeftItem, effectiveLeftLine) + effectiveLeftMargin); + } else if (readParentItem(effectiveLeftItem) == readParentItem(item)) { + setItemX(position(effectiveLeftItem, effectiveLeftLine) + effectiveLeftMargin); } } else if (usedAnchors & effectiveRightAnchor) { //Handle stretching (left + right case is handled in updateLeftAnchor) if (usedAnchors & QQuickAnchors::HCenterAnchor) { qreal width = 0.0; - bool invalid = calcStretch(effectiveHorizontalCenter, effectiveRight, effectiveHorizontalCenterOffset, -effectiveRightMargin, - QQuickAnchorLine::Left, width); + bool invalid = calcStretch(effectiveHorizontalCenterItem, + effectiveHorizontalCenterLine, + effectiveRightItem, effectiveRightLine, + effectiveHorizontalCenterOffset, -effectiveRightMargin, + QQuickAnchors::LeftAnchor, width); if (!invalid) setItemWidth(width*2); } //Handle right - if (effectiveRight.item == item->parentItem()) { - setItemX(adjustedPosition(effectiveRight.item, effectiveRight.anchorLine) - item->width() - effectiveRightMargin); - } else if (effectiveRight.item->parentItem() == item->parentItem()) { - setItemX(position(effectiveRight.item, effectiveRight.anchorLine) - item->width() - effectiveRightMargin); + if (effectiveRightItem == readParentItem(item)) { + setItemX(adjustedPosition(effectiveRightItem, effectiveRightLine) + - readWidth(item) - effectiveRightMargin); + } else if (readParentItem(effectiveRightItem) == readParentItem(item)) { + setItemX(position(effectiveRightItem, effectiveRightLine) + - readWidth(item) - effectiveRightMargin); } } else if (usedAnchors & QQuickAnchors::HCenterAnchor) { //Handle hCenter - if (effectiveHorizontalCenter.item == item->parentItem()) { - setItemX(adjustedPosition(effectiveHorizontalCenter.item, effectiveHorizontalCenter.anchorLine) - hcenter(item) + effectiveHorizontalCenterOffset); - } else if (effectiveHorizontalCenter.item->parentItem() == item->parentItem()) { - setItemX(position(effectiveHorizontalCenter.item, effectiveHorizontalCenter.anchorLine) - hcenter(item) + effectiveHorizontalCenterOffset); + if (effectiveHorizontalCenterItem == readParentItem(item)) { + setItemX(adjustedPosition(effectiveHorizontalCenterItem, effectiveHorizontalCenterLine) - hcenter(item) + effectiveHorizontalCenterOffset); + } else if (readParentItem(effectiveHorizontalCenterItem) == readParentItem(item)) { + setItemX(position(effectiveHorizontalCenterItem, effectiveHorizontalCenterLine) - hcenter(item) + effectiveHorizontalCenterOffset); } } --updatingHorizontalAnchor; @@ -731,13 +802,14 @@ void QQuickAnchorsPrivate::updateHorizontalAnchors() QQuickAnchorLine QQuickAnchors::top() const { Q_D(const QQuickAnchors); - return d->top; + return QQuickAnchorLine(d->topAnchorItem, d->topAnchorLine); } void QQuickAnchors::setTop(const QQuickAnchorLine &edge) { Q_D(QQuickAnchors); - if (!d->checkVAnchorValid(edge) || d->top == edge) + if (!d->checkVAnchorValid(edge) || + (d->topAnchorItem == edge.item && d->topAnchorLine == edge.anchorLine)) return; d->usedAnchors |= TopAnchor; @@ -747,10 +819,11 @@ void QQuickAnchors::setTop(const QQuickAnchorLine &edge) return; } - QQuickItem *oldTop = d->top.item; - d->top = edge; + QQuickItem *oldTop = d->topAnchorItem; + d->topAnchorItem = edge.item; + d->topAnchorLine = edge.anchorLine; d->remDepend(oldTop); - d->addDepend(d->top.item); + d->addDepend(d->topAnchorItem); emit topChanged(); d->updateVerticalAnchors(); } @@ -759,8 +832,9 @@ void QQuickAnchors::resetTop() { Q_D(QQuickAnchors); d->usedAnchors &= ~TopAnchor; - d->remDepend(d->top.item); - d->top = QQuickAnchorLine(); + d->remDepend(d->topAnchorItem); + d->topAnchorItem = Q_NULLPTR; + d->topAnchorLine = QQuickAnchors::InvalidAnchor; emit topChanged(); d->updateVerticalAnchors(); } @@ -768,13 +842,14 @@ void QQuickAnchors::resetTop() QQuickAnchorLine QQuickAnchors::bottom() const { Q_D(const QQuickAnchors); - return d->bottom; + return QQuickAnchorLine(d->bottomAnchorItem, d->bottomAnchorLine); } void QQuickAnchors::setBottom(const QQuickAnchorLine &edge) { Q_D(QQuickAnchors); - if (!d->checkVAnchorValid(edge) || d->bottom == edge) + if (!d->checkVAnchorValid(edge) || + (d->bottomAnchorItem == edge.item && d->bottomAnchorLine == edge.anchorLine)) return; d->usedAnchors |= BottomAnchor; @@ -784,10 +859,11 @@ void QQuickAnchors::setBottom(const QQuickAnchorLine &edge) return; } - QQuickItem *oldBottom = d->bottom.item; - d->bottom = edge; + QQuickItem *oldBottom = d->bottomAnchorItem; + d->bottomAnchorItem = edge.item; + d->bottomAnchorLine = edge.anchorLine; d->remDepend(oldBottom); - d->addDepend(d->bottom.item); + d->addDepend(d->bottomAnchorItem); emit bottomChanged(); d->updateVerticalAnchors(); } @@ -796,8 +872,9 @@ void QQuickAnchors::resetBottom() { Q_D(QQuickAnchors); d->usedAnchors &= ~BottomAnchor; - d->remDepend(d->bottom.item); - d->bottom = QQuickAnchorLine(); + d->remDepend(d->bottomAnchorItem); + d->bottomAnchorItem = Q_NULLPTR; + d->bottomAnchorLine = QQuickAnchors::InvalidAnchor; emit bottomChanged(); d->updateVerticalAnchors(); } @@ -805,13 +882,14 @@ void QQuickAnchors::resetBottom() QQuickAnchorLine QQuickAnchors::verticalCenter() const { Q_D(const QQuickAnchors); - return d->vCenter; + return QQuickAnchorLine(d->vCenterAnchorItem, d->vCenterAnchorLine); } void QQuickAnchors::setVerticalCenter(const QQuickAnchorLine &edge) { Q_D(QQuickAnchors); - if (!d->checkVAnchorValid(edge) || d->vCenter == edge) + if (!d->checkVAnchorValid(edge) || + (d->vCenterAnchorItem == edge.item && d->vCenterAnchorLine == edge.anchorLine)) return; d->usedAnchors |= VCenterAnchor; @@ -821,10 +899,11 @@ void QQuickAnchors::setVerticalCenter(const QQuickAnchorLine &edge) return; } - QQuickItem *oldVCenter = d->vCenter.item; - d->vCenter = edge; + QQuickItem *oldVCenter = d->vCenterAnchorItem; + d->vCenterAnchorItem = edge.item; + d->vCenterAnchorLine = edge.anchorLine; d->remDepend(oldVCenter); - d->addDepend(d->vCenter.item); + d->addDepend(d->vCenterAnchorItem); emit verticalCenterChanged(); d->updateVerticalAnchors(); } @@ -833,8 +912,9 @@ void QQuickAnchors::resetVerticalCenter() { Q_D(QQuickAnchors); d->usedAnchors &= ~VCenterAnchor; - d->remDepend(d->vCenter.item); - d->vCenter = QQuickAnchorLine(); + d->remDepend(d->vCenterAnchorItem); + d->vCenterAnchorItem = Q_NULLPTR; + d->vCenterAnchorLine = QQuickAnchors::InvalidAnchor; emit verticalCenterChanged(); d->updateVerticalAnchors(); } @@ -842,13 +922,14 @@ void QQuickAnchors::resetVerticalCenter() QQuickAnchorLine QQuickAnchors::baseline() const { Q_D(const QQuickAnchors); - return d->baseline; + return QQuickAnchorLine(d->baselineAnchorItem, d->baselineAnchorLine); } void QQuickAnchors::setBaseline(const QQuickAnchorLine &edge) { Q_D(QQuickAnchors); - if (!d->checkVAnchorValid(edge) || d->baseline == edge) + if (!d->checkVAnchorValid(edge) || + (d->baselineAnchorItem == edge.item && d->baselineAnchorLine == edge.anchorLine)) return; d->usedAnchors |= BaselineAnchor; @@ -858,10 +939,11 @@ void QQuickAnchors::setBaseline(const QQuickAnchorLine &edge) return; } - QQuickItem *oldBaseline = d->baseline.item; - d->baseline = edge; + QQuickItem *oldBaseline = d->baselineAnchorItem; + d->baselineAnchorItem = edge.item; + d->baselineAnchorLine = edge.anchorLine; d->remDepend(oldBaseline); - d->addDepend(d->baseline.item); + d->addDepend(d->baselineAnchorItem); emit baselineChanged(); d->updateVerticalAnchors(); } @@ -870,8 +952,9 @@ void QQuickAnchors::resetBaseline() { Q_D(QQuickAnchors); d->usedAnchors &= ~BaselineAnchor; - d->remDepend(d->baseline.item); - d->baseline = QQuickAnchorLine(); + d->remDepend(d->baselineAnchorItem); + d->baselineAnchorItem = Q_NULLPTR; + d->baselineAnchorLine = QQuickAnchors::InvalidAnchor; emit baselineChanged(); d->updateVerticalAnchors(); } @@ -879,13 +962,14 @@ void QQuickAnchors::resetBaseline() QQuickAnchorLine QQuickAnchors::left() const { Q_D(const QQuickAnchors); - return d->left; + return QQuickAnchorLine(d->leftAnchorItem, d->leftAnchorLine); } void QQuickAnchors::setLeft(const QQuickAnchorLine &edge) { Q_D(QQuickAnchors); - if (!d->checkHAnchorValid(edge) || d->left == edge) + if (!d->checkHAnchorValid(edge) || + (d->leftAnchorItem == edge.item && d->leftAnchorLine == edge.anchorLine)) return; d->usedAnchors |= LeftAnchor; @@ -895,10 +979,11 @@ void QQuickAnchors::setLeft(const QQuickAnchorLine &edge) return; } - QQuickItem *oldLeft = d->left.item; - d->left = edge; + QQuickItem *oldLeft = d->leftAnchorItem; + d->leftAnchorItem = edge.item; + d->leftAnchorLine = edge.anchorLine; d->remDepend(oldLeft); - d->addDepend(d->left.item); + d->addDepend(d->leftAnchorItem); emit leftChanged(); d->updateHorizontalAnchors(); } @@ -907,8 +992,9 @@ void QQuickAnchors::resetLeft() { Q_D(QQuickAnchors); d->usedAnchors &= ~LeftAnchor; - d->remDepend(d->left.item); - d->left = QQuickAnchorLine(); + d->remDepend(d->leftAnchorItem); + d->leftAnchorItem = Q_NULLPTR; + d->leftAnchorLine = QQuickAnchors::InvalidAnchor; emit leftChanged(); d->updateHorizontalAnchors(); } @@ -916,13 +1002,14 @@ void QQuickAnchors::resetLeft() QQuickAnchorLine QQuickAnchors::right() const { Q_D(const QQuickAnchors); - return d->right; + return QQuickAnchorLine(d->rightAnchorItem, d->rightAnchorLine); } void QQuickAnchors::setRight(const QQuickAnchorLine &edge) { Q_D(QQuickAnchors); - if (!d->checkHAnchorValid(edge) || d->right == edge) + if (!d->checkHAnchorValid(edge) || + (d->rightAnchorItem == edge.item && d->rightAnchorLine == edge.anchorLine)) return; d->usedAnchors |= RightAnchor; @@ -932,10 +1019,11 @@ void QQuickAnchors::setRight(const QQuickAnchorLine &edge) return; } - QQuickItem *oldRight = d->right.item; - d->right = edge; + QQuickItem *oldRight = d->rightAnchorItem; + d->rightAnchorItem = edge.item; + d->rightAnchorLine = edge.anchorLine; d->remDepend(oldRight); - d->addDepend(d->right.item); + d->addDepend(d->rightAnchorItem); emit rightChanged(); d->updateHorizontalAnchors(); } @@ -944,8 +1032,9 @@ void QQuickAnchors::resetRight() { Q_D(QQuickAnchors); d->usedAnchors &= ~RightAnchor; - d->remDepend(d->right.item); - d->right = QQuickAnchorLine(); + d->remDepend(d->rightAnchorItem); + d->rightAnchorItem = Q_NULLPTR; + d->rightAnchorLine = QQuickAnchors::InvalidAnchor; emit rightChanged(); d->updateHorizontalAnchors(); } @@ -953,13 +1042,14 @@ void QQuickAnchors::resetRight() QQuickAnchorLine QQuickAnchors::horizontalCenter() const { Q_D(const QQuickAnchors); - return d->hCenter; + return QQuickAnchorLine(d->hCenterAnchorItem, d->hCenterAnchorLine); } void QQuickAnchors::setHorizontalCenter(const QQuickAnchorLine &edge) { Q_D(QQuickAnchors); - if (!d->checkHAnchorValid(edge) || d->hCenter == edge) + if (!d->checkHAnchorValid(edge) || + (d->hCenterAnchorItem == edge.item && d->hCenterAnchorLine == edge.anchorLine)) return; d->usedAnchors |= HCenterAnchor; @@ -969,10 +1059,11 @@ void QQuickAnchors::setHorizontalCenter(const QQuickAnchorLine &edge) return; } - QQuickItem *oldHCenter = d->hCenter.item; - d->hCenter = edge; + QQuickItem *oldHCenter = d->hCenterAnchorItem; + d->hCenterAnchorItem = edge.item; + d->hCenterAnchorLine = edge.anchorLine; d->remDepend(oldHCenter); - d->addDepend(d->hCenter.item); + d->addDepend(d->hCenterAnchorItem); emit horizontalCenterChanged(); d->updateHorizontalAnchors(); } @@ -981,8 +1072,9 @@ void QQuickAnchors::resetHorizontalCenter() { Q_D(QQuickAnchors); d->usedAnchors &= ~HCenterAnchor; - d->remDepend(d->hCenter.item); - d->hCenter = QQuickAnchorLine(); + d->remDepend(d->hCenterAnchorItem); + d->hCenterAnchorItem = Q_NULLPTR; + d->hCenterAnchorLine = QQuickAnchors::InvalidAnchor; emit horizontalCenterChanged(); d->updateHorizontalAnchors(); } @@ -1230,7 +1322,7 @@ void QQuickAnchors::setBaselineOffset(qreal offset) QQuickAnchors::Anchors QQuickAnchors::usedAnchors() const { Q_D(const QQuickAnchors); - return d->usedAnchors; + return static_cast<QQuickAnchors::Anchors>(d->usedAnchors); } bool QQuickAnchorsPrivate::checkHValid() const @@ -1250,10 +1342,11 @@ bool QQuickAnchorsPrivate::checkHAnchorValid(QQuickAnchorLine anchor) const if (!anchor.item) { qmlInfo(item) << QQuickAnchors::tr("Cannot anchor to a null item."); return false; - } else if (anchor.anchorLine & QQuickAnchorLine::Vertical_Mask) { + } else if (anchor.anchorLine & QQuickAnchors::Vertical_Mask) { qmlInfo(item) << QQuickAnchors::tr("Cannot anchor a horizontal edge to a vertical edge."); return false; - } else if (anchor.item != item->parentItem() && anchor.item->parentItem() != item->parentItem()){ + } else if (anchor.item != readParentItem(item) + && readParentItem(anchor.item) != readParentItem(item)) { qmlInfo(item) << QQuickAnchors::tr("Cannot anchor to an item that isn't a parent or sibling."); return false; } else if (anchor.item == item) { @@ -1287,10 +1380,11 @@ bool QQuickAnchorsPrivate::checkVAnchorValid(QQuickAnchorLine anchor) const if (!anchor.item) { qmlInfo(item) << QQuickAnchors::tr("Cannot anchor to a null item."); return false; - } else if (anchor.anchorLine & QQuickAnchorLine::Horizontal_Mask) { + } else if (anchor.anchorLine & QQuickAnchors::Horizontal_Mask) { qmlInfo(item) << QQuickAnchors::tr("Cannot anchor a vertical edge to a horizontal edge."); return false; - } else if (anchor.item != item->parentItem() && anchor.item->parentItem() != item->parentItem()){ + } else if (anchor.item != readParentItem(item) + && readParentItem(anchor.item) != readParentItem(item)) { qmlInfo(item) << QQuickAnchors::tr("Cannot anchor to an item that isn't a parent or sibling."); return false; } else if (anchor.item == item){ |