66static inline qreal position(
const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
70 case QQuickAnchors::LeftAnchor:
73 case QQuickAnchors::RightAnchor:
74 ret = readX(item) + readWidth(item);
76 case QQuickAnchors::TopAnchor:
79 case QQuickAnchors::BottomAnchor:
80 ret = readY(item) + readHeight(item);
82 case QQuickAnchors::HCenterAnchor:
83 ret = readX(item) + hcenter(item);
85 case QQuickAnchors::VCenterAnchor:
86 ret = readY(item) + vcenter(item);
88 case QQuickAnchors::BaselineAnchor:
89 ret = readY(item) + readBaselineOffset(item);
541 QQuickAnchors::Anchor edge1Line,
542 QQuickItem *edge2Item,
543 QQuickAnchors::Anchor edge2Line,
546 QQuickAnchors::Anchor line,
547 qreal &stretch)
const
549 bool edge1IsParent = (edge1Item == readParentItem(
item));
550 bool edge2IsParent = (edge2Item == readParentItem(
item));
551 bool edge1IsSibling = (readParentItem(edge1Item) == readParentItem(
item));
552 bool edge2IsSibling = (readParentItem(edge2Item) == readParentItem(
item));
554 bool invalid =
false;
555 if ((edge2IsParent && edge1IsParent) || (edge2IsSibling && edge1IsSibling)) {
556 stretch = (position(edge2Item, edge2Line) + offset2)
557 - (position(edge1Item, edge1Line) + offset1);
558 }
else if (edge2IsParent && edge1IsSibling) {
559 stretch = (position(edge2Item, edge2Line) + offset2)
560 - (position(readParentItem(item), line)
561 + position(edge1Item, edge1Line) + offset1);
562 }
else if (edge2IsSibling && edge1IsParent) {
563 stretch = (position(readParentItem(item), line) + position(edge2Item, edge2Line) + offset2)
564 - (position(edge1Item, edge1Line) + offset1);
576 if (Q_UNLIKELY(updatingVerticalAnchor > 1)) {
578 qmlWarning(item) << QQuickAnchors::tr(
"Possible anchor loop detected on vertical anchor.");
582 ++updatingVerticalAnchor;
583 if (usedAnchors & QQuickAnchors::TopAnchor) {
587 if (usedAnchors & QQuickAnchors::BottomAnchor) {
588 invalid = calcStretch(topAnchorItem, topAnchorLine,
589 bottomAnchorItem, bottomAnchorLine,
590 topMargin, -bottomMargin, QQuickAnchors::TopAnchor, height);
591 }
else if (usedAnchors & QQuickAnchors::VCenterAnchor) {
592 invalid = calcStretch(topAnchorItem, topAnchorLine,
593 vCenterAnchorItem, vCenterAnchorLine,
594 topMargin, vCenterOffset, QQuickAnchors::TopAnchor, height);
598 setItemHeight(height);
602 setItemY(adjustedPosition(topAnchorItem, topAnchorLine) + topMargin);
604 setItemY(position(topAnchorItem, topAnchorLine) + topMargin);
606 }
else if (usedAnchors & QQuickAnchors::BottomAnchor) {
608 if (usedAnchors & QQuickAnchors::VCenterAnchor) {
610 bool invalid = calcStretch(vCenterAnchorItem, vCenterAnchorLine,
611 bottomAnchorItem, bottomAnchorLine,
612 vCenterOffset, -bottomMargin, QQuickAnchors::TopAnchor,
615 setItemHeight(height*2);
620 setItemY(adjustedPosition(bottomAnchorItem, bottomAnchorLine) - readHeight(item) - bottomMargin);
622 setItemY(position(bottomAnchorItem, bottomAnchorLine) - readHeight(item) - bottomMargin);
624 }
else if (usedAnchors & QQuickAnchors::VCenterAnchor) {
629 setItemY(adjustedPosition(vCenterAnchorItem, vCenterAnchorLine)
630 - vcenter(item) + vCenterOffset);
632 setItemY(position(vCenterAnchorItem, vCenterAnchorLine) - vcenter(item) + vCenterOffset);
634 }
else if (usedAnchors & QQuickAnchors::BaselineAnchor) {
637 setItemY(adjustedPosition(baselineAnchorItem, baselineAnchorLine)
638 - readBaselineOffset(item) + baselineOffset);
640 setItemY(position(baselineAnchorItem, baselineAnchorLine)
641 - readBaselineOffset(item) + baselineOffset);
644 --updatingVerticalAnchor;
664 if (updatingHorizontalAnchor < 3) {
665 ++updatingHorizontalAnchor;
666 qreal effectiveRightMargin, effectiveLeftMargin, effectiveHorizontalCenterOffset;
667 QQuickItem *effectiveLeftItem, *effectiveRightItem, *effectiveHorizontalCenterItem;
668 QQuickAnchors::Anchor effectiveLeftLine, effectiveRightLine, effectiveHorizontalCenterLine;
669 QQuickAnchors::Anchor effectiveLeftAnchor, effectiveRightAnchor;
671 effectiveLeftAnchor = QQuickAnchors::RightAnchor;
672 effectiveRightAnchor = QQuickAnchors::LeftAnchor;
674 effectiveLeftLine = reverseAnchorLine(rightAnchorLine);
676 effectiveRightLine = reverseAnchorLine(leftAnchorLine);
678 effectiveHorizontalCenterLine = reverseAnchorLine(hCenterAnchorLine);
679 effectiveLeftMargin = rightMargin;
680 effectiveRightMargin = leftMargin;
681 effectiveHorizontalCenterOffset = -hCenterOffset;
683 effectiveLeftAnchor = QQuickAnchors::LeftAnchor;
684 effectiveRightAnchor = QQuickAnchors::RightAnchor;
686 effectiveLeftLine = leftAnchorLine;
688 effectiveRightLine = rightAnchorLine;
690 effectiveHorizontalCenterLine = hCenterAnchorLine;
691 effectiveLeftMargin = leftMargin;
692 effectiveRightMargin = rightMargin;
693 effectiveHorizontalCenterOffset = hCenterOffset;
696 if (usedAnchors & effectiveLeftAnchor) {
700 if (usedAnchors & effectiveRightAnchor) {
701 invalid = calcStretch(effectiveLeftItem, effectiveLeftLine,
702 effectiveRightItem, effectiveRightLine,
703 effectiveLeftMargin, -effectiveRightMargin,
704 QQuickAnchors::LeftAnchor, width);
705 }
else if (usedAnchors & QQuickAnchors::HCenterAnchor) {
706 invalid = calcStretch(effectiveLeftItem, effectiveLeftLine,
707 effectiveHorizontalCenterItem, effectiveHorizontalCenterLine,
708 effectiveLeftMargin, effectiveHorizontalCenterOffset,
709 QQuickAnchors::LeftAnchor, width);
716 if (effectiveLeftItem == readParentItem(
item)) {
717 setItemX(adjustedPosition(effectiveLeftItem, effectiveLeftLine) + effectiveLeftMargin);
718 }
else if (readParentItem(effectiveLeftItem) == readParentItem(
item)) {
719 setItemX(position(effectiveLeftItem, effectiveLeftLine) + effectiveLeftMargin);
721 }
else if (usedAnchors & effectiveRightAnchor) {
723 if (usedAnchors & QQuickAnchors::HCenterAnchor) {
725 bool invalid = calcStretch(effectiveHorizontalCenterItem,
726 effectiveHorizontalCenterLine,
727 effectiveRightItem, effectiveRightLine,
728 effectiveHorizontalCenterOffset, -effectiveRightMargin,
729 QQuickAnchors::LeftAnchor, width);
731 setItemWidth(width*2);
735 if (effectiveRightItem == readParentItem(
item)) {
736 setItemX(adjustedPosition(effectiveRightItem, effectiveRightLine)
737 - readWidth(item) - effectiveRightMargin);
738 }
else if (readParentItem(effectiveRightItem) == readParentItem(
item)) {
739 setItemX(position(effectiveRightItem, effectiveRightLine)
740 - readWidth(item) - effectiveRightMargin);
742 }
else if (usedAnchors & QQuickAnchors::HCenterAnchor) {
744 if (effectiveHorizontalCenterItem == readParentItem(
item)) {
745 setItemX(adjustedPosition(effectiveHorizontalCenterItem, effectiveHorizontalCenterLine) - hcenter(item) + effectiveHorizontalCenterOffset);
746 }
else if (readParentItem(effectiveHorizontalCenterItem) == readParentItem(
item)) {
747 setItemX(position(effectiveHorizontalCenterItem, effectiveHorizontalCenterLine) - hcenter(item) + effectiveHorizontalCenterOffset);
750 --updatingHorizontalAnchor;
753 qmlWarning(item) << QQuickAnchors::tr(
"Possible anchor loop detected on horizontal anchor.");
843void QQuickAnchors::setVerticalCenter(
const QQuickAnchorLine &edge)
846 if (!d->checkVAnchorValid(edge) ||
847 (d->vCenterAnchorItem == edge.item && d->vCenterAnchorLine == edge.anchorLine))
850 d->usedAnchors |= VCenterAnchor;
852 if (!d->checkVValid()) {
853 d->usedAnchors &= ~VCenterAnchor;
857 QQuickItem *oldVCenter = d->vCenterAnchorItem;
858 d->vCenterAnchorItem = edge.item;
859 d->vCenterAnchorLine = edge.anchorLine;
860 d->remDepend(oldVCenter);
861 d->addDepend(d->vCenterAnchorItem);
862 emit verticalCenterChanged();
863 d->updateVerticalAnchors();
1003void QQuickAnchors::setHorizontalCenter(
const QQuickAnchorLine &edge)
1006 if (!d->checkHAnchorValid(edge) ||
1007 (d->hCenterAnchorItem == edge.item && d->hCenterAnchorLine == edge.anchorLine))
1010 d->usedAnchors |= HCenterAnchor;
1012 if (!d->checkHValid()) {
1013 d->usedAnchors &= ~HCenterAnchor;
1017 QQuickItem *oldHCenter = d->hCenterAnchorItem;
1018 d->hCenterAnchorItem = edge.item;
1019 d->hCenterAnchorLine = edge.anchorLine;
1020 d->remDepend(oldHCenter);
1021 d->addDepend(d->hCenterAnchorItem);
1022 emit horizontalCenterChanged();
1023 d->updateHorizontalAnchors();
1111void QQuickAnchors::setMargins(qreal offset)
1114 if (d->margins == offset)
1116 d->margins = offset;
1118 bool updateHorizontal =
false;
1119 bool updateVertical =
false;
1121 if (!d->rightMarginExplicit && d->rightMargin != offset) {
1122 d->rightMargin = offset;
1123 updateHorizontal =
true;
1124 emit rightMarginChanged();
1126 if (!d->leftMarginExplicit && d->leftMargin != offset) {
1127 d->leftMargin = offset;
1128 updateHorizontal =
true;
1129 emit leftMarginChanged();
1131 if (!d->topMarginExplicit && d->topMargin != offset) {
1132 d->topMargin = offset;
1133 updateVertical =
true;
1134 emit topMarginChanged();
1136 if (!d->bottomMarginExplicit && d->bottomMargin != offset) {
1137 d->bottomMargin = offset;
1138 updateVertical =
true;
1139 emit bottomMarginChanged();
1143 if (updateHorizontal || updateVertical)
1146 if (updateHorizontal)
1147 d->updateHorizontalAnchors();
1149 d->updateVerticalAnchors();
1152 emit marginsChanged();