aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/timelineeditor/timelinesectionitem.h
blob: 812f1159e24f3298332d042d262d49c160554843 (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
203
204
205
206
207
208
209
210
211
212
/****************************************************************************
**
** 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.
**
****************************************************************************/

#pragma once

#include "timelineitem.h"
#include "timelinemovableabstractitem.h"

#include <modelnode.h>
#include <qmltimeline.h>

QT_FORWARD_DECLARE_CLASS(QComboBox)
QT_FORWARD_DECLARE_CLASS(QPainter)

namespace QmlDesigner {

class TimelineSectionItem;

class TimelineBarItem : public TimelineMovableAbstractItem
{
    Q_DECLARE_TR_FUNCTIONS(TimelineBarItem)

    enum class Location { Undefined, Center, Left, Right };

public:
    explicit TimelineBarItem(TimelineSectionItem *parent);

    void itemMoved(const QPointF &start, const QPointF &end) override;
    void commitPosition(const QPointF &point) override;

    bool isLocked() const override;

    TimelineBarItem *asTimelineBarItem() override;
protected:
    void scrollOffsetChanged() override;
    void paint(QPainter *painter,
               const QStyleOptionGraphicsItem *option,
               QWidget *widget = nullptr) override;
    void hoverMoveEvent(QGraphicsSceneHoverEvent *) override;
    void contextMenuEvent(QGraphicsSceneContextMenuEvent * event) override;
private:
    TimelineSectionItem *sectionItem() const;

    void dragInit(const QRectF &rect, const QPointF &pos);
    void dragCenter(QRectF rect, const QPointF &pos, qreal min, qreal max);
    void dragHandle(QRectF rect, const QPointF &pos, qreal min, qreal max);
    bool handleRects(const QRectF &rect, QRectF &left, QRectF &right) const;
    bool isActiveHandle(Location location) const;

    void setOutOfBounds(Location location);
    bool validateBounds(qreal pivot);

private:
    Location m_handle = Location::Undefined;

    Location m_bounds = Location::Undefined;

    qreal m_pivot = 0.0;

    QRectF m_oldRect;

    static constexpr qreal minimumBarWidth = 2.0
                                             * static_cast<qreal>(TimelineConstants::sectionHeight);
};

class TimelineSectionItem : public TimelineItem
{
    Q_OBJECT

public:
    enum { Type = TimelineConstants::timelineSectionItemUserType };

    static TimelineSectionItem *create(const QmlTimeline &timelineScene,
                                       const ModelNode &target,
                                       TimelineItem *parent);

    void invalidateBar();

    int type() const override;

    static void updateData(QGraphicsItem *item);
    static void updateDataForTarget(QGraphicsItem *item, const ModelNode &target, bool *b);
    static void updateFramesForTarget(QGraphicsItem *item, const ModelNode &target);
    static void updateHeightForTarget(QGraphicsItem *item, const ModelNode &target);

    void moveAllFrames(qreal offset);
    void scaleAllFrames(qreal scale);
    qreal firstFrame();
    AbstractView *view() const;
    bool isSelected() const;

    ModelNode targetNode() const;
    QVector<qreal> keyframePositions() const;

protected:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    void resizeEvent(QGraphicsSceneResizeEvent *event) override;
    void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;

private:
    void updateData();
    void updateFrames();
    void updateHeight();
    void invalidateHeight();
    void invalidateProperties();
    void invalidateFrames();
    bool collapsed() const;
    void createPropertyItems();
    qreal rulerWidth() const;
    void toggleCollapsed();
    QList<QGraphicsItem *> propertyItems() const;

    TimelineSectionItem(TimelineItem *parent = nullptr);
    ModelNode m_targetNode;
    QmlTimeline m_timeline;

    TimelineBarItem *m_barItem;
    TimelineItem *m_dummyItem;
};

class TimelineRulerSectionItem : public TimelineItem
{
    Q_OBJECT

signals:
    void rulerClicked(const QPointF &pos);
    void playbackLoopValuesChanged();
    void zoomChanged(int zoom);

public:
    static TimelineRulerSectionItem *create(QGraphicsScene *parentScene, TimelineItem *parent);

    void invalidateRulerSize(const QmlTimeline &timeline);
    void invalidateRulerSize(const qreal length);

    void setZoom(int zoom);

    int zoom() const;
    qreal getFrameTick() const;

    qreal rulerScaling() const;
    qreal rulerDuration() const;
    qreal durationViewportLength() const;
    qreal startFrame() const;
    qreal endFrame() const;
    qreal playbackLoopStart() const;
    qreal playbackLoopEnd() const;

    QComboBox *comboBox() const;

    void setSizeHints(int width);
    void setPlaybackLoopEnabled(bool value);
    void setPlaybackLoopTimes(float start, float end);
    void extendPlaybackLoop(const QList<qreal> &positions, bool reset);
    void updatePlaybackLoop(QGraphicsSceneMouseEvent *event);


signals:
    void addTimelineClicked();

protected:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;

    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

    void resizeEvent(QGraphicsSceneResizeEvent *event) override;

private:
    TimelineRulerSectionItem(TimelineItem *parent = nullptr);

    void paintTicks(QPainter *painter);

    QComboBox *m_comboBox = nullptr;

    qreal m_duration = 0;
    qreal m_start = 0;
    qreal m_end = 0;
    qreal m_scaling = 1;
    qreal m_frameTick = 1.; // distance between 2 tick steps (in frames) on the ruler at current scale
    qreal m_playbackLoopStart = 0.;
    qreal m_playbackLoopEnd = 0.;
    bool m_playbackLoopEnabled = false;
    bool m_isPaintingPlaybackLoopRange = false;
    bool m_isMovingPlaybackStart = false;
};

} // namespace QmlDesigner