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 "selectionindicator.h"
37 #include <QGraphicsScene>
38 #include <formeditorview.h>
39 #include <formeditorwidget.h>
40 #include <zoomaction.h>
42 namespace QmlDesigner {
44 SelectionIndicator::SelectionIndicator(LayerItem *layerItem)
45 : m_layerItem(layerItem)
49 SelectionIndicator::~SelectionIndicator()
54 void SelectionIndicator::show()
56 foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
60 void SelectionIndicator::hide()
62 foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
66 void SelectionIndicator::clear()
69 foreach (QGraphicsItem *item, m_indicatorShapeHash.values())
70 m_layerItem->scene()->removeItem(item);
72 m_indicatorShapeHash.clear();
75 //static void alignVertices(QPolygonF &polygon, double factor)
77 // QMutableVectorIterator<QPointF> iterator(polygon);
78 // while (iterator.hasNext()) {
79 // QPointF &vertex = iterator.next();
80 // vertex.setX(std::floor(vertex.x()) + factor);
81 // vertex.setY(std::floor(vertex.y()) + factor);
86 void SelectionIndicator::setItems(const QList<FormEditorItem*> &itemList)
90 foreach (FormEditorItem *item, itemList) {
91 if (!item->qmlItemNode().isValid())
94 QGraphicsPolygonItem *newSelectionIndicatorGraphicsItem = new QGraphicsPolygonItem(m_layerItem.data());
95 m_indicatorShapeHash.insert(item, newSelectionIndicatorGraphicsItem);
96 QPolygonF boundingRectInSceneSpace(item->mapToScene(item->qmlItemNode().instanceBoundingRect()));
97 // alignVertices(boundingRectInSceneSpace, 0.5 / item->formEditorView()->widget()->zoomAction()->zoomLevel());
98 QPolygonF boundingRectInLayerItemSpace = m_layerItem->mapFromScene(boundingRectInSceneSpace);
99 newSelectionIndicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpace);
100 newSelectionIndicatorGraphicsItem->setFlag(QGraphicsItem::ItemIsSelectable, false);
103 pen.setColor(QColor(108, 141, 221));
104 newSelectionIndicatorGraphicsItem->setPen(pen);
110 void SelectionIndicator::updateItems(const QList<FormEditorItem*> &itemList)
112 foreach (FormEditorItem *item, itemList) {
113 if (m_indicatorShapeHash.contains(item)) {
114 QGraphicsPolygonItem *indicatorGraphicsItem = m_indicatorShapeHash.value(item);
115 QPolygonF boundingRectInSceneSpace(item->mapToScene(item->qmlItemNode().instanceBoundingRect()));
116 // alignVertices(boundingRectInSceneSpace, 0.5 / item->formEditorView()->widget()->zoomAction()->zoomLevel());
117 QPolygonF boundingRectInLayerItemSpace = m_layerItem->mapFromScene(boundingRectInSceneSpace);
118 indicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpace);