1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "selectiontool.h"
35 #include "formeditorscene.h"
36 #include "formeditorview.h"
38 #include "resizehandleitem.h"
39 #include "nodemetainfo.h"
42 #include <QApplication>
43 #include <QGraphicsSceneMouseEvent>
47 namespace QmlDesigner {
49 static const int s_startDragDistance = 20;
50 static const int s_startDragTime = 50;
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)
60 // view()->setCursor(Qt::CrossCursor);
64 SelectionTool::~SelectionTool()
68 void SelectionTool::mousePressEvent(const QList<QGraphicsItem*> &itemList,
69 QGraphicsSceneMouseEvent *event)
71 m_mousePressTimer.start();
72 FormEditorItem* formEditorItem = topFormEditorItem(itemList);
74 && formEditorItem->qmlItemNode().isValid()
75 && !formEditorItem->qmlItemNode().hasChildren()) {
76 m_singleSelectionManipulator.begin(event->scenePos());
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);
83 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
85 if (event->modifiers().testFlag(Qt::AltModifier)) {
86 m_singleSelectionManipulator.begin(event->scenePos());
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);
93 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
95 m_singleSelectionManipulator.end(event->scenePos());
96 view()->changeToMoveTool(event->scenePos());
98 m_rubberbandSelectionManipulator.begin(event->scenePos());
103 void SelectionTool::mouseMoveEvent(const QList<QGraphicsItem*> &/*itemList*/,
104 QGraphicsSceneMouseEvent *event)
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());
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());
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);
125 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::ReplaceSelection);
130 void SelectionTool::hoverMoveEvent(const QList<QGraphicsItem*> &itemList,
131 QGraphicsSceneMouseEvent * /*event*/)
133 if (!itemList.isEmpty()) {
135 ResizeHandleItem* resizeHandle = ResizeHandleItem::fromGraphicsItem(itemList.first());
137 view()->changeToResizeTool();
141 if (topSelectedItemIsMovable(itemList))
142 view()->changeToMoveTool();
145 FormEditorItem *topSelectableItem = 0;
147 foreach(QGraphicsItem* item, itemList)
149 FormEditorItem *formEditorItem = FormEditorItem::fromQGraphicsItem(item);
152 && formEditorItem->qmlItemNode().isValid()
153 && !formEditorItem->qmlItemNode().instanceIsInPositioner()
154 && formEditorItem->qmlItemNode().instanceIsMovable()
155 && (formEditorItem->qmlItemNode().hasShowContent() || !m_selectOnlyContentItems))
157 topSelectableItem = formEditorItem;
162 scene()->highlightBoundingRect(topSelectableItem);
165 void SelectionTool::mouseReleaseEvent(const QList<QGraphicsItem*> &/*itemList*/,
166 QGraphicsSceneMouseEvent *event)
168 if (m_singleSelectionManipulator.isActive()) {
169 m_singleSelectionManipulator.end(event->scenePos());
171 else if (m_rubberbandSelectionManipulator.isActive()) {
173 QPointF mouseMovementVector = m_rubberbandSelectionManipulator.beginPoint() - event->scenePos();
174 if (mouseMovementVector.toPoint().manhattanLength() < s_startDragDistance) {
175 m_singleSelectionManipulator.begin(event->scenePos());
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);
182 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
184 m_singleSelectionManipulator.end(event->scenePos());
186 m_rubberbandSelectionManipulator.update(event->scenePos());
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);
193 m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::ReplaceSelection);
195 m_rubberbandSelectionManipulator.end();
201 void SelectionTool::mouseDoubleClickEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneMouseEvent * event)
203 AbstractFormEditorTool::mouseDoubleClickEvent(itemList, event);
206 void SelectionTool::keyPressEvent(QKeyEvent *event)
208 switch(event->key()) {
213 if (view()->changeToMoveTool())
214 view()->currentTool()->keyPressEvent(event);
219 void SelectionTool::keyReleaseEvent(QKeyEvent * /*keyEvent*/)
224 void SelectionTool::setSelectOnlyContentItems(bool selectOnlyContentItems)
226 m_selectOnlyContentItems = selectOnlyContentItems;
229 void SelectionTool::itemsAboutToRemoved(const QList<FormEditorItem*> &/*itemList*/)
234 //QVariant SelectionTool::itemChange(const QList<QGraphicsItem*> &itemList,
235 // QGraphicsItem::GraphicsItemChange change,
236 // const QVariant &value )
238 // qDebug() << Q_FUNC_INFO;
239 // return QVariant();
242 //void SelectionTool::update()
248 void SelectionTool::clear()
250 m_rubberbandSelectionManipulator.clear(),
251 m_singleSelectionManipulator.clear();
252 m_selectionIndicator.clear();
253 m_resizeIndicator.clear();
256 void SelectionTool::selectedItemsChanged(const QList<FormEditorItem*> &itemList)
258 m_selectionIndicator.setItems(itemList);
259 m_resizeIndicator.setItems(itemList);
262 void SelectionTool::formEditorItemsChanged(const QList<FormEditorItem*> &itemList)
264 m_selectionIndicator.updateItems(itemList);
265 m_resizeIndicator.updateItems(itemList);
268 void SelectionTool::instancesCompleted(const QList<FormEditorItem*> &/*itemList*/)
272 void SelectionTool::selectUnderPoint(QGraphicsSceneMouseEvent *event)
274 m_singleSelectionManipulator.begin(event->scenePos());
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);
281 m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection, m_selectOnlyContentItems);
283 m_singleSelectionManipulator.end(event->scenePos());