1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "resizecontroller.h"
35 #include "formeditoritem.h"
36 #include "layeritem.h"
38 #include <resizehandleitem.h>
40 #include <QGraphicsScene>
42 namespace QmlDesigner {
46 ResizeControllerData::ResizeControllerData(LayerItem *layerItem, FormEditorItem *formEditorItem)
47 : layerItem(layerItem),
48 formEditorItem(formEditorItem),
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)
74 ResizeControllerData::~ResizeControllerData()
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);
89 ResizeController::ResizeController()
90 : m_data(new ResizeControllerData(0, 0))
95 ResizeController::ResizeController(const QSharedPointer<ResizeControllerData> &data)
101 ResizeController::ResizeController(LayerItem *layerItem, FormEditorItem *formEditorItem)
102 : m_data(new ResizeControllerData(layerItem, formEditorItem))
104 m_data->topLeftItem = new ResizeHandleItem(layerItem, *this);
105 m_data->topLeftItem->setZValue(302);
106 m_data->topLeftItem->setCursor(Qt::SizeFDiagCursor);
108 m_data->topRightItem = new ResizeHandleItem(layerItem, *this);
109 m_data->topRightItem->setZValue(301);
110 m_data->topRightItem->setCursor(Qt::SizeBDiagCursor);
112 m_data->bottomLeftItem = new ResizeHandleItem(layerItem, *this);
113 m_data->bottomLeftItem->setZValue(301);
114 m_data->bottomLeftItem->setCursor(Qt::SizeBDiagCursor);
116 m_data->bottomRightItem = new ResizeHandleItem(layerItem, *this);
117 m_data->bottomRightItem->setZValue(305);
118 m_data->bottomRightItem->setCursor(Qt::SizeFDiagCursor);
120 m_data->topItem = new ResizeHandleItem(layerItem, *this);
121 m_data->topItem->setZValue(300);
122 m_data->topItem->setCursor(Qt::SizeVerCursor);
124 m_data->leftItem = new ResizeHandleItem(layerItem, *this);
125 m_data->leftItem->setZValue(300);
126 m_data->leftItem->setCursor(Qt::SizeHorCursor);
128 m_data->rightItem = new ResizeHandleItem(layerItem, *this);
129 m_data->rightItem->setZValue(300);
130 m_data->rightItem->setCursor(Qt::SizeHorCursor);
132 m_data->bottomItem = new ResizeHandleItem(layerItem, *this);
133 m_data->bottomItem->setZValue(300);
134 m_data->bottomItem->setCursor(Qt::SizeVerCursor);
140 bool ResizeController::isValid() const
142 return m_data->formEditorItem && m_data->formEditorItem->qmlItemNode().isValid();
145 void ResizeController::show()
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();
156 void ResizeController::hide()
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();
169 static QPointF topCenter(const QRectF &rect)
171 return QPointF(rect.center().x(), rect.top());
174 static QPointF leftCenter(const QRectF &rect)
176 return QPointF(rect.left(), rect.center().y());
179 static QPointF rightCenter(const QRectF &rect)
181 return QPointF(rect.right(), rect.center().y());
184 static QPointF bottomCenter(const QRectF &rect)
186 return QPointF(rect.center().x(), rect.bottom());
190 void ResizeController::updatePosition()
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()));
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)));
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)));
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));
228 FormEditorItem* ResizeController::formEditorItem() const
230 return m_data->formEditorItem.data();
233 QWeakPointer<ResizeControllerData> ResizeController::weakPointer() const
238 bool ResizeController::isTopLeftHandle(const ResizeHandleItem *handle) const
240 return handle == m_data->topLeftItem;
243 bool ResizeController::isTopRightHandle(const ResizeHandleItem *handle) const
245 return handle == m_data->topRightItem;
248 bool ResizeController::isBottomLeftHandle(const ResizeHandleItem *handle) const
250 return handle == m_data->bottomLeftItem;
253 bool ResizeController::isBottomRightHandle(const ResizeHandleItem *handle) const
255 return handle == m_data->bottomRightItem;
258 bool ResizeController::isTopHandle(const ResizeHandleItem *handle) const
260 return handle == m_data->topItem;
263 bool ResizeController::isLeftHandle(const ResizeHandleItem *handle) const
265 return handle == m_data->leftItem;
268 bool ResizeController::isRightHandle(const ResizeHandleItem *handle) const
270 return handle == m_data->rightItem;
273 bool ResizeController::isBottomHandle(const ResizeHandleItem *handle) const
275 return handle == m_data->bottomItem;