summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Palettes/TimelineGraphicsView/ui/RowTimeline.cpp
blob: ae2fba7c6a3ef4f98798966ac14d1c11a64d6515 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "RowTimeline.h"
#include "RowTree.h"
#include "Ruler.h"
#include "TimelineConstants.h"
#include "Keyframe.h"

#include <QtGui/qpainter.h>
#include <QtWidgets/qgraphicssceneevent.h>

RowTimeline::RowTimeline(Ruler *ruler)
    : InteractiveTimelineItem()
    , m_ruler(ruler)
{
}

void RowTimeline::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    // Background
    QColor bgColor;
    if (m_state == Selected)
        bgColor = TimelineConstants::ROW_COLOR_SELECTED;
    else if (m_state == Hovered)
        bgColor = TimelineConstants::ROW_COLOR_OVER;
    else if (m_rowTree->rowType() == RowType::Property)
        bgColor = TimelineConstants::ROW_COLOR_NORMAL_PROP;
    else
        bgColor = TimelineConstants::ROW_COLOR_NORMAL;
    painter->fillRect(QRect(0, 0, size().width(), size().height() - 1), bgColor);

    // Duration
    if (m_rowTree->rowType() != RowType::Property) {
        painter->save();

        // fully outside ancestors' limits, draw fully hashed
        if (m_minStartX > m_endX || m_maxEndX < m_startX) {
            painter->setBrush(QBrush(QColor(TimelineConstants::ROW_COLOR_DURATION_OFF1),
                                     Qt::BDiagPattern));
            painter->setPen(Qt::NoPen);
            painter->fillRect(QRect(m_startX, 0, m_endX - m_startX, size().height() - 1),
                              QColor(TimelineConstants::ROW_COLOR_DURATION_OFF2));
            painter->drawRect(QRect(m_startX, 0, m_endX - m_startX, size().height() - 1));

            painter->setPen(QPen(QColor(TimelineConstants::ROW_COLOR_DURATION_EDGE), 3));
            painter->drawLine(m_startX, 0, m_startX, size().height() - 1);
            painter->drawLine(m_endX, 0, m_endX, size().height() - 1);
        } else {
            // draw main duration part
            double x = std::max(m_startX, m_minStartX);
            double w = std::min(m_endX, m_maxEndX) - x;
            painter->setPen(Qt::NoPen);
            painter->fillRect(QRect(x, 0, w, size().height() - 1), m_state == Selected
                              ? TimelineConstants::ROW_COLOR_DURATION_SELECTED
                              : TimelineConstants::ROW_COLOR_DURATION);

            // draw hashed part before
            painter->setBrush(QBrush(QColor(TimelineConstants::ROW_COLOR_DURATION_OFF1),
                                     Qt::BDiagPattern));
            if (m_startX < m_minStartX) {
                painter->setPen(Qt::NoPen);
                painter->fillRect(QRect(m_startX, 0, m_minStartX - m_startX, size().height() - 1),
                                  QColor(TimelineConstants::ROW_COLOR_DURATION_OFF2));
                painter->drawRect(QRect(m_startX, 0, m_minStartX - m_startX, size().height() - 1));
                painter->setPen(QColor(TimelineConstants::ROW_COLOR_DURATION_EDGE));
                painter->drawLine(m_minStartX, 0, m_minStartX, size().height() - 1);
            }

            // draw hashed part after
            if (m_endX > m_maxEndX) {
                painter->setPen(Qt::NoPen);
                painter->fillRect(QRect(m_maxEndX, 0, m_endX - m_maxEndX, size().height() - 1),
                                  QColor(TimelineConstants::ROW_COLOR_DURATION_OFF2));
                painter->drawRect(QRect(m_maxEndX, 0, m_endX - m_maxEndX, size().height() - 1));
                painter->setPen(QColor(TimelineConstants::ROW_COLOR_DURATION_EDGE));
                painter->drawLine(m_maxEndX, 0, m_maxEndX, size().height() - 1);
            }

            painter->setPen(QPen(QColor(TimelineConstants::ROW_COLOR_DURATION_EDGE), 2));
            painter->drawLine(m_startX, 0, m_startX, size().height() - 1);
            painter->drawLine(m_endX, 0, m_endX, size().height() - 1);
        }

        painter->restore();
    }

    // Keyframes
    if (m_rowTree->hasPropertyChildren()) { // master keyframes
        static const QPixmap pixKeyframeMasterNormal
                = QPixmap(":/images/Keyframe-Master-Normal.png");
        static const QPixmap pixKeyframeMasterSelected
                = QPixmap(":/images/Keyframe-Master-Selected.png");

        for (auto keyframe : m_keyframes) {
            painter->drawPixmap(timeToX(keyframe->time) - 8.5, 2, keyframe->selected
                                ? pixKeyframeMasterSelected : pixKeyframeMasterNormal);
        }
    } else if (m_rowTree->rowType() == RowType::Property) {
        static const QPixmap pixKeyframePropertyNormal
                = QPixmap(":/images/Keyframe-Property-Normal.png");
        static const QPixmap pixKeyframePropertySelected
                = QPixmap(":/images/Keyframe-Property-Selected.png");

        for (auto keyframe : m_keyframes) {
            painter->drawPixmap(timeToX(keyframe->time) - (keyframe->selected ? 7.5 : 5.5), 2,
                                keyframe->selected ? pixKeyframePropertySelected
                                                   : pixKeyframePropertyNormal);
        }
    }
}

Keyframe *RowTimeline::getClickedKeyframe(const QPointF &scenePos)
{
    QPointF p = mapFromScene(scenePos.x(), scenePos.y());
    double x;

    QList<Keyframe *> keyframes;
    if (m_rowTree->hasPropertyChildren()) {
        const auto childRows = m_rowTree->childRows();
        for (auto child : childRows)
            keyframes.append(child->rowTimeline()->m_keyframes);
    } else {
        keyframes = m_keyframes;
    }

    for (const auto keyframe : qAsConst(keyframes)) {
        x = timeToX(keyframe->time);

        if (p.x() > x - 5 && p.x() < x + 5 && p.y() > 3 && p.y() < 16)
            return keyframe;
    }

    return nullptr;
}

QList<Keyframe *> RowTimeline::getKeyframesInRange(const double left, const double right)
{
    double x;
    double x1 = mapFromScene(left, 0).x();
    double x2 = mapFromScene(right, 0).x();

    QList<Keyframe *> result;
    QList<Keyframe *> keyframes;

    if (m_rowTree->hasPropertyChildren()) {
        const auto childRows = m_rowTree->childRows();
        for (auto child : childRows) {
            if (child->rowType() == RowType::Property)
                keyframes.append(child->rowTimeline()->m_keyframes);
        }
    } else {
        keyframes = m_keyframes;
    }

    for (auto keyframe : qAsConst(keyframes)) {
        x = timeToX(keyframe->time);

        if (x1 < x && x2 > x)
            result.append(keyframe);
    }

    return result;
}

void RowTimeline::insertKeyframe(Keyframe *keyframe)
{
    if (!m_keyframes.contains(keyframe))
        m_keyframes.append(keyframe);
}

void RowTimeline::removeKeyframe(Keyframe *keyframe)
{
    m_keyframes.removeAll(keyframe);
}

void RowTimeline::putSelectedKeyframesOnTop()
{
    if (!m_keyframes.empty())
        std::partition(m_keyframes.begin(), m_keyframes.end(), [](Keyframe *kf) {
            return !kf->selected;
        });

    if (m_rowTree->hasPropertyChildren()) { // has property rows
        const auto childRows = m_rowTree->childRows();
        for (auto child : childRows) {
            std::partition(child->rowTimeline()->m_keyframes.begin(),
                           child->rowTimeline()->m_keyframes.end(), [](Keyframe *kf) {
                return !kf->selected;
            });
        }
    }
}

void RowTimeline::updateKeyframes()
{
    update();

    if (m_rowTree->hasPropertyChildren()) { // master keyframes
        const auto childRows = m_rowTree->childRows();
        for (const auto child : childRows)
            child->rowTimeline()->update();
    }
}

int RowTimeline::getClickedControl(const QPointF &scenePos)
{
    if (m_rowTree->rowType() == RowType::Property)
        return TypeNone;

    QPointF p = mapFromScene(scenePos.x(), scenePos.y());
    if (p.x() > m_startX - TimelineConstants::DURATION_HANDLE_W * .5
            && p.x() < m_startX + TimelineConstants::DURATION_HANDLE_W * .5) {
        return TypeStartHandle;
    } else if (p.x() > m_endX - TimelineConstants::DURATION_HANDLE_W * .5
               && p.x() < m_endX + TimelineConstants::DURATION_HANDLE_W * .5) {
        return TypeEndHandle;
    } else if (p.x() > m_startX && p.x() < m_endX) {
        return TypeDuration;
    }

    return TypeNone;
}

// move the duration area (start/end x)
void RowTimeline::moveDurationBy(double dx)
{
    double dur = m_endX - m_startX;

    m_startX += dx;
    m_endX += dx;

    if (m_startX < TimelineConstants::RULER_EDGE_OFFSET) {
        m_startX = TimelineConstants::RULER_EDGE_OFFSET;
        m_endX = m_startX + dur;
    } else if (m_endX > timeToX(m_ruler->duration())) {
        m_endX = timeToX(m_ruler->duration());
        m_startX = m_endX - dur;
    }

    if (m_rowTree->parentRow() == nullptr) {
        m_minStartX = m_startX;
        m_maxEndX = m_endX;
    }

    m_startTime = xToTime(m_startX);
    m_endTime = xToTime(m_endX);

    updateChildrenMinStartXRecursive(m_rowTree);
    updateChildrenMaxEndXRecursive(m_rowTree);

    update();
}

// convert time values to x
double RowTimeline::timeToX(double time)
{
    return TimelineConstants::RULER_EDGE_OFFSET
            + time * TimelineConstants::RULER_SEC_W * m_ruler->timelineScale();
}

// convert x values to time
double RowTimeline::xToTime(double xPos)
{
    return (xPos - TimelineConstants::RULER_EDGE_OFFSET)
            / (TimelineConstants::RULER_SEC_W * m_ruler->timelineScale());
}

// called after timeline scale is changed to update duration star/end positions
void RowTimeline::updatePosition()
{
    setStartX(timeToX(m_startTime));
    setEndX(timeToX(m_endTime));
}

// Set the position of the start of the row duration
void RowTimeline::setStartX(double startX)
{
    if (startX < TimelineConstants::RULER_EDGE_OFFSET)
        startX = TimelineConstants::RULER_EDGE_OFFSET;
    else if (startX > m_endX - 1)
        startX = m_endX - 1;

    m_startX = startX;
    m_startTime = xToTime(startX);

    if (m_rowTree->parentRow() == nullptr)
        m_minStartX = m_startX;

    updateChildrenMinStartXRecursive(m_rowTree);
    update();
}

// Set the position of the end of the row duration
void RowTimeline::setEndX(double endX)
{
    if (endX < m_startX + 1)
        endX = m_startX + 1;
    else if (endX > timeToX(m_ruler->duration()))
        endX = timeToX(m_ruler->duration());

    m_endX = endX;
    m_endTime = xToTime(endX);

    if (m_rowTree->parentRow() == nullptr)
        m_maxEndX = m_endX;

    updateChildrenMaxEndXRecursive(m_rowTree);
    update();
}

void RowTimeline::updateChildrenMinStartXRecursive(RowTree *rowTree)
{
    if (!rowTree->empty()) {
        const auto childRows = rowTree->childRows();
        for (auto child : childRows) {
            child->rowTimeline()->m_minStartX = std::max(rowTree->rowTimeline()->m_startX,
                                                         rowTree->rowTimeline()->m_minStartX);
            child->rowTimeline()->update();

            updateChildrenMinStartXRecursive(child);
        }
    }
}

void RowTimeline::updateChildrenMaxEndXRecursive(RowTree *rowTree)
{
    if (!rowTree->empty()) {
        const auto childRows = rowTree->childRows();
        for (auto child : childRows) {
            child->rowTimeline()->m_maxEndX = std::min(rowTree->rowTimeline()->m_endX,
                                                       rowTree->rowTimeline()->m_maxEndX);
            child->rowTimeline()->update();

            updateChildrenMaxEndXRecursive(child);
        }
    }
}

void RowTimeline::setStartTime(double startTime)
{
    m_startTime = startTime;
    m_startX = timeToX(startTime);

    if (m_rowTree->parentRow() == nullptr)
        m_minStartX = m_startX;

    updateChildrenMinStartXRecursive(m_rowTree);
    update();
}

void RowTimeline::setEndTime(double endTime)
{
    m_endTime = endTime;
    m_endX = timeToX(endTime);

    if (m_rowTree->parentRow() == nullptr)
        m_maxEndX = m_endX;

    updateChildrenMaxEndXRecursive(m_rowTree);
    update();
}

double RowTimeline::getStartTime() const
{
    return m_startTime;
}

double RowTimeline::getEndTime() const
{
    return m_endTime;
}

void RowTimeline::setState(State state)
{
    m_state = state;
    m_rowTree->m_state = state;

    update();
    m_rowTree->update();
}

void RowTimeline::setRowTree(RowTree *rowTree)
{
    m_rowTree = rowTree;
}

RowTree *RowTimeline::rowTree() const
{
    return m_rowTree;
}

QList<Keyframe *> RowTimeline::keyframes() const
{
    return m_keyframes;
}

RowTimeline *RowTimeline::parentRow() const
{
    if (m_rowTree->m_parentRow == nullptr)
        return nullptr;

    return m_rowTree->m_parentRow->rowTimeline();
}

int RowTimeline::type() const
{
    // Enable the use of qgraphicsitem_cast with this item.
    return TypeRowTimeline;
}