timeremap.cpp 74.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/***************************************************************************
 *   Copyright (C) 2021 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
 *                                                                         *
 *   This program 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.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

#include "timeremap.h"

#include "core.h"
#include "doc/kthumb.h"
24
#include "timecodedisplay.h"
25
26
27
#include "kdenlivesettings.h"
#include "bin/projectclip.h"
#include "project/projectmanager.h"
28
#include "monitor/monitor.h"
29
30
31
32
#include "profiles/profilemodel.hpp"
#include "mainwindow.h"
#include "timeline2/view/timelinewidget.h"
#include "timeline2/view/timelinecontroller.h"
33
#include "timeline2/model/groupsmodel.hpp"
34
#include "macros.hpp"
35
36
37
38
39

#include "kdenlive_debug.h"
#include <QFontDatabase>
#include <QWheelEvent>
#include <QStylePainter>
40
#include <QtMath>
41
42
43
44
45
46
47

#include <KColorScheme>
#include "klocalizedstring.h"

RemapView::RemapView(QWidget *parent)
    : QWidget(parent)
    , m_duration(1)
48
49
    , m_position(0)
    , m_scale(1.)
50
51
    , m_zoomFactor(1)
    , m_zoomStart(0)
52
53
    , m_zoomHandle(0,1)
    , m_moveKeyframeMode(NoMove)
54
    , m_clip(nullptr)
55
    , m_service(nullptr)
56
57
    , m_clickPoint(-1)
    , m_moveNext(true)
58
59
{
    setMouseTracking(true);
60
    setMinimumSize(QSize(150, 80));
61
62
    setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
    int size = QFontInfo(font()).pixelSize() * 3;
63
    setFixedHeight(size * 4);
64
    // Reference height of the rulers
65
    m_lineHeight = int(size / 2.);
66
    // Height of the zoom bar
67
    m_zoomHeight = m_lineHeight * 0.5;
68
69
    // Center of the view
    m_centerPos = (size * 4 - m_zoomHeight - 2) / 2 - 1;
70
    m_offset = qCeil(m_lineHeight / 4);
71
72
    // Bottom of the view (just above zoombar)
    m_bottomView = height() - m_zoomHeight - 2;
73
    setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
74
75
76
77
    int maxWidth = width() - (2 * m_offset);
    m_scale = maxWidth / double(qMax(1, m_duration - 1));
    m_zoomStart = m_zoomHandle.x() * maxWidth;
    m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
78
79
80
    timer.setInterval(500);
    timer.setSingleShot(true);
    connect(&timer, &QTimer::timeout, this, &RemapView::reloadProducer);
81
82
83
84
}

void RemapView::updateInPos(int pos)
{
85
86
87
88
89
90
91
92
93
94
95
96
    if (m_currentKeyframe.second > -1) {
        if (m_moveNext) {
            int offset = pos - m_currentKeyframe.second;
            QMap<int, int>::iterator it = m_keyframes.find(m_currentKeyframe.first);
            while (it != m_keyframes.end()) {
                m_keyframes.insert(it.key(), it.value() + offset);
                it++;
            }
            m_currentKeyframe.second = pos;
        } else {
            m_currentKeyframe.second = pos;
            m_keyframes.insert(m_currentKeyframe.first, m_currentKeyframe.second);
97
        }
98
99
100
        slotSetPosition(pos);
        std::pair<double,double>speeds = getSpeed(m_currentKeyframe);
        emit updateSpeeds(speeds);
101
        updateKeyframes(true);
102
103
104
105
106
107
        update();
    }
}

void RemapView::updateOutPos(int pos)
{
108
109
110
    qDebug()<<"=== MOVING TO POS: "<<pos<<", CURRENT KFR: "<<m_currentKeyframe.first;
    if (m_currentKeyframe.first > -1) {
        if (m_keyframes.contains(pos)) {
111
            // Cannot move kfr over an existing one
112
            qDebug()<<"==== KEYFRAME ALREADY EXISTS AT: "<<pos;
113
114
            return;
        }
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
        QMap<int,int>updated;
        int offset = pos - m_currentKeyframe.first;
        if (m_moveNext) {
            QMap<int, int>::iterator it = m_keyframes.find(m_currentKeyframe.first);
            while (it != m_keyframes.end()) {
                updated.insert(it.key(), it.value());
                it++;
            }
            m_currentKeyframe.first = pos;
        } else {
            m_keyframes.remove(m_currentKeyframe.first);
            m_currentKeyframe.first = pos;
            m_keyframes.insert(m_currentKeyframe.first, m_currentKeyframe.second);
        }
        m_selectedKeyframes = {m_currentKeyframe};
        QMapIterator<int, int> i(updated);
        while (i.hasNext()) {
            i.next();
            m_keyframes.remove(i.key());
        }
        i.toFront();
        while (i.hasNext()) {
            i.next();
            m_keyframes.insert(i.key() + offset, i.value());
        }
        m_bottomPosition = pos - m_inFrame;
        std::pair<double,double>speeds = getSpeed(m_currentKeyframe);
        emit updateSpeeds(speeds);
143
        updateKeyframes(true);
144
145
        update();
    }
146
147
}

148
149
150
151
152
153
int RemapView::remapDuration() const
{
    int maxDuration = 0;
    QMapIterator<int, int> i(m_keyframes);
    while (i.hasNext()) {
        i.next();
154
155
        if (i.key() > maxDuration) {
            maxDuration = i.key();
156
157
        }
    }
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    return maxDuration - m_inFrame + 1;
}

int RemapView::remapMax() const
{
    int maxDuration = 0;
    QMapIterator<int, int> i(m_keyframes);
    while (i.hasNext()) {
        i.next();
        if (i.value() > maxDuration) {
            maxDuration = i.value();
        }
        if (i.key() > maxDuration) {
            maxDuration = i.key();
        }
    }
    return maxDuration - m_inFrame + 1;
}

bool RemapView::movingKeyframe() const
{
    return m_moveKeyframeMode == BottomMove;
180
181
182
}

void RemapView::setBinClipDuration(std::shared_ptr<ProjectClip> clip, int duration)
183
{
184
    m_clip = clip;
185
186
    m_service = clip->originalProducer();
    m_duration = duration;
187
188
    int maxWidth = width() - (2 * m_offset);
    m_scale = maxWidth / double(qMax(1, m_duration - 1));
189
190
191
192
193
194
    m_currentKeyframe = m_currentKeyframeOriginal = {-1,-1};
}

void RemapView::setDuration(std::shared_ptr<Mlt::Producer> service, int duration)
{
    m_clip = nullptr;
195
196
197
198
199
    if (duration < 0) {
        // reset
        m_service = nullptr;
        m_inFrame = 0;
        m_duration = -1;
200
        m_selectedKeyframes.clear();
201
202
203
204
205
206
        return;
    }
    if (service) {
        m_service = service;
    }
    bool keyframeAdded = false;
207
    if (m_duration > 0 && m_service && !m_keyframes.isEmpty()) {
208
209
210
211
212
213
214
215
216
217
        if (duration > m_duration) {
            // The clip was resized, ensure we have a keyframe at the end of the clip will freeze at last keyframe
            QMap<int, int>::const_iterator it = m_keyframes.constEnd();
            it--;
            int lastKeyframePos = it.key();
            int lastKeyframeValue = it.value();
            int lastPos = m_inFrame + duration - 1;
            if (lastPos > lastKeyframePos) {
                keyframeAdded = true;
                std::pair<double,double>speeds = getSpeed({lastKeyframePos,lastKeyframeValue});
218
219
220
221
222
                // Move last keyframe
                it--;
                int updatedVal = it.value() + ((lastPos - it.key()) * speeds.first);
                m_keyframes.remove(lastKeyframePos);
                m_keyframes.insert(lastPos, updatedVal);
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
            }
        } else if (duration < m_duration) {
            keyframeAdded = true;
            QMap<int, int>::const_iterator it = m_keyframes.constEnd();
            it--;
            int lastKeyframePos = it.key();
            int lastKeyframeValue = it.value();
            int lastPos = m_inFrame + duration - 1;
            std::pair<double,double>speeds = getSpeed({lastKeyframePos,lastKeyframeValue});
            if (lastKeyframePos == m_inFrame + m_duration - 1) {
                // Move last keyframe
                it--;
                //TODO: check we still have a keyframe at it pos
                int updatedVal = it.value() + ((lastPos - it.key()) / speeds.first);
                m_keyframes.remove(lastKeyframePos);
                m_keyframes.insert(lastPos, updatedVal);
            }
        }
    }
    if (service == nullptr) {
        // We are updating an existing remap effect due to duration change
        m_duration = qMax(duration, remapMax());
    } else {
        m_duration = duration;
    }
    if (service != nullptr) {
        m_inFrame = m_service->get_in();
    }
251
252
    int maxWidth = width() - (2 * m_offset);
    m_scale = maxWidth / double(qMax(1, m_duration - 1));
253
254
    m_zoomStart = m_zoomHandle.x() * maxWidth;
    m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
255
256
257
258
    if (!m_keyframes.contains(m_currentKeyframe.first)) {
        m_currentKeyframe = m_currentKeyframeOriginal = {-1,-1};
        update();
    }
259
260
261
    if (keyframeAdded) {
        updateKeyframes(false);
    }
262
263
}

264
void RemapView::loadKeyframes(const QString &mapData)
265
266
267
{
    m_keyframes.clear();
    if (mapData.isEmpty()) {
268
269
270
271
272
273
        if (m_inFrame > 0) {
            m_keyframes.insert(0, 0);
        }
        m_keyframes.insert(m_inFrame, m_inFrame);
        m_keyframes.insert(m_inFrame + m_duration - 1, m_inFrame + m_duration - 1);
        updateKeyframes(true);
274
275
276
    } else {
        QStringList str = mapData.split(QLatin1Char(';'));
        for (auto &s : str) {
277
            int pos = m_service->time_to_frames(s.section(QLatin1Char('='), 0, 0).toUtf8().constData());
278
            int val = GenTime(s.section(QLatin1Char('='), 1).toDouble()).frames(pCore->getCurrentFps());
279
            m_keyframes.insert(pos, val);
280
281
            m_duration = qMax(m_duration, pos - m_inFrame);
            m_duration = qMax(m_duration, val - m_inFrame);
282
        }
283
284
285
286
        int maxWidth = width() - (2 * m_offset);
        m_scale = maxWidth / double(qMax(1, m_duration - 1));
        m_zoomStart = m_zoomHandle.x() * maxWidth;
        m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
287
        emit updateMaxDuration(m_duration);
288
289
290
291
292
293
294
295
296
        if (m_keyframes.contains(m_currentKeyframe.first)) {
            emit atKeyframe(true);
            std::pair<double,double>speeds = getSpeed(m_currentKeyframe);
            emit selectedKf(m_currentKeyframe, speeds);
        } else {
            emit atKeyframe(false);
            m_currentKeyframe = {-1,-1};
            emit selectedKf(m_currentKeyframe, {-1,-1});
        }
297
298
299
300
301
302
303
304
305
306
307
    }
    update();
}

void RemapView::mouseMoveEvent(QMouseEvent *event)
{
    event->accept();
    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 = int(((double(event->x()) - m_offset) / zoomFactor + zoomStart ) / m_scale);
308
    int realPos = qMax(m_inFrame, pos + m_inFrame);
309
310
    pos = qBound(0, pos, m_duration - 1);
    GenTime position(pos, pCore->getCurrentFps());
311
312
    if (event->buttons() == Qt::NoButton) {
        bool hoverKeyframe = false;
313
314
        if (event->y() < 2 * m_lineHeight && event->y() > m_lineHeight) {
            // mouse move in top keyframes area
315
316
            std::pair<int,int> keyframe = getClosestKeyframe(pos + m_inFrame);
            if (keyframe.first > -1 && qAbs(keyframe.second - pos - m_inFrame) * m_scale * m_zoomFactor < QApplication::startDragDistance()) {
317
318
                hoverKeyframe = true;
            }
319
320
        } else if (event->y() > m_bottomView - 2 * m_lineHeight && event->y() < m_bottomView - m_lineHeight) {
            // move in bottom keyframe area
321
322
            std::pair<int,int> keyframe = getClosestKeyframe(pos + m_inFrame, true);
            if (keyframe.first > -1 && qAbs(keyframe.first - pos - m_inFrame) * m_scale * m_zoomFactor < QApplication::startDragDistance()) {
323
324
325
326
327
328
329
330
331
                hoverKeyframe = true;
            }
        }
        if (hoverKeyframe) {
            setCursor(Qt::PointingHandCursor);
        } else {
            setCursor(Qt::ArrowCursor);
        }
    } else if ((event->buttons() & Qt::LeftButton) != 0u) {
332
333
334
335
        if (m_hoverZoomIn || m_hoverZoomOut || m_hoverZoom) {
            // Moving zoom handles
            if (m_hoverZoomIn) {
                m_zoomHandle.setX(qMin(qMax(0., double(event->x() - m_offset) / (width() - 2 * m_offset)), m_zoomHandle.y() - 0.015));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
336
337
338
                int maxWidth = width() - (2 * m_offset);
                m_zoomStart = m_zoomHandle.x() * maxWidth;
                m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
339
340
341
342
343
                update();
                return;
            }
            if (m_hoverZoomOut) {
                m_zoomHandle.setY(qMax(qMin(1., double(event->x() - m_offset) / (width() - 2 * m_offset)), m_zoomHandle.x() + 0.015));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
344
345
346
                int maxWidth = width() - (2 * m_offset);
                m_zoomStart = m_zoomHandle.x() * maxWidth;
                m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
                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);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
366
367
368
                int maxWidth = width() - (2 * m_offset);
                m_zoomStart = m_zoomHandle.x() * maxWidth;
                m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
369
370
371
372
373
                update();
            }
            return;
        }
        //qDebug()<<"=== MOVING MOUSE: "<<pos<<" = "<<m_currentKeyframe<<", MOVE KFMODE: "<<m_moveKeyframeMode;
374
        if ((m_currentKeyframe.first == pos + m_inFrame && m_moveKeyframeMode == TopMove) || (m_currentKeyframe.second == pos + m_inFrame && m_moveKeyframeMode == BottomMove)) {
375
376
377
            return;
        }
        if (m_currentKeyframe.first >= 0 && m_moveKeyframeMode != NoMove) {
378
379
            if (m_moveKeyframeMode == BottomMove) {
                // Moving bottom keyframe
380
381
                if (!m_keyframes.contains(pos + m_inFrame)) {
                    int delta = pos + m_inFrame - m_currentKeyframe.first;
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
                    // Check that the move is possible
                    QMapIterator<int, int> i(m_selectedKeyframes);
                    while (i.hasNext()) {
                        i.next();
                        int updatedPos = i.key() + delta;
                        if (!m_selectedKeyframes.contains(updatedPos) && m_keyframes.contains(updatedPos)) {
                            // Don't allow moving over another keyframe
                            qDebug()<<"== MOVE ABORTED; OVERLAPPING EXISTING";
                            return;
                        }
                    }
                    i.toFront();
                    QMap <int,int> updated;
                    while (i.hasNext()) {
                        i.next();
397
398
399
                        if (i.key() < m_currentKeyframe.first) {
                            continue;
                        }
400
                        //qDebug()<<"=== MOVING KFR: "<<i.key()<<" > "<<(i.key() + delta);
401
                        //m_keyframes.insert(i.key() + delta, i.value());
402
403
404
                        updated.insert(i.key() + delta, i.value());
                        m_keyframes.remove(i.key());
                    }
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
                    QMapIterator<int, int> j(updated);
                    while (j.hasNext()) {
                        j.next();
                        m_keyframes.insert(j.key(), j.value());
                    }
                    QMap<int,int> updatedSelection;
                    QMapIterator<int, int> k(m_previousSelection);
                    while (k.hasNext()) {
                        k.next();
                        updatedSelection.insert(k.key() + delta, k.value());
                    }
                    m_previousSelection = updatedSelection;
                    m_currentKeyframe.first += delta;
                    std::pair<double,double>speeds = getSpeed(m_currentKeyframe);
                    emit selectedKf(m_currentKeyframe, speeds);

421
                    m_selectedKeyframes = updated;
422
                    emit seekToPos(-1, pos);
423
                    updateKeyframes(true);
424
                    update();
425
                    return;
426
427
428
                } else {
                    qDebug()<<"=== KEYFRAME :"<< pos<<" ALREADY EXISTS";
                }
429
430
            } else if (m_moveKeyframeMode == TopMove) {
                // Moving top keyframe
431
                auto kfrValues = m_keyframes.values();
432
                //pos = GenTime(m_remapLink->anim_get_double("map", pos)).frames(pCore->getCurrentFps());
433
434
435
436
437
438
439
440
441
442
443
444

                int delta = realPos - m_currentKeyframe.second;
                // Check that the move is possible
                auto selectedValues = m_selectedKeyframes.values();
                QMapIterator<int, int> i(m_selectedKeyframes);
                QMap<int,int>updated;
                while (i.hasNext()) {
                    i.next();
                    m_keyframes.insert(i.key(), i.value() + delta);
                    updated.insert(i.key(), i.value() + delta);
                    if (i.value() == m_currentKeyframe.second) {
                        m_currentKeyframe.second = realPos;
445
446
                    }
                }
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
                QMap<int,int> updatedSelection;
                QMapIterator<int, int> k(m_previousSelection);
                while (k.hasNext()) {
                    k.next();
                    updatedSelection.insert(k.key(), k.value() + delta);
                }
                m_previousSelection = updatedSelection;
                std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
                emit selectedKf(m_currentKeyframe, speeds);
                m_selectedKeyframes = updated;
                slotSetPosition(pos + m_inFrame);
                emit seekToPos(pos + m_inFrame, -1);
                updateKeyframes(true);
                update();
                return;
462
463
464
465
            }
        }
        // Rubberband selection
        if (m_clickPoint >= 0) {
466
            m_clickEnd = pos + m_inFrame;
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
            int min = qMin(m_clickPoint, m_clickEnd);
            int max = qMax(m_clickPoint, m_clickEnd);
            min = qMax(1, min);
            m_selectedKeyframes.clear();
            m_currentKeyframeOriginal = m_currentKeyframe = {-1,-1};
            QMapIterator<int, int> i(m_keyframes);
            while (i.hasNext()) {
                i.next();
                if (i.key() > min && i.key() <= max) {
                    m_selectedKeyframes.insert(i.key(), i.value());
                }
            }
            if (!m_selectedKeyframes.isEmpty()) {
                m_currentKeyframe = {m_selectedKeyframes.firstKey(), m_selectedKeyframes.value(m_selectedKeyframes.firstKey())};
                m_currentKeyframeOriginal = m_currentKeyframe;
            }
            update();
            return;
        }

487
        if (m_moveKeyframeMode == CursorMove || (event->y() < m_centerPos)) {
488
            if (pos != m_position) {
489
490
                slotSetPosition(pos + m_inFrame);
                emit seekToPos(pos + m_inFrame, -1);
491
492
            }
        }
493
        if (m_moveKeyframeMode == CursorMoveBottom || (event->y() > m_centerPos)) {
494
495
496
            if (pos != m_bottomPosition) {
                m_bottomPosition = pos;
                pos = GenTime(m_remapLink->anim_get_double("map", pos + m_inFrame)).frames(pCore->getCurrentFps());
497
                slotSetPosition(pos);
498
                emit seekToPos(-1, m_bottomPosition);
499
500
501
502
            }
        }
        return;
    }
503
    if (event->y() > m_bottomView) {
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
        // 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;
            update();
            return;
        }
    }

    if (m_hoverKeyframe.first != -1 || m_hoverZoomOut || m_hoverZoomIn || m_hoverZoom) {
        m_hoverKeyframe.first = -1;
        m_hoverZoomOut = false;
        m_hoverZoomIn = false;
        m_hoverZoom = false;
        setCursor(Qt::ArrowCursor);
        update();
    }
}

541
542
543
544
545
int RemapView::position() const
{
    return m_position;
}

546
547
548
549
550
551
void RemapView::centerCurrentKeyframe()
{
    if (m_currentKeyframe.first == -1) {
        // No keyframe selected, abort
        return;
    }
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
    //std::pair<int,int> range = getRange(m_currentKeyframe);
    QMap<int,int>nextKeyframes;
    if (m_moveNext) {
        QMap<int, int>::iterator it = m_keyframes.find(m_currentKeyframe.first);
        if (*it != m_keyframes.last() && it != m_keyframes.end()) {
            it++;
            while (it != m_keyframes.end()) {
                nextKeyframes.insert(it.key(), it.value());
                it++;
            }
        }
    }
    m_keyframes.remove(m_currentKeyframe.first);
    int offset = m_bottomPosition + m_inFrame - m_currentKeyframe.first;
    m_currentKeyframe.first = m_bottomPosition + m_inFrame;
    if (offset == 0) {
        // no move
        return;
    }
    m_keyframes.insert(m_currentKeyframe.first, m_currentKeyframe.second);
    QMapIterator<int, int> i(nextKeyframes);
    while (i.hasNext()) {
        i.next();
        m_keyframes.remove(i.key());
    }
    i.toFront();
    while (i.hasNext()) {
        i.next();
        m_keyframes.insert(i.key() + offset, i.value());
    }
    std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
    emit selectedKf(m_currentKeyframe, speeds);
    emit atKeyframe(true);
    updateKeyframes(true);
    update();
}

void RemapView::centerCurrentTopKeyframe()
{
    if (m_currentKeyframe.first == -1) {
        // No keyframe selected, abort
        return;
    }
    //std::pair<int,int> range = getRange(m_currentKeyframe);
    QMap<int,int>nextKeyframes;
    if (m_moveNext) {
        QMap<int, int>::iterator it = m_keyframes.find(m_currentKeyframe.first);
        if (*it != m_keyframes.last() && it != m_keyframes.end()) {
            it++;
            while (it != m_keyframes.end()) {
                nextKeyframes.insert(it.key(), it.value());
                it++;
            }
        }
    }
    int offset = m_position + m_inFrame - m_currentKeyframe.second;
608
609
    m_currentKeyframe.second = m_position + m_inFrame;
    m_keyframes.insert(m_currentKeyframe.first, m_currentKeyframe.second);
610
611
612
613
614
    QMapIterator<int, int> i(nextKeyframes);
    while (i.hasNext()) {
        i.next();
        m_keyframes.insert(i.key(), i.value() + offset);
    }
615
616
617
618
619
620
621
    std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
    emit selectedKf(m_currentKeyframe, speeds);
    emit atKeyframe(true);
    updateKeyframes(true);
    update();
}

622
std::pair<int,int> RemapView::getClosestKeyframe(int pos, bool bottomKeyframe) const
623
624
{
    int deltaMin = -1;
625
    std::pair<int,int> result = {-1,-1};
626
627
628
    QMapIterator<int, int> i(m_keyframes);
    while (i.hasNext()) {
        i.next();
629
        int val = bottomKeyframe ? i.key() : i.value();
630
631
632
        int delta = qAbs(val - pos);
        if (deltaMin == -1 || delta < deltaMin) {
            deltaMin = delta;
633
            result = {i.key(), i.value()};
634
635
        }
    }
636
    return result;
637
638
639
640
641
}

void RemapView::mouseReleaseEvent(QMouseEvent *event)
{
    event->accept();
642
643
644
645
646
    if (m_moveKeyframeMode == TopMove || m_moveKeyframeMode == BottomMove) {
        // Restore original selection
        m_selectedKeyframes = m_previousSelection;
        update();
    }
647
    m_moveKeyframeMode = NoMove;
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
    if (m_keyframesOrigin != m_keyframes) {
        Fun undo = [this, kfr = m_keyframesOrigin]() {
            m_keyframes = kfr;
            if (m_keyframes.contains(m_currentKeyframe.first)) {
                emit atKeyframe(true);
                std::pair<double,double>speeds = getSpeed(m_currentKeyframe);
                emit selectedKf(m_currentKeyframe, speeds);
            } else {
                emit atKeyframe(false);
                m_currentKeyframe = {-1,-1};
                emit selectedKf(m_currentKeyframe, {-1,-1});
            }
            update();
            return true;
        };
        Fun redo = [this, kfr2 = m_keyframes]() {
            m_keyframes = kfr2;
            if (m_keyframes.contains(m_currentKeyframe.first)) {
                emit atKeyframe(true);
                std::pair<double,double>speeds = getSpeed(m_currentKeyframe);
                emit selectedKf(m_currentKeyframe, speeds);
            } else {
                emit atKeyframe(false);
                m_currentKeyframe = {-1,-1};
                emit selectedKf(m_currentKeyframe, {-1,-1});
            }
            update();
            return true;
        };
        pCore->pushUndo(undo, redo, i18n("Edit Timeremap keyframes"));
    }
679
680
681
682
683
684
685
686
687
688
689
690
    qDebug()<<"=== MOUSE RELEASE!!!!!!!!!!!!!";
}

void RemapView::mousePressEvent(QMouseEvent *event)
{
    event->accept();
    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 = int(((event->x() - m_offset) / zoomFactor + zoomStart ) / m_scale);
    pos = qBound(0, pos, m_duration - 1);
    m_moveKeyframeMode = NoMove;
691
    m_keyframesOrigin = m_keyframes;
692
    if (event->button() == Qt::LeftButton) {
693
694
        if (event->y() < m_centerPos) {
            // mouse click in top area
695
            if (event->modifiers() & Qt::ShiftModifier) {
696
                m_clickPoint = pos + m_inFrame;
697
698
                return;
            }
699
            if (event->y() < 2 *m_lineHeight && event->y() > m_lineHeight) {
700
701
702
703
                std::pair<int,int> keyframe = getClosestKeyframe(pos + m_inFrame);
                if (keyframe.first > -1 && qAbs(keyframe.second - (pos + m_inFrame)) * m_scale * m_zoomFactor < QApplication::startDragDistance()) {
                    // Clicked on a top keyframe
                    m_currentKeyframeOriginal = keyframe;
704
705
706
707
708
709
710
711
712
                    if (event->modifiers() & Qt::ControlModifier) {
                        if (m_selectedKeyframes.contains(m_currentKeyframeOriginal.first)) {
                            m_selectedKeyframes.remove(m_currentKeyframeOriginal.first);
                            m_currentKeyframeOriginal.first = -1;
                        } else {
                            m_selectedKeyframes.insert(m_currentKeyframeOriginal.first, m_currentKeyframeOriginal.second);
                        }
                    } else if (!m_selectedKeyframes.contains(m_currentKeyframeOriginal.first)) {
                        m_selectedKeyframes = {m_currentKeyframeOriginal};
713
                    }
714
715
716
717
718
719
720
                    // Select and seek to keyframe
                    m_currentKeyframe = m_currentKeyframeOriginal;
                    // Calculate speeds
                    std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
                    emit selectedKf(m_currentKeyframe, speeds);
                    if (m_currentKeyframeOriginal.first > -1) {
                        m_moveKeyframeMode = TopMove;
721
722
723
724
725
726
727
728
729
730
                        m_previousSelection = m_selectedKeyframes;
                        if (m_moveNext) {
                            QMapIterator<int, int> i(m_keyframes);
                            while (i.hasNext()) {
                                i.next();
                                if (i.key() > m_currentKeyframeOriginal.first) {
                                    m_selectedKeyframes.insert(i.key(), i.value());
                                }
                            }
                        }
731
                        if (KdenliveSettings::keyframeseek()) {
732
733
734
                            slotSetPosition(m_currentKeyframeOriginal.second);
                            m_bottomPosition = m_currentKeyframeOriginal.first - m_inFrame;
                            emit seekToPos(m_currentKeyframeOriginal.second, m_bottomPosition);
735
736
737
                        } else {
                            update();
                        }
738
739
740
                    } else {
                        update();
                    }
741
                    return;
742
743
744
                }
            }
            // no keyframe next to mouse
745
746
747
748
749
750
751
752
753
754
            //m_selectedKeyframes.clear();
            //m_currentKeyframe = m_currentKeyframeOriginal = {-1,-1};
            if (pos != m_position) {
                m_moveKeyframeMode = CursorMove;
                slotSetPosition(pos + m_inFrame);
                emit seekToPos(pos + m_inFrame, -1);
                update();
            }
            return;
        } else if (event->y() > m_bottomView) {
755
756
757
758
759
            // click on zoom area
            if (m_hoverZoom) {
                m_clickOffset = (double(event->x()) - m_offset) / (width() - 2 * m_offset);
            }
            return;
760
761
        } else if (event->y() > m_centerPos && event->y() < m_bottomView) {
            // click in bottom area
762
            if (event->modifiers() & Qt::ShiftModifier) {
763
                m_clickPoint = pos + m_inFrame;
764
765
                return;
            }
766
            if (event->y() > (m_bottomView - 2 * m_lineHeight) && (event->y() < m_bottomView - m_lineHeight)) {
767
768
769
                std::pair<int,int> keyframe = getClosestKeyframe(pos + m_inFrame, true);
                if (keyframe.first > -1 && qAbs(keyframe.first - (pos + m_inFrame)) * m_scale * m_zoomFactor < QApplication::startDragDistance()) {
                    m_currentKeyframeOriginal = keyframe;
770
                    if (event->modifiers() & Qt::ControlModifier) {
771
                        if (m_selectedKeyframes.contains(m_currentKeyframeOriginal.first)) {
772
773
774
775
776
                            m_selectedKeyframes.remove(m_currentKeyframeOriginal.first);
                            m_currentKeyframeOriginal.second = -1;
                        } else {
                            m_selectedKeyframes.insert(m_currentKeyframeOriginal.first, m_currentKeyframeOriginal.second);
                        }
777
                    } else if (!m_selectedKeyframes.contains(m_currentKeyframeOriginal.first)) {
778
                        m_selectedKeyframes = {m_currentKeyframeOriginal};
779
                    }
780
781
782
783
                    // Select and seek to keyframe
                    m_currentKeyframe = m_currentKeyframeOriginal;
                    std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
                    emit selectedKf(m_currentKeyframe, speeds);
784
                    if (m_currentKeyframeOriginal.first > -1) {
785
                        m_moveKeyframeMode = BottomMove;
786
787
788
789
790
791
792
793
794
795
                        m_previousSelection = m_selectedKeyframes;
                        if (m_moveNext) {
                            QMapIterator<int, int> i(m_keyframes);
                            while (i.hasNext()) {
                                i.next();
                                if (i.key() > m_currentKeyframeOriginal.first) {
                                    m_selectedKeyframes.insert(i.key(), i.value());
                                }
                            }
                        }
796
                        if (KdenliveSettings::keyframeseek()) {
797
798
799
800
                            m_bottomPosition = m_currentKeyframeOriginal.first - m_inFrame;
                            int topPos = GenTime(m_remapLink->anim_get_double("map", m_currentKeyframeOriginal.first)).frames(pCore->getCurrentFps());
                            m_position = topPos - m_inFrame;
                            emit seekToPos(topPos, m_bottomPosition);
801
802
803
                        } else {
                            update();
                        }
804
805
806
                    } else {
                        update();
                    }
807
                    return;
808
809
810
                }
            }
            // no keyframe next to mouse
811
812
            //m_selectedKeyframes.clear();
            //m_currentKeyframe = m_currentKeyframeOriginal = {-1,-1};
813
814
815
            if (pos != m_bottomPosition) {
                qDebug()<<"=== MOVING BOTTOM KFR: "<<pos<<" = "<<m_bottomPosition;
                m_bottomPosition = pos;
816
                m_moveKeyframeMode = CursorMoveBottom;
817
                //slotUpdatePosition();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
818
                emit seekToPos(-1, pos);
819
820
                update();
            }
821
822
823
824
825
826
827
            /*int topPos = GenTime(m_remapLink->anim_get_double("map", pos + m_inFrame)).frames(pCore->getCurrentFps());
            if (topPos != m_position + m_inFrame) {

                slotSetPosition(topPos);
                emit seekToPos(-1, pos);
                update();
            }*/
828
            return;
829
        }
830
    } else if (event->button() == Qt::RightButton && event->y() > m_bottomView) {
831
832
833
834
        // 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;
835
836
837
                int maxWidth = width() - (2 * m_offset);
                m_zoomStart = m_zoomHandle.x() * maxWidth;
                m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
838
839
840
841
842
843
                update();
                return;
            }
        } else {
            m_lastZoomHandle = m_zoomHandle;
            m_zoomHandle = QPointF(0, 1);
844
845
846
            int maxWidth = width() - (2 * m_offset);
            m_zoomStart = m_zoomHandle.x() * maxWidth;
            m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
847
848
849
850
851
852
853
854
855
856
            update();
            return;
        }
    }
    if (pos != m_position) {
        //emit seekToPos(pos);
        update();
    }
}

857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
void RemapView::wheelEvent(QWheelEvent *event)
{
    if (event->modifiers() & Qt::AltModifier) {
        // Alt modifier seems to invert x/y axis
        if (event->angleDelta().x() > 0) {
            goPrev();
        } else {
            goNext();
        }
        return;
    }
    if (event->modifiers() & Qt::ControlModifier) {
        int maxWidth = width() - 2 * m_offset;
        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));
        }
        m_zoomStart = m_zoomHandle.x() * maxWidth;
        m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
        update();
        return;
    }
    int change = event->angleDelta().y() > 0 ? -1 : 1;
    int pos = qBound(0, m_position + change, m_duration - 1);
    if (event->y() < m_bottomView) {
        emit seekToPos(pos + m_inFrame, -1);
    } else {
        // Wheel on zoom bar, scroll
        double pos = m_zoomHandle.x();
        double zoomWidth = m_zoomHandle.y() - pos;
        int maxWidth = width() - 2 * m_offset;
        if (event->angleDelta().y() > 0) {
            if (zoomWidth / 2 > pos) {
                pos = 0;
            } else {
                pos -= zoomWidth / 2;
            }
        } else {
            if (pos + zoomWidth + zoomWidth / 2 > 1.) {
                pos = 1. - zoomWidth;
            } else {
                pos += zoomWidth / 2;
            }
        }
        m_zoomHandle = QPointF(pos, pos + zoomWidth);
        m_zoomStart = m_zoomHandle.x() * maxWidth;
        m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
        update();
    }
}


923
void RemapView::slotSetPosition(int pos)
924
{
925
926
    if (pos != m_position + m_inFrame) {
        m_position = pos - m_inFrame;
927
        //int offset = pCore->getItemIn(m_model->getOwnerId());
928
        //emit atKeyframe(m_keyframes.contains(pos));
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
        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);
        }
        update();
    }
}

945
946
947
948
949
950
951
void RemapView::slotSetBottomPosition(int pos)
{
    qDebug()<<"=== SETTING BOTTOM POS: "<<pos;
    m_bottomPosition = pos;
    update();
}

952
953
void RemapView::goNext()
{
954
    // Seek to next keyframe
955
956
957
    QMapIterator<int, int> i(m_keyframes);
    while (i.hasNext()) {
        i.next();
958
        if (i.key() > m_bottomPosition + m_inFrame) {
959
            m_currentKeyframe = {i.key(),i.value()};
960
            m_selectedKeyframes = {m_currentKeyframe};
961
            slotSetPosition(i.key());
962
963
            m_bottomPosition = m_currentKeyframe.first - m_inFrame;
            emit seekToPos(i.value(), m_bottomPosition);
964
965
966
967
968
969
970
971
972
973
974
            std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
            emit selectedKf(m_currentKeyframe, speeds);
            break;
        }
    }
}

void RemapView::goPrev()
{
    // insert keyframe at interpolated position
    bool previousFound = false;
975
    QMap<int, int>::const_iterator it = m_keyframes.constBegin();
976
    while (it.key() < m_bottomPosition + m_inFrame && it != m_keyframes.constEnd()) {
977
978
979
980
981
        it++;
    }
    if (it != m_keyframes.constEnd()) {
        if (it != m_keyframes.constBegin()) {
            it--;
982
        }
983
        m_currentKeyframe = {it.key(), it.value()};
984
985
986
987
        m_selectedKeyframes = {m_currentKeyframe};
        slotSetPosition(m_currentKeyframe.second);
        m_bottomPosition = m_currentKeyframe.first - m_inFrame;
        emit seekToPos(m_currentKeyframe.second, m_bottomPosition);
988
989
990
        std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
        emit selectedKf(m_currentKeyframe, speeds);
        previousFound = true;
991
    }
992

993
994
995
    if (!previousFound && !m_keyframes.isEmpty()) {
        // We are after the last keyframe
        m_currentKeyframe = {m_keyframes.lastKey(), m_keyframes.value(m_keyframes.lastKey())};
996
997
998
999
        m_selectedKeyframes = {m_currentKeyframe};
        slotSetPosition(m_currentKeyframe.second);
        m_bottomPosition = m_currentKeyframe.first - m_inFrame;
        emit seekToPos(m_currentKeyframe.second, m_bottomPosition);
1000
1001
1002
1003
1004
1005
1006
1007
        std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
        emit selectedKf(m_currentKeyframe, speeds);
    }
}

void RemapView::updateBeforeSpeed(double speed)
{
    QMap<int, int>::iterator it = m_keyframes.find(m_currentKeyframe.first);
1008
1009
1010
    QMap<int, int> updatedKfrs;
    QList<int> toDelete;
    if (*it != m_keyframes.first() && it != m_keyframes.end()) {
1011
        it--;
1012
1013
1014
1015
        int updatedLength = (m_currentKeyframe.second - it.value()) * 100. / speed;
        int offset = it.key() + updatedLength - m_currentKeyframe.first;
        m_keyframes.remove(m_currentKeyframe.first);
        m_currentKeyframe.first = it.key() + updatedLength;
1016
        m_keyframes.insert(m_currentKeyframe.first, m_currentKeyframe.second);
1017
1018
1019
        m_bottomPosition = m_currentKeyframe.first;
        m_selectedKeyframes.clear();
        m_selectedKeyframes.insert(m_currentKeyframe.first, m_currentKeyframe.second);
1020
        it+=2;
1021
1022
        // Update all keyframes after that so that we don't alter the speeds
        while (m_moveNext && it != m_keyframes.end()) {
1023
1024
            toDelete << it.key();
            updatedKfrs.insert(it.key() + offset, it.value());
1025
            it++;
1026
        }
1027
1028
1029
1030
1031
1032
1033
1034
        for (int p : toDelete) {
            m_keyframes.remove(p);
        }
        QMapIterator<int, int> i(updatedKfrs);
        while (i.hasNext()) {
            i.next();
            m_keyframes.insert(i.key(), i.value());
        }
1035
        updateKeyframes(true);
1036
1037
1038
1039
1040
1041
1042
1043
1044
        update();
    }
}

void RemapView::updateAfterSpeed(double speed)
{
    QMap<int, int>::iterator it = m_keyframes.find(m_currentKeyframe.first);
    if (*it != m_keyframes.last()) {
        it++;
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
        QMap<int, int> updatedKfrs;
        QList<int> toDelete;
        int updatedLength = (it.value() - m_currentKeyframe.second) * 100. / speed;
        int offset = m_currentKeyframe.first + updatedLength - it.key();
        if (m_moveNext) {
            while (it != m_keyframes.end()) {
                toDelete << it.key();
                updatedKfrs.insert(it.key() + offset, it.value());
                it++;
            }
        } else {
            m_keyframes.insert(m_currentKeyframe.first + updatedLength, it.value());
            m_keyframes.remove(it.key());
        }
        // Update all keyframes after that so that we don't alter the speeds
        for (int p : toDelete) {
            m_keyframes.remove(p);
        }
        QMapIterator<int, int> i(updatedKfrs);
        while (i.hasNext()) {
            i.next();
            m_keyframes.insert(i.key(), i.value());
        }
1068
        updateKeyframes(true);
1069
1070
1071
1072
        update();
    }
}

1073
const QString RemapView::getKeyframesData() const
1074
1075
1076
1077
1078
{
    QStringList result;
    QMapIterator<int, int> i(m_keyframes);
    while (i.hasNext()) {
        i.next();
1079
        result << QString("%1=%2").arg(m_service->frames_to_time(i.key(), mlt_time_clock)).arg(GenTime(i.value(), pCore->getCurrentFps()).seconds());
1080
1081
1082
1083
    }
    return result.join(QLatin1Char(';'));
}

1084
1085
void RemapView::reloadProducer()
{
1086
1087
    if (!m_clip || !m_clip->clipUrl().endsWith(QLatin1String(".mlt"))) {
        qDebug()<<"==== this is not a playlist clip, aborting";
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
        return;
    }
    Mlt::Consumer c(pCore->getCurrentProfile()->profile(), "xml", m_clip->clipUrl().toUtf8().constData());
    QScopedPointer<Mlt::Service> serv(m_clip->originalProducer()->producer());
    if (serv == nullptr) {
        return;
    }
    qDebug()<<"==== GOR PLAYLIST SERVICE: "<<serv->type()<<" / "<<serv->consumer()->type()<<", SAVING TO "<<m_clip->clipUrl();
    Mlt::Multitrack s2(*serv.data());
    qDebug()<<"==== MULTITRACK: "<<s2.count();
    Mlt::Tractor s(pCore->getCurrentProfile()->profile());
    s.set_track(*s2.track(0), 0);
    qDebug()<<"==== GOT TRACKS: "<<s.count();
    int ignore = s.get_int("ignore_points");
    if (ignore) {
        s.set("ignore_points", 0);
    }
    c.connect(s);
    c.set("time_format", "frames");
    c.set("no_meta", 1);
    c.set("no_root", 1);
    //c.set("no_profile", 1);
    c.set("root", "/");
    c.set("store", "kdenlive");
    c.run();
    if (ignore) {
        s.set("ignore_points", ignore);
    }
}

1118
1119
1120
std::pair<double,double> RemapView::getSpeed(std::pair<int,int>kf)
{
    std::pair<double,double> speeds = {-1,-1};
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
    QMap<int, int>::const_iterator it = m_keyframes.constFind(kf.first);
    if (it == m_keyframes.constEnd()) {
        // Not a keyframe
        return speeds;
    }
    if (*it != m_keyframes.first()) {
        it--;
        speeds.first = (double)qAbs(kf.second - it.value()) / qAbs(kf.first - it.key());
        it++;
    }
    if (*it != m_keyframes.last()) {
        it++;
        speeds.second = (double)qAbs(kf.second - it.value()) / qAbs(kf.first - it.key());
    }
    return speeds;
}

std::pair<int,int> RemapView::getRange(std::pair<int,int>kf)
{
    std::pair<int,int> range = {-1,-1};
    QList<int> values = m_keyframes.keys();
    qDebug()<<"==== GOT KEYS: "<<values<<"\nVALUES: "<< m_keyframes.values();

/*    int min =
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
    QMap<int, int>::const_iterator it = m_keyframes.constFind(kf.first);
    if (it == m_keyframes.constEnd()) {
        // Not a keyframe
        return speeds;
    }
    if (*it != m_keyframes.first()) {
        it--;
        speeds.first = (double)qAbs(kf.first - it.key()) / qAbs(kf.second - it.value());
        it++;
    }
    if (*it != m_keyframes.last()) {
        it++;
        speeds.second = (double)qAbs(kf.first - it.key()) / qAbs(kf.second - it.value());
    }
1159
1160
    */
    return range;
1161
1162
}

1163
1164
1165
1166
1167
int RemapView::getKeyframePosition() const
{
    QMapIterator<int, int> i(m_keyframes);
    std::pair<int, int> newKeyframe = {-1,-1};
    std::pair<int, int> previous = {-1,-1};
1168
    newKeyframe.first = m_bottomPosition + m_inFrame;
1169
1170
    while (i.hasNext()) {
        i.next();
1171
        if (i.key() > m_bottomPosition + m_inFrame) {
1172
1173
            if (i.key() == m_keyframes.firstKey()) {
                // This is the first keyframe
1174
1175
                double ratio = (double)(m_position + m_inFrame) / i.value();
                return i.key() * ratio - m_inFrame;
1176
1177
1178
                break;
            } else if (previous.first > -1) {
                std::pair<int,int> current = {i.key(), i.value()};
1179
1180
                double ratio = (double)(m_position + m_inFrame - previous.second) / (current.second - previous.second);
                return previous.first + (qAbs(current.first - previous.first) * ratio) - m_inFrame;
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
                break;
            }
        }
        previous = {i.key(), i.value()};
    }
    if (newKeyframe.second == -1) {
        // We are after the last keyframe
        if (m_keyframes.isEmpty()) {
            return m_position;
        } else {
1191
1192
            double ratio = (double)(m_position + m_inFrame - m_keyframes.value(m_keyframes.lastKey())) / (m_duration - m_keyframes.value(m_keyframes.lastKey()));
            return m_keyframes.lastKey() + (qAbs(m_duration - m_keyframes.value(m_keyframes.lastKey())) * ratio) - m_inFrame;
1193
1194
        }
    }
1195
    return m_bottomPosition;
1196
1197
}

1198
1199
1200
void RemapView::addKeyframe()
{
    // insert or remove keyframe at interpolated position
1201
1202
1203
    if (m_keyframes.contains(m_bottomPosition + m_inFrame)) {
        m_keyframes.remove(m_bottomPosition + m_inFrame);
        if (m_currentKeyframe.first == m_bottomPosition + m_inFrame) {
1204
1205
1206
1207
1208
            m_currentKeyframe = m_currentKeyframeOriginal = {-1,-1};
            std::pair<double,double> speeds = {-1,-1};
            emit selectedKf(m_currentKeyframe, speeds);
        }
        emit atKeyframe(false);
1209
        updateKeyframes(false);
1210
1211
1212
1213
1214
1215
        update();
        return;
    }
    QMapIterator<int, int> i(m_keyframes);
    std::pair<int, int> newKeyframe = {-1,-1};
    std::pair<int, int> previous = {-1,-1};
1216
    newKeyframe.first = m_bottomPosition + m_inFrame;
1217
1218
    while (i.hasNext()) {
        i.next();
1219
        if (i.key() > m_bottomPosition + m_inFrame) {
1220
1221
            if (i.key() == m_keyframes.firstKey()) {
                // This is the first keyframe
1222
                double ratio = (double)(m_bottomPosition + m_inFrame) / i.key();
1223
1224
1225
1226
                newKeyframe.second = i.value() * ratio;
                break;
            } else if (previous.first > -1) {
                std::pair<int,int> current = {i.key(), i.value()};
1227
                double ratio = (double)(m_bottomPosition + m_inFrame - previous.first) / (current.first - previous.first);
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
                qDebug()<<"=== RATIO: "<<ratio;
                newKeyframe.second = previous.second + (qAbs(current.second - previous.second) * ratio);
                break;
            }
        }
        previous = {i.key(), i.value()};
    }
    if (newKeyframe.second == -1) {
        // We are after the last keyframe
        if (m_keyframes.isEmpty()) {
1238
            newKeyframe.second = m_position + m_inFrame;
1239
        } else {
1240
            double ratio = (double)(m_position + m_inFrame - m_keyframes.lastKey()) / (m_duration - m_keyframes.lastKey());
1241
1242
1243
1244
1245
1246
1247
1248
            newKeyframe.second = m_keyframes.value(m_keyframes.lastKey()) + (qAbs(m_duration - m_keyframes.value(m_keyframes.lastKey())) * ratio);
        }
    }
    m_keyframes.insert(newKeyframe.first, newKeyframe.second);
    m_currentKeyframe = newKeyframe;
    std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
    emit selectedKf(newKeyframe, speeds);
    emit atKeyframe(true);
1249
    updateKeyframes(true);
1250
1251
1252
    update();
}

1253
1254
1255
1256
void RemapView::toggleMoveNext(bool moveNext)
{
    m_moveNext = moveNext;
}
1257

1258
1259
void RemapView::refreshOnDurationChanged(int remapDuration)
{
1260
1261
    if (remapDuration != m_duration) {
        m_duration = qMax(remapDuration, remapMax());
1262
1263
1264
1265
1266
1267
1268
        int maxWidth = width() - (2 * m_offset);
        m_scale = maxWidth / double(qMax(1, m_duration - 1));
        m_zoomStart = m_zoomHandle.x() * maxWidth;
        m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
    }
}

1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
void RemapView::resizeEvent(QResizeEvent *event)
{
    int maxWidth = width() - (2 * m_offset);
    m_scale = maxWidth / double(qMax(1, m_duration - 1));
    m_zoomStart = m_zoomHandle.x() * maxWidth;
    m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
    QWidget::resizeEvent(event);
    update();
}

1279
1280
1281
1282
1283
1284
1285
void RemapView::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPalette pal = palette();
    KColorScheme scheme(pal.currentColorGroup(), KColorScheme::Window);
    m_colSelected = palette().highlight().color();
    m_colKeyframe = scheme.foreground(KColorScheme::NormalText).color();
1286
    QColor bg = scheme.background(KColorScheme::AlternateBackground ).color();
1287
    QStylePainter p(this);
1288
1289
    int maxWidth = width() - (2 * m_offset);
    int zoomEnd = qCeil(m_zoomHandle.y() * maxWidth);
1290
    // Top timeline
1291
    p.fillRect(m_offset, 0, maxWidth + 1, m_centerPos, bg);
1292
    // Bottom timeline
1293
    p.fillRect(m_offset, m_bottomView - m_centerPos, maxWidth + 1, m_centerPos, bg);
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
    /* ticks */
    double fps = pCore->getCurrentFps();
    int displayedLength = int(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) {