1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "boundingrecthighlighter.h"
44 #include "qdeclarativeviewinspector.h"
45 #include "qmlinspectorconstants.h"
47 #include <QtWidgets/QGraphicsPolygonItem>
49 #include <QtCore/QTimer>
50 #include <QtCore/QObject>
51 #include <QtCore/QDebug>
53 namespace QmlJSDebugger {
56 BoundingBox::BoundingBox(QGraphicsObject *itemToHighlight, QGraphicsItem *parentItem,
59 highlightedObject(itemToHighlight),
61 highlightPolygonEdge(0)
63 highlightPolygon = new BoundingBoxPolygonItem(parentItem);
64 highlightPolygonEdge = new BoundingBoxPolygonItem(parentItem);
66 highlightPolygon->setPen(QPen(QColor(0, 22, 159)));
67 highlightPolygonEdge->setPen(QPen(QColor(158, 199, 255)));
69 highlightPolygon->setFlag(QGraphicsItem::ItemIsSelectable, false);
70 highlightPolygonEdge->setFlag(QGraphicsItem::ItemIsSelectable, false);
73 BoundingBox::~BoundingBox()
75 highlightedObject.clear();
78 BoundingBoxPolygonItem::BoundingBoxPolygonItem(QGraphicsItem *item) : QGraphicsPolygonItem(item)
81 pen.setColor(QColor(108, 141, 221));
86 int BoundingBoxPolygonItem::type() const
88 return Constants::EditorItemType;
91 BoundingRectHighlighter::BoundingRectHighlighter(QDeclarativeViewInspector *view) :
92 LiveLayerItem(view->declarativeView()->scene()),
97 BoundingRectHighlighter::~BoundingRectHighlighter()
102 void BoundingRectHighlighter::clear()
104 foreach (BoundingBox *box, m_boxes)
105 freeBoundingBox(box);
108 BoundingBox *BoundingRectHighlighter::boxFor(QGraphicsObject *item) const
110 foreach (BoundingBox *box, m_boxes) {
111 if (box->highlightedObject.data() == item)
117 void BoundingRectHighlighter::highlight(QList<QGraphicsObject*> items)
122 QList<BoundingBox *> newBoxes;
123 foreach (QGraphicsObject *itemToHighlight, items) {
124 BoundingBox *box = boxFor(itemToHighlight);
126 box = createBoundingBox(itemToHighlight);
132 if (newBoxes != m_boxes) {
140 void BoundingRectHighlighter::highlight(QGraphicsObject* itemToHighlight)
142 if (!itemToHighlight)
145 BoundingBox *box = boxFor(itemToHighlight);
147 box = createBoundingBox(itemToHighlight);
155 BoundingBox *BoundingRectHighlighter::createBoundingBox(QGraphicsObject *itemToHighlight)
157 if (!m_freeBoxes.isEmpty()) {
158 BoundingBox *box = m_freeBoxes.last();
159 if (box->highlightedObject.isNull()) {
160 box->highlightedObject = itemToHighlight;
161 box->highlightPolygon->show();
162 box->highlightPolygonEdge->show();
163 m_freeBoxes.removeLast();
168 BoundingBox *box = new BoundingBox(itemToHighlight, this, this);
170 connect(itemToHighlight, SIGNAL(xChanged()), this, SLOT(refresh()));
171 connect(itemToHighlight, SIGNAL(yChanged()), this, SLOT(refresh()));
172 connect(itemToHighlight, SIGNAL(widthChanged()), this, SLOT(refresh()));
173 connect(itemToHighlight, SIGNAL(heightChanged()), this, SLOT(refresh()));
174 connect(itemToHighlight, SIGNAL(rotationChanged()), this, SLOT(refresh()));
175 connect(itemToHighlight, SIGNAL(destroyed(QObject*)), this, SLOT(itemDestroyed(QObject*)));
180 void BoundingRectHighlighter::removeBoundingBox(BoundingBox *box)
186 void BoundingRectHighlighter::freeBoundingBox(BoundingBox *box)
188 if (!box->highlightedObject.isNull()) {
189 disconnect(box->highlightedObject.data(), SIGNAL(xChanged()), this, SLOT(refresh()));
190 disconnect(box->highlightedObject.data(), SIGNAL(yChanged()), this, SLOT(refresh()));
191 disconnect(box->highlightedObject.data(), SIGNAL(widthChanged()), this, SLOT(refresh()));
192 disconnect(box->highlightedObject.data(), SIGNAL(heightChanged()), this, SLOT(refresh()));
193 disconnect(box->highlightedObject.data(), SIGNAL(rotationChanged()), this, SLOT(refresh()));
196 box->highlightedObject.clear();
197 box->highlightPolygon->hide();
198 box->highlightPolygonEdge->hide();
199 m_boxes.removeOne(box);
203 void BoundingRectHighlighter::itemDestroyed(QObject *obj)
205 foreach (BoundingBox *box, m_boxes) {
206 if (box->highlightedObject.data() == obj) {
207 freeBoundingBox(box);
213 void BoundingRectHighlighter::highlightAll()
215 foreach (BoundingBox *box, m_boxes) {
216 if (box && box->highlightedObject.isNull()) {
217 // clear all highlights
221 QGraphicsObject *item = box->highlightedObject.data();
223 QRectF boundingRectInSceneSpace(item->mapToScene(item->boundingRect()).boundingRect());
224 QRectF boundingRectInLayerItemSpace = mapRectFromScene(boundingRectInSceneSpace);
225 QRectF bboxRect = m_view->adjustToScreenBoundaries(boundingRectInLayerItemSpace);
226 QRectF edgeRect = bboxRect;
227 edgeRect.adjust(-1, -1, 1, 1);
229 box->highlightPolygon->setPolygon(QPolygonF(bboxRect));
230 box->highlightPolygonEdge->setPolygon(QPolygonF(edgeRect));
234 void BoundingRectHighlighter::refresh()
236 if (!m_boxes.isEmpty())
240 } // namespace QtQuick1
241 } // namespace QmlJSDebugger