From 441403bf60f3a81033a245516a74be2db686d001 Mon Sep 17 00:00:00 2001
From: Thomas Hartmann <Thomas.Hartmann@nokia.com>
Date: Tue, 25 May 2010 17:47:04 +0200
Subject: [PATCH] QmlDesigner.model: QmlAnchors get simplification

Now anchors.verticalCenter & anchors.horizontalCenter
get simplified to anchors.centerIn. We also simplify the fill case.

Task-number: Bauhaus-109
Reviewed-by: Marco Bubke
---
 .../designercore/include/qmlanchors.h         |  1 +
 .../designercore/model/qmlanchors.cpp         | 50 ++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/src/plugins/qmldesigner/designercore/include/qmlanchors.h b/src/plugins/qmldesigner/designercore/include/qmlanchors.h
index 409a51bc88a..ccfd772178f 100644
--- a/src/plugins/qmldesigner/designercore/include/qmlanchors.h
+++ b/src/plugins/qmldesigner/designercore/include/qmlanchors.h
@@ -103,6 +103,7 @@ public:
 
 protected:
     QmlItemNode qmlItemNode() const;
+    void beautify();
 
 private:
     QmlItemNode m_qmlItemNode;
diff --git a/src/plugins/qmldesigner/designercore/model/qmlanchors.cpp b/src/plugins/qmldesigner/designercore/model/qmlanchors.cpp
index 6a71c3e356d..1df6106b95a 100644
--- a/src/plugins/qmldesigner/designercore/model/qmlanchors.cpp
+++ b/src/plugins/qmldesigner/designercore/model/qmlanchors.cpp
@@ -130,6 +130,52 @@ bool QmlAnchors::isValid() const
     return m_qmlItemNode.isValid();
 }
 
+void QmlAnchors::beautify()
+{
+
+    if ((instanceHasAnchor(AnchorLine::Left) &&
+         instanceHasAnchor(AnchorLine::Right) &&
+         instanceHasAnchor(AnchorLine::Top) &&
+         instanceHasAnchor(AnchorLine::Bottom)) &&
+
+        (instanceAnchor(AnchorLine::Left).type() == AnchorLine::Left &&
+         instanceAnchor(AnchorLine::Right).type() == AnchorLine::Right &&
+         instanceAnchor(AnchorLine::Top).type() == AnchorLine::Top &&
+         instanceAnchor(AnchorLine::Bottom).type() == AnchorLine::Bottom) &&
+
+        (instanceAnchor(AnchorLine::Left).qmlItemNode() ==
+         instanceAnchor(AnchorLine::Right).qmlItemNode() &&
+         instanceAnchor(AnchorLine::Top).qmlItemNode() ==
+         instanceAnchor(AnchorLine::Bottom).qmlItemNode()) &&
+        (instanceAnchor(AnchorLine::Left).qmlItemNode() ==
+         instanceAnchor(AnchorLine::Bottom).qmlItemNode())) {
+
+        if (instanceHasAnchor(AnchorLine::Fill))
+            return; //avoid recursion
+
+        QmlItemNode targetNode(instanceAnchor(AnchorLine::Left).qmlItemNode());
+        removeAnchors();
+        setAnchor(AnchorLine::Fill, targetNode, AnchorLine::Fill);
+    }
+
+    if ((instanceHasAnchor(AnchorLine::VerticalCenter) &&
+         instanceHasAnchor(AnchorLine::HorizontalCenter)) &&
+
+        (instanceAnchor(AnchorLine::VerticalCenter).type() == AnchorLine::VerticalCenter &&
+         instanceAnchor(AnchorLine::HorizontalCenter).type() == AnchorLine::HorizontalCenter) &&
+
+        (instanceAnchor(AnchorLine::VerticalCenter).qmlItemNode() ==
+         instanceAnchor(AnchorLine::HorizontalCenter).qmlItemNode())) {
+
+        if (instanceHasAnchor(AnchorLine::Center))
+            return; //avoid recursion
+
+        QmlItemNode targetNode(instanceAnchor(AnchorLine::VerticalCenter).qmlItemNode());
+        removeAnchors();
+        setAnchor(AnchorLine::Center, targetNode, AnchorLine::Center);
+    }
+}
+
 void QmlAnchors::setAnchor(AnchorLine::Type sourceAnchorLine,
                           const QmlItemNode &targetQmlItemNode,
                           AnchorLine::Type targetAnchorLine)
@@ -145,9 +191,11 @@ void QmlAnchors::setAnchor(AnchorLine::Type sourceAnchorLine,
         QString targetExpression = targetQmlItemNode.modelNode().validId();
         if (targetQmlItemNode.modelNode() == qmlItemNode().modelNode().parentProperty().parentModelNode())
             targetExpression = "parent";
-        targetExpression = targetExpression + QLatin1Char('.') + lineTypeToString(targetAnchorLine);
+        if (sourceAnchorLine != AnchorLine::Center && sourceAnchorLine != AnchorLine::Fill)
+            targetExpression = targetExpression + QLatin1Char('.') + lineTypeToString(targetAnchorLine);
         qmlItemNode().modelNode().bindingProperty(propertyName).setExpression(targetExpression);
     }
+    beautify();
 }
 
 bool detectHorizontalCycle(const ModelNode &node, QList<ModelNode> knownNodeList)
-- 
GitLab