timelinefunctions.cpp 101 KB
Newer Older
1
/*
Camille Moulin's avatar
Camille Moulin committed
2
SPDX-FileCopyrightText: 2017 Jean-Baptiste Mardelle <jb@kdenlive.org>
3
4
This file is part of Kdenlive. See www.kdenlive.org.

Camille Moulin's avatar
Camille Moulin committed
5
SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
6
7
8
*/

#include "timelinefunctions.hpp"
9
10
11
12
#include "bin/bin.h"
#include "bin/projectclip.h"
#include "bin/projectfolder.h"
#include "bin/projectitemmodel.h"
13
#include "bin/model/subtitlemodel.hpp"
Julius Künzel's avatar
Julius Künzel committed
14
#include "bin/model/markerlistmodel.hpp"
15
#include "clipmodel.hpp"
16
#include "compositionmodel.hpp"
Nicolas Carion's avatar
linting    
Nicolas Carion committed
17
#include "core.h"
18
#include "doc/kdenlivedoc.h"
19
#include "effects/effectstack/model/effectstackmodel.hpp"
20
21
22
#include "groupsmodel.hpp"
#include "timelineitemmodel.hpp"
#include "trackmodel.hpp"
23
#include "transitions/transitionsrepository.hpp"
24
#include "mainwindow.h"
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
25
#include "project/projectmanager.h"
26

27
#include <QApplication>
28
#include <QDebug>
29
#include <QInputDialog>
30
#include <QSemaphore>
Nicolas Carion's avatar
linting    
Nicolas Carion committed
31
#include <klocalizedstring.h>
32
#include <unordered_map>
33

Vincent Pinon's avatar
Vincent Pinon committed
34
35
#ifdef CRASH_AUTO_TEST
#include "logger.hpp"
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wfloat-equal"
#pragma GCC diagnostic ignored "-Wshadow"
#pragma GCC diagnostic ignored "-Wpedantic"
#include <rttr/registration>
#pragma GCC diagnostic pop

RTTR_REGISTRATION
{
    using namespace rttr;
    registration::class_<TimelineFunctions>("TimelineFunctions")
        .method("requestClipCut", select_overload<bool(std::shared_ptr<TimelineItemModel>, int, int)>(&TimelineFunctions::requestClipCut))(
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
50
51
52
            parameter_names("timeline", "clipId", "position"))
        .method("requestDeleteBlankAt", select_overload<bool(const std::shared_ptr<TimelineItemModel>&, int, int, bool)>(&TimelineFunctions::requestDeleteBlankAt))(
            parameter_names("timeline", "trackId", "position", "affectAllTracks"));
53
}
Vincent Pinon's avatar
Vincent Pinon committed
54
55
56
57
58
59
60
61
#else
#define TRACE_STATIC(...)
#define TRACE_RES(...)
#endif

QStringList waitingBinIds;
QMap<QString, QString> mappedIds;
QMap<int, int> tracksMap;
62
QMap<int, int> spacerUngroupedItems;
63
int spacerMinPosition;
Vincent Pinon's avatar
Vincent Pinon committed
64
QSemaphore semaphore(1);
65

66
67
bool TimelineFunctions::cloneClip(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, int &newId, PlaylistState::ClipState state, Fun &undo,
                                  Fun &redo)
Nicolas Carion's avatar
Nicolas Carion committed
68
{
69
70
    // Special case: slowmotion clips
    double clipSpeed = timeline->m_allClips[clipId]->getSpeed();
71
    bool warp_pitch = timeline->m_allClips[clipId]->getIntProperty(QStringLiteral("warp_pitch"));
72
73
    int audioStream = timeline->m_allClips[clipId]->getIntProperty(QStringLiteral("audio_index"));
    bool res = timeline->requestClipCreation(timeline->getClipBinId(clipId), newId, state, audioStream, clipSpeed, warp_pitch, undo, redo);
Nicolas Carion's avatar
Nicolas Carion committed
74
    timeline->m_allClips[newId]->m_endlessResize = timeline->m_allClips[clipId]->m_endlessResize;
75

76
77
78
    // copy useful timeline properties
    timeline->m_allClips[clipId]->passTimelineProperties(timeline->m_allClips[newId]);

Nicolas Carion's avatar
Nicolas Carion committed
79
80
81
82
    int duration = timeline->getClipPlaytime(clipId);
    int init_duration = timeline->getClipPlaytime(newId);
    if (duration != init_duration) {
        int in = timeline->m_allClips[clipId]->getIn();
83
84
        res = res && timeline->requestItemResize(newId, init_duration - in, false, true, undo, redo);
        res = res && timeline->requestItemResize(newId, duration, true, true, undo, redo);
Nicolas Carion's avatar
Nicolas Carion committed
85
86
87
88
89
90
    }
    if (!res) {
        return false;
    }
    std::shared_ptr<EffectStackModel> sourceStack = timeline->getClipEffectStackModel(clipId);
    std::shared_ptr<EffectStackModel> destStack = timeline->getClipEffectStackModel(newId);
91
    destStack->importEffects(sourceStack, state);
Nicolas Carion's avatar
Nicolas Carion committed
92
93
94
    return res;
}

Nicolas Carion's avatar
Nicolas Carion committed
95
bool TimelineFunctions::requestMultipleClipsInsertion(const std::shared_ptr<TimelineItemModel> &timeline, const QStringList &binIds, int trackId, int position,
96
                                                      QList<int> &clipIds, bool logUndo, bool refreshView)
97
98
99
100
101
{
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };
    for (const QString &binId : binIds) {
        int clipId;
102
        if (timeline->requestClipInsertion(binId, trackId, position, clipId, logUndo, refreshView, false, undo, redo)) {
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
            clipIds.append(clipId);
            position += timeline->getItemPlaytime(clipId);
        } else {
            undo();
            clipIds.clear();
            return false;
        }
    }

    if (logUndo) {
        pCore->pushUndo(undo, redo, i18n("Insert Clips"));
    }

    return true;
}

Nicolas Carion's avatar
Nicolas Carion committed
119
bool TimelineFunctions::processClipCut(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, int position, int &newId, Fun &undo, Fun &redo)
120
{
121
122
123
124
125
    bool isSubtitle = timeline->isSubTitle(clipId);
    int trackId = isSubtitle ? -1 : timeline->getClipTrackId(clipId);
    int trackDuration = isSubtitle ? -1 : timeline->getTrackById_const(trackId)->trackDuration();
    int start = timeline->getItemPosition(clipId);
    int duration = timeline->getItemPlaytime(clipId);
126
127
128
    if (start > position || (start + duration) < position) {
        return false;
    }
129
130
131
    if (isSubtitle) {
        return timeline->cutSubtitle(position, undo, redo);
    }
132
    PlaylistState::ClipState state = timeline->m_allClips[clipId]->clipState();
133
    // Check if clip has an end Mix
134
    bool res = cloneClip(timeline, clipId, newId, state, undo, redo);
135
    timeline->m_blockRefresh = true;
136
    res = res && timeline->requestItemResize(clipId, position - start, true, true, undo, redo);
Nicolas Carion's avatar
Nicolas Carion committed
137
    int newDuration = timeline->getClipPlaytime(clipId);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
138
139
140
141
142
    // parse effects
    std::shared_ptr<EffectStackModel> sourceStack = timeline->getClipEffectStackModel(clipId);
    sourceStack->cleanFadeEffects(true, undo, redo);
    std::shared_ptr<EffectStackModel> destStack = timeline->getClipEffectStackModel(newId);
    destStack->cleanFadeEffects(false, undo, redo);
143
    res = res && timeline->requestItemResize(newId, duration - newDuration, false, true, undo, redo);
144
145
    // The next requestclipmove does not check for duration change since we don't invalidate timeline, so check duration change now
    bool durationChanged = trackDuration != timeline->getTrackById_const(trackId)->trackDuration();
146
    timeline->m_allClips[newId]->setSubPlaylistIndex(timeline->m_allClips[clipId]->getSubPlaylistIndex(), trackId);
147
    res = res && timeline->requestClipMove(newId, trackId, position, true, true, false, true, undo, redo);
148
149
150
151
152
153
154
155
156
157
158
159
    
    if (timeline->getTrackById_const(trackId)->hasEndMix(clipId)) {
        Fun local_undo = [timeline, trackId, clipId, newId]() { 
            timeline->getTrackById_const(trackId)->reAssignEndMix(newId, clipId);
            return true; };
        Fun local_redo = [timeline, trackId, clipId, newId]() {
            timeline->getTrackById_const(trackId)->reAssignEndMix(clipId, newId);
            return true; };
        local_redo();
        UPDATE_UNDO_REDO_NOLOCK(local_redo, local_undo, undo, redo);
    }
    
160
161
    if (durationChanged) {
        // Track length changed, check project duration
162
        Fun updateDuration = [timeline]() {
163
164
165
166
167
168
            timeline->updateDuration();
            return true;
        };
        updateDuration();
        PUSH_LAMBDA(updateDuration, redo);
    }
169
    timeline->m_blockRefresh = false;
Nicolas Carion's avatar
Nicolas Carion committed
170
171
    return res;
}
172

Nicolas Carion's avatar
Nicolas Carion committed
173
174
bool TimelineFunctions::requestClipCut(std::shared_ptr<TimelineItemModel> timeline, int clipId, int position)
{
175
176
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };
177
    TRACE_STATIC(timeline, clipId, position);
Nicolas Carion's avatar
Nicolas Carion committed
178
    bool result = TimelineFunctions::requestClipCut(timeline, clipId, position, undo, redo);
179
180
181
    if (result) {
        pCore->pushUndo(undo, redo, i18n("Cut clip"));
    }
182
    TRACE_RES(result);
183
184
185
    return result;
}

Nicolas Carion's avatar
Nicolas Carion committed
186
bool TimelineFunctions::requestClipCut(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, int position, Fun &undo, Fun &redo)
187
{
188
189
190
191
    const std::unordered_set<int> clipselect = timeline->getGroupElements(clipId);
    // Remove locked items
    std::unordered_set<int> clips;
    for (int cid : clipselect) {
192
193
194
195
        if (timeline->isSubTitle(cid)) {
            clips.insert(cid);
            continue;
        }
196
197
198
        if (!timeline->isClip(cid)) {
            continue;
        }
199
        int tk = timeline->getClipTrackId(cid);
200
        if (tk != -1 && !timeline->getTrackById_const(tk)->isLocked()) {
201
202
203
            clips.insert(cid);
        }
    }
204
205
206
207
208
209
210
211
212
213
    // Shall we reselect after the split
    int trackToSelect = -1;
    if (timeline->isClip(clipId) && timeline->m_allClips[clipId]->selected) {
        int mainIn = timeline->getItemPosition(clipId);
        int mainOut = mainIn + timeline->getItemPlaytime(clipId);
        if (position > mainIn && position < mainOut) {
            trackToSelect = timeline->getItemTrackId(clipId);
        }
    }

214
215
216
    // We need to call clearSelection before attempting the split or the group split will be corrupted by the selection group (no undo support)
    timeline->requestClearSelection();

217
    std::unordered_set<int> topElements;
218
219
    std::transform(clips.begin(), clips.end(), std::inserter(topElements, topElements.begin()), [&](int id) { return timeline->m_groups->getRootId(id); });

Nicolas Carion's avatar
Nicolas Carion committed
220
    int count = 0;
221
    QList<int> newIds;
Nicolas Carion's avatar
Nicolas Carion committed
222
    QList<int> clipsToCut;
Nicolas Carion's avatar
Nicolas Carion committed
223
    for (int cid : clips) {
224
        if (!timeline->isClip(cid) && !timeline->isSubTitle(cid)) {
225
226
            continue;
        }
227
228
        int start = timeline->getItemPosition(cid);
        int duration = timeline->getItemPlaytime(cid);
Nicolas Carion's avatar
Nicolas Carion committed
229
        if (start < position && (start + duration) > position) {
230
            clipsToCut << cid;
Nicolas Carion's avatar
Nicolas Carion committed
231
232
        }
    }
233
234
235
    if (clipsToCut.isEmpty()) {
        return true;
    }
Vincent Pinon's avatar
Vincent Pinon committed
236
    for (int cid : qAsConst(clipsToCut)) {
237
238
239
240
241
242
243
        count++;
        int newId;
        bool res = processClipCut(timeline, cid, position, newId, undo, redo);
        if (!res) {
            bool undone = undo();
            Q_ASSERT(undone);
            return false;
Nicolas Carion's avatar
Nicolas Carion committed
244
        }
245
246
247
248
        // splitted elements go temporarily in the same group as original ones.
        timeline->m_groups->setInGroupOf(newId, cid, undo, redo);
        newIds << newId;
    }
Nicolas Carion's avatar
Nicolas Carion committed
249
    if (count > 0 && timeline->m_groups->isInGroup(clipId)) {
250
        // we now split the group hierarchy.
Nicolas Carion's avatar
Nicolas Carion committed
251
        // As a splitting criterion, we compare start point with split position
252
        auto criterion = [timeline, position](int cid) { return timeline->getItemPosition(cid) < position; };
253
254
        bool res = true;
        for (const int topId : topElements) {
255
            qDebug()<<"// CHECKING REGROUP ELEMENT: "<<topId<<", ISCLIP: "<<timeline->isClip(topId)<<timeline->isGroup(topId);
Nicolas Carion's avatar
Nicolas Carion committed
256
            res = res && timeline->m_groups->split(topId, criterion, undo, redo);
257
        }
Nicolas Carion's avatar
Nicolas Carion committed
258
259
260
261
262
263
        if (!res) {
            bool undone = undo();
            Q_ASSERT(undone);
            return false;
        }
    }
264
265
266
267
    if (count > 0 && trackToSelect > -1) {
        int newClip = timeline->getClipByPosition(trackToSelect, position);
        if (newClip > -1) {
            timeline->requestSetSelection({newClip});
268
        }
269
    }
Nicolas Carion's avatar
Nicolas Carion committed
270
    return count > 0;
271
}   
272

273
274
275
276
277
278
bool TimelineFunctions::requestClipCutAll(std::shared_ptr<TimelineItemModel> timeline, int position)
{
    QVector<std::shared_ptr<TrackModel>> affectedTracks;
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };

Vincent Pinon's avatar
Vincent Pinon committed
279
    for (const auto &track: timeline->m_allTracks) {
280
281
282
283
284
285
        if (!track->isLocked()) {
            affectedTracks << track;
        }
    }

    if (affectedTracks.isEmpty()) {
286
        pCore->displayMessage(i18n("All tracks are locked"), ErrorMessage, 500);
287
288
289
290
        return false;
    }

    unsigned count = 0;
Vincent Pinon's avatar
Vincent Pinon committed
291
    for (auto track: qAsConst(affectedTracks)) {
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
        int clipId = track->getClipByPosition(position);
        if (clipId > -1) {
            // Found clip at position in track, cut it. Update undo/redo as we go.
            if (!TimelineFunctions::requestClipCut(timeline, clipId, position, undo, redo)) {
                qWarning() << "Failed to cut clip " << clipId << " at " << position;
                pCore->displayMessage(i18n("Failed to cut clip"), ErrorMessage, 500);
                // Undo all cuts made, assert successful undo.
                bool undone = undo();
                Q_ASSERT(undone);
                return false;
            }
            count++;
        }
    }

    if (!count) {
308
        pCore->displayMessage(i18n("No clips to cut"), ErrorMessage);
309
310
311
312
313
314
315
    } else {
        pCore->pushUndo(undo, redo, i18n("Cut all clips"));
    }

    return count > 0;
}

Nicolas Carion's avatar
Nicolas Carion committed
316
int TimelineFunctions::requestSpacerStartOperation(const std::shared_ptr<TimelineItemModel> &timeline, int trackId, int position)
317
{
Nicolas Carion's avatar
Nicolas Carion committed
318
    std::unordered_set<int> clips = timeline->getItemsInRange(trackId, position, -1);
319
    timeline->requestClearSelection();
320
    spacerMinPosition = -1;
Nicolas Carion's avatar
Nicolas Carion committed
321
    if (!clips.empty()) {
322
323
324
325
326
327
        // Remove grouped items that are before the click position
        // First get top groups ids
        std::unordered_set<int> roots;
        spacerUngroupedItems.clear();
        std::transform(clips.begin(), clips.end(), std::inserter(roots, roots.begin()), [&](int id) { return timeline->m_groups->getRootId(id); });
        std::unordered_set<int> groupsToRemove;
328
329
        int firstCid = -1;
        int firstPosition = -1;
330
331
332
333
334
335
        for (int r : roots) {
            if (timeline->isGroup(r)) {
                std::unordered_set<int> leaves = timeline->m_groups->getLeaves(r);
                std::unordered_set<int> leavesToRemove;
                std::unordered_set<int> leavesToKeep;
                for (int l : leaves) {
336
337
                    int pos = timeline->getItemPosition(l);
                    if (pos + timeline->getItemPlaytime(l) < position) {
338
339
340
                        leavesToRemove.insert(l);
                    } else {
                        leavesToKeep.insert(l);
341
342
343
344
345
                        // Find first item
                        if (firstPosition == -1 || pos < firstPosition) {
                            firstCid = l;
                            firstPosition = pos;
                        }
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
                    }
                }
                if (leavesToKeep.size() == 1) {
                    // Only 1 item left in group, group will be deleted
                    int master = *leavesToKeep.begin();
                    roots.insert(master);
                    for (int l : leavesToRemove) {
                        spacerUngroupedItems.insert(l, master);
                    }
                    groupsToRemove.insert(r);
                } else {
                    for (int l : leavesToRemove) {
                        spacerUngroupedItems.insert(l, r);
                    }
                }
361
362
363
364
365
366
            } else {
                int pos = timeline->getItemPosition(r);
                if (firstPosition == -1 || pos < firstPosition) {
                    firstCid = r;
                    firstPosition = pos;
                }
367
368
369
370
371
372
373
374
375
376
377
378
379
            }
        }
        for (int r : groupsToRemove) {
            roots.erase(r);
        }
        Fun undo = []() { return true; };
        Fun redo = []() { return true; };
        QMapIterator<int, int> i(spacerUngroupedItems);
        while (i.hasNext()) {
            i.next();
            timeline->m_groups->ungroupItem(i.key(), undo, redo);
        }
        timeline->requestSetSelection(roots);
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
        if (firstPosition > 0) {
            // Find minimum position, parse all tracks
            if (trackId > -1) {
                // Easy, check blank size
                int spaceDuration = timeline->getTrackById_const(trackId)->getBlankSizeAtPos(firstPosition - 1);
                if (spaceDuration > 0 ) {
                    spacerMinPosition = firstPosition - spaceDuration;
                }
            } else {
                // Check space in all tracks
                auto it = timeline->m_allTracks.cbegin();
                int space = -1;
                while (it != timeline->m_allTracks.cend()) {
                    int spaceDuration = timeline->getTrackById_const((*it)->getId())->getBlankSizeAtPos(firstPosition - 1);
                    if (space == -1 || spaceDuration < space) {
                        space = spaceDuration;
                    }
                    ++it;
                }
                if (space > -1) {
                    spacerMinPosition = firstPosition - space;
                }
            }
        }
404
        return (firstCid);
405
406
407
    }
    return -1;
}
408

409
bool TimelineFunctions::requestSpacerEndOperation(const std::shared_ptr<TimelineItemModel> &timeline, int itemId, int startPosition, int endPosition, int affectedTrack, bool moveGuides, Fun &undo, Fun &redo)
410
411
{
    // Move group back to original position
412
    spacerMinPosition = -1;
413
414
415
    int track = timeline->getItemTrackId(itemId);
    bool isClip = timeline->isClip(itemId);
    if (isClip) {
416
        timeline->requestClipMove(itemId, track, startPosition, true, false, false, false, true);
417
    } else if (timeline->isComposition(itemId)) {
418
        timeline->requestCompositionMove(itemId, track, startPosition, false, false);
419
420
    } else {
        timeline->requestSubtitleMove(itemId, startPosition, false, false);
421
    }
422
    // Move guides
423
    if (moveGuides) {
424
425
        GenTime fromPos(startPosition, pCore->getCurrentFps());
        GenTime toPos(endPosition, pCore->getCurrentFps());
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
426
427
        QList<CommentedTime> guides = pCore->projectManager()->getGuideModel()->getMarkersInRange(startPosition, -1);
        pCore->projectManager()->getGuideModel()->moveMarkers(guides, fromPos, toPos, undo, redo);
428
429
    }

430
    std::unordered_set<int> clips = timeline->getGroupElements(itemId);
431
    int mainGroup = timeline->m_groups->getRootId(itemId);
432
    bool final = false;
433
434
435
436
437
438
439
440
441
442
443
444
445
    bool liftOk = true;
    if (timeline->m_editMode == TimelineMode::OverwriteEdit && endPosition < startPosition) {
        // Remove zone between end and start pos
        if (affectedTrack == -1) {
            // touch all tracks
            auto it = timeline->m_allTracks.cbegin();
            while (it != timeline->m_allTracks.cend()) {
                int target_track = (*it)->getId();
                if (!timeline->getTrackById_const(target_track)->isLocked()) {
                    liftOk = liftOk && TimelineFunctions::liftZone(timeline, target_track, QPoint(endPosition, startPosition), undo, redo);
                }
                ++it;
            }
446
        } else if (timeline->isTrack(affectedTrack)) {
447
448
449
            liftOk = TimelineFunctions::liftZone(timeline, affectedTrack, QPoint(endPosition, startPosition), undo, redo);
        }
        // The lift operation destroys selection group, so regroup now
450
        if (clips.size() > 1) {
451
452
453
454
455
456
457
            timeline->requestSetSelection(clips);
            mainGroup = timeline->m_groups->getRootId(itemId);
        }
    }
    if (liftOk && (mainGroup > -1 || clips.size() == 1)) {
        if (clips.size() > 1) {
            final = timeline->requestGroupMove(itemId, mainGroup, 0, endPosition - startPosition, true, true, undo, redo);
458
459
        } else {
            // only 1 clip to be moved
460
            if (isClip) {
461
                final = timeline->requestClipMove(itemId, track, endPosition, true, true, true, true, undo, redo);
462
            } else if (timeline->isComposition(itemId)) {
463
                final = timeline->requestCompositionMove(itemId, track, -1, endPosition, true, true, undo, redo);
464
            } else {
465
                final = timeline->requestSubtitleMove(itemId, endPosition, true, true, true, true, undo, redo);
466
            }
467
        }
468
    }
469
    timeline->requestClearSelection();
470
    if (final) {
471
472
        if (startPosition < endPosition) {
            pCore->pushUndo(undo, redo, i18n("Insert space"));
Nicolas Carion's avatar
Nicolas Carion committed
473
474
        } else {
            pCore->pushUndo(undo, redo, i18n("Remove space"));
475
        }
476
477
478
479
480
481
482
483
484
485
486
487
488
489
        // Regroup temporarily ungrouped items
        QMapIterator<int, int> i(spacerUngroupedItems);
        Fun local_undo = []() { return true; };
        Fun local_redo = []() { return true; };
        while (i.hasNext()) {
            i.next();
            if (timeline->isGroup(i.value())) {
                timeline->m_groups->setInGroupOf(i.key(), i.value(), local_undo, local_redo);
            } else {
                std::unordered_set<int> items = {i.key(), i.value()};
                timeline->m_groups->groupItems(items, local_undo, local_redo);
            }
        }
        spacerUngroupedItems.clear();
490
        return true;
491
492
    } else {
        undo();
493
494
495
    }
    return false;
}
496

497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513

bool TimelineFunctions::breakAffectedGroups(const std::shared_ptr<TimelineItemModel> &timeline, QVector<int> tracks, QPoint zone, Fun &undo, Fun &redo)
{
    // Check if we have grouped clips that are on unaffected tracks, and ungroup them
    bool result = true;
    std::unordered_set<int> affectedItems;
    // First find all affected items
    for (int &trackId : tracks) {
        std::unordered_set<int> items = timeline->getItemsInRange(trackId, zone.x(), zone.y());
        affectedItems.insert(items.begin(), items.end());
    }
    for (int item : affectedItems) {
        if (timeline->m_groups->isInGroup(item)) {
            int groupId = timeline->m_groups->getRootId(item);
            std::unordered_set<int> all_children = timeline->m_groups->getLeaves(groupId);
            for (int child: all_children) {
                int childTrackId = timeline->getItemTrackId(child);
514
                if (!tracks.contains(childTrackId) && timeline->m_groups->isInGroup(child)) {
515
516
517
518
519
520
521
522
523
                    // This item should not be affected by the operation, ungroup it
                    result = result && timeline->requestClipUngroup(child, undo, redo);
                }
            }
        }
    }
    return result;
}

Nicolas Carion's avatar
Nicolas Carion committed
524
bool TimelineFunctions::extractZone(const std::shared_ptr<TimelineItemModel> &timeline, QVector<int> tracks, QPoint zone, bool liftOnly)
525
526
527
528
{
    // Start undoable command
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };
529
    bool result = true;
530
    result = breakAffectedGroups(timeline, tracks, zone, undo, redo);
531

532
    for (int &trackId : tracks) {
533
534
535
        if (timeline->getTrackById_const(trackId)->isLocked()) {
            continue;
        }
536
        result = result && TimelineFunctions::liftZone(timeline, trackId, zone, undo, redo);
537
538
    }
    if (result && !liftOnly) {
539
        result = TimelineFunctions::removeSpace(timeline, zone, undo, redo, tracks);
540
    }
541
    pCore->pushUndo(undo, redo, liftOnly ? i18n("Lift zone") : i18n("Extract zone"));
542
543
544
    return result;
}

Nicolas Carion's avatar
Nicolas Carion committed
545
bool TimelineFunctions::insertZone(const std::shared_ptr<TimelineItemModel> &timeline, QList<int> trackIds, const QString &binId, int insertFrame, QPoint zone,
546
                                   bool overwrite, bool useTargets)
547
548
549
{
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };
550
551
552
553
    bool res = TimelineFunctions::insertZone(timeline, trackIds, binId, insertFrame, zone, overwrite, useTargets, undo, redo);
    if (res) {
        pCore->pushUndo(undo, redo, overwrite ? i18n("Overwrite zone") : i18n("Insert zone"));
    } else {
554
        pCore->displayMessage(i18n("Could not insert zone"), ErrorMessage);
555
556
557
558
559
560
561
562
563
        undo();
    }
    return res;
}

bool TimelineFunctions::insertZone(const std::shared_ptr<TimelineItemModel> &timeline, QList<int> trackIds, const QString &binId, int insertFrame, QPoint zone,
                                   bool overwrite, bool useTargets, Fun &undo, Fun &redo)
{
    // Start undoable command
564
    bool result = true;
565
566
    QVector<int> affectedTracks;
    auto it = timeline->m_allTracks.cbegin();
567
    if (!useTargets) {
568
569
570
571
572
573
574
575
576
        // Timeline drop in overwrite mode
        for (int target_track : trackIds) {
            if (!timeline->getTrackById_const(target_track)->isLocked()) {
                affectedTracks << target_track;
            }
        }
    } else {
        while (it != timeline->m_allTracks.cend()) {
            int target_track = (*it)->getId();
577
            if (timeline->getTrackById_const(target_track)->shouldReceiveTimelineOp()) {
578
579
580
581
582
583
                affectedTracks << target_track;
            } else if (trackIds.contains(target_track)) {
                // Track is marked as target but not active, remove it
                trackIds.removeAll(target_track);
            }
            ++it;
584
585
        }
    }
586
    if (affectedTracks.isEmpty()) {
587
        pCore->displayMessage(i18n("Please activate a track by clicking on a track's label"), ErrorMessage);
588
589
        return false;
    }
590
    result = breakAffectedGroups(timeline, affectedTracks, QPoint(insertFrame, insertFrame + (zone.y() - zone.x())), undo, redo);
591
    if (overwrite) {
592
        // Cut all tracks
Vincent Pinon's avatar
Vincent Pinon committed
593
        for (int target_track : qAsConst(affectedTracks)) {
594
            result = result && TimelineFunctions::liftZone(timeline, target_track, QPoint(insertFrame, insertFrame + (zone.y() - zone.x())), undo, redo);
595
            if (!result) {
596
                qDebug() << "// LIFTING ZONE FAILED\n";
597
598
                break;
            }
599
        }
600
    } else {
601
        // Cut all tracks
Vincent Pinon's avatar
Vincent Pinon committed
602
        for (int target_track : qAsConst(affectedTracks)) {
603
604
605
            int startClipId = timeline->getClipByPosition(target_track, insertFrame);
            if (startClipId > -1) {
                // There is a clip, cut it
606
                result = result && TimelineFunctions::requestClipCut(timeline, startClipId, insertFrame, undo, redo);
607
            }
608
        }
609
        result = result && TimelineFunctions::requestInsertSpace(timeline, QPoint(insertFrame, insertFrame + (zone.y() - zone.x())), undo, redo, affectedTracks);
610
    }
611
    if (result) {
612
613
        if (!trackIds.isEmpty()) {
            int newId = -1;
614
615
616
617
618
619
            QString binClipId;
            if (binId.contains(QLatin1Char('/'))) {
                binClipId = QString("%1/%2/%3").arg(binId.section(QLatin1Char('/'), 0, 0)).arg(zone.x()).arg(zone.y() - 1);
            } else {
                binClipId = QString("%1/%2/%3").arg(binId).arg(zone.x()).arg(zone.y() - 1);
            }
620
            result = timeline->requestClipInsertion(binClipId, trackIds.first(), insertFrame, newId, true, true, useTargets, undo, redo, affectedTracks);
621
        }
622
    }
623
    return result;
624
625
}

Nicolas Carion's avatar
Nicolas Carion committed
626
bool TimelineFunctions::liftZone(const std::shared_ptr<TimelineItemModel> &timeline, int trackId, QPoint zone, Fun &undo, Fun &redo)
627
628
629
{
    // Check if there is a clip at start point
    int startClipId = timeline->getClipByPosition(trackId, zone.x());
630
631
    if (startClipId > -1) {
        // There is a clip, cut it
632
        if (timeline->getClipPosition(startClipId) < zone.x()) {
633
            qDebug() << "/// CUTTING AT START: " << zone.x() << ", ID: " << startClipId;
634
            TimelineFunctions::requestClipCut(timeline, startClipId, zone.x(), undo, redo);
635
            qDebug() << "/// CUTTING AT START DONE";
636
        }
637
638
639
640
    }
    int endClipId = timeline->getClipByPosition(trackId, zone.y());
    if (endClipId > -1) {
        // There is a clip, cut it
641
        if (timeline->getClipPosition(endClipId) + timeline->getClipPlaytime(endClipId) > zone.y()) {
642
            qDebug() << "/// CUTTING AT END: " << zone.y() << ", ID: " << endClipId;
643
            TimelineFunctions::requestClipCut(timeline, endClipId, zone.y(), undo, redo);
644
            qDebug() << "/// CUTTING AT END DONE";
645
        }
646
    }
Nicolas Carion's avatar
Nicolas Carion committed
647
    std::unordered_set<int> clips = timeline->getItemsInRange(trackId, zone.x(), zone.y());
648
    for (const auto &clipId : clips) {
649
        timeline->requestClipUngroup(clipId, undo, redo);
650
        timeline->requestItemDeletion(clipId, undo, redo);
651
    }
652
653
654
    return true;
}

655
bool TimelineFunctions::removeSpace(const std::shared_ptr<TimelineItemModel> &timeline, QPoint zone, Fun &undo, Fun &redo, QVector<int> allowedTracks, bool useTargets)
656
{
657
    std::unordered_set<int> clips;
658
659
660
661
662
663
664
665
666
667
668
669
670
    if (useTargets) {
        auto it = timeline->m_allTracks.cbegin();
        while (it != timeline->m_allTracks.cend()) {
            int target_track = (*it)->getId();
            if (timeline->getTrackById_const(target_track)->shouldReceiveTimelineOp()) {
                std::unordered_set<int> subs = timeline->getItemsInRange(target_track, zone.y() - 1, -1, true);
                clips.insert(subs.begin(), subs.end());
            }
            ++it;
        }
    } else {
        for (int &tid : allowedTracks) {
            std::unordered_set<int> subs = timeline->getItemsInRange(tid, zone.y() - 1, -1, true);
671
672
673
            clips.insert(subs.begin(), subs.end());
        }
    }
674
675
676
677
    if (clips.size() == 0) {
        // TODO: inform user no change will be performed
        return true;
    }
678
    bool result = false;
679
680
681
682
683
684
    timeline->requestSetSelection(clips);
    int itemId = *clips.begin();
    int targetTrackId = timeline->getItemTrackId(itemId);
    int targetPos = timeline->getItemPosition(itemId) + zone.x() - zone.y();

    if (timeline->m_groups->isInGroup(itemId)) {
685
        result = timeline->requestGroupMove(itemId, timeline->m_groups->getRootId(itemId), 0, zone.x() - zone.y(), true, true, undo, redo, true, true, true, allowedTracks);
686
687
688
689
690
691
692
693
    } else if (timeline->isClip(itemId)) {
        result = timeline->requestClipMove(itemId, targetTrackId, targetPos, true, true, true, true, undo, redo);
    } else {
        result = timeline->requestCompositionMove(itemId, targetTrackId, timeline->m_allCompositions[itemId]->getForcedTrack(), targetPos, true, true, undo, redo);
    }
    timeline->requestClearSelection();
    if (!result) {
        undo();
694
695
696
697
    }
    return result;
}

698
bool TimelineFunctions::requestInsertSpace(const std::shared_ptr<TimelineItemModel> &timeline, QPoint zone, Fun &undo, Fun &redo, QVector<int> allowedTracks)
699
{
700
701
702
    timeline->requestClearSelection();
    Fun local_undo = []() { return true; };
    Fun local_redo = []() { return true; };
703
    std::unordered_set<int> items;
704
    if (allowedTracks.isEmpty()) {
705
706
707
708
        // Select clips in all tracks
        items = timeline->getItemsInRange(-1, zone.x(), -1, true);
    } else {
        // Select clips in target and active tracks only
709
710
711
        for (int target_track : allowedTracks) {
            std::unordered_set<int> subs = timeline->getItemsInRange(target_track, zone.x(), -1, true);
            items.insert(subs.begin(), subs.end());
712
713
        }
    }
714
715
716
717
    if (items.empty()) {
        return true;
    }
    timeline->requestSetSelection(items);
718
    bool result = true;
719
720
721
722
723
724
725
    int itemId = *(items.begin());
    int targetTrackId = timeline->getItemTrackId(itemId);
    int targetPos = timeline->getItemPosition(itemId) + zone.y() - zone.x();

    // TODO the three move functions should be unified in a "requestItemMove" function
    if (timeline->m_groups->isInGroup(itemId)) {
        result =
726
            result && timeline->requestGroupMove(itemId, timeline->m_groups->getRootId(itemId), 0, zone.y() - zone.x(), true, true, local_undo, local_redo, true, true, true, allowedTracks);
727
    } else if (timeline->isClip(itemId)) {
728
        result = result && timeline->requestClipMove(itemId, targetTrackId, targetPos, true, true, true, true, local_undo, local_redo);
729
730
731
732
733
734
735
736
737
    } else {
        result = result && timeline->requestCompositionMove(itemId, targetTrackId, timeline->m_allCompositions[itemId]->getForcedTrack(), targetPos, true, true,
                                                            local_undo, local_redo);
    }
    timeline->requestClearSelection();
    if (!result) {
        bool undone = local_undo();
        Q_ASSERT(undone);
        pCore->displayMessage(i18n("Cannot move selected group"), ErrorMessage);
738
    }
739
    UPDATE_UNDO_REDO_NOLOCK(local_redo, local_undo, undo, redo);
740
    return result;
741
}
742

Nicolas Carion's avatar
Nicolas Carion committed
743
bool TimelineFunctions::requestItemCopy(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, int trackId, int position)
744
745
746
747
{
    Q_ASSERT(timeline->isClip(clipId) || timeline->isComposition(clipId));
    Fun undo = []() { return true; };
    Fun redo = []() { return true; };
748
749
    int deltaTrack = timeline->getTrackPosition(trackId) - timeline->getTrackPosition(timeline->getItemTrackId(clipId));
    int deltaPos = position - timeline->getItemPosition(clipId);
750
    std::unordered_set<int> allIds = timeline->getGroupElements(clipId);
Nicolas Carion's avatar
Nicolas Carion committed
751
    std::unordered_map<int, int> mapping; // keys are ids of the source clips, values are ids of the copied clips
752
    bool res = true;
753
754
    for (int id : allIds) {
        int newId = -1;
755
        if (timeline->isClip(id)) {
756
            PlaylistState::ClipState state = timeline->m_allClips[id]->clipState();
757
            res = cloneClip(timeline, id, newId, state, undo, redo);
758
759
760
761
            res = res && (newId != -1);
        }
        int target_position = timeline->getItemPosition(id) + deltaPos;
        int target_track_position = timeline->getTrackPosition(timeline->getItemTrackId(id)) + deltaTrack;
762
763
764
765
        if (target_track_position >= 0 && target_track_position < timeline->getTracksCount()) {
            auto it = timeline->m_allTracks.cbegin();
            std::advance(it, target_track_position);
            int target_track = (*it)->getId();
766
            if (timeline->isClip(id)) {
767
                res = res && timeline->requestClipMove(newId, target_track, target_position, true, true, true, true, undo, redo);
768
769
            } else {
                const QString &transitionId = timeline->m_allCompositions[id]->getAssetId();
770
                std::unique_ptr<Mlt::Properties> transProps(timeline->m_allCompositions[id]->properties());
Nicolas Carion's avatar
Nicolas Carion committed
771
772
                res = res && timeline->requestCompositionInsertion(transitionId, target_track, -1, target_position,
                                                                   timeline->m_allCompositions[id]->getPlaytime(), std::move(transProps), newId, undo, redo);
773
            }
774
775
776
777
778
779
780
781
        } else {
            res = false;
        }
        if (!res) {
            bool undone = undo();
            Q_ASSERT(undone);
            return false;
        }
Nicolas Carion's avatar
Nicolas Carion committed
782
783
        mapping[id] = newId;
    }
784
    qDebug() << "Successful copy, copying groups...";
785
    res = timeline->m_groups->copyGroups(mapping, undo, redo);
Nicolas Carion's avatar
Nicolas Carion committed
786
787
788
789
    if (!res) {
        bool undone = undo();
        Q_ASSERT(undone);
        return false;
790
791
792
793
    }
    return true;
}

Nicolas Carion's avatar
Nicolas Carion committed
794
void TimelineFunctions::showClipKeyframes(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, bool value)
795
796
797
{
    timeline->m_allClips[clipId]->setShowKeyframes(value);
    QModelIndex modelIndex = timeline->makeClipIndexFromID(clipId);
Vincent Pinon's avatar
Vincent Pinon committed
798
    emit timeline->dataChanged(modelIndex, modelIndex, {TimelineModel::ShowKeyframesRole});
799
}
800

Nicolas Carion's avatar
Nicolas Carion committed
801
void TimelineFunctions::showCompositionKeyframes(const std::shared_ptr<TimelineItemModel> &timeline, int compoId, bool value)
802
803
804
{
    timeline->m_allCompositions[compoId]->setShowKeyframes(value);
    QModelIndex modelIndex = timeline->makeCompositionIndexFromID(compoId);
Vincent Pinon's avatar
Vincent Pinon committed
805
    emit timeline->dataChanged(modelIndex, modelIndex, {TimelineModel::ShowKeyframesRole});
806
807
}

808
bool TimelineFunctions::switchEnableState(const std::shared_ptr<TimelineItemModel> &timeline, std::unordered_set<int> selection)
809
{
810
811
    Fun undo = []() { return true; };
    Fun redo = []() { return true; };
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
    bool result = false;
    bool disable = true;
    for (int clipId : selection) {
        if (!timeline->isClip(clipId)) {
            continue;
        }
        PlaylistState::ClipState oldState = timeline->getClipPtr(clipId)->clipState();
        PlaylistState::ClipState state = PlaylistState::Disabled;
        disable = true;
        if (oldState == PlaylistState::Disabled) {
            state = timeline->getTrackById_const(timeline->getClipTrackId(clipId))->trackType();
            disable = false;
        }
        result = changeClipState(timeline, clipId, state, undo, redo);
        if (!result) {
            break;
        }
    }
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
    // Update action name since clip will be switched
    int id = *selection.begin();
    Fun local_redo = []() { return true; };
    Fun local_undo = []() { return true; };
    if (timeline->isClip(id)) {
        bool disabled = timeline->m_allClips[id]->clipState() == PlaylistState::Disabled;
        QAction *action = pCore->window()->actionCollection()->action(QStringLiteral("clip_switch"));
        local_redo = [disabled, action]() {
            action->setText(disabled ? i18n("Enable clip") : i18n("Disable clip"));
            return true;
        };
        local_undo = [disabled, action]() {
            action->setText(disabled ? i18n("Disable clip") : i18n("Enable clip"));
            return true;
        };
    }
846
    if (result) {
847
848
849
        local_redo();
        UPDATE_UNDO_REDO_NOLOCK(local_redo, local_undo, undo, redo);
        pCore->pushUndo(undo, redo, disable ? i18n("Disable clip") : i18n("Enable clip"));
850
851
852
853
    }
    return result;
}

Nicolas Carion's avatar
Nicolas Carion committed
854
bool TimelineFunctions::changeClipState(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, PlaylistState::ClipState status, Fun &undo, Fun &redo)
855
{
856
857
    int track = timeline->getClipTrackId(clipId);
    int start = -1;
858
    bool invalidate = false;
859
860
    if (track > -1) {
        if (!timeline->getTrackById_const(track)->isAudioTrack()) {
861
            invalidate = true;
862
        }
863
        start = timeline->getItemPosition(clipId);
864
    }
865
866
    Fun local_undo = []() { return true; };
    Fun local_redo = []() { return true; };
867
868
869
    // For the state change to work, we need to unplant/replant the clip
    bool result = true;
    if (track > -1) {
870
        result = timeline->getTrackById(track)->requestClipDeletion(clipId, true, invalidate, local_undo, local_redo, false, false);
871
872
873
874
    }
    result = timeline->m_allClips[clipId]->setClipState(status, local_undo, local_redo);
    if (result && track > -1) {
        result = timeline->getTrackById(track)->requestClipInsertion(clipId, start, true, true, local_undo, local_redo);
875
    }
876
    UPDATE_UNDO_REDO_NOLOCK(local_redo, local_undo, undo, redo);
877
878
    return result;
}
879

Nicolas Carion's avatar
Nicolas Carion committed
880
bool TimelineFunctions::requestSplitAudio(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, int audioTarget)
881
882
883
884
{
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };
    const std::unordered_set<int> clips = timeline->getGroupElements(clipId);
885
    bool done = false;
886
    // Now clear selection so we don't mess with groups
887
    timeline->requestClearSelection(false, undo, redo);
888
    for (int cid : clips) {
889
        if (!timeline->getClipPtr(cid)->canBeAudio() || timeline->getClipPtr(cid)->clipState() == PlaylistState::AudioOnly) {
890
            // clip without audio or audio only, skip
Pino Toscano's avatar
Pino Toscano committed
891
            pCore->displayMessage(i18n("One or more clips do not have audio, or are already audio"), ErrorMessage);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
892
            return false;
893
        }
894
895
        int position = timeline->getClipPosition(cid);
        int track = timeline->getClipTrackId(cid);
896
897
898
899
900
901
902
903
904
        QList<int> possibleTracks;
        if (audioTarget >= 0) {
            possibleTracks = {audioTarget};
        } else {
            int mirror = timeline->getMirrorAudioTrackId(track);
            if (mirror > -1) {
                possibleTracks = {mirror};
            }
        }
905
        if (possibleTracks.isEmpty()) {
906
907
            // No available audio track for splitting, abort
            undo();
908
            pCore->displayMessage(i18n("No available audio track for restore operation"), ErrorMessage);
909
910
            return false;
        }
911
        int newId;
912
        bool res = cloneClip(timeline, cid, newId, PlaylistState::AudioOnly, undo, redo);
913
914
915
        if (!res) {
            bool undone = undo();
            Q_ASSERT(undone);
916
            pCore->displayMessage(i18n("Audio restore failed"), ErrorMessage);
917
918
919
920
            return false;
        }