eb4db78f3192b31cecb7c4e8e4d93fe1f3345840
[profile/ivi/qtdeclarative.git] / src / plugins / qmltooling / qmldbg_qtquick1 / liverubberbandselectionmanipulator.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "liverubberbandselectionmanipulator.h"
43
44 #include "qdeclarativeviewinspector_p.h"
45
46 #include <QtWidgets/QGraphicsItem>
47
48 #include <QtCore/QDebug>
49
50 namespace QmlJSDebugger {
51 namespace QtQuick1 {
52
53 LiveRubberBandSelectionManipulator::LiveRubberBandSelectionManipulator(QGraphicsObject *layerItem,
54                                                                        QDeclarativeViewInspector *editorView)
55     : m_selectionRectangleElement(layerItem),
56       m_editorView(editorView),
57       m_beginFormEditorItem(0),
58       m_isActive(false)
59 {
60     m_selectionRectangleElement.hide();
61 }
62
63 void LiveRubberBandSelectionManipulator::clear()
64 {
65     m_selectionRectangleElement.clear();
66     m_isActive = false;
67     m_beginPoint = QPointF();
68     m_itemList.clear();
69     m_oldSelectionList.clear();
70 }
71
72 QGraphicsItem *LiveRubberBandSelectionManipulator::topFormEditorItem(const QList<QGraphicsItem*>
73                                                                      &itemList)
74 {
75     if (itemList.isEmpty())
76         return 0;
77
78     return itemList.first();
79 }
80
81 void LiveRubberBandSelectionManipulator::begin(const QPointF &beginPoint)
82 {
83     m_beginPoint = beginPoint;
84     m_selectionRectangleElement.setRect(m_beginPoint, m_beginPoint);
85     m_selectionRectangleElement.show();
86     m_isActive = true;
87     QDeclarativeViewInspectorPrivate *inspectorPrivate
88             = QDeclarativeViewInspectorPrivate::get(m_editorView);
89     m_beginFormEditorItem = topFormEditorItem(inspectorPrivate->selectableItems(beginPoint));
90     m_oldSelectionList = m_editorView->selectedItems();
91 }
92
93 void LiveRubberBandSelectionManipulator::update(const QPointF &updatePoint)
94 {
95     m_selectionRectangleElement.setRect(m_beginPoint, updatePoint);
96 }
97
98 void LiveRubberBandSelectionManipulator::end()
99 {
100     m_oldSelectionList.clear();
101     m_selectionRectangleElement.hide();
102     m_isActive = false;
103 }
104
105 void LiveRubberBandSelectionManipulator::select(SelectionType selectionType)
106 {
107     QDeclarativeViewInspectorPrivate *inspectorPrivate
108             = QDeclarativeViewInspectorPrivate::get(m_editorView);
109     QList<QGraphicsItem*> itemList
110             = inspectorPrivate->selectableItems(m_selectionRectangleElement.rect(),
111                                                Qt::IntersectsItemShape);
112     QList<QGraphicsItem*> newSelectionList;
113
114     foreach (QGraphicsItem* item, itemList) {
115         if (item
116                 && item->parentItem()
117                 && !newSelectionList.contains(item)
118                 //&& m_beginFormEditorItem->childItems().contains(item) // TODO activate this test
119                 )
120         {
121             newSelectionList.append(item);
122         }
123     }
124
125     if (newSelectionList.isEmpty() && m_beginFormEditorItem)
126         newSelectionList.append(m_beginFormEditorItem);
127
128     QList<QGraphicsItem*> resultList;
129
130     switch (selectionType) {
131     case AddToSelection: {
132         resultList.append(m_oldSelectionList);
133         resultList.append(newSelectionList);
134     }
135         break;
136     case ReplaceSelection: {
137         resultList.append(newSelectionList);
138     }
139         break;
140     case RemoveFromSelection: {
141         QSet<QGraphicsItem*> oldSelectionSet(m_oldSelectionList.toSet());
142         QSet<QGraphicsItem*> newSelectionSet(newSelectionList.toSet());
143         resultList.append(oldSelectionSet.subtract(newSelectionSet).toList());
144     }
145     }
146
147     m_editorView->setSelectedItems(resultList);
148 }
149
150
151 void LiveRubberBandSelectionManipulator::setItems(const QList<QGraphicsItem*> &itemList)
152 {
153     m_itemList = itemList;
154 }
155
156 QPointF LiveRubberBandSelectionManipulator::beginPoint() const
157 {
158     return m_beginPoint;
159 }
160
161 bool LiveRubberBandSelectionManipulator::isActive() const
162 {
163     return m_isActive;
164 }
165
166 } // namespace QtQuick1
167 } // namespace QmlJSDebugger