Imported Upstream version 1.0.1
[gammaray-debian.git] / qmldebugcontrol / qmljsdebugger / editor / subcomponentmasklayeritem.cpp
blob12197f15ba0f49c2b1d5967bb6cb45fb66002738
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 "subcomponentmasklayeritem.h"
34 #include "qmlinspectorconstants.h"
35 #include "qdeclarativeviewinspector.h"
36 #include <QtGui/QPolygonF>
38 namespace QmlJSDebugger {
40 SubcomponentMaskLayerItem::SubcomponentMaskLayerItem(QDeclarativeViewInspector *inspector,
41 QGraphicsItem *parentItem) :
42 QGraphicsPolygonItem(parentItem),
43 m_inspector(inspector),
44 m_currentItem(0),
45 m_borderRect(new QGraphicsRectItem(this))
47 m_borderRect->setRect(0,0,0,0);
48 m_borderRect->setPen(QPen(QColor(60, 60, 60), 1));
49 m_borderRect->setData(Constants::EditorItemDataKey, QVariant(true));
51 setBrush(QBrush(QColor(160,160,160)));
52 setPen(Qt::NoPen);
55 int SubcomponentMaskLayerItem::type() const
57 return Constants::EditorItemType;
60 static QRectF resizeRect(const QRectF &newRect, const QRectF &oldRect)
62 QRectF result = newRect;
63 if (oldRect.left() < newRect.left())
64 result.setLeft(oldRect.left());
66 if (oldRect.top() < newRect.top())
67 result.setTop(oldRect.top());
69 if (oldRect.right() > newRect.right())
70 result.setRight(oldRect.right());
72 if (oldRect.bottom() > newRect.bottom())
73 result.setBottom(oldRect.bottom());
75 return result;
78 static QPolygonF regionToPolygon(const QRegion &region)
80 QPainterPath path;
81 foreach (const QRect &rect, region.rects())
82 path.addRect(rect);
83 return path.toFillPolygon();
86 void SubcomponentMaskLayerItem::setCurrentItem(QGraphicsItem *item)
88 QGraphicsItem *prevItem = m_currentItem;
89 m_currentItem = item;
91 if (!m_currentItem)
92 return;
94 QRect viewRect = m_inspector->declarativeView()->rect();
95 viewRect = m_inspector->declarativeView()->mapToScene(viewRect).boundingRect().toRect();
97 QRectF itemRect = item->boundingRect() | item->childrenBoundingRect();
98 itemRect = item->mapRectToScene(itemRect);
100 // if updating the same item as before, resize the rectangle only bigger, not smaller.
101 if (prevItem == item && prevItem != 0) {
102 m_itemPolyRect = resizeRect(itemRect, m_itemPolyRect);
103 } else {
104 m_itemPolyRect = itemRect;
106 QRectF borderRect = m_itemPolyRect;
107 borderRect.adjust(-1, -1, 1, 1);
108 m_borderRect->setRect(borderRect);
110 const QRegion externalRegion = QRegion(viewRect).subtracted(m_itemPolyRect.toRect());
111 setPolygon(regionToPolygon(externalRegion));
114 QGraphicsItem *SubcomponentMaskLayerItem::currentItem() const
116 return m_currentItem;
119 } // namespace QmlJSDebugger