OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / formeditor / anchorlinecontroller.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 "anchorlinecontroller.h"
35
36 #include "formeditoritem.h"
37 #include "layeritem.h"
38 #include <QGraphicsScene>
39
40 #include "anchorlinehandleitem.h"
41
42 namespace QmlDesigner {
43
44
45 AnchorLineControllerData::AnchorLineControllerData(LayerItem *layerItem, FormEditorItem *formEditorItem)
46     : layerItem(layerItem),
47     formEditorItem(formEditorItem),
48     topItem(0),
49     leftItem(0),
50     rightItem(0),
51     bottomItem(0)
52 {
53 }
54
55 AnchorLineControllerData::AnchorLineControllerData(const AnchorLineControllerData &other)
56     : layerItem(other.layerItem),
57     formEditorItem(other.formEditorItem),
58     topItem(other.topItem),
59     leftItem(other.leftItem),
60     rightItem(other.rightItem),
61     bottomItem(other.bottomItem)
62 {}
63
64 AnchorLineControllerData::~AnchorLineControllerData()
65 {
66     if (layerItem) {
67         layerItem->scene()->removeItem(topItem);
68         layerItem->scene()->removeItem(leftItem);
69         layerItem->scene()->removeItem(rightItem);
70         layerItem->scene()->removeItem(bottomItem);
71     }
72 }
73
74
75 AnchorLineController::AnchorLineController()
76    : m_data(new AnchorLineControllerData(0, 0))
77 {
78
79 }
80
81 AnchorLineController::AnchorLineController(const QSharedPointer<AnchorLineControllerData> &data)
82     : m_data(data)
83 {
84
85 }
86
87 AnchorLineController::AnchorLineController(LayerItem *layerItem, FormEditorItem *formEditorItem)
88     : m_data(new AnchorLineControllerData(layerItem, formEditorItem))
89 {
90     m_data->topItem = new AnchorLineHandleItem(layerItem, *this);
91     m_data->topItem->setZValue(300);
92
93     m_data->leftItem = new AnchorLineHandleItem(layerItem, *this);
94     m_data->leftItem->setZValue(300);
95
96     m_data->rightItem = new AnchorLineHandleItem(layerItem, *this);
97     m_data->rightItem->setZValue(300);
98
99     m_data->bottomItem = new AnchorLineHandleItem(layerItem, *this);
100     m_data->bottomItem->setZValue(300);
101
102     updatePosition();
103 }
104
105
106 bool AnchorLineController::isValid() const
107 {
108     return m_data->formEditorItem != 0;
109 }
110
111 void AnchorLineController::show(AnchorLine::Type anchorLineMask)
112 {
113     if (anchorLineMask & AnchorLine::Top)
114         m_data->topItem->show();
115     else
116         m_data->topItem->hide();
117
118     if (anchorLineMask & AnchorLine::Left)
119         m_data->leftItem->show();
120     else
121         m_data->leftItem->hide();
122
123     if (anchorLineMask & AnchorLine::Right)
124         m_data->rightItem->show();
125     else
126         m_data->rightItem->hide();
127
128     if (anchorLineMask & AnchorLine::Bottom)
129         m_data->bottomItem->show();
130     else
131         m_data->bottomItem->hide();
132 }
133
134 void AnchorLineController::hide()
135 {
136     m_data->topItem->hide();
137     m_data->leftItem->hide();
138     m_data->rightItem->hide();
139     m_data->bottomItem->hide();
140 }
141
142 static QPainterPath rectToPath(const QRectF &rect)
143 {
144     QPainterPath path;
145     path.addRoundedRect(rect, 4, 4);
146
147     return path;
148 }
149
150 void AnchorLineController::updatePosition()
151 {
152     QRectF boundingRect = m_data->formEditorItem->qmlItemNode().instanceBoundingRect();
153
154     QRectF topBoundingRect(boundingRect);
155     QRectF leftBoundingRect(boundingRect);
156     QRectF bottomBoundingRect(boundingRect);
157     QRectF rightBoundingRect(boundingRect);
158
159
160     if (formEditorItem()->isContainer()) {
161         topBoundingRect.setBottom(boundingRect.top() + 6);
162         topBoundingRect.adjust(7, -5, -7, 0);
163
164         leftBoundingRect.setRight(boundingRect.left() + 6);
165         leftBoundingRect.adjust(-5, 7, 0, -7);
166
167         bottomBoundingRect.setTop(boundingRect.bottom() - 6);
168         bottomBoundingRect.adjust(7, 0, -7, 5);
169
170         rightBoundingRect.setLeft(boundingRect.right() - 6);
171         rightBoundingRect.adjust(0, 7, 5, -7);
172
173     } else {
174         double height = qMin(boundingRect.height() / 4., 10.);
175         double width = qMin(boundingRect.width() / 4., 10.);
176
177         topBoundingRect.setHeight(height);
178         topBoundingRect.adjust(width, -4, -width, -1);
179
180         leftBoundingRect.setWidth(width);
181         leftBoundingRect.adjust(-4, height, -1, -height);
182
183         bottomBoundingRect.setTop(boundingRect.bottom() - height);
184         bottomBoundingRect.adjust(width, 1, -width, 4);
185
186         rightBoundingRect.setLeft(boundingRect.right() - width);
187         rightBoundingRect.adjust(1, height, 4, -height);
188     }
189
190     m_data->topItem->setHandlePath(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
191                                                                         rectToPath(topBoundingRect)));
192     m_data->leftItem->setHandlePath(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
193                                                                          rectToPath(leftBoundingRect)));
194     m_data->bottomItem->setHandlePath(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
195                                                                            rectToPath(bottomBoundingRect)));
196     m_data->rightItem->setHandlePath(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
197                                                                           rectToPath(rightBoundingRect)));
198 }
199
200
201 FormEditorItem* AnchorLineController::formEditorItem() const
202 {
203     return m_data->formEditorItem;
204 }
205
206 QWeakPointer<AnchorLineControllerData> AnchorLineController::weakPointer() const
207 {
208     return m_data;
209 }
210
211
212 bool AnchorLineController::isTopHandle(const AnchorLineHandleItem *handle) const
213 {
214     return handle == m_data->topItem;
215 }
216
217 bool AnchorLineController::isLeftHandle(const AnchorLineHandleItem *handle) const
218 {
219     return handle == m_data->leftItem;
220 }
221
222 bool AnchorLineController::isRightHandle(const AnchorLineHandleItem *handle) const
223 {
224     return handle == m_data->rightItem;
225 }
226
227 bool AnchorLineController::isBottomHandle(const AnchorLineHandleItem *handle) const
228 {
229     return handle == m_data->bottomItem;
230 }
231
232 void AnchorLineController::clearHighlight()
233 {
234     m_data->topItem->setHiglighted(false);
235     m_data->leftItem->setHiglighted(false);
236     m_data->rightItem->setHiglighted(false);
237     m_data->bottomItem->setHiglighted(false);
238 }
239
240 } // namespace QmlDesigner