OSDN Git Service

04a97f9ee1b9f0d46cb0c519b0dd1c8ac6d3aa9f
[qt-creator-jp/qt-creator-jp.git] / share / qtcreator / qml / qmljsdebugger / editor / liverubberbandselectionmanipulator.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 "liverubberbandselectionmanipulator.h"
35 #include "../qdeclarativeviewobserver_p.h"
36
37 #include <QtGui/QGraphicsItem>
38
39 #include <QtCore/QDebug>
40
41 namespace QmlJSDebugger {
42
43 LiveRubberBandSelectionManipulator::LiveRubberBandSelectionManipulator(QGraphicsObject *layerItem,
44                                                                        QDeclarativeViewObserver *editorView)
45     : m_selectionRectangleElement(layerItem),
46       m_editorView(editorView),
47       m_beginFormEditorItem(0),
48       m_isActive(false)
49 {
50     m_selectionRectangleElement.hide();
51 }
52
53 void LiveRubberBandSelectionManipulator::clear()
54 {
55     m_selectionRectangleElement.clear();
56     m_isActive = false;
57     m_beginPoint = QPointF();
58     m_itemList.clear();
59     m_oldSelectionList.clear();
60 }
61
62 QGraphicsItem *LiveRubberBandSelectionManipulator::topFormEditorItem(const QList<QGraphicsItem*>
63                                                                      &itemList)
64 {
65     if (itemList.isEmpty())
66         return 0;
67
68     return itemList.first();
69 }
70
71 void LiveRubberBandSelectionManipulator::begin(const QPointF &beginPoint)
72 {
73     m_beginPoint = beginPoint;
74     m_selectionRectangleElement.setRect(m_beginPoint, m_beginPoint);
75     m_selectionRectangleElement.show();
76     m_isActive = true;
77     QDeclarativeViewObserverPrivate *observerPrivate
78             = QDeclarativeViewObserverPrivate::get(m_editorView);
79     m_beginFormEditorItem = topFormEditorItem(observerPrivate->selectableItems(beginPoint));
80     m_oldSelectionList = m_editorView->selectedItems();
81 }
82
83 void LiveRubberBandSelectionManipulator::update(const QPointF &updatePoint)
84 {
85     m_selectionRectangleElement.setRect(m_beginPoint, updatePoint);
86 }
87
88 void LiveRubberBandSelectionManipulator::end()
89 {
90     m_oldSelectionList.clear();
91     m_selectionRectangleElement.hide();
92     m_isActive = false;
93 }
94
95 void LiveRubberBandSelectionManipulator::select(SelectionType selectionType)
96 {
97     QDeclarativeViewObserverPrivate *observerPrivate
98             = QDeclarativeViewObserverPrivate::get(m_editorView);
99     QList<QGraphicsItem*> itemList
100             = observerPrivate->selectableItems(m_selectionRectangleElement.rect(),
101                                                Qt::IntersectsItemShape);
102     QList<QGraphicsItem*> newSelectionList;
103
104     foreach (QGraphicsItem* item, itemList) {
105         if (item
106                 && item->parentItem()
107                 && !newSelectionList.contains(item)
108                 //&& m_beginFormEditorItem->childItems().contains(item) // TODO activate this test
109                 )
110         {
111             newSelectionList.append(item);
112         }
113     }
114
115     if (newSelectionList.isEmpty() && m_beginFormEditorItem)
116         newSelectionList.append(m_beginFormEditorItem);
117
118     QList<QGraphicsItem*> resultList;
119
120     switch(selectionType) {
121     case AddToSelection: {
122         resultList.append(m_oldSelectionList);
123         resultList.append(newSelectionList);
124     }
125         break;
126     case ReplaceSelection: {
127         resultList.append(newSelectionList);
128     }
129         break;
130     case RemoveFromSelection: {
131         QSet<QGraphicsItem*> oldSelectionSet(m_oldSelectionList.toSet());
132         QSet<QGraphicsItem*> newSelectionSet(newSelectionList.toSet());
133         resultList.append(oldSelectionSet.subtract(newSelectionSet).toList());
134     }
135     }
136
137     m_editorView->setSelectedItems(resultList);
138 }
139
140
141 void LiveRubberBandSelectionManipulator::setItems(const QList<QGraphicsItem*> &itemList)
142 {
143     m_itemList = itemList;
144 }
145
146 QPointF LiveRubberBandSelectionManipulator::beginPoint() const
147 {
148     return m_beginPoint;
149 }
150
151 bool LiveRubberBandSelectionManipulator::isActive() const
152 {
153     return m_isActive;
154 }
155
156 } // namespace QmlJSDebugger