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 "boundingrecthighlighter.h"
34 #include "qdeclarativeviewinspector.h"
35 #include "qmlinspectorconstants.h"
37 #include <QtGui/QGraphicsPolygonItem>
39 #include <QtCore/QTimer>
40 #include <QtCore/QObject>
41 #include <QtCore/QDebug>
43 namespace QmlJSDebugger
{
45 BoundingBox::BoundingBox(QGraphicsObject
*itemToHighlight
, QGraphicsItem
*parentItem
,
48 highlightedObject(itemToHighlight
),
50 highlightPolygonEdge(0)
52 highlightPolygon
= new BoundingBoxPolygonItem(parentItem
);
53 highlightPolygonEdge
= new BoundingBoxPolygonItem(parentItem
);
55 highlightPolygon
->setPen(QPen(QColor(0, 22, 159)));
56 highlightPolygonEdge
->setPen(QPen(QColor(158, 199, 255)));
58 highlightPolygon
->setFlag(QGraphicsItem::ItemIsSelectable
, false);
59 highlightPolygonEdge
->setFlag(QGraphicsItem::ItemIsSelectable
, false);
62 BoundingBox::~BoundingBox()
64 highlightedObject
.clear();
67 BoundingBoxPolygonItem::BoundingBoxPolygonItem(QGraphicsItem
*item
) : QGraphicsPolygonItem(item
)
70 pen
.setColor(QColor(108, 141, 221));
75 int BoundingBoxPolygonItem::type() const
77 return Constants::EditorItemType
;
80 BoundingRectHighlighter::BoundingRectHighlighter(QDeclarativeViewInspector
*view
) :
81 LiveLayerItem(view
->declarativeView()->scene()),
86 BoundingRectHighlighter::~BoundingRectHighlighter()
91 void BoundingRectHighlighter::clear()
93 foreach (BoundingBox
*box
, m_boxes
)
97 BoundingBox
*BoundingRectHighlighter::boxFor(QGraphicsObject
*item
) const
99 foreach (BoundingBox
*box
, m_boxes
) {
100 if (box
->highlightedObject
.data() == item
)
106 void BoundingRectHighlighter::highlight(QList
<QGraphicsObject
*> items
)
111 QList
<BoundingBox
*> newBoxes
;
112 foreach (QGraphicsObject
*itemToHighlight
, items
) {
113 BoundingBox
*box
= boxFor(itemToHighlight
);
115 box
= createBoundingBox(itemToHighlight
);
121 if (newBoxes
!= m_boxes
) {
129 void BoundingRectHighlighter::highlight(QGraphicsObject
* itemToHighlight
)
131 if (!itemToHighlight
)
134 BoundingBox
*box
= boxFor(itemToHighlight
);
136 box
= createBoundingBox(itemToHighlight
);
144 BoundingBox
*BoundingRectHighlighter::createBoundingBox(QGraphicsObject
*itemToHighlight
)
146 if (!m_freeBoxes
.isEmpty()) {
147 BoundingBox
*box
= m_freeBoxes
.last();
148 if (box
->highlightedObject
.isNull()) {
149 box
->highlightedObject
= itemToHighlight
;
150 box
->highlightPolygon
->show();
151 box
->highlightPolygonEdge
->show();
152 m_freeBoxes
.removeLast();
157 BoundingBox
*box
= new BoundingBox(itemToHighlight
, this, this);
159 connect(itemToHighlight
, SIGNAL(xChanged()), this, SLOT(refresh()));
160 connect(itemToHighlight
, SIGNAL(yChanged()), this, SLOT(refresh()));
161 connect(itemToHighlight
, SIGNAL(widthChanged()), this, SLOT(refresh()));
162 connect(itemToHighlight
, SIGNAL(heightChanged()), this, SLOT(refresh()));
163 connect(itemToHighlight
, SIGNAL(rotationChanged()), this, SLOT(refresh()));
164 connect(itemToHighlight
, SIGNAL(destroyed(QObject
*)), this, SLOT(itemDestroyed(QObject
*)));
169 void BoundingRectHighlighter::removeBoundingBox(BoundingBox
*box
)
175 void BoundingRectHighlighter::freeBoundingBox(BoundingBox
*box
)
177 if (!box
->highlightedObject
.isNull()) {
178 disconnect(box
->highlightedObject
.data(), SIGNAL(xChanged()), this, SLOT(refresh()));
179 disconnect(box
->highlightedObject
.data(), SIGNAL(yChanged()), this, SLOT(refresh()));
180 disconnect(box
->highlightedObject
.data(), SIGNAL(widthChanged()), this, SLOT(refresh()));
181 disconnect(box
->highlightedObject
.data(), SIGNAL(heightChanged()), this, SLOT(refresh()));
182 disconnect(box
->highlightedObject
.data(), SIGNAL(rotationChanged()), this, SLOT(refresh()));
185 box
->highlightedObject
.clear();
186 box
->highlightPolygon
->hide();
187 box
->highlightPolygonEdge
->hide();
188 m_boxes
.removeOne(box
);
192 void BoundingRectHighlighter::itemDestroyed(QObject
*obj
)
194 foreach (BoundingBox
*box
, m_boxes
) {
195 if (box
->highlightedObject
.data() == obj
) {
196 freeBoundingBox(box
);
202 void BoundingRectHighlighter::highlightAll()
204 foreach (BoundingBox
*box
, m_boxes
) {
205 if (box
&& box
->highlightedObject
.isNull()) {
206 // clear all highlights
210 QGraphicsObject
*item
= box
->highlightedObject
.data();
212 QRectF
boundingRectInSceneSpace(item
->mapToScene(item
->boundingRect()).boundingRect());
213 QRectF boundingRectInLayerItemSpace
= mapRectFromScene(boundingRectInSceneSpace
);
214 QRectF bboxRect
= m_view
->adjustToScreenBoundaries(boundingRectInLayerItemSpace
);
215 QRectF edgeRect
= bboxRect
;
216 edgeRect
.adjust(-1, -1, 1, 1);
218 box
->highlightPolygon
->setPolygon(QPolygonF(bboxRect
));
219 box
->highlightPolygonEdge
->setPolygon(QPolygonF(edgeRect
));
223 void BoundingRectHighlighter::refresh()
225 if (!m_boxes
.isEmpty())
230 } // namespace QmlJSDebugger
232 #include "boundingrecthighlighter.moc"