67static inline qreal position(
const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
71 case QQuickAnchors::LeftAnchor:
74 case QQuickAnchors::RightAnchor:
75 ret = readX(item) + readWidth(item);
77 case QQuickAnchors::TopAnchor:
80 case QQuickAnchors::BottomAnchor:
81 ret = readY(item) + readHeight(item);
83 case QQuickAnchors::HCenterAnchor:
84 ret = readX(item) + hcenter(item);
86 case QQuickAnchors::VCenterAnchor:
87 ret = readY(item) + vcenter(item);
89 case QQuickAnchors::BaselineAnchor:
90 ret = readY(item) + readBaselineOffset(item);
542 QQuickAnchors::Anchor edge1Line,
543 QQuickItem *edge2Item,
544 QQuickAnchors::Anchor edge2Line,
547 QQuickAnchors::Anchor line,
548 qreal &stretch)
const
550 bool edge1IsParent = (edge1Item == readParentItem(
item));
551 bool edge2IsParent = (edge2Item == readParentItem(
item));
552 bool edge1IsSibling = (readParentItem(edge1Item) == readParentItem(
item));
553 bool edge2IsSibling = (readParentItem(edge2Item) == readParentItem(
item));
555 bool invalid =
false;
556 if ((edge2IsParent && edge1IsParent) || (edge2IsSibling && edge1IsSibling)) {
557 stretch = (position(edge2Item, edge2Line) + offset2)
558 - (position(edge1Item, edge1Line) + offset1);
559 }
else if (edge2IsParent && edge1IsSibling) {
560 stretch = (position(edge2Item, edge2Line) + offset2)
561 - (position(readParentItem(item), line)
562 + position(edge1Item, edge1Line) + offset1);
563 }
else if (edge2IsSibling && edge1IsParent) {
564 stretch = (position(readParentItem(item), line) + position(edge2Item, edge2Line) + offset2)
565 - (position(edge1Item, edge1Line) + offset1);
577 if (Q_UNLIKELY(updatingVerticalAnchor > 1)) {
579 qmlWarning(item) << QQuickAnchors::tr(
"Possible anchor loop detected on vertical anchor.");
583 ++updatingVerticalAnchor;
584 if (usedAnchors & QQuickAnchors::TopAnchor) {
588 if (usedAnchors & QQuickAnchors::BottomAnchor) {
589 invalid = calcStretch(topAnchorItem, topAnchorLine,
590 bottomAnchorItem, bottomAnchorLine,
591 topMargin, -bottomMargin, QQuickAnchors::TopAnchor, height);
592 }
else if (usedAnchors & QQuickAnchors::VCenterAnchor) {
593 invalid = calcStretch(topAnchorItem, topAnchorLine,
594 vCenterAnchorItem, vCenterAnchorLine,
595 topMargin, vCenterOffset, QQuickAnchors::TopAnchor, height);
599 setItemHeight(height);
603 setItemY(adjustedPosition(topAnchorItem, topAnchorLine) + topMargin);
605 setItemY(position(topAnchorItem, topAnchorLine) + topMargin);
607 }
else if (usedAnchors & QQuickAnchors::BottomAnchor) {
609 if (usedAnchors & QQuickAnchors::VCenterAnchor) {
611 bool invalid = calcStretch(vCenterAnchorItem, vCenterAnchorLine,
612 bottomAnchorItem, bottomAnchorLine,
613 vCenterOffset, -bottomMargin, QQuickAnchors::TopAnchor,
616 setItemHeight(height*2);
621 setItemY(adjustedPosition(bottomAnchorItem, bottomAnchorLine) - readHeight(item) - bottomMargin);
623 setItemY(position(bottomAnchorItem, bottomAnchorLine) - readHeight(item) - bottomMargin);
625 }
else if (usedAnchors & QQuickAnchors::VCenterAnchor) {
630 setItemY(adjustedPosition(vCenterAnchorItem, vCenterAnchorLine)
631 - vcenter(item) + vCenterOffset);
633 setItemY(position(vCenterAnchorItem, vCenterAnchorLine) - vcenter(item) + vCenterOffset);
635 }
else if (usedAnchors & QQuickAnchors::BaselineAnchor) {
638 setItemY(adjustedPosition(baselineAnchorItem, baselineAnchorLine)
639 - readBaselineOffset(item) + baselineOffset);
641 setItemY(position(baselineAnchorItem, baselineAnchorLine)
642 - readBaselineOffset(item) + baselineOffset);
645 --updatingVerticalAnchor;
665 if (updatingHorizontalAnchor < 3) {
666 ++updatingHorizontalAnchor;
667 qreal effectiveRightMargin, effectiveLeftMargin, effectiveHorizontalCenterOffset;
668 QQuickItem *effectiveLeftItem, *effectiveRightItem, *effectiveHorizontalCenterItem;
669 QQuickAnchors::Anchor effectiveLeftLine, effectiveRightLine, effectiveHorizontalCenterLine;
670 QQuickAnchors::Anchor effectiveLeftAnchor, effectiveRightAnchor;
672 effectiveLeftAnchor = QQuickAnchors::RightAnchor;
673 effectiveRightAnchor = QQuickAnchors::LeftAnchor;
675 effectiveLeftLine = reverseAnchorLine(rightAnchorLine);
677 effectiveRightLine = reverseAnchorLine(leftAnchorLine);
679 effectiveHorizontalCenterLine = reverseAnchorLine(hCenterAnchorLine);
680 effectiveLeftMargin = rightMargin;
681 effectiveRightMargin = leftMargin;
682 effectiveHorizontalCenterOffset = -hCenterOffset;
684 effectiveLeftAnchor = QQuickAnchors::LeftAnchor;
685 effectiveRightAnchor = QQuickAnchors::RightAnchor;
687 effectiveLeftLine = leftAnchorLine;
689 effectiveRightLine = rightAnchorLine;
691 effectiveHorizontalCenterLine = hCenterAnchorLine;
692 effectiveLeftMargin = leftMargin;
693 effectiveRightMargin = rightMargin;
694 effectiveHorizontalCenterOffset = hCenterOffset;
697 if (usedAnchors & effectiveLeftAnchor) {
701 if (usedAnchors & effectiveRightAnchor) {
702 invalid = calcStretch(effectiveLeftItem, effectiveLeftLine,
703 effectiveRightItem, effectiveRightLine,
704 effectiveLeftMargin, -effectiveRightMargin,
705 QQuickAnchors::LeftAnchor, width);
706 }
else if (usedAnchors & QQuickAnchors::HCenterAnchor) {
707 invalid = calcStretch(effectiveLeftItem, effectiveLeftLine,
708 effectiveHorizontalCenterItem, effectiveHorizontalCenterLine,
709 effectiveLeftMargin, effectiveHorizontalCenterOffset,
710 QQuickAnchors::LeftAnchor, width);
717 if (effectiveLeftItem == readParentItem(
item)) {
718 setItemX(adjustedPosition(effectiveLeftItem, effectiveLeftLine) + effectiveLeftMargin);
719 }
else if (readParentItem(effectiveLeftItem) == readParentItem(
item)) {
720 setItemX(position(effectiveLeftItem, effectiveLeftLine) + effectiveLeftMargin);
722 }
else if (usedAnchors & effectiveRightAnchor) {
724 if (usedAnchors & QQuickAnchors::HCenterAnchor) {
726 bool invalid = calcStretch(effectiveHorizontalCenterItem,
727 effectiveHorizontalCenterLine,
728 effectiveRightItem, effectiveRightLine,
729 effectiveHorizontalCenterOffset, -effectiveRightMargin,
730 QQuickAnchors::LeftAnchor, width);
732 setItemWidth(width*2);
736 if (effectiveRightItem == readParentItem(
item)) {
737 setItemX(adjustedPosition(effectiveRightItem, effectiveRightLine)
738 - readWidth(item) - effectiveRightMargin);
739 }
else if (readParentItem(effectiveRightItem) == readParentItem(
item)) {
740 setItemX(position(effectiveRightItem, effectiveRightLine)
741 - readWidth(item) - effectiveRightMargin);
743 }
else if (usedAnchors & QQuickAnchors::HCenterAnchor) {
745 if (effectiveHorizontalCenterItem == readParentItem(
item)) {
746 setItemX(adjustedPosition(effectiveHorizontalCenterItem, effectiveHorizontalCenterLine) - hcenter(item) + effectiveHorizontalCenterOffset);
747 }
else if (readParentItem(effectiveHorizontalCenterItem) == readParentItem(
item)) {
748 setItemX(position(effectiveHorizontalCenterItem, effectiveHorizontalCenterLine) - hcenter(item) + effectiveHorizontalCenterOffset);
751 --updatingHorizontalAnchor;
754 qmlWarning(item) << QQuickAnchors::tr(
"Possible anchor loop detected on horizontal anchor.");
844void QQuickAnchors::setVerticalCenter(
const QQuickAnchorLine &edge)
847 if (!d->checkVAnchorValid(edge) ||
848 (d->vCenterAnchorItem == edge.item && d->vCenterAnchorLine == edge.anchorLine))
851 d->usedAnchors |= VCenterAnchor;
853 if (!d->checkVValid()) {
854 d->usedAnchors &= ~VCenterAnchor;
858 QQuickItem *oldVCenter = d->vCenterAnchorItem;
859 d->vCenterAnchorItem = edge.item;
860 d->vCenterAnchorLine = edge.anchorLine;
861 d->remDepend(oldVCenter);
862 d->addDepend(d->vCenterAnchorItem);
863 emit verticalCenterChanged();
864 d->updateVerticalAnchors();
1004void QQuickAnchors::setHorizontalCenter(
const QQuickAnchorLine &edge)
1007 if (!d->checkHAnchorValid(edge) ||
1008 (d->hCenterAnchorItem == edge.item && d->hCenterAnchorLine == edge.anchorLine))
1011 d->usedAnchors |= HCenterAnchor;
1013 if (!d->checkHValid()) {
1014 d->usedAnchors &= ~HCenterAnchor;
1018 QQuickItem *oldHCenter = d->hCenterAnchorItem;
1019 d->hCenterAnchorItem = edge.item;
1020 d->hCenterAnchorLine = edge.anchorLine;
1021 d->remDepend(oldHCenter);
1022 d->addDepend(d->hCenterAnchorItem);
1023 emit horizontalCenterChanged();
1024 d->updateHorizontalAnchors();
1112void QQuickAnchors::setMargins(qreal offset)
1115 if (d->margins == offset)
1117 d->margins = offset;
1119 bool updateHorizontal =
false;
1120 bool updateVertical =
false;
1122 if (!d->rightMarginExplicit && d->rightMargin != offset) {
1123 d->rightMargin = offset;
1124 updateHorizontal =
true;
1125 emit rightMarginChanged();
1127 if (!d->leftMarginExplicit && d->leftMargin != offset) {
1128 d->leftMargin = offset;
1129 updateHorizontal =
true;
1130 emit leftMarginChanged();
1132 if (!d->topMarginExplicit && d->topMargin != offset) {
1133 d->topMargin = offset;
1134 updateVertical =
true;
1135 emit topMarginChanged();
1137 if (!d->bottomMarginExplicit && d->bottomMargin != offset) {
1138 d->bottomMargin = offset;
1139 updateVertical =
true;
1140 emit bottomMarginChanged();
1144 if (updateHorizontal || updateVertical)
1147 if (updateHorizontal)
1148 d->updateHorizontalAnchors();
1150 d->updateVerticalAnchors();
1153 emit marginsChanged();