timeremap.cpp 74.6 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
218
219
220
221
        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});
                if (lastKeyframePos == m_inFrame + m_duration - 1) {
                    // Move last keyframe
                    //TODO: check we still have a keyframe at it pos
                    it--;
222
                    int updatedVal = it.value() + ((lastPos - it.key()) * speeds.first);
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
                    m_keyframes.remove(lastKeyframePos);
                    m_keyframes.insert(lastPos, updatedVal);
                } else {
                    // Add a keyframe at end
                    int updatedVal = it.value() + (lastPos - it.key());
                    m_keyframes.insert(lastPos, updatedVal);
                }
            }
        } 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();
    }
258
259
    int maxWidth = width() - (2 * m_offset);
    m_scale = maxWidth / double(qMax(1, m_duration - 1));
260
261
    m_zoomStart = m_zoomHandle.x() * maxWidth;
    m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
262
263
264
265
    if (!m_keyframes.contains(m_currentKeyframe.first)) {
        m_currentKeyframe = m_currentKeyframeOriginal = {-1,-1};
        update();
    }
266
267
268
    if (keyframeAdded) {
        updateKeyframes(false);
    }
269
270
}

271
void RemapView::loadKeyframes(const QString &mapData)
272
273
274
{
    m_keyframes.clear();
    if (mapData.isEmpty()) {
275
276
277
278
279
280
        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);
281
282
283
    } else {
        QStringList str = mapData.split(QLatin1Char(';'));
        for (auto &s : str) {
284
            int pos = m_service->time_to_frames(s.section(QLatin1Char('='), 0, 0).toUtf8().constData());
285
            int val = GenTime(s.section(QLatin1Char('='), 1).toDouble()).frames(pCore->getCurrentFps());
286
            m_keyframes.insert(pos, val);
287
288
            m_duration = qMax(m_duration, pos - m_inFrame);
            m_duration = qMax(m_duration, val - m_inFrame);
289
        }
290
291
292
293
        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);
294
        emit updateMaxDuration(m_duration);
295
296
297
298
299
300
301
302
303
        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});
        }
304
305
306
307
308
309
310
311
312
313
314
    }
    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);
315
    int realPos = qMax(m_inFrame, pos + m_inFrame);
316
317
    pos = qBound(0, pos, m_duration - 1);
    GenTime position(pos, pCore->getCurrentFps());
318
319
    if (event->buttons() == Qt::NoButton) {
        bool hoverKeyframe = false;
320
321
        if (event->y() < 2 * m_lineHeight && event->y() > m_lineHeight) {
            // mouse move in top keyframes area
322
323
            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()) {
324
325
                hoverKeyframe = true;
            }
326
327
        } else if (event->y() > m_bottomView - 2 * m_lineHeight && event->y() < m_bottomView - m_lineHeight) {
            // move in bottom keyframe area
328
329
            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()) {
330
331
332
333
334
335
336
337
338
                hoverKeyframe = true;
            }
        }
        if (hoverKeyframe) {
            setCursor(Qt::PointingHandCursor);
        } else {
            setCursor(Qt::ArrowCursor);
        }
    } else if ((event->buttons() & Qt::LeftButton) != 0u) {
339
340
341
342
        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
343
344
345
                int maxWidth = width() - (2 * m_offset);
                m_zoomStart = m_zoomHandle.x() * maxWidth;
                m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
346
347
348
349
350
                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
351
352
353
                int maxWidth = width() - (2 * m_offset);
                m_zoomStart = m_zoomHandle.x() * maxWidth;
                m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
                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
373
374
375
                int maxWidth = width() - (2 * m_offset);
                m_zoomStart = m_zoomHandle.x() * maxWidth;
                m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
376
377
378
379
380
                update();
            }
            return;
        }
        //qDebug()<<"=== MOVING MOUSE: "<<pos<<" = "<<m_currentKeyframe<<", MOVE KFMODE: "<<m_moveKeyframeMode;
381
        if ((m_currentKeyframe.first == pos + m_inFrame && m_moveKeyframeMode == TopMove) || (m_currentKeyframe.second == pos + m_inFrame && m_moveKeyframeMode == BottomMove)) {
382
383
384
            return;
        }
        if (m_currentKeyframe.first >= 0 && m_moveKeyframeMode != NoMove) {
385
386
            if (m_moveKeyframeMode == BottomMove) {
                // Moving bottom keyframe
387
388
                if (!m_keyframes.contains(pos + m_inFrame)) {
                    int delta = pos + m_inFrame - m_currentKeyframe.first;
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
                    // 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();
404
405
406
                        if (i.key() < m_currentKeyframe.first) {
                            continue;
                        }
407
                        //qDebug()<<"=== MOVING KFR: "<<i.key()<<" > "<<(i.key() + delta);
408
                        //m_keyframes.insert(i.key() + delta, i.value());
409
410
411
                        updated.insert(i.key() + delta, i.value());
                        m_keyframes.remove(i.key());
                    }
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
                    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);

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

                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;
452
453
                    }
                }
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
                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;
469
470
471
472
            }
        }
        // Rubberband selection
        if (m_clickPoint >= 0) {
473
            m_clickEnd = pos + m_inFrame;
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
            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;
        }

494
        if (m_moveKeyframeMode == CursorMove || (event->y() < m_centerPos)) {
495
            if (pos != m_position) {
496
497
                slotSetPosition(pos + m_inFrame);
                emit seekToPos(pos + m_inFrame, -1);
498
499
            }
        }
500
        if (m_moveKeyframeMode == CursorMoveBottom || (event->y() > m_centerPos)) {
501
502
503
            if (pos != m_bottomPosition) {
                m_bottomPosition = pos;
                pos = GenTime(m_remapLink->anim_get_double("map", pos + m_inFrame)).frames(pCore->getCurrentFps());
504
                slotSetPosition(pos);
505
                emit seekToPos(-1, m_bottomPosition);
506
507
508
509
            }
        }
        return;
    }
510
    if (event->y() > m_bottomView) {
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
541
542
543
544
545
546
547
        // 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();
    }
}

548
549
550
551
552
int RemapView::position() const
{
    return m_position;
}

553
554
555
556
557
558
void RemapView::centerCurrentKeyframe()
{
    if (m_currentKeyframe.first == -1) {
        // No keyframe selected, abort
        return;
    }
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
608
609
610
611
612
613
614
    //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;
615
616
    m_currentKeyframe.second = m_position + m_inFrame;
    m_keyframes.insert(m_currentKeyframe.first, m_currentKeyframe.second);
617
618
619
620
621
    QMapIterator<int, int> i(nextKeyframes);
    while (i.hasNext()) {
        i.next();
        m_keyframes.insert(i.key(), i.value() + offset);
    }
622
623
624
625
626
627
628
    std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
    emit selectedKf(m_currentKeyframe, speeds);
    emit atKeyframe(true);
    updateKeyframes(true);
    update();
}

629
std::pair<int,int> RemapView::getClosestKeyframe(int pos, bool bottomKeyframe) const
630
631
{
    int deltaMin = -1;
632
    std::pair<int,int> result = {-1,-1};
633
634
635
    QMapIterator<int, int> i(m_keyframes);
    while (i.hasNext()) {
        i.next();
636
        int val = bottomKeyframe ? i.key() : i.value();
637
638
639
        int delta = qAbs(val - pos);
        if (deltaMin == -1 || delta < deltaMin) {
            deltaMin = delta;
640
            result = {i.key(), i.value()};
641
642
        }
    }
643
    return result;
644
645
646
647
648
}

void RemapView::mouseReleaseEvent(QMouseEvent *event)
{
    event->accept();
649
650
651
652
653
    if (m_moveKeyframeMode == TopMove || m_moveKeyframeMode == BottomMove) {
        // Restore original selection
        m_selectedKeyframes = m_previousSelection;
        update();
    }
654
    m_moveKeyframeMode = NoMove;
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
680
681
682
683
684
685
    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"));
    }
686
687
688
689
690
691
692
693
694
695
696
697
    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;
698
    m_keyframesOrigin = m_keyframes;
699
    if (event->button() == Qt::LeftButton) {
700
701
        if (event->y() < m_centerPos) {
            // mouse click in top area
702
            if (event->modifiers() & Qt::ShiftModifier) {
703
                m_clickPoint = pos + m_inFrame;
704
705
                return;
            }
706
            if (event->y() < 2 *m_lineHeight && event->y() > m_lineHeight) {
707
708
709
710
                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;
711
712
713
714
715
716
717
718
719
                    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};
720
                    }
721
722
723
724
725
726
727
                    // 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;
728
729
730
731
732
733
734
735
736
737
                        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());
                                }
                            }
                        }
738
                        if (KdenliveSettings::keyframeseek()) {
739
740
741
                            slotSetPosition(m_currentKeyframeOriginal.second);
                            m_bottomPosition = m_currentKeyframeOriginal.first - m_inFrame;
                            emit seekToPos(m_currentKeyframeOriginal.second, m_bottomPosition);
742
743
744
                        } else {
                            update();
                        }
745
746
747
                    } else {
                        update();
                    }
748
                    return;
749
750
751
                }
            }
            // no keyframe next to mouse
752
753
754
755
756
757
758
759
760
761
            //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) {
762
763
764
765
766
            // click on zoom area
            if (m_hoverZoom) {
                m_clickOffset = (double(event->x()) - m_offset) / (width() - 2 * m_offset);
            }
            return;
767
768
        } else if (event->y() > m_centerPos && event->y() < m_bottomView) {
            // click in bottom area
769
            if (event->modifiers() & Qt::ShiftModifier) {
770
                m_clickPoint = pos + m_inFrame;
771
772
                return;
            }
773
            if (event->y() > (m_bottomView - 2 * m_lineHeight) && (event->y() < m_bottomView - m_lineHeight)) {
774
775
776
                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;
777
                    if (event->modifiers() & Qt::ControlModifier) {
778
                        if (m_selectedKeyframes.contains(m_currentKeyframeOriginal.first)) {
779
780
781
782
783
                            m_selectedKeyframes.remove(m_currentKeyframeOriginal.first);
                            m_currentKeyframeOriginal.second = -1;
                        } else {
                            m_selectedKeyframes.insert(m_currentKeyframeOriginal.first, m_currentKeyframeOriginal.second);
                        }
784
                    } else if (!m_selectedKeyframes.contains(m_currentKeyframeOriginal.first)) {
785
                        m_selectedKeyframes = {m_currentKeyframeOriginal};
786
                    }
787
788
789
790
                    // Select and seek to keyframe
                    m_currentKeyframe = m_currentKeyframeOriginal;
                    std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
                    emit selectedKf(m_currentKeyframe, speeds);
791
                    if (m_currentKeyframeOriginal.first > -1) {
792
                        m_moveKeyframeMode = BottomMove;
793
794
795
796
797
798
799
800
801
802
                        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());
                                }
                            }
                        }
803
                        if (KdenliveSettings::keyframeseek()) {
804
805
806
807
                            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);
808
809
810
                        } else {
                            update();
                        }
811
812
813
                    } else {
                        update();
                    }
814
                    return;
815
816
817
                }
            }
            // no keyframe next to mouse
818
819
            //m_selectedKeyframes.clear();
            //m_currentKeyframe = m_currentKeyframeOriginal = {-1,-1};
820
821
822
            if (pos != m_bottomPosition) {
                qDebug()<<"=== MOVING BOTTOM KFR: "<<pos<<" = "<<m_bottomPosition;
                m_bottomPosition = pos;
823
                m_moveKeyframeMode = CursorMoveBottom;
824
                //slotUpdatePosition();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
825
                emit seekToPos(-1, pos);
826
827
                update();
            }
828
829
830
831
832
833
834
            /*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();
            }*/
835
            return;
836
        }
837
    } else if (event->button() == Qt::RightButton && event->y() > m_bottomView) {
838
839
840
841
        // 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;
842
843
844
                int maxWidth = width() - (2 * m_offset);
                m_zoomStart = m_zoomHandle.x() * maxWidth;
                m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
845
846
847
848
849
850
                update();
                return;
            }
        } else {
            m_lastZoomHandle = m_zoomHandle;
            m_zoomHandle = QPointF(0, 1);
851
852
853
            int maxWidth = width() - (2 * m_offset);
            m_zoomStart = m_zoomHandle.x() * maxWidth;
            m_zoomFactor = maxWidth / (m_zoomHandle.y() * maxWidth - m_zoomStart);
854
855
856
857
858
859
860
861
862
863
            update();
            return;
        }
    }
    if (pos != m_position) {
        //emit seekToPos(pos);
        update();
    }
}

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
923
924
925
926
927
928
929
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();
    }
}


930
void RemapView::slotSetPosition(int pos)
931
{
932
933
    if (pos != m_position + m_inFrame) {
        m_position = pos - m_inFrame;
934
        //int offset = pCore->getItemIn(m_model->getOwnerId());
935
        //emit atKeyframe(m_keyframes.contains(pos));
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
        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();
    }
}

952
953
954
955
956
957
958
void RemapView::slotSetBottomPosition(int pos)
{
    qDebug()<<"=== SETTING BOTTOM POS: "<<pos;
    m_bottomPosition = pos;
    update();
}

959
960
void RemapView::goNext()
{
961
    // Seek to next keyframe
962
963
964
    QMapIterator<int, int> i(m_keyframes);
    while (i.hasNext()) {
        i.next();
965
        if (i.key() > m_bottomPosition + m_inFrame) {
966
            m_currentKeyframe = {i.key(),i.value()};
967
            m_selectedKeyframes = {m_currentKeyframe};
968
            slotSetPosition(i.key());
969
970
            m_bottomPosition = m_currentKeyframe.first - m_inFrame;
            emit seekToPos(i.value(), m_bottomPosition);
971
972
973
974
975
976
977
978
979
980
981
            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;
982
    QMap<int, int>::const_iterator it = m_keyframes.constBegin();
983
    while (it.key() < m_bottomPosition + m_inFrame && it != m_keyframes.constEnd()) {
984
985
986
987
988
        it++;
    }
    if (it != m_keyframes.constEnd()) {
        if (it != m_keyframes.constBegin()) {
            it--;
989
        }
990
        m_currentKeyframe = {it.key(), it.value()};
991
992
993
994
        m_selectedKeyframes = {m_currentKeyframe};
        slotSetPosition(m_currentKeyframe.second);
        m_bottomPosition = m_currentKeyframe.first - m_inFrame;
        emit seekToPos(m_currentKeyframe.second, m_bottomPosition);
995
996
997
        std::pair<double,double> speeds = getSpeed(m_currentKeyframe);
        emit selectedKf(m_currentKeyframe, speeds);
        previousFound = true;
998
    }
999

1000
1001
1002
    if (!previousFound && !m_keyframes.isEmpty()) {
        // We are after the last keyframe
        m_currentKeyframe = {m_keyframes.lastKey(), m_keyframes.value(m_keyframes.lastKey())};
1003
1004
1005
1006
        m_selectedKeyframes = {m_currentKeyframe};
        slotSetPosition(m_currentKeyframe.second);
        m_bottomPosition = m_currentKeyframe.first - m_inFrame;
        emit seekToPos(m_currentKeyframe.second, m_bottomPosition);
1007
1008
1009
1010
1011
1012
1013
1014
        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);
1015
1016
1017
    QMap<int, int> updatedKfrs;
    QList<int> toDelete;
    if (*it != m_keyframes.first() && it != m_keyframes.end()) {
1018
        it--;
1019
1020
1021
1022
        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;
1023
        m_keyframes.insert(m_currentKeyframe.first, m_currentKeyframe.second);
1024
1025
1026
        m_bottomPosition = m_currentKeyframe.first;
        m_selectedKeyframes.clear();
        m_selectedKeyframes.insert(m_currentKeyframe.first, m_currentKeyframe.second);
1027
        it+=2;
1028
1029
        // Update all keyframes after that so that we don't alter the speeds
        while (m_moveNext && it != m_keyframes.end()) {
1030
1031
            toDelete << it.key();
            updatedKfrs.insert(it.key() + offset, it.value());
1032
            it++;
1033
        }
1034
1035
1036
1037
1038
1039
1040
1041
        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());
        }
1042
        updateKeyframes(true);
1043
1044
1045
1046
1047
1048
1049
1050
1051
        update();
    }
}

void RemapView::updateAfterSpeed(double speed)
{
    QMap<int, int>::iterator it = m_keyframes.find(m_currentKeyframe.first);
    if (*it != m_keyframes.last()) {
        it++;
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
        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());
        }
1075
        updateKeyframes(true);
1076
1077
1078
1079
        update();
    }
}

1080
const QString RemapView::getKeyframesData() const
1081
1082
1083
1084
1085
{
    QStringList result;
    QMapIterator<int, int> i(m_keyframes);
    while (i.hasNext()) {
        i.next();
1086
        result << QString("%1=%2").arg(m_service->frames_to_time(i.key(), mlt_time_clock)).arg(GenTime(i.value(), pCore->getCurrentFps()).seconds());
1087
1088
1089
1090
    }
    return result.join(QLatin1Char(';'));
}

1091
1092
void RemapView::reloadProducer()
{
1093
1094
    if (!m_clip || !m_clip->clipUrl().endsWith(QLatin1String(".mlt"))) {
        qDebug()<<"==== this is not a playlist clip, aborting";
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
        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);
    }
}

1125
1126
1127
std::pair<double,double> RemapView::getSpeed(std::pair<int,int>kf)
{
    std::pair<double,double> speeds = {-1,-1};
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
    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 =
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
    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());
    }
1166
1167
    */
    return range;
1168
1169
}

1170
1171
1172
1173
1174
int RemapView::getKeyframePosition() const
{
    QMapIterator<int, int> i(m_keyframes);
    std::pair<int, int> newKeyframe = {-1,-1};
    std::pair<int, int> previous = {-1,-1};
1175
    newKeyframe.first = m_bottomPosition + m_inFrame;
1176
1177
    while (i.hasNext()) {
        i.next();
1178
        if (i.key() > m_bottomPosition + m_inFrame) {
1179
1180
            if (i.key() == m_keyframes.firstKey()) {
                // This is the first keyframe
1181
1182
                double ratio = (double)(m_position + m_inFrame) / i.value();
                return i.key() * ratio - m_inFrame;
1183
1184
1185
                break;
            } else if (previous.first > -1) {
                std::pair<int,int> current = {i.key(), i.value()};
1186
1187
                double ratio = (double)(m_position + m_inFrame - previous.second) / (current.second - previous.second);
                return previous.first + (qAbs(current.first - previous.first) * ratio) - m_inFrame;
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
                break;
            }
        }
        previous = {i.key(), i.value()};
    }
    if (newKeyframe.second == -1) {
        // We are after the last keyframe
        if (m_keyframes.isEmpty()) {
            return m_position;
        } else {
1198
1199
            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;
1200
1201
        }
    }
1202
    return m_bottomPosition;
1203
1204
}

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

1260
1261
1262
1263
void RemapView::toggleMoveNext(bool moveNext)
{
    m_moveNext = moveNext;
}
1264

1265
1266
void RemapView::refreshOnDurationChanged(int remapDuration)
{
1267
1268
    if (remapDuration != m_duration) {
        m_duration = qMax(remapDuration, remapMax());
1269
1270
1271
1272
1273
1274
1275
        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);
    }
}

1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
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();
}

1286
1287
1288
1289
1290
1291
1292
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();
1293
    QColor bg = scheme.background(KColorScheme::AlternateBackground ).color();
1294
    QStylePainter p(this);