OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / formeditor / selectiontool.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "selectiontool.h"
34 #include "formeditorscene.h"
35 #include "formeditorview.h"
36
37 #include "resizehandleitem.h"
38 #include "nodemetainfo.h"
39
40
41 #include <QApplication>
42 #include <QGraphicsSceneMouseEvent>
43 #include <QtDebug>
44 #include <QClipboard>
45
46 namespace QmlDesigner {
47
48 static const int s_startDragDistance = 20;
49 static const int s_startDragTime = 50;
50
51 SelectionTool::SelectionTool(FormEditorView *editorView)
52     : AbstractFormEditorTool(editorView),
53     m_rubberbandSelectionManipulator(editorView->scene()->manipulatorLayerItem(), editorView),
54     m_singleSelectionManipulator(editorView),
55     m_selectionIndicator(editorView->scene()->manipulatorLayerItem()),
56     m_resizeIndicator(editorView->scene()->manipulatorLayerItem()),
57     m_selectOnlyContentItems(false)
58 {
59 //    view()->setCursor(Qt::CrossCursor);
60 }
61
62
63 SelectionTool::~SelectionTool()
64 {
65 }
66
67 void SelectionTool::mousePressEvent(const QList<QGraphicsItem*> &itemList,
68                                     QGraphicsSceneMouseEvent *event)
69 {
70     m_mousePressTimer.start();
71     FormEditorItem* formEditorItem = topFormEditorItem(itemList);
72     if (formEditorItem
73         && formEditorItem->qmlItemNode().isValid()
74         && !formEditorItem->qmlItemNode().hasChildren()) {
75         m_singleSelectionManipulator.begin(event->scenePos());
76
77         if (event->modifiers().testFlag(Qt::ControlModifier))
78             m_singleSelectionManipulator.select(SingleSelectionManipulator::RemoveFromSelection, m_selectOnlyContentItems);
79         else if (event->modifiers().testFlag(Qt::ShiftModifier))
80             m_singleSelectionManipulator.select(SingleSelectionManipulator::AddToSelection, m_selectOnlyContentItems);
81         else
82             m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
83     } else {
84         if (event->modifiers().testFlag(Qt::AltModifier)) {
85             m_singleSelectionManipulator.begin(event->scenePos());
86
87             if (event->modifiers().testFlag(Qt::ControlModifier))
88                 m_singleSelectionManipulator.select(SingleSelectionManipulator::RemoveFromSelection, m_selectOnlyContentItems);
89             else if (event->modifiers().testFlag(Qt::ShiftModifier))
90                 m_singleSelectionManipulator.select(SingleSelectionManipulator::AddToSelection, m_selectOnlyContentItems);
91             else
92                 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
93
94             m_singleSelectionManipulator.end(event->scenePos());
95             view()->changeToMoveTool(event->scenePos());
96         } else {
97             m_rubberbandSelectionManipulator.begin(event->scenePos());
98         }
99     }
100 }
101
102 void SelectionTool::mouseMoveEvent(const QList<QGraphicsItem*> &/*itemList*/,
103                                    QGraphicsSceneMouseEvent *event)
104 {
105     if (m_singleSelectionManipulator.isActive()) {
106         QPointF mouseMovementVector = m_singleSelectionManipulator.beginPoint() - event->scenePos();
107         if ((mouseMovementVector.toPoint().manhattanLength() > s_startDragDistance)
108             && (m_mousePressTimer.elapsed() > s_startDragTime)) {
109             m_singleSelectionManipulator.end(event->scenePos());
110             view()->changeToMoveTool(m_singleSelectionManipulator.beginPoint());
111             return;
112         }
113     } else if (m_rubberbandSelectionManipulator.isActive()) {
114         QPointF mouseMovementVector = m_rubberbandSelectionManipulator.beginPoint() - event->scenePos();
115         if ((mouseMovementVector.toPoint().manhattanLength() > s_startDragDistance)
116             && (m_mousePressTimer.elapsed() > s_startDragTime)) {
117             m_rubberbandSelectionManipulator.update(event->scenePos());
118
119             if (event->modifiers().testFlag(Qt::ControlModifier))
120                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::RemoveFromSelection);
121             else if (event->modifiers().testFlag(Qt::ShiftModifier))
122                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::AddToSelection);
123             else
124                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::ReplaceSelection);
125         }
126     }
127 }
128
129 void SelectionTool::hoverMoveEvent(const QList<QGraphicsItem*> &itemList,
130                         QGraphicsSceneMouseEvent * /*event*/)
131 {
132     if (!itemList.isEmpty()) {
133
134         ResizeHandleItem* resizeHandle = ResizeHandleItem::fromGraphicsItem(itemList.first());
135         if (resizeHandle) {
136             view()->changeToResizeTool();
137             return;
138         }
139
140         if (topSelectedItemIsMovable(itemList))
141             view()->changeToMoveTool();
142     }
143
144     FormEditorItem *topSelectableItem = 0;
145
146     foreach(QGraphicsItem* item, itemList)
147     {
148         FormEditorItem *formEditorItem = FormEditorItem::fromQGraphicsItem(item);
149
150         if (formEditorItem
151             && formEditorItem->qmlItemNode().isValid()
152             && !formEditorItem->qmlItemNode().instanceIsInPositioner()
153             && formEditorItem->qmlItemNode().instanceIsMovable()
154             && (formEditorItem->qmlItemNode().hasShowContent() || !m_selectOnlyContentItems))
155         {
156             topSelectableItem = formEditorItem;
157             break;
158         }
159     }
160
161     scene()->highlightBoundingRect(topSelectableItem);
162 }
163
164 void SelectionTool::mouseReleaseEvent(const QList<QGraphicsItem*> &/*itemList*/,
165                                       QGraphicsSceneMouseEvent *event)
166 {
167     if (m_singleSelectionManipulator.isActive()) {
168         m_singleSelectionManipulator.end(event->scenePos());
169     }
170     else if (m_rubberbandSelectionManipulator.isActive()) {
171
172         QPointF mouseMovementVector = m_rubberbandSelectionManipulator.beginPoint() - event->scenePos();
173         if (mouseMovementVector.toPoint().manhattanLength() < s_startDragDistance) {
174             m_singleSelectionManipulator.begin(event->scenePos());
175
176             if (event->modifiers().testFlag(Qt::ControlModifier))
177                 m_singleSelectionManipulator.select(SingleSelectionManipulator::RemoveFromSelection, m_selectOnlyContentItems);
178             else if (event->modifiers().testFlag(Qt::ShiftModifier))
179                 m_singleSelectionManipulator.select(SingleSelectionManipulator::AddToSelection, m_selectOnlyContentItems);
180             else
181                 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
182
183             m_singleSelectionManipulator.end(event->scenePos());
184         } else {
185             m_rubberbandSelectionManipulator.update(event->scenePos());
186
187             if (event->modifiers().testFlag(Qt::ControlModifier))
188                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::RemoveFromSelection);
189             else if (event->modifiers().testFlag(Qt::ShiftModifier))
190                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::AddToSelection);
191             else
192                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::ReplaceSelection);
193
194             m_rubberbandSelectionManipulator.end();
195         }
196     }
197
198 }
199
200 void SelectionTool::mouseDoubleClickEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneMouseEvent * event)
201 {
202     AbstractFormEditorTool::mouseDoubleClickEvent(itemList, event);
203 }
204
205 void SelectionTool::keyPressEvent(QKeyEvent *event)
206 {
207     switch(event->key()) {
208         case Qt::Key_Left:
209         case Qt::Key_Right:
210         case Qt::Key_Up:
211         case Qt::Key_Down:
212             if (view()->changeToMoveTool())
213                 view()->currentTool()->keyPressEvent(event);
214             break;
215     }
216 }
217
218 void SelectionTool::keyReleaseEvent(QKeyEvent * /*keyEvent*/)
219 {
220
221 }
222
223 void SelectionTool::setSelectOnlyContentItems(bool selectOnlyContentItems)
224 {
225     m_selectOnlyContentItems = selectOnlyContentItems;
226 }
227
228 void SelectionTool::itemsAboutToRemoved(const QList<FormEditorItem*> &/*itemList*/)
229 {
230
231 }
232
233 //QVariant SelectionTool::itemChange(const QList<QGraphicsItem*> &itemList,
234 //                                           QGraphicsItem::GraphicsItemChange change,
235 //                                           const QVariant &value )
236 //{
237 //    qDebug() << Q_FUNC_INFO;
238 //    return QVariant();
239 //}
240
241 //void SelectionTool::update()
242 //{
243 //
244 //}
245
246
247 void SelectionTool::clear()
248 {
249     m_rubberbandSelectionManipulator.clear(),
250     m_singleSelectionManipulator.clear();
251     m_selectionIndicator.clear();
252     m_resizeIndicator.clear();
253 }
254
255 void SelectionTool::selectedItemsChanged(const QList<FormEditorItem*> &itemList)
256 {
257     m_selectionIndicator.setItems(itemList);
258     m_resizeIndicator.setItems(itemList);
259 }
260
261 void SelectionTool::formEditorItemsChanged(const QList<FormEditorItem*> &itemList)
262 {
263     m_selectionIndicator.updateItems(itemList);
264     m_resizeIndicator.updateItems(itemList);
265 }
266
267 void SelectionTool::instancesCompleted(const QList<FormEditorItem*> &/*itemList*/)
268 {
269 }
270
271 void SelectionTool::selectUnderPoint(QGraphicsSceneMouseEvent *event)
272 {
273     m_singleSelectionManipulator.begin(event->scenePos());
274
275     if (event->modifiers().testFlag(Qt::ControlModifier))
276         m_singleSelectionManipulator.select(SingleSelectionManipulator::RemoveFromSelection, m_selectOnlyContentItems);
277     else if (event->modifiers().testFlag(Qt::ShiftModifier))
278         m_singleSelectionManipulator.select(SingleSelectionManipulator::AddToSelection, m_selectOnlyContentItems);
279     else
280         m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
281
282     m_singleSelectionManipulator.end(event->scenePos());
283 }
284
285 }