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 "selectionindicator.h"
38 #include <QGraphicsScene>
39 #include <formeditorview.h>
40 #include <formeditorwidget.h>
41 #include <zoomaction.h>
43 namespace QmlDesigner {
45 SelectionIndicator::SelectionIndicator(LayerItem *layerItem)
46 : m_layerItem(layerItem)
50 SelectionIndicator::~SelectionIndicator()
55 void SelectionIndicator::show()
57 foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
61 void SelectionIndicator::hide()
63 foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
67 void SelectionIndicator::clear()
70 foreach (QGraphicsItem *item, m_indicatorShapeHash.values())
71 m_layerItem->scene()->removeItem(item);
73 m_indicatorShapeHash.clear();
76 //static void alignVertices(QPolygonF &polygon, double factor)
78 // QMutableVectorIterator<QPointF> iterator(polygon);
79 // while (iterator.hasNext()) {
80 // QPointF &vertex = iterator.next();
81 // vertex.setX(std::floor(vertex.x()) + factor);
82 // vertex.setY(std::floor(vertex.y()) + factor);
87 void SelectionIndicator::setItems(const QList<FormEditorItem*> &itemList)
91 foreach (FormEditorItem *item, itemList) {
92 if (!item->qmlItemNode().isValid())
95 QGraphicsPolygonItem *newSelectionIndicatorGraphicsItem = new QGraphicsPolygonItem(m_layerItem.data());
96 m_indicatorShapeHash.insert(item, newSelectionIndicatorGraphicsItem);
97 QPolygonF boundingRectInSceneSpace(item->mapToScene(item->qmlItemNode().instanceBoundingRect()));
98 // alignVertices(boundingRectInSceneSpace, 0.5 / item->formEditorView()->widget()->zoomAction()->zoomLevel());
99 QPolygonF boundingRectInLayerItemSpace = m_layerItem->mapFromScene(boundingRectInSceneSpace);
100 newSelectionIndicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpace);
101 newSelectionIndicatorGraphicsItem->setFlag(QGraphicsItem::ItemIsSelectable, false);
104 pen.setColor(QColor(108, 141, 221));
105 newSelectionIndicatorGraphicsItem->setPen(pen);
111 void SelectionIndicator::updateItems(const QList<FormEditorItem*> &itemList)
113 foreach (FormEditorItem *item, itemList) {
114 if (m_indicatorShapeHash.contains(item)) {
115 QGraphicsPolygonItem *indicatorGraphicsItem = m_indicatorShapeHash.value(item);
116 QPolygonF boundingRectInSceneSpace(item->mapToScene(item->qmlItemNode().instanceBoundingRect()));
117 // alignVertices(boundingRectInSceneSpace, 0.5 / item->formEditorView()->widget()->zoomAction()->zoomLevel());
118 QPolygonF boundingRectInLayerItemSpace = m_layerItem->mapFromScene(boundingRectInSceneSpace);
119 indicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpace);