diff --git a/src/framework/ui/uianchorlayout.cpp b/src/framework/ui/uianchorlayout.cpp index 33979ac3..3f05dc91 100644 --- a/src/framework/ui/uianchorlayout.cpp +++ b/src/framework/ui/uianchorlayout.cpp @@ -55,19 +55,27 @@ int Anchor::getAnchorLinePoint() const } return -9999; } - bool UIAnchorLayout::addAnchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, const AnchorLine& anchorLine) { Anchor anchor(anchoredElement, anchoredEdge, anchorLine); UIElementPtr anchorLineElement = anchor.getAnchorLineElement(); + if(!anchorLineElement) { + logError("ERROR: could not find the element to anchor on, wrong id?"); + return false; + } + // we can never anchor with itself if(anchoredElement == anchorLineElement) { logError("ERROR: anchoring with itself is not possible"); return false; } - // TODO: check if itself is already anchored on the anchor element + // we must never anchor to an anchor child + if(hasElementInAnchorTree(anchorLineElement, anchoredElement)) { + logError("ERROR: anchors is miss configurated, you must never make anchor chains in loops"); + return false; + } // setup the anchor m_anchors.push_back(anchor); @@ -144,6 +152,17 @@ void UIAnchorLayout::recalculateChildrenLayout(const UIElementPtr& parent) } } +bool UIAnchorLayout::hasElementInAnchorTree(const UIElementPtr& element, const UIElementPtr& treeAnchor) +{ + foreach(const Anchor& anchor, m_anchors) { + if(anchor.getAnchorLineElement() == treeAnchor) { + if(anchor.getAnchoredElement() == element || hasElementInAnchorTree(element, anchor.getAnchoredElement())) + return true; + } + } + return false; +} + UI::AnchorPoint UIAnchorLayout::parseAnchorPoint(const std::string& anchorPointStr) { if(anchorPointStr == "left") diff --git a/src/framework/ui/uianchorlayout.h b/src/framework/ui/uianchorlayout.h index f99d4e98..b4f9cb42 100644 --- a/src/framework/ui/uianchorlayout.h +++ b/src/framework/ui/uianchorlayout.h @@ -66,6 +66,7 @@ public: void recalculateElementLayout(const UIElementPtr& element); void recalculateChildrenLayout(const UIElementPtr& parent); + bool hasElementInAnchorTree(const UIElementPtr& element, const UIElementPtr& treeAnchor); static UI::AnchorPoint parseAnchorPoint(const std::string& anchorPointStr); private: