Qt 4.8
qdeclarativeviewinspector.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 
44 
46 #include "editor/zoomtool.h"
47 #include "editor/colorpickertool.h"
48 #include "editor/livelayeritem.h"
50 
51 #include <QtDeclarative/QDeclarativeItem>
52 #include <QtGui/QWidget>
53 #include <QtGui/QMouseEvent>
54 #include <QtGui/QGraphicsObject>
55 
56 namespace QmlJSDebugger {
57 
59  q(q)
60 {
61 }
62 
64 {
65 }
66 
68  QObject *parent) :
69  AbstractViewInspector(parent),
71 {
72  data->view = view;
73  data->manipulatorLayer = new LiveLayerItem(view->scene());
74  data->selectionTool = new LiveSelectionTool(this);
75  data->zoomTool = new ZoomTool(this);
76  data->colorPickerTool = new ColorPickerTool(this);
77  data->boundingRectHighlighter = new BoundingRectHighlighter(this);
78  setCurrentTool(data->selectionTool);
79 
80  // to capture ChildRemoved event when viewport changes
81  data->view->installEventFilter(this);
82 
83  data->setViewport(data->view->viewport());
84 
85  connect(data->view, SIGNAL(statusChanged(QDeclarativeView::Status)),
86  data.data(), SLOT(_q_onStatusChanged(QDeclarativeView::Status)));
87 
88  connect(data->colorPickerTool, SIGNAL(selectedColorChanged(QColor)),
90  connect(data->colorPickerTool, SIGNAL(selectedColorChanged(QColor)),
91  this, SLOT(sendColorChanged(QColor)));
92 
94 }
95 
97 {
98 }
99 
101 {
102  QList<QGraphicsItem*> items;
103  QList<QGraphicsObject*> gfxObjects;
104  foreach (QObject *obj, objects) {
105  if (QDeclarativeItem *declarativeItem = qobject_cast<QDeclarativeItem*>(obj)) {
106  items << declarativeItem;
107  gfxObjects << declarativeItem;
108  }
109  }
110  if (designModeBehavior()) {
111  data->setSelectedItemsForTools(items);
112  data->clearHighlight();
113  data->highlight(gfxObjects);
114  }
115 }
116 
118 {
119  data->clearHighlight();
121 }
122 
124 {
125  switch (tool) {
127  data->changeToColorPickerTool();
128  break;
130  data->changeToMarqueeSelectTool();
131  break;
133  data->changeToSingleSelectTool();
134  break;
136  data->changeToZoomTool();
137  break;
138  }
139 }
140 
142 {
144 }
145 
147 {
148  return data->view->engine();
149 }
150 
152 {
153  if (viewport.data() == widget)
154  return;
155 
156  if (viewport)
157  viewport.data()->removeEventFilter(q);
158 
159  viewport = widget;
160  if (viewport) {
161  // make sure we get mouse move events
162  viewport.data()->setMouseTracking(true);
163  viewport.data()->installEventFilter(q);
164  }
165 }
166 
168 {
169  clearHighlight();
171 }
172 
174 {
175  if (obj == data->view) {
176  // Event from view
177  if (event->type() == QEvent::ChildRemoved) {
178  // Might mean that viewport has changed
179  if (data->view->viewport() != data->viewport.data())
180  data->setViewport(data->view->viewport());
181  }
182  return QObject::eventFilter(obj, event);
183  }
184 
185  return AbstractViewInspector::eventFilter(obj, event);
186 }
187 
189 {
190  data->clearHighlight();
191  return AbstractViewInspector::leaveEvent(event);
192 }
193 
195 {
196  QList<QGraphicsItem*> selItems = data->selectableItems(event->pos());
197  if (!selItems.isEmpty()) {
198  declarativeView()->setToolTip(currentTool()->titleForItem(selItems.first()));
199  } else {
201  }
202 
204 }
205 
207 {
208  if (!newParent)
209  return;
210 
211  object->setParent(newParent);
212  QDeclarativeItem *newParentItem = qobject_cast<QDeclarativeItem*>(newParent);
214  if (newParentItem && item)
215  item->setParentItem(newParentItem);
216 }
217 
219 {
221  if (QGraphicsItem *item = qobject_cast<QGraphicsObject*>(obj))
222  items.removeOne(item);
223  setSelectedItems(items);
224 }
225 
227 {
228  foreach (const QWeakPointer<QGraphicsObject> &obj, currentSelection) {
229  if (QGraphicsItem *item = obj.data()) {
230  if (!items.contains(item)) {
232  this, SLOT(_q_removeFromSelection(QObject*)));
233  currentSelection.removeOne(obj);
234  }
235  }
236  }
237 
238  foreach (QGraphicsItem *item, items) {
239  if (QGraphicsObject *obj = item->toGraphicsObject()) {
240  if (!currentSelection.contains(obj)) {
242  this, SLOT(_q_removeFromSelection(QObject*)));
243  currentSelection.append(obj);
244  }
245  }
246  }
247 
248  q->currentTool()->updateSelectedItems();
249 }
250 
252 {
253  QList<QWeakPointer<QGraphicsObject> > oldList = currentSelection;
254  setSelectedItemsForTools(items);
255  if (oldList != currentSelection) {
256  QList<QObject*> objectList;
257  foreach (const QWeakPointer<QGraphicsObject> &graphicsObject, currentSelection) {
258  if (graphicsObject)
259  objectList << graphicsObject.data();
260  }
261 
262  q->sendCurrentObjects(objectList);
263  }
264 }
265 
267 {
268  QList<QGraphicsItem *> selection;
269  foreach (const QWeakPointer<QGraphicsObject> &selectedObject, currentSelection) {
270  if (selectedObject.data())
271  selection << selectedObject.data();
272  }
273 
274  return selection;
275 }
276 
278 {
279  data->setSelectedItems(items);
280 }
281 
283 {
284  return data->selectedItems();
285 }
286 
288 {
289  return data->view;
290 }
291 
293 {
294  boundingRectHighlighter->clear();
295 }
296 
298 {
299  if (items.isEmpty())
300  return;
301 
302  QList<QGraphicsObject*> objectList;
303  foreach (QGraphicsItem *item, items) {
304  QGraphicsItem *child = item;
305 
306  if (child) {
307  QGraphicsObject *childObject = child->toGraphicsObject();
308  if (childObject)
309  objectList << childObject;
310  }
311  }
312 
313  boundingRectHighlighter->highlight(objectList);
314 }
315 
317  const QPointF &scenePos) const
318 {
319  QList<QGraphicsItem*> itemlist = view->scene()->items(scenePos);
320  return filterForSelection(itemlist);
321 }
322 
324 {
325  QList<QGraphicsItem*> itemlist = view->items(pos);
326  return filterForSelection(itemlist);
327 }
328 
330  const QRectF &sceneRect, Qt::ItemSelectionMode selectionMode) const
331 {
332  QList<QGraphicsItem*> itemlist = view->scene()->items(sceneRect, selectionMode);
333  return filterForSelection(itemlist);
334 }
335 
337 {
338  selectionTool->setRubberbandSelectionMode(false);
339 
340  changeToSelectTool();
341 
342  emit q->selectToolActivated();
343  q->sendCurrentTool(Constants::SelectionToolMode);
344 }
345 
347 {
348  if (q->currentTool() == selectionTool)
349  return;
350 
351  q->currentTool()->clear();
352  q->setCurrentTool(selectionTool);
353  q->currentTool()->clear();
354  q->currentTool()->updateSelectedItems();
355 }
356 
358 {
359  changeToSelectTool();
360  selectionTool->setRubberbandSelectionMode(true);
361 
362  emit q->marqueeSelectToolActivated();
363  q->sendCurrentTool(Constants::MarqueeSelectionToolMode);
364 }
365 
367 {
368  q->currentTool()->clear();
369  q->setCurrentTool(zoomTool);
370  q->currentTool()->clear();
371 
372  emit q->zoomToolActivated();
373  q->sendCurrentTool(Constants::ZoomMode);
374 }
375 
377 {
378  if (q->currentTool() == colorPickerTool)
379  return;
380 
381  q->currentTool()->clear();
382  q->setCurrentTool(colorPickerTool);
383  q->currentTool()->clear();
384 
385  emit q->colorPickerActivated();
386  q->sendCurrentTool(Constants::ColorPickerMode);
387 }
388 
389 
390 static bool isEditorItem(QGraphicsItem *item)
391 {
392  return (item->type() == Constants::EditorItemType
395 }
396 
398  QList<QGraphicsItem*> &itemlist) const
399 {
400  foreach (QGraphicsItem *item, itemlist) {
401  if (isEditorItem(item))
402  itemlist.removeOne(item);
403  }
404 
405  return itemlist;
406 }
407 
409 {
410  if (status == QDeclarativeView::Ready)
411  q->sendReloaded();
412 }
413 
414 // adjusts bounding boxes on edges of screen to be visible
416 {
417  int marginFromEdge = 1;
418  QRectF boundingRect(boundingRectInSceneSpace);
419  if (qAbs(boundingRect.left()) - 1 < 2)
420  boundingRect.setLeft(marginFromEdge);
421 
422  QRect rect = data->view->rect();
423 
424  if (boundingRect.right() >= rect.right())
425  boundingRect.setRight(rect.right() - marginFromEdge);
426 
427  if (qAbs(boundingRect.top()) - 1 < 2)
428  boundingRect.setTop(marginFromEdge);
429 
430  if (boundingRect.bottom() >= rect.bottom())
431  boundingRect.setBottom(rect.bottom() - marginFromEdge);
432 
433  return boundingRect;
434 }
435 
436 } // namespace QmlJSDebugger
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
void reparentQmlObject(QObject *object, QObject *newParent)
qreal right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:527
void selectedColorChanged(const QColor &color)
QPointer< QWidget > widget
void setLeft(qreal pos)
Sets the left edge of the rectangle to the given x coordinate.
Definition: qrect.h:670
The QDeclarativeView class provides a widget for displaying a Qt Declarative user interface...
QDeclarativeViewInspector(QDeclarativeView *view, QObject *parent=0)
QScopedPointer< QDeclarativeViewInspectorPrivate > data
#define SLOT(a)
Definition: qobjectdefs.h:226
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
qreal left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:525
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void changeTool(InspectorProtocol::Tool tool)
void setTop(qreal pos)
Sets the top edge of the rectangle to the given y coordinate.
Definition: qrect.h:674
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
void setBottom(qreal pos)
Sets the bottom edge of the rectangle to the given y coordinate.
Definition: qrect.h:676
bool toBool() const
Returns the variant as a bool if the variant has type() Bool.
Definition: qvariant.cpp:2691
ItemSelectionMode
Definition: qnamespace.h:1503
int bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:249
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * qobject_cast(QObject *object)
Definition: qobject.h:375
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
T * data() const
void setRight(qreal pos)
Sets the right edge of the rectangle to the given x coordinate.
Definition: qrect.h:672
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
static bool isEditorItem(QGraphicsItem *item)
static const QRectF boundingRect(const QPointF *points, int pointCount)
QGraphicsObject * toGraphicsObject()
Return the graphics item cast to a QGraphicsObject, if the class is actually a graphics object...
void setParentItem(QDeclarativeItem *parent)
bool removeOne(const T &t)
Removes the first occurrence of value in the list and returns true on success; otherwise returns fals...
Definition: qlist.h:796
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void changeCurrentObjects(const QList< QObject *> &objects)
QList< QGraphicsItem * > filterForSelection(QList< QGraphicsItem *> &itemlist) const
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
The QDeclarativeItem class provides the most basic of all visual items in QML.
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
QVariant data(int key) const
Returns this item&#39;s custom data for the key key as a QVariant.
#define emit
Definition: qobjectdefs.h:76
virtual int type() const
Returns the type of an item as an int.
virtual bool mouseMoveEvent(QMouseEvent *event)
static const char * data(const QByteArray &arr)
QRectF adjustToScreenBoundaries(const QRectF &boundingRectInSceneSpace)
virtual bool eventFilter(QObject *, QEvent *)
Filters events if this object has been installed as an event filter for the watched object...
Definition: qobject.cpp:1375
void setToolTip(const QString &)
Definition: qwidget.cpp:11600
QList< QGraphicsItem * > selectableItems(const QPoint &pos) const
void setMouseTracking(bool enable)
Definition: qwidget.h:990
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
bool eventFilter(QObject *, QEvent *)
Filters events if this object has been installed as an event filter for the watched object...
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
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 setSelectedItems(QList< QGraphicsItem *> items)
void setSelectedItems(const QList< QGraphicsItem *> &items)
void setSelectedItemsForTools(const QList< QGraphicsItem *> &items)
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
The QDeclarativeEngine class provides an environment for instantiating QML components.
bool eventFilter(QObject *obj, QEvent *event)
Filters events if this object has been installed as an event filter for the watched object...
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void _q_onStatusChanged(QDeclarativeView::Status status)
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
T * data() const
Definition: qpointer.h:79
qreal top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:526
void highlight(const QList< QGraphicsObject *> &item)
qreal bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:528
static const int EditorItemDataKey
Status
Specifies the loading status of the QDeclarativeView.
The QGraphicsObject class provides a base class for all graphics items that require signals...
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
QGraphicsScene * scene() const
Returns a pointer to the scene that is currently visualized in the view.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62