Qt 4.8
boundingrecthighlighter.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** 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.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
43 
44 #include "../qdeclarativeviewinspector.h"
45 #include "../qmlinspectorconstants.h"
46 
47 #include <QtGui/QGraphicsPolygonItem>
48 
49 #include <QtCore/QTimer>
50 #include <QtCore/QObject>
51 #include <QtCore/QDebug>
52 
53 namespace QmlJSDebugger {
54 
56  QObject *parent)
57  : QObject(parent),
58  highlightedObject(itemToHighlight),
59  highlightPolygon(0),
60  highlightPolygonEdge(0)
61 {
62  highlightPolygon = new BoundingBoxPolygonItem(parentItem);
64 
65  highlightPolygon->setPen(QPen(QColor(0, 22, 159)));
66  highlightPolygonEdge->setPen(QPen(QColor(158, 199, 255)));
67 
70 }
71 
73 {
75 }
76 
78 {
79  QPen pen;
80  pen.setColor(QColor(108, 141, 221));
81  pen.setWidth(1);
82  setPen(pen);
83 }
84 
86 {
88 }
89 
91  LiveLayerItem(view->declarativeView()->scene()),
92  m_view(view)
93 {
94 }
95 
97 {
98 
99 }
100 
102 {
103  foreach (BoundingBox *box, m_boxes)
104  freeBoundingBox(box);
105 }
106 
108 {
109  foreach (BoundingBox *box, m_boxes) {
110  if (box->highlightedObject.data() == item)
111  return box;
112  }
113  return 0;
114 }
115 
117 {
118  if (items.isEmpty())
119  return;
120 
121  QList<BoundingBox *> newBoxes;
122  foreach (QGraphicsObject *itemToHighlight, items) {
123  BoundingBox *box = boxFor(itemToHighlight);
124  if (!box)
125  box = createBoundingBox(itemToHighlight);
126 
127  newBoxes << box;
128  }
129  qSort(newBoxes);
130 
131  if (newBoxes != m_boxes) {
132  clear();
133  m_boxes << newBoxes;
134  }
135 
136  highlightAll();
137 }
138 
140 {
141  if (!itemToHighlight)
142  return;
143 
144  BoundingBox *box = boxFor(itemToHighlight);
145  if (!box) {
146  box = createBoundingBox(itemToHighlight);
147  m_boxes << box;
148  qSort(m_boxes);
149  }
150 
151  highlightAll();
152 }
153 
155 {
156  if (!m_freeBoxes.isEmpty()) {
157  BoundingBox *box = m_freeBoxes.last();
158  if (box->highlightedObject.isNull()) {
159  box->highlightedObject = itemToHighlight;
160  box->highlightPolygon->show();
161  box->highlightPolygonEdge->show();
162  m_freeBoxes.removeLast();
163  return box;
164  }
165  }
166 
167  BoundingBox *box = new BoundingBox(itemToHighlight, this, this);
168 
169  connect(itemToHighlight, SIGNAL(xChanged()), this, SLOT(refresh()));
170  connect(itemToHighlight, SIGNAL(yChanged()), this, SLOT(refresh()));
171  connect(itemToHighlight, SIGNAL(widthChanged()), this, SLOT(refresh()));
172  connect(itemToHighlight, SIGNAL(heightChanged()), this, SLOT(refresh()));
173  connect(itemToHighlight, SIGNAL(rotationChanged()), this, SLOT(refresh()));
174  connect(itemToHighlight, SIGNAL(destroyed(QObject*)), this, SLOT(itemDestroyed(QObject*)));
175 
176  return box;
177 }
178 
180 {
181  delete box;
182  box = 0;
183 }
184 
186 {
187  if (!box->highlightedObject.isNull()) {
193  }
194 
195  box->highlightedObject.clear();
196  box->highlightPolygon->hide();
197  box->highlightPolygonEdge->hide();
198  m_boxes.removeOne(box);
199  m_freeBoxes << box;
200 }
201 
203 {
204  foreach (BoundingBox *box, m_boxes) {
205  if (box->highlightedObject.data() == obj) {
206  freeBoundingBox(box);
207  break;
208  }
209  }
210 }
211 
213 {
214  foreach (BoundingBox *box, m_boxes) {
215  if (box && box->highlightedObject.isNull()) {
216  // clear all highlights
217  clear();
218  return;
219  }
220  QGraphicsObject *item = box->highlightedObject.data();
221 
222  QRectF boundingRectInSceneSpace(item->mapToScene(item->boundingRect()).boundingRect());
223  QRectF boundingRectInLayerItemSpace = mapRectFromScene(boundingRectInSceneSpace);
224  QRectF bboxRect = m_view->adjustToScreenBoundaries(boundingRectInLayerItemSpace);
225  QRectF edgeRect = bboxRect;
226  edgeRect.adjust(-1, -1, 1, 1);
227 
228  box->highlightPolygon->setPolygon(QPolygonF(bboxRect));
229  box->highlightPolygonEdge->setPolygon(QPolygonF(edgeRect));
230  }
231 }
232 
234 {
235  if (!m_boxes.isEmpty())
236  highlightAll();
237 }
238 
239 
240 } // namespace QmlJSDebugger
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
void heightChanged()
virtual QRectF boundingRect() const =0
This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be ...
#define SLOT(a)
Definition: qobjectdefs.h:226
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
QGraphicsPolygonItem * highlightPolygon
bool isNull() const
Returns true if this object is holding a reference to a null pointer.
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
The QGraphicsPolygonItem class provides a polygon item that you can add to a QGraphicsScene.
void rotationChanged()
This signal gets emitted whenever the roation of the item changes.
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:64
T * data() const
BoundingBox(QGraphicsObject *itemToHighlight, QGraphicsItem *parentItem, QObject *parent=0)
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void setColor(const QColor &color)
Sets the color of this pen&#39;s brush to the given color.
Definition: qpen.cpp:787
#define SIGNAL(a)
Definition: qobjectdefs.h:227
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
int type() const
Reimplemented Function
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
QRectF mapRectFromScene(const QRectF &rect) const
Maps the rectangle rect, which is in scene coordinates, to this item&#39;s coordinate system...
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
BoundingBox * boxFor(QGraphicsObject *item) const
BoundingBox * createBoundingBox(QGraphicsObject *itemToHighlight)
The QPolygonF class provides a vector of points using floating point precision.
Definition: qpolygon.h:134
void setPen(const QPen &pen)
Sets the pen for this item to pen.
BoundingRectHighlighter(QDeclarativeViewInspector *view)
QRectF adjustToScreenBoundaries(const QRectF &boundingRectInSceneSpace)
void qSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:177
QPen pen() const
Returns the item&#39;s pen.
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
void adjust(qreal x1, qreal y1, qreal x2, qreal y2)
Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle.
Definition: qrect.h:778
void setPolygon(const QPolygonF &polygon)
Sets the item&#39;s polygon to be the given polygon.
void hide()
Hides the item.
void yChanged()
This signal gets emitted whenever the y position of the item changes.
void xChanged()
This signal gets emitted whenever the x position of the item changes.
void clear()
Clears this QWeakPointer object, dropping the reference that it may have had to the pointer...
QRectF boundingRect() const
This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be ...
QGraphicsScene * scene() const
Returns the current scene for the item, or 0 if the item is not stored in a scene.
The QGraphicsObject class provides a base class for all graphics items that require signals...
void setFlag(GraphicsItemFlag flag, bool enabled=true)
If enabled is true, the item flag flag is enabled; otherwise, it is disabled.
QPointF mapToScene(const QPointF &point) const
Maps the point point, which is in this item&#39;s coordinate system, to the scene&#39;s coordinate system...
QGraphicsPolygonItem * highlightPolygonEdge
void highlight(QList< QGraphicsObject *> items)
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
void show()
Shows the item.
void setWidth(int width)
Sets the pen width to the given width in pixels with integer precision.
Definition: qpen.cpp:667
QWeakPointer< QGraphicsObject > highlightedObject