1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "selectiontool.h"
34 #include "formeditorscene.h"
35 #include "formeditorview.h"
37 #include "resizehandleitem.h"
38 #include "nodemetainfo.h"
41 #include <QApplication>
42 #include <QGraphicsSceneMouseEvent>
46 namespace QmlDesigner {
48 static const int s_startDragDistance = 20;
49 static const int s_startDragTime = 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)
59 // view()->setCursor(Qt::CrossCursor);
63 SelectionTool::~SelectionTool()
67 void SelectionTool::mousePressEvent(const QList<QGraphicsItem*> &itemList,
68 QGraphicsSceneMouseEvent *event)
70 m_mousePressTimer.start();
71 FormEditorItem* formEditorItem = topFormEditorItem(itemList);
73 && formEditorItem->qmlItemNode().isValid()
74 && !formEditorItem->qmlItemNode().hasChildren()) {
75 m_singleSelectionManipulator.begin(event->scenePos());
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);
82 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
84 if (event->modifiers().testFlag(Qt::AltModifier)) {
85 m_singleSelectionManipulator.begin(event->scenePos());
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);
92 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
94 m_singleSelectionManipulator.end(event->scenePos());
95 view()->changeToMoveTool(event->scenePos());
97 m_rubberbandSelectionManipulator.begin(event->scenePos());
102 void SelectionTool::mouseMoveEvent(const QList<QGraphicsItem*> &/*itemList*/,
103 QGraphicsSceneMouseEvent *event)
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());
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());
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);
124 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::ReplaceSelection);
129 void SelectionTool::hoverMoveEvent(const QList<QGraphicsItem*> &itemList,
130 QGraphicsSceneMouseEvent * /*event*/)
132 if (!itemList.isEmpty()) {
134 ResizeHandleItem* resizeHandle = ResizeHandleItem::fromGraphicsItem(itemList.first());
136 view()->changeToResizeTool();
140 if (topSelectedItemIsMovable(itemList))
141 view()->changeToMoveTool();
144 FormEditorItem *topSelectableItem = 0;
146 foreach(QGraphicsItem* item, itemList)
148 FormEditorItem *formEditorItem = FormEditorItem::fromQGraphicsItem(item);
151 && formEditorItem->qmlItemNode().isValid()
152 && !formEditorItem->qmlItemNode().instanceIsInPositioner()
153 && formEditorItem->qmlItemNode().instanceIsMovable()
154 && (formEditorItem->qmlItemNode().hasShowContent() || !m_selectOnlyContentItems))
156 topSelectableItem = formEditorItem;
161 scene()->highlightBoundingRect(topSelectableItem);
164 void SelectionTool::mouseReleaseEvent(const QList<QGraphicsItem*> &/*itemList*/,
165 QGraphicsSceneMouseEvent *event)
167 if (m_singleSelectionManipulator.isActive()) {
168 m_singleSelectionManipulator.end(event->scenePos());
170 else if (m_rubberbandSelectionManipulator.isActive()) {
172 QPointF mouseMovementVector = m_rubberbandSelectionManipulator.beginPoint() - event->scenePos();
173 if (mouseMovementVector.toPoint().manhattanLength() < s_startDragDistance) {
174 m_singleSelectionManipulator.begin(event->scenePos());
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);
181 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
183 m_singleSelectionManipulator.end(event->scenePos());
185 m_rubberbandSelectionManipulator.update(event->scenePos());
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);
192 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::ReplaceSelection);
194 m_rubberbandSelectionManipulator.end();
200 void SelectionTool::mouseDoubleClickEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneMouseEvent * event)
202 AbstractFormEditorTool::mouseDoubleClickEvent(itemList, event);
205 void SelectionTool::keyPressEvent(QKeyEvent *event)
207 switch(event->key()) {
212 if (view()->changeToMoveTool())
213 view()->currentTool()->keyPressEvent(event);
218 void SelectionTool::keyReleaseEvent(QKeyEvent * /*keyEvent*/)
223 void SelectionTool::setSelectOnlyContentItems(bool selectOnlyContentItems)
225 m_selectOnlyContentItems = selectOnlyContentItems;
228 void SelectionTool::itemsAboutToRemoved(const QList<FormEditorItem*> &/*itemList*/)
233 //QVariant SelectionTool::itemChange(const QList<QGraphicsItem*> &itemList,
234 // QGraphicsItem::GraphicsItemChange change,
235 // const QVariant &value )
237 // qDebug() << Q_FUNC_INFO;
238 // return QVariant();
241 //void SelectionTool::update()
247 void SelectionTool::clear()
249 m_rubberbandSelectionManipulator.clear(),
250 m_singleSelectionManipulator.clear();
251 m_selectionIndicator.clear();
252 m_resizeIndicator.clear();
255 void SelectionTool::selectedItemsChanged(const QList<FormEditorItem*> &itemList)
257 m_selectionIndicator.setItems(itemList);
258 m_resizeIndicator.setItems(itemList);
261 void SelectionTool::formEditorItemsChanged(const QList<FormEditorItem*> &itemList)
263 m_selectionIndicator.updateItems(itemList);
264 m_resizeIndicator.updateItems(itemList);
267 void SelectionTool::instancesCompleted(const QList<FormEditorItem*> &/*itemList*/)
271 void SelectionTool::selectUnderPoint(QGraphicsSceneMouseEvent *event)
273 m_singleSelectionManipulator.begin(event->scenePos());
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);
280 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
282 m_singleSelectionManipulator.end(event->scenePos());