Qt 4.8
subcomponentmasklayeritem.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 "../qmlinspectorconstants.h"
45 #include "../qdeclarativeviewinspector.h"
46 
47 #include <QtGui/QPolygonF>
48 
49 namespace QmlJSDebugger {
50 
52  QGraphicsItem *parentItem) :
53  QGraphicsPolygonItem(parentItem),
54  m_inspector(inspector),
55  m_currentItem(0),
56  m_borderRect(new QGraphicsRectItem(this))
57 {
58  m_borderRect->setRect(0,0,0,0);
59  m_borderRect->setPen(QPen(QColor(60, 60, 60), 1));
61 
62  setBrush(QBrush(QColor(160,160,160)));
64 }
65 
67 {
69 }
70 
71 static QRectF resizeRect(const QRectF &newRect, const QRectF &oldRect)
72 {
73  QRectF result = newRect;
74  if (oldRect.left() < newRect.left())
75  result.setLeft(oldRect.left());
76 
77  if (oldRect.top() < newRect.top())
78  result.setTop(oldRect.top());
79 
80  if (oldRect.right() > newRect.right())
81  result.setRight(oldRect.right());
82 
83  if (oldRect.bottom() > newRect.bottom())
84  result.setBottom(oldRect.bottom());
85 
86  return result;
87 }
88 
89 static QPolygonF regionToPolygon(const QRegion &region)
90 {
91  QPainterPath path;
92  foreach (const QRect &rect, region.rects())
93  path.addRect(rect);
94  return path.toFillPolygon();
95 }
96 
98 {
99  QGraphicsItem *prevItem = m_currentItem;
100  m_currentItem = item;
101 
102  if (!m_currentItem)
103  return;
104 
105  QRect viewRect = m_inspector->declarativeView()->rect();
106  viewRect = m_inspector->declarativeView()->mapToScene(viewRect).boundingRect().toRect();
107 
108  QRectF itemRect = item->boundingRect() | item->childrenBoundingRect();
109  itemRect = item->mapRectToScene(itemRect);
110 
111  // if updating the same item as before, resize the rectangle only bigger, not smaller.
112  if (prevItem == item && prevItem != 0) {
114  } else {
116  }
117  QRectF borderRect = m_itemPolyRect;
118  borderRect.adjust(-1, -1, 1, 1);
119  m_borderRect->setRect(borderRect);
120 
121  const QRegion externalRegion = QRegion(viewRect).subtracted(m_itemPolyRect.toRect());
122  setPolygon(regionToPolygon(externalRegion));
123 }
124 
126 {
127  return m_currentItem;
128 }
129 
130 } // namespace QmlJSDebugger
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
qreal right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:527
QPointF mapToScene(const QPoint &point) const
Returns the viewport coordinate point mapped to scene coordinates.
The QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
QRectF childrenBoundingRect() const
Returns the bounding rect of this item&#39;s descendants (i.e., its children, their children, etc.) in local coordinates.
void setLeft(qreal pos)
Sets the left edge of the rectangle to the given x coordinate.
Definition: qrect.h:670
SubcomponentMaskLayerItem(QDeclarativeViewInspector *inspector, QGraphicsItem *parentItem=0)
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
static QPolygonF regionToPolygon(const QRegion &region)
virtual QRectF boundingRect() const =0
This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be ...
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
void setTop(qreal pos)
Sets the top edge of the rectangle to the given y coordinate.
Definition: qrect.h:674
void setBottom(qreal pos)
Sets the bottom edge of the rectangle to the given y coordinate.
Definition: qrect.h:676
QRectF mapRectToScene(const QRectF &rect) const
Maps the rectangle rect, which is in this item&#39;s coordinate system, to the scene coordinate system...
The QGraphicsPolygonItem class provides a polygon item that you can add to a QGraphicsScene.
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:64
void setRight(qreal pos)
Sets the right edge of the rectangle to the given x coordinate.
Definition: qrect.h:672
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
QPolygonF toFillPolygon(const QMatrix &matrix=QMatrix()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
The QPolygonF class provides a vector of points using floating point precision.
Definition: qpolygon.h:134
void setData(int key, const QVariant &value)
Sets this item&#39;s custom data for the key key to value.
void setPen(const QPen &pen)
Sets the pen for this item to pen.
void addRect(const QRectF &rect)
Adds the given rectangle to this path as a closed subpath.
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
QRect toRect() const
Returns a QRect based on the values of this rectangle.
Definition: qrect.h:845
QRect rect() const
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
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.
QVector< QRect > rects() const
Returns an array of non-overlapping rectangles that make up the region.
Definition: qregion.cpp:4412
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void setRect(const QRectF &rect)
Sets the item&#39;s rectangle to be the given rectangle.
qreal top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:526
qreal bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:528
static const int EditorItemDataKey
QRegion subtracted(const QRegion &r) const
Returns a region which is r subtracted from this region.
Definition: qregion.h:114
static QRectF resizeRect(const QRectF &newRect, const QRectF &oldRect)
void setBrush(const QBrush &brush)
Sets the item&#39;s brush to brush.