OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / share / qtcreator / qml / qmljsdebugger / editor / subcomponentmasklayeritem.cpp
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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "subcomponentmasklayeritem.h"
35 #include "qmlobserverconstants.h"
36 #include "qdeclarativeviewobserver.h"
37 #include <QPolygonF>
38
39 namespace QmlJSDebugger {
40
41 SubcomponentMaskLayerItem::SubcomponentMaskLayerItem(QDeclarativeViewObserver *observer,
42                                                      QGraphicsItem *parentItem) :
43     QGraphicsPolygonItem(parentItem),
44     m_observer(observer),
45     m_currentItem(0),
46     m_borderRect(new QGraphicsRectItem(this))
47 {
48     m_borderRect->setRect(0,0,0,0);
49     m_borderRect->setPen(QPen(QColor(60, 60, 60), 1));
50     m_borderRect->setData(Constants::EditorItemDataKey, QVariant(true));
51
52     setBrush(QBrush(QColor(160,160,160)));
53     setPen(Qt::NoPen);
54 }
55
56 int SubcomponentMaskLayerItem::type() const
57 {
58     return Constants::EditorItemType;
59 }
60
61 static QRectF resizeRect(const QRectF &newRect, const QRectF &oldRect)
62 {
63     QRectF result = newRect;
64     if (oldRect.left() < newRect.left())
65         result.setLeft(oldRect.left());
66
67     if (oldRect.top() < newRect.top())
68         result.setTop(oldRect.top());
69
70     if (oldRect.right() > newRect.right())
71         result.setRight(oldRect.right());
72
73     if (oldRect.bottom() > newRect.bottom())
74         result.setBottom(oldRect.bottom());
75
76     return result;
77 }
78
79
80 void SubcomponentMaskLayerItem::setCurrentItem(QGraphicsItem *item)
81 {
82     QGraphicsItem *prevItem = m_currentItem;
83     m_currentItem = item;
84
85     if (!m_currentItem)
86         return;
87
88     QPolygonF viewPoly(QRectF(m_observer->declarativeView()->rect()));
89     viewPoly = m_observer->declarativeView()->mapToScene(viewPoly.toPolygon());
90
91     QRectF itemRect = item->boundingRect() | item->childrenBoundingRect();
92     QPolygonF itemPoly(itemRect);
93     itemPoly = item->mapToScene(itemPoly);
94
95     // if updating the same item as before, resize the rectangle only bigger, not smaller.
96     if (prevItem == item && prevItem != 0) {
97         m_itemPolyRect = resizeRect(itemPoly.boundingRect(), m_itemPolyRect);
98     } else {
99         m_itemPolyRect = itemPoly.boundingRect();
100     }
101     QRectF borderRect = m_itemPolyRect;
102     borderRect.adjust(-1, -1, 1, 1);
103     m_borderRect->setRect(borderRect);
104
105     itemPoly = viewPoly.subtracted(QPolygonF(m_itemPolyRect));
106     setPolygon(itemPoly);
107 }
108
109 QGraphicsItem *SubcomponentMaskLayerItem::currentItem() const
110 {
111     return m_currentItem;
112 }
113
114 } // namespace QmlJSDebugger