1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "anchormanipulator.h"
36 #include "formeditoritem.h"
37 #include "formeditorscene.h"
38 #include "formeditorview.h"
40 #include <rewritertransaction.h>
42 namespace QmlDesigner {
44 AnchorManipulator::AnchorManipulator(FormEditorView *view)
45 : m_beginFormEditorItem(0),
46 m_beginAnchorLine(AnchorLine::Invalid),
51 AnchorManipulator::~AnchorManipulator()
56 void AnchorManipulator::begin(FormEditorItem *beginItem, AnchorLine::Type anchorLine)
58 m_beginFormEditorItem = beginItem;
59 m_beginAnchorLine = anchorLine;
62 static double offset(const QPointF &topLeft, const QPointF &bottomRight, AnchorLine::Type anchorLine)
65 case AnchorLine::Top : return topLeft.y();
66 case AnchorLine::Left : return topLeft.x();
67 case AnchorLine::Bottom : return bottomRight.y();
68 case AnchorLine::Right : return bottomRight.x();
75 void AnchorManipulator::setMargin(FormEditorItem *endItem, AnchorLine::Type endAnchorLine)
77 QPointF beginItemTopLeft(m_beginFormEditorItem->mapToParent(m_beginFormEditorItem->qmlItemNode().instanceBoundingRect().topLeft()));
78 QPointF endItemTopLeft(m_beginFormEditorItem->parentItem()->mapFromItem(endItem, endItem->qmlItemNode().instanceBoundingRect().topLeft()));
80 QPointF beginItemBottomRight(m_beginFormEditorItem->mapToParent(m_beginFormEditorItem->qmlItemNode().instanceBoundingRect().bottomRight()));
81 QPointF endItemBottomRight(m_beginFormEditorItem->parentItem()->mapFromItem(endItem, endItem->qmlItemNode().instanceBoundingRect().bottomRight()));
83 QPointF topLeftAnchorOffset = beginItemTopLeft - endItemTopLeft;
84 QPointF bottomRightAnchorOffset = endItemBottomRight - beginItemBottomRight;
87 double anchorOffset = 0.0;
88 if (m_beginAnchorLine & (AnchorLine::Bottom | AnchorLine::Right)) {
89 anchorOffset = offset(endItemTopLeft, endItemBottomRight, endAnchorLine) -
90 offset(beginItemTopLeft, beginItemBottomRight, m_beginAnchorLine);
92 anchorOffset = offset(beginItemTopLeft, beginItemBottomRight, m_beginAnchorLine) -
93 offset(endItemTopLeft, endItemBottomRight, endAnchorLine);
96 m_beginFormEditorItem->qmlItemNode().anchors().setMargin(m_beginAnchorLine, anchorOffset);
98 void AnchorManipulator::addAnchor(FormEditorItem *endItem, AnchorLine::Type endAnchorLine)
100 RewriterTransaction m_rewriterTransaction = m_view->beginRewriterTransaction();
101 setMargin(endItem, endAnchorLine);
103 m_beginFormEditorItem->qmlItemNode().anchors().setAnchor(m_beginAnchorLine,
104 endItem->qmlItemNode(),
108 void AnchorManipulator::removeAnchor()
110 RewriterTransaction transaction = m_view->beginRewriterTransaction();
111 QmlAnchors anchors(m_beginFormEditorItem->qmlItemNode().anchors());
112 if (anchors.instanceHasAnchor(m_beginAnchorLine)) {
113 anchors.removeAnchor(m_beginAnchorLine);
114 anchors.removeMargin(m_beginAnchorLine);
118 void AnchorManipulator::clear()
120 m_beginFormEditorItem = 0;
121 m_beginAnchorLine = AnchorLine::Invalid;
124 bool AnchorManipulator::isActive() const
126 return m_beginFormEditorItem && m_beginAnchorLine != AnchorLine::Invalid;
129 AnchorLine::Type AnchorManipulator::beginAnchorLine() const
131 return m_beginAnchorLine;
134 bool AnchorManipulator::beginAnchorLineIsHorizontal() const
136 return beginAnchorLine() & AnchorLine::HorizontalMask;
138 bool AnchorManipulator::beginAnchorLineIsVertical() const
140 return beginAnchorLine() & AnchorLine::HorizontalMask;
143 FormEditorItem *AnchorManipulator::beginFormEditorItem() const
145 return m_beginFormEditorItem;
148 } // namespace QmlDesigner