OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / formeditor / anchormanipulator.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "anchormanipulator.h"
35
36 #include "formeditoritem.h"
37 #include "formeditorscene.h"
38 #include "formeditorview.h"
39 #include <model.h>
40 #include <rewritertransaction.h>
41
42 namespace QmlDesigner {
43
44 AnchorManipulator::AnchorManipulator(FormEditorView *view)
45   : m_beginFormEditorItem(0),
46     m_beginAnchorLine(AnchorLine::Invalid),
47     m_view(view)
48 {
49 }
50
51 AnchorManipulator::~AnchorManipulator()
52 {
53     clear();
54 }
55
56 void AnchorManipulator::begin(FormEditorItem *beginItem, AnchorLine::Type anchorLine)
57 {
58    m_beginFormEditorItem = beginItem;
59    m_beginAnchorLine = anchorLine;
60 }
61
62 static double offset(const QPointF &topLeft, const QPointF &bottomRight, AnchorLine::Type anchorLine)
63 {
64      switch(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();
69         default: break;
70     }
71
72      return 0.0;
73 }
74
75 void AnchorManipulator::setMargin(FormEditorItem *endItem, AnchorLine::Type endAnchorLine)
76 {
77     QPointF beginItemTopLeft(m_beginFormEditorItem->mapToParent(m_beginFormEditorItem->qmlItemNode().instanceBoundingRect().topLeft()));
78     QPointF endItemTopLeft(m_beginFormEditorItem->parentItem()->mapFromItem(endItem, endItem->qmlItemNode().instanceBoundingRect().topLeft()));
79
80     QPointF beginItemBottomRight(m_beginFormEditorItem->mapToParent(m_beginFormEditorItem->qmlItemNode().instanceBoundingRect().bottomRight()));
81     QPointF endItemBottomRight(m_beginFormEditorItem->parentItem()->mapFromItem(endItem, endItem->qmlItemNode().instanceBoundingRect().bottomRight()));
82
83     QPointF topLeftAnchorOffset = beginItemTopLeft - endItemTopLeft;
84     QPointF bottomRightAnchorOffset = endItemBottomRight - beginItemBottomRight;
85
86
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);
91     } else {
92         anchorOffset = offset(beginItemTopLeft, beginItemBottomRight, m_beginAnchorLine) -
93                        offset(endItemTopLeft, endItemBottomRight, endAnchorLine);
94     }
95
96     m_beginFormEditorItem->qmlItemNode().anchors().setMargin(m_beginAnchorLine, anchorOffset);
97 }
98 void AnchorManipulator::addAnchor(FormEditorItem *endItem, AnchorLine::Type endAnchorLine)
99 {
100     RewriterTransaction m_rewriterTransaction = m_view->beginRewriterTransaction();
101     setMargin(endItem, endAnchorLine);
102
103     m_beginFormEditorItem->qmlItemNode().anchors().setAnchor(m_beginAnchorLine,
104                                                               endItem->qmlItemNode(),
105                                                               endAnchorLine);
106 }
107
108 void AnchorManipulator::removeAnchor()
109 {
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);
115     }
116 }
117
118 void AnchorManipulator::clear()
119 {
120     m_beginFormEditorItem = 0;
121     m_beginAnchorLine = AnchorLine::Invalid;
122 }
123
124 bool AnchorManipulator::isActive() const
125 {
126     return m_beginFormEditorItem && m_beginAnchorLine != AnchorLine::Invalid;
127 }
128
129 AnchorLine::Type AnchorManipulator::beginAnchorLine() const
130 {
131     return m_beginAnchorLine;
132 }
133
134 bool AnchorManipulator::beginAnchorLineIsHorizontal() const
135 {
136     return beginAnchorLine() & AnchorLine::HorizontalMask;
137 }
138 bool AnchorManipulator::beginAnchorLineIsVertical() const
139 {
140     return beginAnchorLine() & AnchorLine::HorizontalMask;
141 }
142
143 FormEditorItem *AnchorManipulator::beginFormEditorItem() const
144 {
145     return m_beginFormEditorItem;
146 }
147
148 } // namespace QmlDesigner