aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/timelineeditor/timelineselectiontool.cpp
blob: 9919d64352d91378cec4f66ddece5f5725d2876a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** 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.
**
****************************************************************************/

#include "timelineselectiontool.h"
#include "timelineconstants.h"
#include "timelinegraphicsscene.h"
#include "timelinemovableabstractitem.h"
#include "timelinepropertyitem.h"
#include "timelinetooldelegate.h"

#include <QGraphicsRectItem>
#include <QGraphicsView>
#include <QGraphicsSceneMouseEvent>
#include <QScrollBar>
#include <QPen>

namespace QmlDesigner {

TimelineSelectionTool::TimelineSelectionTool(AbstractScrollGraphicsScene *scene,
                                             TimelineToolDelegate *delegate)
    : TimelineAbstractTool(scene, delegate)
    , m_selectionRect(new QGraphicsRectItem)
{
    scene->addItem(m_selectionRect);
    QPen pen(Qt::black);
    pen.setJoinStyle(Qt::MiterJoin);
    pen.setCosmetic(true);
    m_selectionRect->setPen(pen);
    m_selectionRect->setBrush(QColor(128, 128, 128, 50));
    m_selectionRect->setZValue(100);
    m_selectionRect->hide();
}

TimelineSelectionTool::~TimelineSelectionTool() = default;

SelectionMode TimelineSelectionTool::selectionMode(QGraphicsSceneMouseEvent *event)
{
    if (event->modifiers().testFlag(Qt::ControlModifier)) {
        if (event->modifiers().testFlag(Qt::ShiftModifier))
            return SelectionMode::Add;
        else
            return SelectionMode::Toggle;
    }

    return SelectionMode::New;
}

void TimelineSelectionTool::mousePressEvent([[maybe_unused]] TimelineMovableAbstractItem *item,
                                            [[maybe_unused]] QGraphicsSceneMouseEvent *event)
{
    if (event->buttons() == Qt::LeftButton && selectionMode(event) == SelectionMode::New)
        deselect();
}

void TimelineSelectionTool::mouseMoveEvent([[maybe_unused]] TimelineMovableAbstractItem *item,
                                           QGraphicsSceneMouseEvent *event)
{
    if (event->buttons() == Qt::LeftButton) {
        auto endPoint = event->scenePos();

        const qreal xMin = TimelineConstants::sectionWidth;
        const qreal xMax = scene()->graphicsView()->width()
                           - TimelineConstants::timelineLeftOffset - 1;
        const qreal yMin = qMax(TimelineConstants::rulerHeight,
                                scene()->graphicsView()->verticalScrollBar()->value());
        const qreal yMax = yMin + scene()->graphicsView()->height() - 1;

        endPoint.rx() = qBound(xMin, endPoint.x(), xMax);
        endPoint.ry() = qBound(yMin, endPoint.y(), yMax);

        m_selectionRect->setRect(QRectF(startPosition(), endPoint).normalized());
        m_selectionRect->show();

        aboutToSelect(selectionMode(event),
                      scene()->items(m_selectionRect->rect(), Qt::IntersectsItemBoundingRect));
    }
}

void TimelineSelectionTool::mouseReleaseEvent([[maybe_unused]] TimelineMovableAbstractItem *item,
                                              QGraphicsSceneMouseEvent *event)
{
    commitSelection(selectionMode(event));

    reset();
}

void TimelineSelectionTool::mouseDoubleClickEvent(TimelineMovableAbstractItem *item,
                                                  [[maybe_unused]] QGraphicsSceneMouseEvent *event)
{
    if (item)
        item->itemDoubleClicked();

    reset();
}

void TimelineSelectionTool::keyPressEvent([[maybe_unused]] QKeyEvent *keyEvent) {}

void TimelineSelectionTool::keyReleaseEvent([[maybe_unused]] QKeyEvent *keyEvent) {}

void TimelineSelectionTool::deselect()
{
    resetHighlights();
    scene()->clearSelection();
    delegate()->clearSelection();
}

void TimelineSelectionTool::reset()
{
    m_selectionRect->hide();
    m_selectionRect->setRect(0, 0, 0, 0);
    resetHighlights();
}

void TimelineSelectionTool::resetHighlights()
{
    for (auto *keyframe : qAsConst(m_aboutToSelectBuffer))
        if (scene()->isKeyframeSelected(keyframe))
            keyframe->setHighlighted(true);
        else
            keyframe->setHighlighted(false);

    m_aboutToSelectBuffer.clear();
}

void TimelineSelectionTool::aboutToSelect(SelectionMode mode, QList<QGraphicsItem *> items)
{
    resetHighlights();
    m_playbackLoopTimeSteps.clear();

    for (auto *item : items) {
        if (auto *keyframe = TimelineMovableAbstractItem::asTimelineKeyframeItem(item)) {
            // if keyframe's center isn't inside m_selectionRect, discard it
            if (!m_selectionRect->rect().contains(keyframe->rect().center() + item->scenePos()))
                continue;

            if (mode == SelectionMode::Remove)
                keyframe->setHighlighted(false);
            else if (mode == SelectionMode::Toggle)
                if (scene()->isKeyframeSelected(keyframe))
                    keyframe->setHighlighted(false);
                else
                    keyframe->setHighlighted(true);
            else
                keyframe->setHighlighted(true);

            if (mode == SelectionMode::Toggle || mode == SelectionMode::Add)  // TODO: Timeline keyframe highlight with selection tool is set as or added to loop range. Select shortcut for this QDS-4941
                m_playbackLoopTimeSteps << keyframe->mapFromSceneToFrame((keyframe->rect().center() + item->scenePos()).x());

            m_aboutToSelectBuffer << keyframe;
        } else if (auto *barItem = TimelineMovableAbstractItem::asTimelineBarItem(item)) {
            QRectF rect = barItem->rect();
            QPointF center = rect.center() + item->scenePos();
            QPointF left = QPointF(rect.left(), 0) + item->scenePos();
            QPointF right = QPointF(rect.right(), 0) + item->scenePos();
            if (mode == SelectionMode::Add) {  // TODO: Timeline bar item highlight with selection tool is added to loop range. Select shortcut for this QDS-4941
                if (m_selectionRect->rect().contains(left))
                    m_playbackLoopTimeSteps << barItem->mapFromSceneToFrame(left.x());
                if (m_selectionRect->rect().contains(right))
                    m_playbackLoopTimeSteps << barItem->mapFromSceneToFrame(right.x());
                if (m_selectionRect->rect().contains(center))
                    m_playbackLoopTimeSteps << barItem->mapFromSceneToFrame(center.x());
            } else if (mode == SelectionMode::Toggle && m_selectionRect->rect().contains(center)) { // TODO: Timeline bar item highlight with selection tool is set as loop range. Select shortcut for this QDS-4941
                m_playbackLoopTimeSteps << barItem->mapFromSceneToFrame(left.x());
                m_playbackLoopTimeSteps << barItem->mapFromSceneToFrame(right.x());
            }
        }
    }
}

void TimelineSelectionTool::commitSelection(SelectionMode mode)
{
    if (m_playbackLoopTimeSteps.count())
        qobject_cast<TimelineGraphicsScene *>(scene())->layoutRuler()->extendPlaybackLoop(m_playbackLoopTimeSteps,
                                                                                          mode == SelectionMode::Toggle);  // TODO: Highlighting items with selection tool is set or added to loop range. Select shortcut for this QDS-4941
    scene()->selectKeyframes(mode, m_aboutToSelectBuffer);
    m_aboutToSelectBuffer.clear();
    m_playbackLoopTimeSteps.clear();
}

} // End namespace QmlDesigner.