OSDN Git Service

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