OSDN Git Service

8125ae21e2b6f6e513661f8119c8c24d0fe9a408
[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 (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 "selectiontool.h"
35 #include "formeditorscene.h"
36 #include "formeditorview.h"
37
38 #include "resizehandleitem.h"
39 #include "nodemetainfo.h"
40
41
42 #include <QApplication>
43 #include <QGraphicsSceneMouseEvent>
44 #include <QtDebug>
45 #include <QClipboard>
46
47 namespace QmlDesigner {
48
49 static const int s_startDragDistance = 20;
50 static const int s_startDragTime = 50;
51
52 SelectionTool::SelectionTool(FormEditorView *editorView)
53     : AbstractFormEditorTool(editorView),
54     m_rubberbandSelectionManipulator(editorView->scene()->manipulatorLayerItem(), editorView),
55     m_singleSelectionManipulator(editorView),
56     m_selectionIndicator(editorView->scene()->manipulatorLayerItem()),
57     m_resizeIndicator(editorView->scene()->manipulatorLayerItem()),
58     m_selectOnlyContentItems(false)
59 {
60 //    view()->setCursor(Qt::CrossCursor);
61 }
62
63
64 SelectionTool::~SelectionTool()
65 {
66 }
67
68 void SelectionTool::mousePressEvent(const QList<QGraphicsItem*> &itemList,
69                                     QGraphicsSceneMouseEvent *event)
70 {
71     m_mousePressTimer.start();
72     FormEditorItem* formEditorItem = topFormEditorItem(itemList);
73     if (formEditorItem
74         && formEditorItem->qmlItemNode().isValid()
75         && !formEditorItem->qmlItemNode().hasChildren()) {
76         m_singleSelectionManipulator.begin(event->scenePos());
77
78         if (event->modifiers().testFlag(Qt::ControlModifier))
79             m_singleSelectionManipulator.select(SingleSelectionManipulator::RemoveFromSelection, m_selectOnlyContentItems);
80         else if (event->modifiers().testFlag(Qt::ShiftModifier))
81             m_singleSelectionManipulator.select(SingleSelectionManipulator::AddToSelection, m_selectOnlyContentItems);
82         else
83             m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
84     } else {
85         if (event->modifiers().testFlag(Qt::AltModifier)) {
86             m_singleSelectionManipulator.begin(event->scenePos());
87
88             if (event->modifiers().testFlag(Qt::ControlModifier))
89                 m_singleSelectionManipulator.select(SingleSelectionManipulator::RemoveFromSelection, m_selectOnlyContentItems);
90             else if (event->modifiers().testFlag(Qt::ShiftModifier))
91                 m_singleSelectionManipulator.select(SingleSelectionManipulator::AddToSelection, m_selectOnlyContentItems);
92             else
93                 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
94
95             m_singleSelectionManipulator.end(event->scenePos());
96             view()->changeToMoveTool(event->scenePos());
97         } else {
98             m_rubberbandSelectionManipulator.begin(event->scenePos());
99         }
100     }
101 }
102
103 void SelectionTool::mouseMoveEvent(const QList<QGraphicsItem*> &/*itemList*/,
104                                    QGraphicsSceneMouseEvent *event)
105 {
106     if (m_singleSelectionManipulator.isActive()) {
107         QPointF mouseMovementVector = m_singleSelectionManipulator.beginPoint() - event->scenePos();
108         if ((mouseMovementVector.toPoint().manhattanLength() > s_startDragDistance)
109             && (m_mousePressTimer.elapsed() > s_startDragTime)) {
110             m_singleSelectionManipulator.end(event->scenePos());
111             view()->changeToMoveTool(m_singleSelectionManipulator.beginPoint());
112             return;
113         }
114     } else if (m_rubberbandSelectionManipulator.isActive()) {
115         QPointF mouseMovementVector = m_rubberbandSelectionManipulator.beginPoint() - event->scenePos();
116         if ((mouseMovementVector.toPoint().manhattanLength() > s_startDragDistance)
117             && (m_mousePressTimer.elapsed() > s_startDragTime)) {
118             m_rubberbandSelectionManipulator.update(event->scenePos());
119
120             if (event->modifiers().testFlag(Qt::ControlModifier))
121                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::RemoveFromSelection);
122             else if (event->modifiers().testFlag(Qt::ShiftModifier))
123                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::AddToSelection);
124             else
125                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::ReplaceSelection);
126         }
127     }
128 }
129
130 void SelectionTool::hoverMoveEvent(const QList<QGraphicsItem*> &itemList,
131                         QGraphicsSceneMouseEvent * /*event*/)
132 {
133     if (!itemList.isEmpty()) {
134
135         ResizeHandleItem* resizeHandle = ResizeHandleItem::fromGraphicsItem(itemList.first());
136         if (resizeHandle) {
137             view()->changeToResizeTool();
138             return;
139         }
140
141         if (topSelectedItemIsMovable(itemList))
142             view()->changeToMoveTool();
143     }
144
145     FormEditorItem *topSelectableItem = 0;
146
147     foreach(QGraphicsItem* item, itemList)
148     {
149         FormEditorItem *formEditorItem = FormEditorItem::fromQGraphicsItem(item);
150
151         if (formEditorItem
152             && formEditorItem->qmlItemNode().isValid()
153             && !formEditorItem->qmlItemNode().instanceIsInPositioner()
154             && formEditorItem->qmlItemNode().instanceIsMovable()
155             && (formEditorItem->qmlItemNode().hasShowContent() || !m_selectOnlyContentItems))
156         {
157             topSelectableItem = formEditorItem;
158             break;
159         }
160     }
161
162     scene()->highlightBoundingRect(topSelectableItem);
163 }
164
165 void SelectionTool::mouseReleaseEvent(const QList<QGraphicsItem*> &/*itemList*/,
166                                       QGraphicsSceneMouseEvent *event)
167 {
168     if (m_singleSelectionManipulator.isActive()) {
169         m_singleSelectionManipulator.end(event->scenePos());
170     }
171     else if (m_rubberbandSelectionManipulator.isActive()) {
172
173         QPointF mouseMovementVector = m_rubberbandSelectionManipulator.beginPoint() - event->scenePos();
174         if (mouseMovementVector.toPoint().manhattanLength() < s_startDragDistance) {
175             m_singleSelectionManipulator.begin(event->scenePos());
176
177             if (event->modifiers().testFlag(Qt::ControlModifier))
178                 m_singleSelectionManipulator.select(SingleSelectionManipulator::RemoveFromSelection, m_selectOnlyContentItems);
179             else if (event->modifiers().testFlag(Qt::ShiftModifier))
180                 m_singleSelectionManipulator.select(SingleSelectionManipulator::AddToSelection, m_selectOnlyContentItems);
181             else
182                 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
183
184             m_singleSelectionManipulator.end(event->scenePos());
185         } else {
186             m_rubberbandSelectionManipulator.update(event->scenePos());
187
188             if (event->modifiers().testFlag(Qt::ControlModifier))
189                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::RemoveFromSelection);
190             else if (event->modifiers().testFlag(Qt::ShiftModifier))
191                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::AddToSelection);
192             else
193                 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::ReplaceSelection);
194
195             m_rubberbandSelectionManipulator.end();
196         }
197     }
198
199 }
200
201 void SelectionTool::mouseDoubleClickEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneMouseEvent * event)
202 {
203     AbstractFormEditorTool::mouseDoubleClickEvent(itemList, event);
204 }
205
206 void SelectionTool::keyPressEvent(QKeyEvent *event)
207 {
208     switch(event->key()) {
209         case Qt::Key_Left:
210         case Qt::Key_Right:
211         case Qt::Key_Up:
212         case Qt::Key_Down:
213             if (view()->changeToMoveTool())
214                 view()->currentTool()->keyPressEvent(event);
215             break;
216     }
217 }
218
219 void SelectionTool::keyReleaseEvent(QKeyEvent * /*keyEvent*/)
220 {
221
222 }
223
224 void SelectionTool::setSelectOnlyContentItems(bool selectOnlyContentItems)
225 {
226     m_selectOnlyContentItems = selectOnlyContentItems;
227 }
228
229 void SelectionTool::itemsAboutToRemoved(const QList<FormEditorItem*> &/*itemList*/)
230 {
231
232 }
233
234 //QVariant SelectionTool::itemChange(const QList<QGraphicsItem*> &itemList,
235 //                                           QGraphicsItem::GraphicsItemChange change,
236 //                                           const QVariant &value )
237 //{
238 //    qDebug() << Q_FUNC_INFO;
239 //    return QVariant();
240 //}
241
242 //void SelectionTool::update()
243 //{
244 //
245 //}
246
247
248 void SelectionTool::clear()
249 {
250     m_rubberbandSelectionManipulator.clear(),
251     m_singleSelectionManipulator.clear();
252     m_selectionIndicator.clear();
253     m_resizeIndicator.clear();
254 }
255
256 void SelectionTool::selectedItemsChanged(const QList<FormEditorItem*> &itemList)
257 {
258     m_selectionIndicator.setItems(itemList);
259     m_resizeIndicator.setItems(itemList);
260 }
261
262 void SelectionTool::formEditorItemsChanged(const QList<FormEditorItem*> &itemList)
263 {
264     m_selectionIndicator.updateItems(itemList);
265     m_resizeIndicator.updateItems(itemList);
266 }
267
268 void SelectionTool::instancesCompleted(const QList<FormEditorItem*> &/*itemList*/)
269 {
270 }
271
272 void SelectionTool::selectUnderPoint(QGraphicsSceneMouseEvent *event)
273 {
274     m_singleSelectionManipulator.begin(event->scenePos());
275
276     if (event->modifiers().testFlag(Qt::ControlModifier))
277         m_singleSelectionManipulator.select(SingleSelectionManipulator::RemoveFromSelection, m_selectOnlyContentItems);
278     else if (event->modifiers().testFlag(Qt::ShiftModifier))
279         m_singleSelectionManipulator.select(SingleSelectionManipulator::AddToSelection, m_selectOnlyContentItems);
280     else
281         m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
282
283     m_singleSelectionManipulator.end(event->scenePos());
284 }
285
286 }