OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / formeditor / resizecontroller.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "resizecontroller.h"
34
35 #include "formeditoritem.h"
36 #include "layeritem.h"
37
38 #include <resizehandleitem.h>
39 #include <QCursor>
40 #include <QGraphicsScene>
41
42 namespace QmlDesigner {
43
44
45
46 ResizeControllerData::ResizeControllerData(LayerItem *layerItem, FormEditorItem *formEditorItem)
47     : layerItem(layerItem),
48     formEditorItem(formEditorItem),
49     topLeftItem(0),
50     topRightItem(0),
51     bottomLeftItem(0),
52     bottomRightItem(0),
53     topItem(0),
54     leftItem(0),
55     rightItem(0),
56     bottomItem(0)
57 {
58
59 }
60
61 ResizeControllerData::ResizeControllerData(const ResizeControllerData &other)
62     : layerItem(other.layerItem),
63     formEditorItem(other.formEditorItem),
64     topLeftItem(other.topLeftItem),
65     topRightItem(other.topRightItem),
66     bottomLeftItem(other.bottomLeftItem),
67     bottomRightItem(other.bottomRightItem),
68     topItem(other.topItem),
69     leftItem(other.leftItem),
70     rightItem(other.rightItem),
71     bottomItem(other.bottomItem)
72 {}
73
74 ResizeControllerData::~ResizeControllerData()
75 {
76     if (layerItem) {
77         layerItem->scene()->removeItem(topLeftItem);
78         layerItem->scene()->removeItem(topRightItem);
79         layerItem->scene()->removeItem(bottomLeftItem);
80         layerItem->scene()->removeItem(bottomRightItem);
81         layerItem->scene()->removeItem(topItem);
82         layerItem->scene()->removeItem(leftItem);
83         layerItem->scene()->removeItem(rightItem);
84         layerItem->scene()->removeItem(bottomItem);
85     }
86 }
87
88
89 ResizeController::ResizeController()
90    : m_data(new ResizeControllerData(0, 0))
91 {
92
93 }
94
95 ResizeController::ResizeController(const QSharedPointer<ResizeControllerData> &data)
96     : m_data(data)
97 {
98
99 }
100
101 ResizeController::ResizeController(LayerItem *layerItem, FormEditorItem *formEditorItem)
102     : m_data(new ResizeControllerData(layerItem, formEditorItem))
103 {
104     m_data->topLeftItem = new ResizeHandleItem(layerItem, *this);
105     m_data->topLeftItem->setZValue(302);
106     m_data->topLeftItem->setCursor(Qt::SizeFDiagCursor);
107
108     m_data->topRightItem = new ResizeHandleItem(layerItem, *this);
109     m_data->topRightItem->setZValue(301);
110     m_data->topRightItem->setCursor(Qt::SizeBDiagCursor);
111
112     m_data->bottomLeftItem = new ResizeHandleItem(layerItem, *this);
113     m_data->bottomLeftItem->setZValue(301);
114     m_data->bottomLeftItem->setCursor(Qt::SizeBDiagCursor);
115
116     m_data->bottomRightItem = new ResizeHandleItem(layerItem, *this);
117     m_data->bottomRightItem->setZValue(305);
118     m_data->bottomRightItem->setCursor(Qt::SizeFDiagCursor);
119
120     m_data->topItem = new ResizeHandleItem(layerItem, *this);
121     m_data->topItem->setZValue(300);
122     m_data->topItem->setCursor(Qt::SizeVerCursor);
123
124     m_data->leftItem = new ResizeHandleItem(layerItem, *this);
125     m_data->leftItem->setZValue(300);
126     m_data->leftItem->setCursor(Qt::SizeHorCursor);
127
128     m_data->rightItem = new ResizeHandleItem(layerItem, *this);
129     m_data->rightItem->setZValue(300);
130     m_data->rightItem->setCursor(Qt::SizeHorCursor);
131
132     m_data->bottomItem = new ResizeHandleItem(layerItem, *this);
133     m_data->bottomItem->setZValue(300);
134     m_data->bottomItem->setCursor(Qt::SizeVerCursor);
135
136     updatePosition();
137 }
138
139
140 bool ResizeController::isValid() const
141 {
142     return m_data->formEditorItem && m_data->formEditorItem->qmlItemNode().isValid();
143 }
144
145 void ResizeController::show()
146 {
147     m_data->topLeftItem->show();
148     m_data->topRightItem->show();
149     m_data->bottomLeftItem->show();
150     m_data->bottomRightItem->show();
151     m_data->topItem->show();
152     m_data->leftItem->show();
153     m_data->rightItem->show();
154     m_data->bottomItem->show();
155 }
156 void ResizeController::hide()
157 {
158     m_data->topLeftItem->hide();
159     m_data->topRightItem->hide();
160     m_data->bottomLeftItem->hide();
161     m_data->bottomRightItem->hide();
162     m_data->topItem->hide();
163     m_data->leftItem->hide();
164     m_data->rightItem->hide();
165     m_data->bottomItem->hide();
166 }
167
168
169 static QPointF topCenter(const QRectF &rect)
170 {
171     return QPointF(rect.center().x(), rect.top());
172 }
173
174 static QPointF leftCenter(const QRectF &rect)
175 {
176     return QPointF(rect.left(), rect.center().y());
177 }
178
179 static QPointF rightCenter(const QRectF &rect)
180 {
181     return QPointF(rect.right(), rect.center().y());
182 }
183
184 static QPointF bottomCenter(const QRectF &rect)
185 {
186     return QPointF(rect.center().x(), rect.bottom());
187 }
188
189
190 void ResizeController::updatePosition()
191 {
192     if (isValid()) {
193
194         QRectF boundingRect = m_data->formEditorItem->qmlItemNode().instanceBoundingRect();
195         QPointF topLeftPointInLayerSpace(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
196                                                                            boundingRect.topLeft()));
197         QPointF topRightPointInLayerSpace(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
198                                                                             boundingRect.topRight()));
199         QPointF bottomLeftPointInLayerSpace(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
200                                                                               boundingRect.bottomLeft()));
201         QPointF bottomRightPointInLayerSpace(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
202                                                                                boundingRect.bottomRight()));
203
204         QPointF topPointInLayerSpace(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
205                                                                        topCenter(boundingRect)));
206         QPointF leftPointInLayerSpace(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
207                                                                         leftCenter(boundingRect)));
208
209         QPointF rightPointInLayerSpace(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
210                                                                          rightCenter(boundingRect)));
211         QPointF bottomPointInLayerSpace(m_data->formEditorItem->mapToItem(m_data->layerItem.data(),
212                                                                           bottomCenter(boundingRect)));
213
214
215
216         m_data->topRightItem->setHandlePosition(topRightPointInLayerSpace, boundingRect.topRight());
217         m_data->topLeftItem->setHandlePosition(topLeftPointInLayerSpace, boundingRect.topLeft());
218         m_data->bottomLeftItem->setHandlePosition(bottomLeftPointInLayerSpace, boundingRect.bottomLeft());
219         m_data->bottomRightItem->setHandlePosition(bottomRightPointInLayerSpace, boundingRect.bottomRight());
220         m_data->topItem->setHandlePosition(topPointInLayerSpace, topCenter(boundingRect));
221         m_data->leftItem->setHandlePosition(leftPointInLayerSpace, leftCenter(boundingRect));
222         m_data->rightItem->setHandlePosition(rightPointInLayerSpace, rightCenter(boundingRect));
223         m_data->bottomItem->setHandlePosition(bottomPointInLayerSpace, bottomCenter(boundingRect));
224     }
225 }
226
227
228 FormEditorItem* ResizeController::formEditorItem() const
229 {
230     return m_data->formEditorItem.data();
231 }
232
233 QWeakPointer<ResizeControllerData> ResizeController::weakPointer() const
234 {
235     return m_data;
236 }
237
238 bool ResizeController::isTopLeftHandle(const ResizeHandleItem *handle) const
239 {
240     return handle == m_data->topLeftItem;
241 }
242
243 bool ResizeController::isTopRightHandle(const ResizeHandleItem *handle) const
244 {
245     return handle == m_data->topRightItem;
246 }
247
248 bool ResizeController::isBottomLeftHandle(const ResizeHandleItem *handle) const
249 {
250     return handle == m_data->bottomLeftItem;
251 }
252
253 bool ResizeController::isBottomRightHandle(const ResizeHandleItem *handle) const
254 {
255     return handle == m_data->bottomRightItem;
256 }
257
258 bool ResizeController::isTopHandle(const ResizeHandleItem *handle) const
259 {
260     return handle == m_data->topItem;
261 }
262
263 bool ResizeController::isLeftHandle(const ResizeHandleItem *handle) const
264 {
265     return handle == m_data->leftItem;
266 }
267
268 bool ResizeController::isRightHandle(const ResizeHandleItem *handle) const
269 {
270     return handle == m_data->rightItem;
271 }
272
273 bool ResizeController::isBottomHandle(const ResizeHandleItem *handle) const
274 {
275     return handle == m_data->bottomItem;
276 }
277
278 }