timelinefunctions.cpp 92.5 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
/*
Copyright (C) 2017  Jean-Baptiste Mardelle <jb@kdenlive.org>
This file is part of Kdenlive. See www.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) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
by the membership of KDE e.V.), which shall act as a proxy
defined in Section 14 of version 3 of the license.

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, see <http://www.gnu.org/licenses/>.
*/

#include "timelinefunctions.hpp"
23
24
25
26
#include "bin/bin.h"
#include "bin/projectclip.h"
#include "bin/projectfolder.h"
#include "bin/projectitemmodel.h"
27
#include "bin/model/subtitlemodel.hpp"
28
#include "clipmodel.hpp"
29
#include "compositionmodel.hpp"
Nicolas Carion's avatar
linting    
Nicolas Carion committed
30
#include "core.h"
31
#include "doc/kdenlivedoc.h"
32
#include "effects/effectstack/model/effectstackmodel.hpp"
33
#include "groupsmodel.hpp"
34
#include "logger.hpp"
35
36
#include "timelineitemmodel.hpp"
#include "trackmodel.hpp"
37
#include "transitions/transitionsrepository.hpp"
38
#include "mainwindow.h"
39

40
#include <QApplication>
41
#include <QDebug>
42
#include <QInputDialog>
43
#include <QSemaphore>
Nicolas Carion's avatar
linting    
Nicolas Carion committed
44
#include <klocalizedstring.h>
45
#include <unordered_map>
46

47
48
49
50
51
52
53
54
55
#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

56
57
58
QStringList waitingBinIds;
QMap<QString, QString> mappedIds;
QMap<int, int> tracksMap;
59
QSemaphore semaphore(1);
60

61
62
63
64
65
66
67
68
RTTR_REGISTRATION
{
    using namespace rttr;
    registration::class_<TimelineFunctions>("TimelineFunctions")
        .method("requestClipCut", select_overload<bool(std::shared_ptr<TimelineItemModel>, int, int)>(&TimelineFunctions::requestClipCut))(
            parameter_names("timeline", "clipId", "position"));
}

69
70
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
71
{
72
73
    // Special case: slowmotion clips
    double clipSpeed = timeline->m_allClips[clipId]->getSpeed();
74
    bool warp_pitch = timeline->m_allClips[clipId]->getIntProperty(QStringLiteral("warp_pitch"));
75
76
    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
77
    timeline->m_allClips[newId]->m_endlessResize = timeline->m_allClips[clipId]->m_endlessResize;
78

79
80
81
    // copy useful timeline properties
    timeline->m_allClips[clipId]->passTimelineProperties(timeline->m_allClips[newId]);

Nicolas Carion's avatar
Nicolas Carion committed
82
83
84
85
    int duration = timeline->getClipPlaytime(clipId);
    int init_duration = timeline->getClipPlaytime(newId);
    if (duration != init_duration) {
        int in = timeline->m_allClips[clipId]->getIn();
86
87
        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
88
89
90
91
92
93
    }
    if (!res) {
        return false;
    }
    std::shared_ptr<EffectStackModel> sourceStack = timeline->getClipEffectStackModel(clipId);
    std::shared_ptr<EffectStackModel> destStack = timeline->getClipEffectStackModel(newId);
94
    destStack->importEffects(sourceStack, state);
Nicolas Carion's avatar
Nicolas Carion committed
95
96
97
    return res;
}

Nicolas Carion's avatar
Nicolas Carion committed
98
bool TimelineFunctions::requestMultipleClipsInsertion(const std::shared_ptr<TimelineItemModel> &timeline, const QStringList &binIds, int trackId, int position,
99
                                                      QList<int> &clipIds, bool logUndo, bool refreshView)
100
101
102
103
104
{
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };
    for (const QString &binId : binIds) {
        int clipId;
105
        if (timeline->requestClipInsertion(binId, trackId, position, clipId, logUndo, refreshView, false, undo, redo)) {
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
            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
122
bool TimelineFunctions::processClipCut(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, int position, int &newId, Fun &undo, Fun &redo)
123
{
124
125
126
127
128
    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);
129
130
131
    if (start > position || (start + duration) < position) {
        return false;
    }
132
133
134
    if (isSubtitle) {
        return timeline->cutSubtitle(position, undo, redo);
    }
135
    PlaylistState::ClipState state = timeline->m_allClips[clipId]->clipState();
136
    // Check if clip has an end Mix
137
    bool res = cloneClip(timeline, clipId, newId, state, undo, redo);
138
    timeline->m_blockRefresh = true;
139
    res = res && timeline->requestItemResize(clipId, position - start, true, true, undo, redo);
Nicolas Carion's avatar
Nicolas Carion committed
140
    int newDuration = timeline->getClipPlaytime(clipId);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
141
142
143
144
145
    // 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);
146
    res = res && timeline->requestItemResize(newId, duration - newDuration, false, true, undo, redo);
147
148
    // 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();
149
    timeline->m_allClips[newId]->setSubPlaylistIndex(timeline->m_allClips[clipId]->getSubPlaylistIndex(), trackId);
150
    res = res && timeline->requestClipMove(newId, trackId, position, true, true, false, true, undo, redo);
151
152
153
154
155
156
157
158
159
160
161
162
    
    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);
    }
    
163
164
    if (durationChanged) {
        // Track length changed, check project duration
165
        Fun updateDuration = [timeline]() {
166
167
168
169
170
171
            timeline->updateDuration();
            return true;
        };
        updateDuration();
        PUSH_LAMBDA(updateDuration, redo);
    }
172
    timeline->m_blockRefresh = false;
Nicolas Carion's avatar
Nicolas Carion committed
173
174
    return res;
}
175

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

Nicolas Carion's avatar
Nicolas Carion committed
189
bool TimelineFunctions::requestClipCut(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, int position, Fun &undo, Fun &redo)
190
{
191
192
193
194
    const std::unordered_set<int> clipselect = timeline->getGroupElements(clipId);
    // Remove locked items
    std::unordered_set<int> clips;
    for (int cid : clipselect) {
195
196
197
198
        if (timeline->isSubTitle(cid)) {
            clips.insert(cid);
            continue;
        }
199
200
201
        if (!timeline->isClip(cid)) {
            continue;
        }
202
        int tk = timeline->getClipTrackId(cid);
203
        if (tk != -1 && !timeline->getTrackById_const(tk)->isLocked()) {
204
205
206
            clips.insert(cid);
        }
    }
207
208
209
210
211
212
213
214
215
216
    // 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);
        }
    }

217
218
219
    // 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();

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

276
277
278
279
280
281
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
282
    for (const auto &track: timeline->m_allTracks) {
283
284
285
286
287
288
289
290
291
292
293
        if (!track->isLocked()) {
            affectedTracks << track;
        }
    }

    if (affectedTracks.isEmpty()) {
        pCore->displayMessage(i18n("All tracks are locked"), InformationMessage, 500);
        return false;
    }

    unsigned count = 0;
Vincent Pinon's avatar
Vincent Pinon committed
294
    for (auto track: qAsConst(affectedTracks)) {
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
        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) {
        pCore->displayMessage(i18n("No clips to cut"), InformationMessage);
    } else {
        pCore->pushUndo(undo, redo, i18n("Cut all clips"));
    }

    return count > 0;
}

Nicolas Carion's avatar
Nicolas Carion committed
319
int TimelineFunctions::requestSpacerStartOperation(const std::shared_ptr<TimelineItemModel> &timeline, int trackId, int position)
320
{
Nicolas Carion's avatar
Nicolas Carion committed
321
    std::unordered_set<int> clips = timeline->getItemsInRange(trackId, position, -1);
Nicolas Carion's avatar
Nicolas Carion committed
322
    if (!clips.empty()) {
323
        timeline->requestSetSelection(clips);
324
        return (*clips.cbegin());
325
326
327
    }
    return -1;
}
328

329
bool TimelineFunctions::requestSpacerEndOperation(const std::shared_ptr<TimelineItemModel> &timeline, int itemId, int startPosition, int endPosition, int affectedTrack)
330
331
{
    // Move group back to original position
332
333
334
    int track = timeline->getItemTrackId(itemId);
    bool isClip = timeline->isClip(itemId);
    if (isClip) {
335
        timeline->requestClipMove(itemId, track, startPosition, true, false, false);
336
    } else if (timeline->isComposition(itemId)) {
337
        timeline->requestCompositionMove(itemId, track, startPosition, false, false);
338
339
    } else {
        timeline->requestSubtitleMove(itemId, startPosition, false, false);
340
341
    }
    std::unordered_set<int> clips = timeline->getGroupElements(itemId);
342
343
344
    // Start undoable command
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };
345
    int mainGroup = timeline->m_groups->getRootId(itemId);
346
    bool final = false;
347
348
349
350
351
352
353
354
355
356
357
358
359
    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;
            }
360
        } else if (timeline->isTrack(affectedTrack)) {
361
362
363
            liftOk = TimelineFunctions::liftZone(timeline, affectedTrack, QPoint(endPosition, startPosition), undo, redo);
        }
        // The lift operation destroys selection group, so regroup now
364
        if (clips.size() > 1) {
365
366
367
368
369
370
371
            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);
372
373
        } else {
            // only 1 clip to be moved
374
            if (isClip) {
375
                final = timeline->requestClipMove(itemId, track, endPosition, true, true, true, true, undo, redo);
376
            } else if (timeline->isComposition(itemId)) {
377
                final = timeline->requestCompositionMove(itemId, track, -1, endPosition, true, true, undo, redo);
378
            } else {
379
                final = timeline->requestSubtitleMove(itemId, endPosition, true, true, true, true, undo, redo);
380
            }
381
        }
382
    }
383
    timeline->requestClearSelection();
384
    if (final) {
385
386
        if (startPosition < endPosition) {
            pCore->pushUndo(undo, redo, i18n("Insert space"));
Nicolas Carion's avatar
Nicolas Carion committed
387
388
        } else {
            pCore->pushUndo(undo, redo, i18n("Remove space"));
389
        }
390
        return true;
391
392
    } else {
        undo();
393
394
395
    }
    return false;
}
396

397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

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);
414
                if (!tracks.contains(childTrackId) && timeline->m_groups->isInGroup(child)) {
415
416
417
418
419
420
421
422
423
                    // 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
424
bool TimelineFunctions::extractZone(const std::shared_ptr<TimelineItemModel> &timeline, QVector<int> tracks, QPoint zone, bool liftOnly)
425
426
427
428
{
    // Start undoable command
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };
429
    bool result = true;
430
    result = breakAffectedGroups(timeline, tracks, zone, undo, redo);
431

432
    for (int &trackId : tracks) {
433
434
435
        if (timeline->getTrackById_const(trackId)->isLocked()) {
            continue;
        }
436
        result = result && TimelineFunctions::liftZone(timeline, trackId, zone, undo, redo);
437
438
    }
    if (result && !liftOnly) {
439
        result = TimelineFunctions::removeSpace(timeline, zone, undo, redo, tracks);
440
    }
441
    pCore->pushUndo(undo, redo, liftOnly ? i18n("Lift zone") : i18n("Extract zone"));
442
443
444
    return result;
}

Nicolas Carion's avatar
Nicolas Carion committed
445
bool TimelineFunctions::insertZone(const std::shared_ptr<TimelineItemModel> &timeline, QList<int> trackIds, const QString &binId, int insertFrame, QPoint zone,
446
                                   bool overwrite, bool useTargets)
447
448
449
{
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };
450
451
452
453
454
455
456
457
458
459
460
461
462
463
    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 {
        pCore->displayMessage(i18n("Could not insert zone"), InformationMessage);
        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
464
    bool result = true;
465
466
    QVector<int> affectedTracks;
    auto it = timeline->m_allTracks.cbegin();
467
    if (!useTargets) {
468
469
470
471
472
473
474
475
476
        // 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();
477
            if (timeline->getTrackById_const(target_track)->shouldReceiveTimelineOp()) {
478
479
480
481
482
483
                affectedTracks << target_track;
            } else if (trackIds.contains(target_track)) {
                // Track is marked as target but not active, remove it
                trackIds.removeAll(target_track);
            }
            ++it;
484
485
        }
    }
486
    if (affectedTracks.isEmpty()) {
487
        pCore->displayMessage(i18n("Please activate a track by clicking on a track's label"), InformationMessage);
488
489
        return false;
    }
490
    result = breakAffectedGroups(timeline, affectedTracks, QPoint(insertFrame, insertFrame + (zone.y() - zone.x())), undo, redo);
491
    if (overwrite) {
492
        // Cut all tracks
Vincent Pinon's avatar
Vincent Pinon committed
493
        for (int target_track : qAsConst(affectedTracks)) {
494
            result = result && TimelineFunctions::liftZone(timeline, target_track, QPoint(insertFrame, insertFrame + (zone.y() - zone.x())), undo, redo);
495
            if (!result) {
496
                qDebug() << "// LIFTING ZONE FAILED\n";
497
498
                break;
            }
499
        }
500
    } else {
501
        // Cut all tracks
Vincent Pinon's avatar
Vincent Pinon committed
502
        for (int target_track : qAsConst(affectedTracks)) {
503
504
505
            int startClipId = timeline->getClipByPosition(target_track, insertFrame);
            if (startClipId > -1) {
                // There is a clip, cut it
506
                result = result && TimelineFunctions::requestClipCut(timeline, startClipId, insertFrame, undo, redo);
507
            }
508
        }
509
        result = result && TimelineFunctions::requestInsertSpace(timeline, QPoint(insertFrame, insertFrame + (zone.y() - zone.x())), undo, redo, affectedTracks);
510
    }
511
    if (result) {
512
513
        if (!trackIds.isEmpty()) {
            int newId = -1;
514
515
516
517
518
519
            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);
            }
520
            result = timeline->requestClipInsertion(binClipId, trackIds.first(), insertFrame, newId, true, true, useTargets, undo, redo, affectedTracks);
521
        }
522
    }
523
    return result;
524
525
}

Nicolas Carion's avatar
Nicolas Carion committed
526
bool TimelineFunctions::liftZone(const std::shared_ptr<TimelineItemModel> &timeline, int trackId, QPoint zone, Fun &undo, Fun &redo)
527
528
529
{
    // Check if there is a clip at start point
    int startClipId = timeline->getClipByPosition(trackId, zone.x());
530
531
    if (startClipId > -1) {
        // There is a clip, cut it
532
        if (timeline->getClipPosition(startClipId) < zone.x()) {
533
            qDebug() << "/// CUTTING AT START: " << zone.x() << ", ID: " << startClipId;
534
            TimelineFunctions::requestClipCut(timeline, startClipId, zone.x(), undo, redo);
535
            qDebug() << "/// CUTTING AT START DONE";
536
        }
537
538
539
540
    }
    int endClipId = timeline->getClipByPosition(trackId, zone.y());
    if (endClipId > -1) {
        // There is a clip, cut it
541
        if (timeline->getClipPosition(endClipId) + timeline->getClipPlaytime(endClipId) > zone.y()) {
542
            qDebug() << "/// CUTTING AT END: " << zone.y() << ", ID: " << endClipId;
543
            TimelineFunctions::requestClipCut(timeline, endClipId, zone.y(), undo, redo);
544
            qDebug() << "/// CUTTING AT END DONE";
545
        }
546
    }
Nicolas Carion's avatar
Nicolas Carion committed
547
    std::unordered_set<int> clips = timeline->getItemsInRange(trackId, zone.x(), zone.y());
548
    for (const auto &clipId : clips) {
549
        timeline->requestClipUngroup(clipId, undo, redo);
550
        timeline->requestItemDeletion(clipId, undo, redo);
551
    }
552
553
554
    return true;
}

555
bool TimelineFunctions::removeSpace(const std::shared_ptr<TimelineItemModel> &timeline, QPoint zone, Fun &undo, Fun &redo, QVector<int> allowedTracks, bool useTargets)
556
{
557
    std::unordered_set<int> clips;
558
559
560
561
562
563
564
565
566
567
568
569
570
    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);
571
572
573
            clips.insert(subs.begin(), subs.end());
        }
    }
574
575
576
577
    if (clips.size() == 0) {
        // TODO: inform user no change will be performed
        return true;
    }
578
    bool result = false;
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
    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)) {
        result = timeline->requestGroupMove(itemId, timeline->m_groups->getRootId(itemId), 0, zone.x() - zone.y(), true, true, undo, redo, true, true, allowedTracks);
    } 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();
594
595
596
597
    }
    return result;
}

598
bool TimelineFunctions::requestInsertSpace(const std::shared_ptr<TimelineItemModel> &timeline, QPoint zone, Fun &undo, Fun &redo, QVector<int> allowedTracks)
599
{
600
601
602
    timeline->requestClearSelection();
    Fun local_undo = []() { return true; };
    Fun local_redo = []() { return true; };
603
    std::unordered_set<int> items;
604
    if (allowedTracks.isEmpty()) {
605
606
607
608
        // Select clips in all tracks
        items = timeline->getItemsInRange(-1, zone.x(), -1, true);
    } else {
        // Select clips in target and active tracks only
609
610
611
        for (int target_track : allowedTracks) {
            std::unordered_set<int> subs = timeline->getItemsInRange(target_track, zone.x(), -1, true);
            items.insert(subs.begin(), subs.end());
612
613
        }
    }
614
615
616
617
    if (items.empty()) {
        return true;
    }
    timeline->requestSetSelection(items);
618
    bool result = true;
619
620
621
622
623
624
625
    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 =
626
            result && timeline->requestGroupMove(itemId, timeline->m_groups->getRootId(itemId), 0, zone.y() - zone.x(), true, true, local_undo, local_redo, true, true, allowedTracks);
627
    } else if (timeline->isClip(itemId)) {
628
        result = result && timeline->requestClipMove(itemId, targetTrackId, targetPos, true, true, true, true, local_undo, local_redo);
629
630
631
632
633
634
635
636
637
    } 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);
638
    }
639
    UPDATE_UNDO_REDO_NOLOCK(local_redo, local_undo, undo, redo);
640
    return result;
641
}
642

Nicolas Carion's avatar
Nicolas Carion committed
643
bool TimelineFunctions::requestItemCopy(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, int trackId, int position)
644
645
646
647
{
    Q_ASSERT(timeline->isClip(clipId) || timeline->isComposition(clipId));
    Fun undo = []() { return true; };
    Fun redo = []() { return true; };
648
649
    int deltaTrack = timeline->getTrackPosition(trackId) - timeline->getTrackPosition(timeline->getItemTrackId(clipId));
    int deltaPos = position - timeline->getItemPosition(clipId);
650
    std::unordered_set<int> allIds = timeline->getGroupElements(clipId);
Nicolas Carion's avatar
Nicolas Carion committed
651
    std::unordered_map<int, int> mapping; // keys are ids of the source clips, values are ids of the copied clips
652
    bool res = true;
653
654
    for (int id : allIds) {
        int newId = -1;
655
        if (timeline->isClip(id)) {
656
            PlaylistState::ClipState state = timeline->m_allClips[id]->clipState();
657
            res = cloneClip(timeline, id, newId, state, undo, redo);
658
659
660
661
            res = res && (newId != -1);
        }
        int target_position = timeline->getItemPosition(id) + deltaPos;
        int target_track_position = timeline->getTrackPosition(timeline->getItemTrackId(id)) + deltaTrack;
662
663
664
665
        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();
666
            if (timeline->isClip(id)) {
667
                res = res && timeline->requestClipMove(newId, target_track, target_position, true, true, true, true, undo, redo);
668
669
            } else {
                const QString &transitionId = timeline->m_allCompositions[id]->getAssetId();
670
                std::unique_ptr<Mlt::Properties> transProps(timeline->m_allCompositions[id]->properties());
Nicolas Carion's avatar
Nicolas Carion committed
671
672
                res = res && timeline->requestCompositionInsertion(transitionId, target_track, -1, target_position,
                                                                   timeline->m_allCompositions[id]->getPlaytime(), std::move(transProps), newId, undo, redo);
673
            }
674
675
676
677
678
679
680
681
        } else {
            res = false;
        }
        if (!res) {
            bool undone = undo();
            Q_ASSERT(undone);
            return false;
        }
Nicolas Carion's avatar
Nicolas Carion committed
682
683
        mapping[id] = newId;
    }
684
    qDebug() << "Successful copy, coping groups...";
685
    res = timeline->m_groups->copyGroups(mapping, undo, redo);
Nicolas Carion's avatar
Nicolas Carion committed
686
687
688
689
    if (!res) {
        bool undone = undo();
        Q_ASSERT(undone);
        return false;
690
691
692
693
    }
    return true;
}

Nicolas Carion's avatar
Nicolas Carion committed
694
void TimelineFunctions::showClipKeyframes(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, bool value)
695
696
697
{
    timeline->m_allClips[clipId]->setShowKeyframes(value);
    QModelIndex modelIndex = timeline->makeClipIndexFromID(clipId);
Vincent Pinon's avatar
Vincent Pinon committed
698
    emit timeline->dataChanged(modelIndex, modelIndex, {TimelineModel::ShowKeyframesRole});
699
}
700

Nicolas Carion's avatar
Nicolas Carion committed
701
void TimelineFunctions::showCompositionKeyframes(const std::shared_ptr<TimelineItemModel> &timeline, int compoId, bool value)
702
703
704
{
    timeline->m_allCompositions[compoId]->setShowKeyframes(value);
    QModelIndex modelIndex = timeline->makeCompositionIndexFromID(compoId);
Vincent Pinon's avatar
Vincent Pinon committed
705
    emit timeline->dataChanged(modelIndex, modelIndex, {TimelineModel::ShowKeyframesRole});
706
707
}

708
bool TimelineFunctions::switchEnableState(const std::shared_ptr<TimelineItemModel> &timeline, std::unordered_set<int> selection)
709
{
710
711
    Fun undo = []() { return true; };
    Fun redo = []() { return true; };
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
    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;
        }
    }
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
    // 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;
        };
    }
746
    if (result) {
747
748
749
        local_redo();
        UPDATE_UNDO_REDO_NOLOCK(local_redo, local_undo, undo, redo);
        pCore->pushUndo(undo, redo, disable ? i18n("Disable clip") : i18n("Enable clip"));
750
751
752
753
    }
    return result;
}

Nicolas Carion's avatar
Nicolas Carion committed
754
bool TimelineFunctions::changeClipState(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, PlaylistState::ClipState status, Fun &undo, Fun &redo)
755
{
756
757
    int track = timeline->getClipTrackId(clipId);
    int start = -1;
758
    bool invalidate = false;
759
760
    if (track > -1) {
        if (!timeline->getTrackById_const(track)->isAudioTrack()) {
761
            invalidate = true;
762
        }
763
        start = timeline->getItemPosition(clipId);
764
    }
765
766
    Fun local_undo = []() { return true; };
    Fun local_redo = []() { return true; };
767
768
769
    // For the state change to work, we need to unplant/replant the clip
    bool result = true;
    if (track > -1) {
770
        result = timeline->getTrackById(track)->requestClipDeletion(clipId, true, invalidate, local_undo, local_redo, false, false);
771
772
773
774
    }
    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);
775
    }
776
    UPDATE_UNDO_REDO_NOLOCK(local_redo, local_undo, undo, redo);
777
778
    return result;
}
779

Nicolas Carion's avatar
Nicolas Carion committed
780
bool TimelineFunctions::requestSplitAudio(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, int audioTarget)
781
782
783
784
{
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };
    const std::unordered_set<int> clips = timeline->getGroupElements(clipId);
785
    bool done = false;
786
    // Now clear selection so we don't mess with groups
787
    timeline->requestClearSelection(false, undo, redo);
788
    for (int cid : clips) {
789
        if (!timeline->getClipPtr(cid)->canBeAudio() || timeline->getClipPtr(cid)->clipState() == PlaylistState::AudioOnly) {
790
            // clip without audio or audio only, skip
Pino Toscano's avatar
Pino Toscano committed
791
            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
792
            return false;
793
        }
794
795
        int position = timeline->getClipPosition(cid);
        int track = timeline->getClipTrackId(cid);
796
797
798
799
800
801
802
803
804
        QList<int> possibleTracks;
        if (audioTarget >= 0) {
            possibleTracks = {audioTarget};
        } else {
            int mirror = timeline->getMirrorAudioTrackId(track);
            if (mirror > -1) {
                possibleTracks = {mirror};
            }
        }
805
        if (possibleTracks.isEmpty()) {
806
807
            // No available audio track for splitting, abort
            undo();
808
            pCore->displayMessage(i18n("No available audio track for restore operation"), ErrorMessage);
809
810
            return false;
        }
811
        int newId;
812
        bool res = cloneClip(timeline, cid, newId, PlaylistState::AudioOnly, undo, redo);
813
814
815
        if (!res) {
            bool undone = undo();
            Q_ASSERT(undone);
816
            pCore->displayMessage(i18n("Audio restore failed"), ErrorMessage);
817
818
819
820
            return false;
        }
        bool success = false;
        while (!success && !possibleTracks.isEmpty()) {
821
            int newTrack = possibleTracks.takeFirst();
822
            success = timeline->requestClipMove(newId, newTrack, position, true, true, false, true, undo, redo);
823
        }
824
825
        TimelineFunctions::changeClipState(timeline, cid, PlaylistState::VideoOnly, undo, redo);
        success = success && timeline->m_groups->createGroupAtSameLevel(cid, std::unordered_set<int>{newId}, GroupType::AVSplit, undo, redo);
826
        if (!success) {
827
828
            bool undone = undo();
            Q_ASSERT(undone);
829
            pCore->displayMessage(i18n("Audio restore failed"), ErrorMessage);
830
831
            return false;
        }
832
        done = true;
833
    }
834
    if (done) {
835
        timeline->requestSetSelection(clips, undo, redo);
836
        pCore->pushUndo(undo, redo, i18n("Restore Audio"));
837
838
    }
    return done;
839
}
840

Nicolas Carion's avatar
Nicolas Carion committed
841
bool TimelineFunctions::requestSplitVideo(const std::shared_ptr<TimelineItemModel> &timeline, int clipId, int videoTarget)
842
843
844
845
846
847
{
    std::function<bool(void)> undo = []() { return true; };
    std::function<bool(void)> redo = []() { return true; };
    const std::unordered_set<int> clips = timeline->getGroupElements(clipId);
    bool done = false;
    // Now clear selection so we don't mess with groups
848
    timeline->requestClearSelection();
849
850
851
852
853
854
    for (int cid : clips) {
        if (!timeline->getClipPtr(cid)->canBeVideo() || timeline->getClipPtr(cid)->clipState() == PlaylistState::VideoOnly) {
            // clip without audio or audio only, skip
            continue;
        }
        int position = timeline->getClipPosition(cid);
855
856
857
858
859
860
861
862
863
864
        int track = timeline->getClipTrackId(cid);
        QList<int> possibleTracks;
        if (videoTarget >= 0) {
            possibleTracks = {videoTarget};
        } else {
            int mirror = timeline->getMirrorVideoTrackId(track);
            if (mirror > -1) {
                possibleTracks = {mirror};
            }
        }
865
866
867
        if (possibleTracks.isEmpty()) {
            // No available audio track for splitting, abort
            undo();
868
            pCore->displayMessage(i18n("No available video track for restore operation"), ErrorMessage);
869
870
871
            return false;
        }
        int newId;
872
        bool res = cloneClip(timeline, cid, newId, PlaylistState::VideoOnly, undo, redo);
873
874
875
        if (!res) {
            bool undone = undo();
            Q_ASSERT(undone);
876
            pCore->displayMessage(i18n("Video restore failed"), ErrorMessage);
877
878
879
880
881
            return false;
        }
        bool success = false;
        while (!success && !possibleTracks.isEmpty()) {
            int newTrack = possibleTracks.takeFirst();
882
            success = timeline->requestClipMove(newId, newTrack, position, true, true, true, true, undo, redo);
883
        }
884
885
        TimelineFunctions::changeClipState(timeline, cid, PlaylistState::AudioOnly, undo, redo);
        success = success && timeline->m_groups->createGroupAtSameLevel(cid, std::unordered_set<int>{newId}, GroupType::AVSplit, undo, redo);
886
887
888
        if (!success) {
            bool undone = undo();
            Q_ASSERT(undone);
889
            pCore->displayMessage(i18n("Video restore failed"), ErrorMessage);
890
891
892
893
894
            return false;
        }
        done = true;
    }
    if (done) {
895
        pCore->pushUndo(undo, redo, i18n("Restore Video"));
896
897
898
899
    }
    return done;
}

Nicolas Carion's avatar
Nicolas Carion committed
900
void TimelineFunctions::setCompositionATrack(const std::shared_ptr<TimelineItemModel> &timeline, int cid, int aTrack)
901
{