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 "anchorlinecontroller.h"
36 #include "formeditoritem.h"
37 #include "layeritem.h"
38 #include <QGraphicsScene>
40 #include "anchorlinehandleitem.h"
42 namespace QmlDesigner {
45 AnchorLineControllerData::AnchorLineControllerData(LayerItem *layerItem, FormEditorItem *formEditorItem)
46 : layerItem(layerItem),
47 formEditorItem(formEditorItem),
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)
64 AnchorLineControllerData::~AnchorLineControllerData()
67 layerItem->scene()->removeItem(topItem);
68 layerItem->scene()->removeItem(leftItem);
69 layerItem->scene()->removeItem(rightItem);
70 layerItem->scene()->removeItem(bottomItem);
75 AnchorLineController::AnchorLineController()
76 : m_data(new AnchorLineControllerData(0, 0))
81 AnchorLineController::AnchorLineController(const QSharedPointer<AnchorLineControllerData> &data)
87 AnchorLineController::AnchorLineController(LayerItem *layerItem, FormEditorItem *formEditorItem)
88 : m_data(new AnchorLineControllerData(layerItem, formEditorItem))
90 m_data->topItem = new AnchorLineHandleItem(layerItem, *this);
91 m_data->topItem->setZValue(300);
93 m_data->leftItem = new AnchorLineHandleItem(layerItem, *this);
94 m_data->leftItem->setZValue(300);
96 m_data->rightItem = new AnchorLineHandleItem(layerItem, *this);
97 m_data->rightItem->setZValue(300);
99 m_data->bottomItem = new AnchorLineHandleItem(layerItem, *this);
100 m_data->bottomItem->setZValue(300);
106 bool AnchorLineController::isValid() const
108 return m_data->formEditorItem != 0;
111 void AnchorLineController::show(AnchorLine::Type anchorLineMask)
113 if (anchorLineMask & AnchorLine::Top)
114 m_data->topItem->show();
116 m_data->topItem->hide();
118 if (anchorLineMask & AnchorLine::Left)
119 m_data->leftItem->show();
121 m_data->leftItem->hide();
123 if (anchorLineMask & AnchorLine::Right)
124 m_data->rightItem->show();
126 m_data->rightItem->hide();
128 if (anchorLineMask & AnchorLine::Bottom)
129 m_data->bottomItem->show();
131 m_data->bottomItem->hide();
134 void AnchorLineController::hide()
136 m_data->topItem->hide();
137 m_data->leftItem->hide();
138 m_data->rightItem->hide();
139 m_data->bottomItem->hide();
142 static QPainterPath rectToPath(const QRectF &rect)
145 path.addRoundedRect(rect, 4, 4);
150 void AnchorLineController::updatePosition()
152 QRectF boundingRect = m_data->formEditorItem->qmlItemNode().instanceBoundingRect();
154 QRectF topBoundingRect(boundingRect);
155 QRectF leftBoundingRect(boundingRect);
156 QRectF bottomBoundingRect(boundingRect);
157 QRectF rightBoundingRect(boundingRect);
160 if (formEditorItem()->isContainer()) {
161 topBoundingRect.setBottom(boundingRect.top() + 6);
162 topBoundingRect.adjust(7, -5, -7, 0);
164 leftBoundingRect.setRight(boundingRect.left() + 6);
165 leftBoundingRect.adjust(-5, 7, 0, -7);
167 bottomBoundingRect.setTop(boundingRect.bottom() - 6);
168 bottomBoundingRect.adjust(7, 0, -7, 5);
170 rightBoundingRect.setLeft(boundingRect.right() - 6);
171 rightBoundingRect.adjust(0, 7, 5, -7);
174 double height = qMin(boundingRect.height() / 4., 10.);
175 double width = qMin(boundingRect.width() / 4., 10.);
177 topBoundingRect.setHeight(height);
178 topBoundingRect.adjust(width, -4, -width, -1);
180 leftBoundingRect.setWidth(width);
181 leftBoundingRect.adjust(-4, height, -1, -height);
183 bottomBoundingRect.setTop(boundingRect.bottom() - height);
184 bottomBoundingRect.adjust(width, 1, -width, 4);
186 rightBoundingRect.setLeft(boundingRect.right() - width);
187 rightBoundingRect.adjust(1, height, 4, -height);
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)));
201 FormEditorItem* AnchorLineController::formEditorItem() const
203 return m_data->formEditorItem;
206 QWeakPointer<AnchorLineControllerData> AnchorLineController::weakPointer() const
212 bool AnchorLineController::isTopHandle(const AnchorLineHandleItem *handle) const
214 return handle == m_data->topItem;
217 bool AnchorLineController::isLeftHandle(const AnchorLineHandleItem *handle) const
219 return handle == m_data->leftItem;
222 bool AnchorLineController::isRightHandle(const AnchorLineHandleItem *handle) const
224 return handle == m_data->rightItem;
227 bool AnchorLineController::isBottomHandle(const AnchorLineHandleItem *handle) const
229 return handle == m_data->bottomItem;
232 void AnchorLineController::clearHighlight()
234 m_data->topItem->setHiglighted(false);
235 m_data->leftItem->setHiglighted(false);
236 m_data->rightItem->setHiglighted(false);
237 m_data->bottomItem->setHiglighted(false);
240 } // namespace QmlDesigner