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 "liveselectionindicator.h"
35 #include "../qdeclarativeviewobserver_p.h"
36 #include "qmlobserverconstants.h"
38 #include <QtCore/QDebug>
40 #include <QtGui/QGraphicsPolygonItem>
41 #include <QtGui/QGraphicsObject>
42 #include <QtGui/QGraphicsScene>
47 namespace QmlJSDebugger {
49 LiveSelectionIndicator::LiveSelectionIndicator(QDeclarativeViewObserver *editorView,
50 QGraphicsObject *layerItem)
51 : m_layerItem(layerItem), m_view(editorView)
55 LiveSelectionIndicator::~LiveSelectionIndicator()
60 void LiveSelectionIndicator::show()
62 foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
66 void LiveSelectionIndicator::hide()
68 foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
72 void LiveSelectionIndicator::clear()
74 if (!m_layerItem.isNull()) {
75 QHashIterator<QGraphicsItem*, QGraphicsPolygonItem *> iter(m_indicatorShapeHash);
76 while(iter.hasNext()) {
78 m_layerItem.data()->scene()->removeItem(iter.value());
83 m_indicatorShapeHash.clear();
87 QPolygonF LiveSelectionIndicator::addBoundingRectToPolygon(QGraphicsItem *item, QPolygonF &polygon)
89 // ### remove this if statement when QTBUG-12172 gets fixed
90 if (item->boundingRect() != QRectF(0,0,0,0)) {
91 QPolygonF bounding = item->mapToScene(item->boundingRect());
92 if (bounding.isClosed()) //avoid crashes if there is an infinite scale.
93 polygon = polygon.united(bounding);
96 foreach (QGraphicsItem *child, item->childItems()) {
97 if (!QDeclarativeViewObserverPrivate::get(m_view)->isEditorItem(child))
98 addBoundingRectToPolygon(child, polygon);
103 void LiveSelectionIndicator::setItems(const QList<QWeakPointer<QGraphicsObject> > &itemList)
107 // set selections to also all children if they are not editor items
109 foreach (QWeakPointer<QGraphicsObject> object, itemList) {
113 QGraphicsItem *item = object.data();
115 QGraphicsPolygonItem *newSelectionIndicatorGraphicsItem
116 = new QGraphicsPolygonItem(m_layerItem.data());
117 if (!m_indicatorShapeHash.contains(item)) {
118 m_indicatorShapeHash.insert(item, newSelectionIndicatorGraphicsItem);
120 QPolygonF boundingShapeInSceneSpace;
121 addBoundingRectToPolygon(item, boundingShapeInSceneSpace);
124 = m_view->adjustToScreenBoundaries(boundingShapeInSceneSpace.boundingRect());
125 QPolygonF boundingRectInLayerItemSpace = m_layerItem.data()->mapFromScene(boundingRect);
128 pen.setColor(QColor(108, 141, 221));
129 newSelectionIndicatorGraphicsItem->setData(Constants::EditorItemDataKey,
131 newSelectionIndicatorGraphicsItem->setFlag(QGraphicsItem::ItemIsSelectable, false);
132 newSelectionIndicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpace);
133 newSelectionIndicatorGraphicsItem->setPen(pen);
138 } //namespace QmlJSDebugger