OSDN Git Service

d0ceb3d94d5e5908c76cf742f0dc4f90763575ed
[qt-creator-jp/qt-creator-jp.git] / share / qtcreator / qml / qmljsdebugger / editor / liveselectionindicator.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 "liveselectionindicator.h"
35 #include "../qdeclarativeviewobserver_p.h"
36 #include "qmlobserverconstants.h"
37
38 #include <QtCore/QDebug>
39
40 #include <QtGui/QGraphicsPolygonItem>
41 #include <QtGui/QGraphicsObject>
42 #include <QtGui/QGraphicsScene>
43 #include <QtGui/QPen>
44
45 #include <cmath>
46
47 namespace QmlJSDebugger {
48
49 LiveSelectionIndicator::LiveSelectionIndicator(QDeclarativeViewObserver *editorView,
50                                        QGraphicsObject *layerItem)
51     : m_layerItem(layerItem), m_view(editorView)
52 {
53 }
54
55 LiveSelectionIndicator::~LiveSelectionIndicator()
56 {
57     clear();
58 }
59
60 void LiveSelectionIndicator::show()
61 {
62     foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
63         item->show();
64 }
65
66 void LiveSelectionIndicator::hide()
67 {
68     foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
69         item->hide();
70 }
71
72 void LiveSelectionIndicator::clear()
73 {
74     if (!m_layerItem.isNull()) {
75         QHashIterator<QGraphicsItem*, QGraphicsPolygonItem *> iter(m_indicatorShapeHash);
76         while(iter.hasNext()) {
77             iter.next();
78             m_layerItem.data()->scene()->removeItem(iter.value());
79             delete iter.value();
80         }
81     }
82
83     m_indicatorShapeHash.clear();
84
85 }
86
87 QPolygonF LiveSelectionIndicator::addBoundingRectToPolygon(QGraphicsItem *item, QPolygonF &polygon)
88 {
89     // ### remove this if statement when QTBUG-12172 gets fixed
90     if (item->boundingRect() != QRectF(0,0,0,0)) {
91         QPolygonF bounding = item->mapToScene(item->boundingRect());
92         if (bounding.isClosed()) //avoid crashes if there is an infinite scale.
93             polygon = polygon.united(bounding);
94     }
95
96     foreach (QGraphicsItem *child, item->childItems()) {
97         if (!QDeclarativeViewObserverPrivate::get(m_view)->isEditorItem(child))
98             addBoundingRectToPolygon(child, polygon);
99     }
100     return polygon;
101 }
102
103 void LiveSelectionIndicator::setItems(const QList<QWeakPointer<QGraphicsObject> > &itemList)
104 {
105     clear();
106
107     // set selections to also all children if they are not editor items
108
109     foreach (QWeakPointer<QGraphicsObject> object, itemList) {
110         if (object.isNull())
111             continue;
112
113         QGraphicsItem *item = object.data();
114
115         QGraphicsPolygonItem *newSelectionIndicatorGraphicsItem
116                 = new QGraphicsPolygonItem(m_layerItem.data());
117         if (!m_indicatorShapeHash.contains(item)) {
118             m_indicatorShapeHash.insert(item, newSelectionIndicatorGraphicsItem);
119
120             QPolygonF boundingShapeInSceneSpace;
121             addBoundingRectToPolygon(item, boundingShapeInSceneSpace);
122
123             QRectF boundingRect
124                     = m_view->adjustToScreenBoundaries(boundingShapeInSceneSpace.boundingRect());
125             QPolygonF boundingRectInLayerItemSpace = m_layerItem.data()->mapFromScene(boundingRect);
126
127             QPen pen;
128             pen.setColor(QColor(108, 141, 221));
129             newSelectionIndicatorGraphicsItem->setData(Constants::EditorItemDataKey,
130                                                        QVariant(true));
131             newSelectionIndicatorGraphicsItem->setFlag(QGraphicsItem::ItemIsSelectable, false);
132             newSelectionIndicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpace);
133             newSelectionIndicatorGraphicsItem->setPen(pen);
134         }
135     }
136 }
137
138 } //namespace QmlJSDebugger
139