selectiontool.cpp 11.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
18 19 20 21 22
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
25 26 27 28

#include "selectiontool.h"
#include "formeditorscene.h"
#include "formeditorview.h"
29 30
#include "formeditorwidget.h"
#include "formeditorgraphicsview.h"
31 32 33

#include "resizehandleitem.h"

34
#include <nodemetainfo.h>
35 36

#include <QGraphicsSceneMouseEvent>
hjk's avatar
hjk committed
37
#include <QDebug>
38 39 40

namespace QmlDesigner {

41 42
const int s_startDragDistance = 20;
const int s_startDragTime = 50;
Marco Bubke's avatar
Marco Bubke committed
43

44 45 46 47 48
SelectionTool::SelectionTool(FormEditorView *editorView)
    : AbstractFormEditorTool(editorView),
    m_rubberbandSelectionManipulator(editorView->scene()->manipulatorLayerItem(), editorView),
    m_singleSelectionManipulator(editorView),
    m_selectionIndicator(editorView->scene()->manipulatorLayerItem()),
49
    m_resizeIndicator(editorView->scene()->manipulatorLayerItem()),
Marco Bubke's avatar
Marco Bubke committed
50
    m_anchorIndicator(editorView->scene()->manipulatorLayerItem()),
51
    m_bindingIndicator(editorView->scene()->manipulatorLayerItem()),
52
    m_contentNotEditableIndicator(editorView->scene()->manipulatorLayerItem())
53
{
54
    m_selectionIndicator.setCursor(Qt::ArrowCursor);
55 56 57 58 59 60 61 62 63 64
}


SelectionTool::~SelectionTool()
{
}

void SelectionTool::mousePressEvent(const QList<QGraphicsItem*> &itemList,
                                    QGraphicsSceneMouseEvent *event)
{
65 66
    if (event->button() == Qt::LeftButton) {
        m_mousePressTimer.start();
67
        FormEditorItem* formEditorItem = nearestFormEditorItem(event->scenePos(), itemList);
68
        if (formEditorItem
69
                && formEditorItem->qmlItemNode().isValid()) {
70 71
            m_singleSelectionManipulator.begin(event->scenePos());

72 73
            m_itemAlreadySelected = toQmlItemNodeList(view()->selectedModelNodes()).contains(formEditorItem->qmlItemNode())
                    || !view()->hasSingleSelectedModelNode();
74
        } else {
75 76 77 78
            if (event->modifiers().testFlag(Qt::AltModifier)) {
                m_singleSelectionManipulator.begin(event->scenePos());

                if (event->modifiers().testFlag(Qt::ControlModifier))
79
                    m_singleSelectionManipulator.select(SingleSelectionManipulator::RemoveFromSelection);
80
                else if (event->modifiers().testFlag(Qt::ShiftModifier))
81
                    m_singleSelectionManipulator.select(SingleSelectionManipulator::AddToSelection);
82
                else
83
                    m_singleSelectionManipulator.select(SingleSelectionManipulator::ReplaceSelection);
84 85 86 87 88 89

                m_singleSelectionManipulator.end(event->scenePos());
                view()->changeToMoveTool(event->scenePos());
            } else {
                m_rubberbandSelectionManipulator.begin(event->scenePos());
            }
90 91
        }
    }
92

93
    AbstractFormEditorTool::mousePressEvent(itemList, event);
94 95 96 97 98 99 100
}

void SelectionTool::mouseMoveEvent(const QList<QGraphicsItem*> &/*itemList*/,
                                   QGraphicsSceneMouseEvent *event)
{
    if (m_singleSelectionManipulator.isActive()) {
        QPointF mouseMovementVector = m_singleSelectionManipulator.beginPoint() - event->scenePos();
Marco Bubke's avatar
Marco Bubke committed
101 102
        if ((mouseMovementVector.toPoint().manhattanLength() > s_startDragDistance)
            && (m_mousePressTimer.elapsed() > s_startDragTime)) {
103
            m_singleSelectionManipulator.end(event->scenePos());
104 105
            if (m_itemAlreadySelected)
                view()->changeToMoveTool(m_singleSelectionManipulator.beginPoint());
106 107 108 109
            return;
        }
    } else if (m_rubberbandSelectionManipulator.isActive()) {
        QPointF mouseMovementVector = m_rubberbandSelectionManipulator.beginPoint() - event->scenePos();
Marco Bubke's avatar
Marco Bubke committed
110 111
        if ((mouseMovementVector.toPoint().manhattanLength() > s_startDragDistance)
            && (m_mousePressTimer.elapsed() > s_startDragTime)) {
112 113 114 115 116 117 118 119 120 121 122 123 124
            m_rubberbandSelectionManipulator.update(event->scenePos());

            if (event->modifiers().testFlag(Qt::ControlModifier))
                m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::RemoveFromSelection);
            else if (event->modifiers().testFlag(Qt::ShiftModifier))
                m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::AddToSelection);
            else
                m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::ReplaceSelection);
        }
    }
}

void SelectionTool::hoverMoveEvent(const QList<QGraphicsItem*> &itemList,
125
                        QGraphicsSceneMouseEvent * event)
126
{
127
    if (!itemList.isEmpty()) {
128

129 130 131 132 133
        ResizeHandleItem* resizeHandle = ResizeHandleItem::fromGraphicsItem(itemList.first());
        if (resizeHandle) {
            view()->changeToResizeTool();
            return;
        }
134

135
        if ((topSelectedItemIsMovable(itemList) && !view()->hasSingleSelectedModelNode())
136
                || (selectedItemCursorInMovableArea(event->scenePos())
137
                && !event->modifiers().testFlag(Qt::ControlModifier)
138
                && !event->modifiers().testFlag(Qt::ShiftModifier))) {
139
            view()->changeToMoveTool();
140 141
            return;
        }
142
    }
143

144
    FormEditorItem *topSelectableItem = nearestFormEditorItem(event->scenePos(), itemList);
145

146 147 148 149 150 151 152 153

    if (topSelectableItem && toQmlItemNodeList(view()->selectedModelNodes()).contains(topSelectableItem->qmlItemNode())
            && topSelectedItemIsMovable({topSelectableItem})) {
        view()->formEditorWidget()->graphicsView()->viewport()->setCursor(Qt::SizeAllCursor);
    } else {
        view()->formEditorWidget()->graphicsView()->viewport()->unsetCursor();
    }

154
    scene()->highlightBoundingRect(topSelectableItem);
155 156

    m_contentNotEditableIndicator.setItems(toFormEditorItemList(itemList));
157 158
}

159
void SelectionTool::mouseReleaseEvent(const QList<QGraphicsItem*> &itemList,
160 161
                                      QGraphicsSceneMouseEvent *event)
{
162 163
    if (event->button() == Qt::LeftButton) {
        if (m_singleSelectionManipulator.isActive()) {
164 165 166 167 168 169
            if (event->modifiers().testFlag(Qt::ControlModifier))
                m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection);
            else if (event->modifiers().testFlag(Qt::ShiftModifier))
                m_singleSelectionManipulator.select(SingleSelectionManipulator::InvertSelection);
            else
                m_singleSelectionManipulator.select(SingleSelectionManipulator::ReplaceSelection);
170 171
            m_singleSelectionManipulator.end(event->scenePos());
        } else if (m_rubberbandSelectionManipulator.isActive()) {
172

173 174 175
            QPointF mouseMovementVector = m_rubberbandSelectionManipulator.beginPoint() - event->scenePos();
            if (mouseMovementVector.toPoint().manhattanLength() < s_startDragDistance) {
                m_singleSelectionManipulator.begin(event->scenePos());
176

177 178 179 180 181 182
                if (event->modifiers().testFlag(Qt::ControlModifier))
                    m_singleSelectionManipulator.select(SingleSelectionManipulator::RemoveFromSelection);
                else if (event->modifiers().testFlag(Qt::ShiftModifier))
                    m_singleSelectionManipulator.select(SingleSelectionManipulator::AddToSelection);
                else
                    m_singleSelectionManipulator.select(SingleSelectionManipulator::ReplaceSelection);
183

184 185 186
                m_singleSelectionManipulator.end(event->scenePos());
            } else {
                m_rubberbandSelectionManipulator.update(event->scenePos());
187

188 189 190 191 192 193
                if (event->modifiers().testFlag(Qt::ControlModifier))
                    m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::RemoveFromSelection);
                else if (event->modifiers().testFlag(Qt::ShiftModifier))
                    m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::AddToSelection);
                else
                    m_rubberbandSelectionManipulator.select(RubberBandSelectionManipulator::ReplaceSelection);
194

195 196
                m_rubberbandSelectionManipulator.end();
            }
197 198 199
        }
    }

200
    AbstractFormEditorTool::mouseReleaseEvent(itemList, event);
201 202
}

203
void SelectionTool::mouseDoubleClickEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneMouseEvent * event)
204
{
205
    AbstractFormEditorTool::mouseDoubleClickEvent(itemList, event);
206 207 208 209
}

void SelectionTool::keyPressEvent(QKeyEvent *event)
{
210
    switch (event->key()) {
211 212 213 214
        case Qt::Key_Left:
        case Qt::Key_Right:
        case Qt::Key_Up:
        case Qt::Key_Down:
215 216
            if (view()->changeToMoveTool())
                view()->currentTool()->keyPressEvent(event);
217 218 219 220 221 222 223 224 225
            break;
    }
}

void SelectionTool::keyReleaseEvent(QKeyEvent * /*keyEvent*/)
{

}

226
void SelectionTool::dragLeaveEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphicsSceneDragDropEvent * /*event*/)
227 228 229
{
}

230
void SelectionTool::dragMoveEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphicsSceneDragDropEvent * /*event*/)
231 232 233
{
}

234 235 236 237 238 239 240 241 242 243 244
void SelectionTool::itemsAboutToRemoved(const QList<FormEditorItem*> &/*itemList*/)
{

}

void SelectionTool::clear()
{
    m_rubberbandSelectionManipulator.clear(),
    m_singleSelectionManipulator.clear();
    m_selectionIndicator.clear();
    m_resizeIndicator.clear();
Marco Bubke's avatar
Marco Bubke committed
245
    m_anchorIndicator.clear();
246
    m_bindingIndicator.clear();
247
    m_contentNotEditableIndicator.clear();
248 249

    AbstractFormEditorTool::clear();
250 251 252 253 254 255
}

void SelectionTool::selectedItemsChanged(const QList<FormEditorItem*> &itemList)
{
    m_selectionIndicator.setItems(itemList);
    m_resizeIndicator.setItems(itemList);
Marco Bubke's avatar
Marco Bubke committed
256
    m_anchorIndicator.setItems(itemList);
257
    m_bindingIndicator.setItems(itemList);
258 259 260 261
}

void SelectionTool::formEditorItemsChanged(const QList<FormEditorItem*> &itemList)
{
262 263 264 265 266 267
    const QList<FormEditorItem*> selectedItemList = filterSelectedModelNodes(itemList);

    m_selectionIndicator.updateItems(selectedItemList);
    m_resizeIndicator.updateItems(selectedItemList);
    m_anchorIndicator.updateItems(selectedItemList);
    m_bindingIndicator.updateItems(selectedItemList);
268
    m_contentNotEditableIndicator.updateItems(selectedItemList);
269 270
}

271 272 273 274
void SelectionTool::instancesCompleted(const QList<FormEditorItem*> &/*itemList*/)
{
}

275 276 277 278 279
void SelectionTool::instancesParentChanged(const QList<FormEditorItem *> &/*itemList*/)
{

}

280 281 282 283
void SelectionTool::instancePropertyChange(const QList<QPair<ModelNode, PropertyName> > & /*propertyList*/)
{
}

284 285 286 287 288
void SelectionTool::selectUnderPoint(QGraphicsSceneMouseEvent *event)
{
    m_singleSelectionManipulator.begin(event->scenePos());

    if (event->modifiers().testFlag(Qt::ControlModifier))
289
        m_singleSelectionManipulator.select(SingleSelectionManipulator::RemoveFromSelection);
290
    else if (event->modifiers().testFlag(Qt::ShiftModifier))
291
        m_singleSelectionManipulator.select(SingleSelectionManipulator::AddToSelection);
292
    else
293
        m_singleSelectionManipulator.select(SingleSelectionManipulator::ReplaceSelection);
294 295 296 297

    m_singleSelectionManipulator.end(event->scenePos());
}

298 299 300 301
void SelectionTool::focusLost()
{
}

302
}