Imported Upstream version 1.0.1
[gammaray-debian.git] / qmldebugcontrol / qmljsdebugger / editor / boundingrecthighlighter.cpp
blobdd062517491779f39d25c5e669085b721f953466
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
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.
23 ** Other Usage
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,
46 QObject *parent)
47 : QObject(parent),
48 highlightedObject(itemToHighlight),
49 highlightPolygon(0),
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)
69 QPen pen;
70 pen.setColor(QColor(108, 141, 221));
71 pen.setWidth(1);
72 setPen(pen);
75 int BoundingBoxPolygonItem::type() const
77 return Constants::EditorItemType;
80 BoundingRectHighlighter::BoundingRectHighlighter(QDeclarativeViewInspector *view) :
81 LiveLayerItem(view->declarativeView()->scene()),
82 m_view(view)
86 BoundingRectHighlighter::~BoundingRectHighlighter()
91 void BoundingRectHighlighter::clear()
93 foreach (BoundingBox *box, m_boxes)
94 freeBoundingBox(box);
97 BoundingBox *BoundingRectHighlighter::boxFor(QGraphicsObject *item) const
99 foreach (BoundingBox *box, m_boxes) {
100 if (box->highlightedObject.data() == item)
101 return box;
103 return 0;
106 void BoundingRectHighlighter::highlight(QList<QGraphicsObject*> items)
108 if (items.isEmpty())
109 return;
111 QList<BoundingBox *> newBoxes;
112 foreach (QGraphicsObject *itemToHighlight, items) {
113 BoundingBox *box = boxFor(itemToHighlight);
114 if (!box)
115 box = createBoundingBox(itemToHighlight);
117 newBoxes << box;
119 qSort(newBoxes);
121 if (newBoxes != m_boxes) {
122 clear();
123 m_boxes << newBoxes;
126 highlightAll();
129 void BoundingRectHighlighter::highlight(QGraphicsObject* itemToHighlight)
131 if (!itemToHighlight)
132 return;
134 BoundingBox *box = boxFor(itemToHighlight);
135 if (!box) {
136 box = createBoundingBox(itemToHighlight);
137 m_boxes << box;
138 qSort(m_boxes);
141 highlightAll();
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();
153 return box;
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*)));
166 return box;
169 void BoundingRectHighlighter::removeBoundingBox(BoundingBox *box)
171 delete box;
172 box = 0;
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);
189 m_freeBoxes << box;
192 void BoundingRectHighlighter::itemDestroyed(QObject *obj)
194 foreach (BoundingBox *box, m_boxes) {
195 if (box->highlightedObject.data() == obj) {
196 freeBoundingBox(box);
197 break;
202 void BoundingRectHighlighter::highlightAll()
204 foreach (BoundingBox *box, m_boxes) {
205 if (box && box->highlightedObject.isNull()) {
206 // clear all highlights
207 clear();
208 return;
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())
226 highlightAll();
230 } // namespace QmlJSDebugger
232 #include "boundingrecthighlighter.moc"