keyframeview.cpp 31.1 KB
Newer Older
1
2
/***************************************************************************
 *   Copyright (C) 2011 by Till Theato (root@ttill.de)                     *
3
 *   Copyright (C) 2017 by Nicolas Carion                                  *
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *   This file is part of Kdenlive (www.kdenlive.org).                     *
 *                                                                         *
 *   Kdenlive is free software: you can redistribute it and/or modify      *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   Kdenlive is distributed in the hope that it will be useful,           *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with Kdenlive.  If not, see <http://www.gnu.org/licenses/>.     *
 ***************************************************************************/

#include "keyframeview.hpp"
21
#include "assets/keyframes/model/keyframemodellist.hpp"
22
23
24
25
#include "core.h"
#include "kdenlivesettings.h"

#include <QMouseEvent>
26
#include <QApplication>
27
#include <QStylePainter>
28
#include <QtMath>
29
30
31

#include <KColorScheme>
#include <QFontDatabase>
Nicolas Carion's avatar
Nicolas Carion committed
32
#include <utility>
33

34
KeyframeView::KeyframeView(std::shared_ptr<KeyframeModelList> model, int duration, QWidget *parent)
35
    : QWidget(parent)
Nicolas Carion's avatar
Nicolas Carion committed
36
    , m_model(std::move(model))
37
    , m_duration(duration)
38
39
40
41
42
    , m_position(0)
    , m_currentKeyframe(-1)
    , m_currentKeyframeOriginal(-1)
    , m_hoverKeyframe(-1)
    , m_scale(1)
43
44
    , m_zoomFactor(1)
    , m_zoomStart(0)
45
    , m_moveKeyframeMode(false)
46
47
    , m_clickPoint(-1)
    , m_clickEnd(-1)
48
49
50
51
52
    , m_zoomHandle(0,1)
    , m_hoverZoomIn(false)
    , m_hoverZoomOut(false)
    , m_hoverZoom(false)
    , m_clickOffset(0)
53
54
55
56
57
{
    setMouseTracking(true);
    setMinimumSize(QSize(150, 20));
    setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
    QPalette p = palette();
58
    KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window);
59
60
    m_colSelected = palette().highlight().color();
    m_colKeyframe = scheme.foreground(KColorScheme::NormalText).color();
61
62
63
64
    m_size = QFontInfo(font()).pixelSize() * 3;
    m_lineHeight = m_size / 2.1;
    m_zoomHeight = m_size * 3 / 4;
    m_offset = m_size / 4;
65
    setFixedHeight(m_size);
66
    setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
67
    connect(m_model.get(), &KeyframeModelList::modelChanged, this, &KeyframeView::slotModelChanged);
68
69
}

70
71
void KeyframeView::slotModelChanged()
{
72
73
    int offset = pCore->getItemIn(m_model->getOwnerId());
    emit atKeyframe(m_model->hasKeyframe(m_position + offset), m_model->singleKeyframe());
74
    emit modified();
75
76
    update();
}
77

78
void KeyframeView::slotSetPosition(int pos, bool isInRange)
79
{
80
81
82
83
84
    if (!isInRange) {
        m_position = -1;
        update();
        return;
    }
85
86
    if (pos != m_position) {
        m_position = pos;
87
88
        int offset = pCore->getItemIn(m_model->getOwnerId());
        emit atKeyframe(m_model->hasKeyframe(pos + offset), m_model->singleKeyframe());
89
90
91
92
93
94
95
96
97
98
99
100
        double zoomPos = (double) m_position / m_duration;
        if (zoomPos < m_zoomHandle.x()) {
            double interval = m_zoomHandle.y() - m_zoomHandle.x();
            zoomPos = qBound(0.0, zoomPos - interval / 5, 1.0);
            m_zoomHandle.setX(zoomPos);
            m_zoomHandle.setY(zoomPos + interval);
        } else if (zoomPos > m_zoomHandle.y()) {
            double interval = m_zoomHandle.y() - m_zoomHandle.x();
            zoomPos = qBound(0.0, zoomPos + interval / 5, 1.0);
            m_zoomHandle.setX(zoomPos - interval);
            m_zoomHandle.setY(zoomPos);
        }
101
102
103
104
        update();
    }
}

105
106
107
108
109
void KeyframeView::initKeyframePos()
{
    emit atKeyframe(m_model->hasKeyframe(m_position), m_model->singleKeyframe());
}

110
111
112
113
114
115
116
117
118
119
120
121
122
123
void KeyframeView::slotDuplicateKeyframe()
{
    int offset = pCore->getItemIn(m_model->getOwnerId());
    if (m_currentKeyframe > -1 && !m_model->hasKeyframe(m_position + offset)) {
        Fun undo = []() { return true; };
        Fun redo = []() { return true; };
        int delta = m_position - m_currentKeyframe;
        for (int kf : m_selectedKeyframes) {
            m_model->duplicateKeyframeWithUndo(GenTime(kf + offset, pCore->getCurrentFps()), GenTime(kf + delta + offset, pCore->getCurrentFps()), undo, redo);
        }
        pCore->pushUndo(undo, redo, i18n("Duplicate keyframe"));
    }
}

124
void KeyframeView::slotAddKeyframe(int pos)
125
126
127
128
{
    if (pos < 0) {
        pos = m_position;
    }
129
    int offset = pCore->getItemIn(m_model->getOwnerId());
130
    m_model->addKeyframe(GenTime(pos + offset, pCore->getCurrentFps()), (KeyframeType)KdenliveSettings::defaultkeyframeinterp());
131
132
}

133
134
135
136
137
const QString KeyframeView::getAssetId()
{
    return m_model->getAssetId();
}

138
void KeyframeView::slotAddRemove()
139
{
140
    emit activateEffect();
141
142
    int offset = pCore->getItemIn(m_model->getOwnerId());
    if (m_model->hasKeyframe(m_position + offset)) {
143
144
145
146
147
148
        if (m_selectedKeyframes.contains(m_position)) {
            // Delete all selected keyframes
            slotRemoveKeyframe(m_selectedKeyframes);
        } else {
            slotRemoveKeyframe({m_position});
        }
149
    } else {
150
        slotAddKeyframe(m_position);
151
152
153
    }
}

154
155
void KeyframeView::slotEditType(int type, const QPersistentModelIndex &index)
{
156
157
    int offset = pCore->getItemIn(m_model->getOwnerId());
    if (m_model->hasKeyframe(m_position + offset)) {
158
        m_model->updateKeyframeType(GenTime(m_position + offset, pCore->getCurrentFps()), type, index);
159
160
161
    }
}

162
void KeyframeView::slotRemoveKeyframe(QVector<int> positions)
163
{
164
165
166
167
168
    if (m_model->singleKeyframe()) {
        // Don't allow zero keyframe
        pCore->displayMessage(i18n("Cannot remove the last keyframe"), MessageType::InformationMessage, 500);
        return;
    }
169
    int offset = pCore->getItemIn(m_model->getOwnerId());
170
171
172
    Fun undo = []() { return true; };
    Fun redo = []() { return true; };
    for (int pos : positions) {
173
174
175
176
        if (pos == 0) {
            // Don't allow moving first keyframe
            continue;
        }
177
178
179
180
181
182
        if (m_selectedKeyframes.contains(pos)) {
            m_selectedKeyframes.removeAll(pos);
        }
        m_model->removeKeyframeWithUndo(GenTime(pos + offset, pCore->getCurrentFps()), undo, redo);
    }
    pCore->pushUndo(undo, redo, i18np("Remove keyframe", "Remove keyframes", positions.size()));
183
184
185
186
187
}

void KeyframeView::setDuration(int dur)
{
    m_duration = dur;
188
189
190
    int offset = pCore->getItemIn(m_model->getOwnerId());
    emit atKeyframe(m_model->hasKeyframe(m_position + offset), m_model->singleKeyframe());
    update();
191
192
193
194
}

void KeyframeView::slotGoToNext()
{
195
    emit activateEffect();
196
    if (m_position == m_duration - 1) {
197
198
199
200
        return;
    }

    bool ok;
201
    int offset = pCore->getItemIn(m_model->getOwnerId());
202
    auto next = m_model->getNextKeyframe(GenTime(m_position + offset, pCore->getCurrentFps()), &ok);
203
204

    if (ok) {
205
        emit seekToPos(qMin((int)next.first.frames(pCore->getCurrentFps()) - offset, m_duration - 1));
206
207
    } else {
        // no keyframe after current position
208
        emit seekToPos(m_duration - 1);
209
210
211
212
213
    }
}

void KeyframeView::slotGoToPrev()
{
214
    emit activateEffect();
215
216
217
218
219
    if (m_position == 0) {
        return;
    }

    bool ok;
220
221
    int offset = pCore->getItemIn(m_model->getOwnerId());
    auto prev = m_model->getPrevKeyframe(GenTime(m_position + offset, pCore->getCurrentFps()), &ok);
222
223

    if (ok) {
224
        emit seekToPos(qMax(0, (int)prev.first.frames(pCore->getCurrentFps()) - offset));
225
226
    } else {
        // no keyframe after current position
227
        emit seekToPos(m_duration - 1);
228
229
230
    }
}

231
232
void KeyframeView::slotCenterKeyframe()
{
233
    if (m_currentKeyframeOriginal == -1 || m_currentKeyframeOriginal == m_position || m_currentKeyframeOriginal == 0) {
234
235
236
237
238
239
        return;
    }
    int offset = pCore->getItemIn(m_model->getOwnerId());
    if (!m_model->hasKeyframe(m_currentKeyframeOriginal + offset)) {
        return;
    }
240
241
242
    Fun undo = []() { return true; };
    Fun redo = []() { return true; };
    int delta = m_position - m_currentKeyframeOriginal;
243
244
245
    QVector<int>currentSelection = m_selectedKeyframes;
    int sourcePosition = m_currentKeyframeOriginal;
    QVector<int>updatedSelection;
246
    for (int kf : m_selectedKeyframes) {
247
248
249
250
        if (kf == 0) {
            // Dont't allow moving first keyframe
            continue;
        }
251
252
253
        GenTime initPos(kf + offset, pCore->getCurrentFps());
        GenTime targetPos(kf + delta + offset, pCore->getCurrentFps());
        m_model->moveKeyframeWithUndo(initPos, targetPos, undo, redo);
254
        updatedSelection << (kf + delta);
255
    }
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    Fun local_redo = [this, updatedSelection]() {
        m_currentKeyframe = m_currentKeyframeOriginal = m_position;
        m_selectedKeyframes = updatedSelection;
        update();
        return true;
    };
    Fun local_undo = [this, currentSelection, sourcePosition]() {
        m_currentKeyframe = m_currentKeyframeOriginal = sourcePosition;
        m_selectedKeyframes = currentSelection;
        update();
        return true;
    };
    local_redo();
    PUSH_LAMBDA(local_redo, redo);
    PUSH_FRONT_LAMBDA(local_undo, undo);
271
    pCore->pushUndo(undo, redo, i18n("Move keyframe"));
272
273
}

274
275
void KeyframeView::mousePressEvent(QMouseEvent *event)
{
276
    emit activateEffect();
277
    int offset = pCore->getItemIn(m_model->getOwnerId());
278
279
280
281
282
    double zoomStart = m_zoomHandle.x() * (width() - 2 * m_offset);
    double zoomEnd = m_zoomHandle.y() * (width() - 2 * m_offset);
    double zoomFactor = (width() - 2 * m_offset) / (zoomEnd - zoomStart);
    int pos = ((event->x() - m_offset) / zoomFactor + zoomStart ) / m_scale;
    pos = qBound(0, pos, m_duration - 1);
283
    m_moveKeyframeMode = false;
284
    if (event->button() == Qt::LeftButton) {
285
        if (event->y() < m_lineHeight) {
286
287
288
            // mouse click in keyframes area
            bool ok;
            GenTime position(pos + offset, pCore->getCurrentFps());
289
290
291
292
            if (event->modifiers() & Qt::ShiftModifier) {
                m_clickPoint = pos;
                return;
            }
293
            auto keyframe = m_model->getClosestKeyframe(position, &ok);
294
            if (ok && qAbs(keyframe.first.frames(pCore->getCurrentFps()) - pos - offset) * m_scale * m_zoomFactor < QApplication::startDragDistance()) {
295
                m_currentKeyframeOriginal = keyframe.first.frames(pCore->getCurrentFps()) - offset;
296
297
298
                if (event->modifiers() & Qt::ControlModifier) {
                    if (m_selectedKeyframes.contains(m_currentKeyframeOriginal)) {
                        m_selectedKeyframes.removeAll(m_currentKeyframeOriginal);
299
                        m_currentKeyframeOriginal = -1;
300
                    } else {
301
302
303
304
305
                        m_selectedKeyframes << m_currentKeyframeOriginal;
                    }
                } else if (!m_selectedKeyframes.contains(m_currentKeyframeOriginal)) {
                    m_selectedKeyframes = {m_currentKeyframeOriginal};
                }
306
307
                // Select and seek to keyframe
                m_currentKeyframe = m_currentKeyframeOriginal;
308
309
310
311
312
313
314
                if (m_currentKeyframeOriginal > -1) {
                    m_moveKeyframeMode = true;
                    if (KdenliveSettings::keyframeseek()) {
                        emit seekToPos(m_currentKeyframeOriginal);
                    } else {
                        update();
                    }
315
316
317
                } else {
                    update();
                }
318
319
                return;
            }
320
            // no keyframe next to mouse
321
            m_selectedKeyframes.clear();
322
            m_currentKeyframe = m_currentKeyframeOriginal = -1;
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
        } else if (event->y() > m_zoomHeight + 2) {
            // click on zoom area
            if (m_hoverZoom) {
                m_clickOffset = ((double) event->x() - m_offset) / (width() - 2 * m_offset);
            }
            return;
        }
    } else if (event->button() == Qt::RightButton && event->y() > m_zoomHeight + 2) {
        // Right click on zoom, switch between no zoom and last zoom status
        if (m_zoomHandle == QPointF(0, 1)) {
            if (!m_lastZoomHandle.isNull()) {
                m_zoomHandle = m_lastZoomHandle;
                update();
                return;
            }
        } else {
            m_lastZoomHandle = m_zoomHandle;
            m_zoomHandle = QPointF(0, 1);
            update();
342
            return;
343
344
        }
    }
345
    emit seekToPos(pos);
346
347
348
349
350
    update();
}

void KeyframeView::mouseMoveEvent(QMouseEvent *event)
{
351
    int offset = pCore->getItemIn(m_model->getOwnerId());
352
353
354
355
356
    double zoomStart = m_zoomHandle.x() * (width() - 2 * m_offset);
    double zoomEnd = m_zoomHandle.y() * (width() - 2 * m_offset);
    double zoomFactor = (width() - 2 * m_offset) / (zoomEnd - zoomStart);
    int pos = ((event->x() - m_offset) / zoomFactor + zoomStart ) / m_scale;
    pos = qBound(0, pos, m_duration - 1);
357
    GenTime position(pos + offset, pCore->getCurrentFps());
358
    if ((event->buttons() & Qt::LeftButton) != 0u) {
359
360
361
        if (m_hoverZoomIn || m_hoverZoomOut || m_hoverZoom) {
            // Moving zoom handles
            if (m_hoverZoomIn) {
362
                m_zoomHandle.setX(qMin(qMax(0., (double)(event->x() - m_offset) / (width() - 2 * m_offset)), m_zoomHandle.y() - 0.015));
363
364
365
366
                update();
                return;
            }
            if (m_hoverZoomOut) {
367
                m_zoomHandle.setY(qMax(qMin(1., (double)(event->x() - m_offset) / (width() - 2 * m_offset)), m_zoomHandle.x() + 0.015));
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
                update();
                return;
            }
            // moving zoom zone
            if (m_hoverZoom) {
                double clickOffset = ((double)event->x() - m_offset) / (width() - 2 * m_offset) - m_clickOffset;
                double newX = m_zoomHandle.x() + clickOffset;
                if (newX < 0) {
                    clickOffset = - m_zoomHandle.x();
                    newX = 0;
                }
                double newY = m_zoomHandle.y() + clickOffset;
                if (newY > 1) {
                    clickOffset = 1 - m_zoomHandle.y();
                    newY = 1;
                    newX = m_zoomHandle.x() + clickOffset;
                }
                m_clickOffset = ((double)event->x() - m_offset) / (width() - 2 * m_offset);
                m_zoomHandle = QPointF(newX, newY);
                update();
            }
            return;
        }
391
392
393
        if (m_currentKeyframe == pos) {
            return;
        }
394
        if (m_currentKeyframe > 0 && m_moveKeyframeMode) {
395
            if (!m_model->hasKeyframe(pos + offset)) {
396
                int delta = pos - m_currentKeyframe;
397
398
                // Check that the move is possible
                for (int kf : m_selectedKeyframes) {
399
400
401
                    int updatedPos = kf + offset + delta;
                    if (!m_selectedKeyframes.contains(updatedPos) && m_model->hasKeyframe(updatedPos)) {
                        // Don't allow moving over another keyframe
402
403
404
405
                        return;
                    }
                }
                for (int kf : m_selectedKeyframes) {
406
407
408
409
                    if (kf == 0) {
                        // Don't allow moving first keyframe
                        continue;
                    }
410
411
412
413
414
415
416
417
418
                    GenTime currentPos(kf + offset, pCore->getCurrentFps());
                    GenTime updatedPos(kf + offset + delta, pCore->getCurrentFps());
                    if (m_model->moveKeyframe(currentPos, updatedPos, false)) {
                        if (kf == m_currentKeyframe) {
                           m_currentKeyframe = pos;
                        }
                    }
                }
                for (int &kf : m_selectedKeyframes) {
419
420
421
422
                    if (kf == 0) {
                        // Don't allow moving first keyframe
                        continue;
                    }
423
                    kf += delta;
424
425
426
                }
            }
        }
427
428
429
430
431
432
433
        // Rubberband selection
        if (m_clickPoint >= 0) {
            m_clickEnd = pos;
            int min = qMin(m_clickPoint, m_clickEnd);
            int max = qMax(m_clickPoint, m_clickEnd);
            min = qMax(1, min);
            m_selectedKeyframes.clear();
434
            m_currentKeyframeOriginal = m_currentKeyframe = -1;
435
436
437
438
439
440
441
            double fps = pCore->getCurrentFps();
            for (const auto &keyframe : *m_model.get()) {
                int pos = keyframe.first.frames(fps) - offset;
                if (pos > min && pos < max) {
                    m_selectedKeyframes << pos;
                }
            }
442
443
444
            if (!m_selectedKeyframes.isEmpty()) {
                m_currentKeyframeOriginal = m_currentKeyframe = m_selectedKeyframes.first();
            }
445
446
447
448
            update();
            return;
        }
        
449
        if (!m_moveKeyframeMode || KdenliveSettings::keyframeseek()) {
450
451
            emit seekToPos(pos);
        }
452
453
454
455
456
        return;
    }
    if (event->y() < m_lineHeight) {
        bool ok;
        auto keyframe = m_model->getClosestKeyframe(position, &ok);
457
        if (ok && qAbs(((position.frames(pCore->getCurrentFps()) - keyframe.first.frames(pCore->getCurrentFps())) * m_scale) * m_zoomFactor) < QApplication::startDragDistance()) {
458
            m_hoverKeyframe = keyframe.first.frames(pCore->getCurrentFps()) - offset;
459
            setCursor(Qt::PointingHandCursor);
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
            m_hoverZoomIn = false;
            m_hoverZoomOut = false;
            m_hoverZoom = false;
            update();
            return;
        }
    }
    if (event->y() > m_zoomHeight + 2) {
        // Moving in zoom area
        if (qAbs(event->x() - m_offset - (m_zoomHandle.x() * (width() - 2 * m_offset))) < QApplication::startDragDistance()) {
            setCursor(Qt::SizeHorCursor);
            m_hoverZoomIn = true;
            m_hoverZoomOut = false;
            m_hoverZoom = false;
            update();
            return;
        }
        if (qAbs(event->x() - m_offset - (m_zoomHandle.y() * (width() - 2 * m_offset))) < QApplication::startDragDistance()) {
            setCursor(Qt::SizeHorCursor);
            m_hoverZoomOut = true;
            m_hoverZoomIn = false;
            m_hoverZoom = false;
            update();
            return;
        }
        if (m_zoomHandle != QPointF(0, 1) && event->x() > m_offset + (m_zoomHandle.x() * (width() - 2 * m_offset)) && event->x() < m_offset + (m_zoomHandle.y() * (width() - 2 * m_offset))) {
            setCursor(Qt::PointingHandCursor);
            m_hoverZoom = true;
            m_hoverZoomIn = false;
            m_hoverZoomOut = false;
490
491
492
493
494
            update();
            return;
        }
    }

495
    if (m_hoverKeyframe != -1 || m_hoverZoomOut || m_hoverZoomIn || m_hoverZoom) {
496
        m_hoverKeyframe = -1;
497
498
499
        m_hoverZoomOut = false;
        m_hoverZoomIn = false;
        m_hoverZoom = false;
500
501
502
503
504
505
506
507
        setCursor(Qt::ArrowCursor);
        update();
    }
}

void KeyframeView::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
508
    m_moveKeyframeMode = false;
509
510
511
512
513
    m_clickPoint = -1;
    if (m_clickEnd >= 0) {
        m_clickEnd = -1;
        update();
    }
514
    if (m_currentKeyframe >= 0 && m_currentKeyframeOriginal != m_currentKeyframe) {
515
        int offset = pCore->getItemIn(m_model->getOwnerId());
516
517
518
        int delta = m_currentKeyframe - m_currentKeyframeOriginal;
        // Move back all keyframes to their initial positions
        for (int kf : m_selectedKeyframes) {
519
520
521
522
            if (kf == 0) {
                // Don't allow moving first keyframe
                continue;
            }
523
524
525
526
527
528
529
530
            GenTime initPos(kf - delta + offset, pCore->getCurrentFps());
            GenTime targetPos(kf + offset, pCore->getCurrentFps());
            m_model->moveKeyframe(targetPos, initPos, false);
        }
        // Move all keyframes to their new positions
        Fun undo = []() { return true; };
        Fun redo = []() { return true; };
        for (int kf : m_selectedKeyframes) {
531
532
533
534
            if (kf == 0) {
                // Don't allow moving first keyframe
                continue;
            }
535
536
537
538
            GenTime initPos(kf - delta + offset, pCore->getCurrentFps());
            GenTime targetPos(kf + offset, pCore->getCurrentFps());
            m_model->moveKeyframeWithUndo(initPos, targetPos, undo, redo);
        }
539
        m_currentKeyframeOriginal = m_currentKeyframe;
540
541
        pCore->pushUndo(undo, redo, i18np("Move keyframe", "Move keyframes", m_selectedKeyframes.size()));
        qDebug() << "RELEASING keyframe move" << delta;
542
543
544
545
546
547
    }
}

void KeyframeView::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && event->y() < m_lineHeight) {
548
        int offset = pCore->getItemIn(m_model->getOwnerId());
549
550
551
552
553
        double zoomStart = m_zoomHandle.x() * (width() - 2 * m_offset);
        double zoomEnd = m_zoomHandle.y() * (width() - 2 * m_offset);
        double zoomFactor = (width() - 2 * m_offset) / (zoomEnd - zoomStart);
        int pos = ((event->x() - m_offset) / zoomFactor + zoomStart ) / m_scale;
        pos = qBound(0, pos, m_duration - 1);
554
        GenTime position(pos + offset, pCore->getCurrentFps());
555
556
        bool ok;
        auto keyframe = m_model->getClosestKeyframe(position, &ok);
557
        if (ok && qAbs(keyframe.first.frames(pCore->getCurrentFps()) - pos - offset)* m_scale * m_zoomFactor < QApplication::startDragDistance()) {
558
559
560
            if (keyframe.first.frames(pCore->getCurrentFps()) != offset) {
                m_model->removeKeyframe(keyframe.first);
                if (keyframe.first.frames(pCore->getCurrentFps()) == m_currentKeyframe + offset) {
561
562
563
                    if (m_selectedKeyframes.contains(m_currentKeyframe)) {
                        m_selectedKeyframes.removeAll(m_currentKeyframe);
                    }
564
565
566
567
568
                    m_currentKeyframe = m_currentKeyframeOriginal = -1;
                }
                if (keyframe.first.frames(pCore->getCurrentFps()) == m_position + offset) {
                    emit atKeyframe(false, m_model->singleKeyframe());
                }
569
570
571
572
573
            }
            return;
        }

        // add new keyframe
574
        m_model->addKeyframe(position, (KeyframeType)KdenliveSettings::defaultkeyframeinterp());
575
576
577
578
579
580
581
    } else {
        QWidget::mouseDoubleClickEvent(event);
    }
}

void KeyframeView::wheelEvent(QWheelEvent *event)
{
582
    if (event->modifiers() & Qt::AltModifier) {
583
584
        // Alt modifier seems to invert x/y axis
        if (event->angleDelta().x() > 0) {
585
586
587
588
589
590
            slotGoToPrev();
        } else {
            slotGoToNext();
        }
        return;
    }
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    if (event->modifiers() & Qt::ControlModifier) {
        int maxWidth = width() - 2 * m_offset;
        m_zoomStart = m_zoomHandle.x() * maxWidth;
        m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
        double scaledPos = m_position * m_scale;
        double zoomRange = (m_zoomHandle.y() - m_zoomHandle.x()) * maxWidth;
        if (event->angleDelta().y() > 0) {
            zoomRange /= 1.5;
        } else {
            zoomRange *= 1.5;
        }
        if (zoomRange < 5) {
            // Don't allow too small zoombar
            return;
        }
        double length = (scaledPos - zoomRange / 2) / maxWidth;
        m_zoomHandle.setX(qMax(0., length));
        if (length < 0) {
            m_zoomHandle.setY(qMin(1.0, (scaledPos + zoomRange / 2) / maxWidth - length));
        } else {
            m_zoomHandle.setY(qMin(1.0, (scaledPos + zoomRange / 2) / maxWidth));
        }
        update();
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
616
    int change = event->angleDelta().y() > 0 ? -1 : 1;
617
    int pos = qBound(0, m_position + change, m_duration - 1);
618
    emit seekToPos(pos);
619
620
621
622
623
624
625
}

void KeyframeView::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QStylePainter p(this);
626
627
    int maxWidth = width() - 2 * m_offset;
    m_scale = maxWidth / (double)(m_duration - 1);
628
    int headOffset = m_lineHeight / 2;
629
    int offset = pCore->getItemIn(m_model->getOwnerId());
630
    m_zoomStart = m_zoomHandle.x() * maxWidth;
631
632
    m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
    int zoomEnd = qCeil(m_zoomHandle.y() * maxWidth);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
    /* ticks */
    double fps = pCore->getCurrentFps();
    int displayedLength = m_duration / m_zoomFactor / fps;
    double factor = 1;
    if (displayedLength < 2) {
        // 1 frame tick
    } else if (displayedLength < 30 ) {
        // 1 sec tick
        factor = fps;
    } else if (displayedLength < 150) {
        // 5 sec tick
        factor = 5 * fps;
    } else if (displayedLength < 300) {
        // 10 sec tick
        factor = 10 * fps;
    } else if (displayedLength < 900) {
        // 30 sec tick
        factor = 30 * fps;
    } else if (displayedLength < 1800) {
        // 1 min. tick
        factor = 60 * fps;
    } else if (displayedLength < 9000) {
        // 5 min tick
        factor = 300 * fps;
    } else if (displayedLength < 18000) {
        // 10 min tick
        factor = 600 * fps;
    } else {
        // 30 min tick
        factor = 1800 * fps;
    }

    // Position of left border in frames
    double tickOffset = m_zoomStart * m_zoomFactor;
    double frameSize = factor * m_scale * m_zoomFactor;
    int base = tickOffset / frameSize;
    tickOffset = frameSize - (tickOffset - (base * frameSize));
    // Draw frame ticks
    int scaledTick = 0;
    for (int i = 0; i < maxWidth / frameSize; i++) {
        scaledTick = m_offset + (i * frameSize) + tickOffset;
        if (scaledTick >= maxWidth + m_offset) {
            break;
        }
        p.drawLine(QPointF(scaledTick , m_lineHeight + 1), QPointF(scaledTick, m_lineHeight - 3));
    }

680
681
682
683
684

    /*
     * keyframes
     */
    for (const auto &keyframe : *m_model.get()) {
685
        int pos = keyframe.first.frames(fps) - offset;
686
        if (pos < 0) continue;
687
688
689
690
        double scaledPos = pos * m_scale;
        if (scaledPos < m_zoomStart || qFloor(scaledPos) > zoomEnd) {
            continue;
        }
691
692
693
        if (pos == m_currentKeyframe) {
            p.setBrush(Qt::red);
        } else if (m_selectedKeyframes.contains(pos)) {
694
            p.setBrush(Qt::darkRed);
695
        } else if (pos == m_currentKeyframe || pos == m_hoverKeyframe) {
696
            p.setBrush(m_colSelected);
697
698
699
        } else {
            p.setBrush(m_colKeyframe);
        }
700
701
        scaledPos -= m_zoomStart;
        scaledPos *= m_zoomFactor;
702
703
        scaledPos += m_offset;
        p.drawLine(QPointF(scaledPos, headOffset), QPointF(scaledPos, m_lineHeight - 1));
704
        switch (keyframe.second.first) {
Nicolas Carion's avatar
Nicolas Carion committed
705
706
707
708
709
710
711
712
713
714
715
716
717
        case KeyframeType::Linear: {
            QPolygonF position = QPolygonF() << QPointF(-headOffset / 2.0, headOffset / 2.0) << QPointF(0, 0) << QPointF(headOffset / 2.0, headOffset / 2.0)
                                             << QPointF(0, headOffset);
            position.translate(scaledPos, 0);
            p.drawPolygon(position);
            break;
        }
        case KeyframeType::Discrete:
            p.drawRect(QRectF(scaledPos - headOffset / 2.0, 0, headOffset, headOffset));
            break;
        default:
            p.drawEllipse(QRectF(scaledPos - headOffset / 2.0, 0, headOffset, headOffset));
            break;
718
        }
719
720
721
    }

    p.setPen(palette().dark().color());
722

723
724
725
726
    /*
     * Time-"line"
     */
    p.setPen(m_colKeyframe);
727
    p.drawLine(m_offset, m_lineHeight, width() - m_offset, m_lineHeight);
728
729
    p.drawLine(m_offset, m_lineHeight - headOffset / 2, m_offset, m_lineHeight + headOffset / 2);
    p.drawLine(width() - m_offset, m_lineHeight - headOffset / 2, width() - m_offset, m_lineHeight + headOffset / 2);
730
731

    /*
732
     * current position cursor
733
     */
734
    if (m_position >= 0 && m_position < m_duration) {
735
        double scaledPos = m_position * m_scale;
736
        if (scaledPos >= m_zoomStart && qFloor(scaledPos) <= zoomEnd) {
737
738
            scaledPos -= m_zoomStart;
            scaledPos *= m_zoomFactor;
739
740
            scaledPos += m_offset;
            QPolygon pa(3);
741
742
            int cursorwidth = (m_zoomHeight - m_lineHeight) / 1.8;
            QPolygonF position = QPolygonF() << QPointF(-cursorwidth, m_zoomHeight - 3) << QPointF(cursorwidth, m_zoomHeight - 3) << QPointF(0, m_lineHeight + 1);
743
744
745
746
747
            position.translate(scaledPos, 0);
            p.setBrush(m_colKeyframe);
            p.drawPolygon(position);
        }
    }
748
749
750
751
752
753
754
755
    // Rubberband
    if (m_clickEnd >= 0) {
        int min = (qMin(m_clickPoint, m_clickEnd) * m_scale - m_zoomStart) * m_zoomFactor + m_offset;
        int max = (qMax(m_clickPoint, m_clickEnd) * m_scale - m_zoomStart) * m_zoomFactor + m_offset;
        p.setOpacity(0.5);
        p.fillRect(QRect(min, 0, max - min, m_lineHeight), palette().highlight());
        p.setOpacity(1);
    }
756

757
758
759
760
761
762
    // Zoom bar
    p.setPen(Qt::NoPen);
    p.setBrush(palette().mid());
    p.drawRoundedRect(m_offset, m_zoomHeight + 3, width() - 2 * m_offset, m_size - m_zoomHeight - 3, m_lineHeight / 5, m_lineHeight / 5);
    p.setBrush(palette().highlight());
    p.drawRoundedRect(m_offset + (width() - m_offset) * m_zoomHandle.x(), m_zoomHeight + 3, (width() - 2 * m_offset) * (m_zoomHandle.y() - m_zoomHandle.x()), m_size - m_zoomHeight - 3, m_lineHeight / 5, m_lineHeight / 5);
763
}
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800


void KeyframeView::copyCurrentValue(QModelIndex ix, const  QString paramName)
{
    const QString val = m_model->getInterpolatedValue(m_position, ix).toString();
    QString newVal;
    const QStringList vals = val.split(QLatin1Char(' '));
    int offset = pCore->getItemIn(m_model->getOwnerId());
    qDebug()<<"=== COPYING VALS: "<<val<<", PARAM NAME_ "<<paramName;
    QUndoCommand *parentCommand = new QUndoCommand();
    for (int kf : m_selectedKeyframes) {
        QString oldValue = m_model->getInterpolatedValue(kf, ix).toString();
        QStringList oldVals = oldValue.split(QLatin1Char(' '));
        if (paramName == QLatin1String("spinX")) {
            oldVals[0] = vals.at(0);
            newVal = oldVals.join(QLatin1Char(' '));
            parentCommand->setText(i18n("Update keyframes X position"));
        } else if (paramName == QLatin1String("spinY")) {
            oldVals[1] = vals.at(1);
            newVal = oldVals.join(QLatin1Char(' '));
            parentCommand->setText(i18n("Update keyframes Y position"));
        } else if (paramName == QLatin1String("spinW")) {
            oldVals[2] = vals.at(2);
            newVal = oldVals.join(QLatin1Char(' '));
            parentCommand->setText(i18n("Update keyframes width"));
        } else if (paramName == QLatin1String("spinH")) {
            oldVals[3] = vals.at(3);
            newVal = oldVals.join(QLatin1Char(' '));
            parentCommand->setText(i18n("Update keyframes height"));
        } else if (paramName == QLatin1String("spinO")) {
            oldVals[4] = vals.at(4);
            newVal = oldVals.join(QLatin1Char(' '));
            parentCommand->setText(i18n("Update keyframes opacity"));
        } else {
            newVal = val;
            parentCommand->setText(i18n("Update keyframes value"));
        }
801
802
803
804
        bool result = m_model->updateKeyframe(GenTime(kf + offset, pCore->getCurrentFps()), newVal, ix, parentCommand);
        if (result) {
            pCore->displayMessage(i18n("Keyframe value copied"), InformationMessage);
        }
805
806
807
    }
    pCore->pushUndo(parentCommand);
}