main.qml 32 KB
Newer Older
luyikei's avatar
luyikei committed
1 2
import QtQuick 2.0
import QtQuick.Controls 1.4
3
import QtQuick.Dialogs 1.2
luyikei's avatar
luyikei committed
4 5 6 7 8 9

Rectangle {
    id: page
    anchors.fill: parent
    color: "#777777"
    border.width: 0
10
    focus: true
luyikei's avatar
luyikei committed
11

12
    property int length // in frames
13
    property int cursorPosition: 0 // in frames
14
    property int initPosOfCursor: 150
15 16
    property double ppu: 10 // Pixels Per minimum Unit
    property double unit: 3000 // In milliseconds therefore ppu / unit = Pixels Per milliseconds
luyikei's avatar
luyikei committed
17 18
    property double fps: 29.97
    property int maxZ: 100
19
    property int scale: 4
luyikei's avatar
luyikei committed
20
    property var allClips: [] // Actual clip item objects
21
    property var allClipsDict: ({}) // Actual clip item objects
22
    property var selectedClips: [] // Selected clip uuids
luyikei's avatar
luyikei committed
23 24
    property var allTransitions: [] // Actual transition item objects
    property var allTransitionsDict: ({}) // Actual transition item objects
luyikei's avatar
luyikei committed
25
    property var groups: [] // list of lists of clip uuids
26
    property var linkedClipsDict: ({}) // Uuid
27
    property alias isMagneticMode: magneticModeButton.selected
28
    property bool isCutMode: false
luyikei's avatar
luyikei committed
29
    property bool isTransitionMode: transitionModeButton.selected
30
    property bool dragging: false
luyikei's avatar
luyikei committed
31
    property int trackHeight: 60
luyikei's avatar
luyikei committed
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    readonly property int magneticMargin: 25

    function findNewPosition( newPos, target, dragSource, useMagneticMode ) {
        if ( useMagneticMode === true ) {
            var leastDistance = ptof( magneticMargin );
            // Check two times
            for ( var k = 0; k < 2; ++k ) {
                for ( var j = 0; j < markers.count; ++j ) {
                    var mPos = markers.get( j ).position;
                    if ( Math.abs( newPos - mPos ) < leastDistance ) {
                        leastDistance = Math.abs( newPos - mPos );
                        newPos = mPos;
                    }
                    else if ( Math.abs( newPos + target.length - 1 - mPos ) < leastDistance ) {
                        leastDistance = Math.abs( newPos + target.length - 1 - mPos );
                        newPos = mPos - target.length + 1;
                    }
                }
            }
            // Magnet for the left edge of the timeline
            if ( newPos < ptof( magneticMargin ) )
                newPos = 0;
        }

        // Collision detection
        var isCollided = true;
        var currentTrack = trackContainer( target.type )["tracks"].get( target.newTrackId );
        if ( currentTrack )
            var clips = currentTrack["clips"];
        else
            return target.position;
        for ( j = 0; j < clips.count + 2 && isCollided; ++j ) {
            isCollided = false;
            for ( k = 0; k < clips.count; ++k ) {
                var clip = clips.get( k );
                if ( clip.uuid === target.uuid ||
                     ( clip.uuid === dragSource.uuid && target.newTrackId !== dragSource.newTrackId )
                   )
                    continue;
                var cPos = clip.uuid === dragSource.uuid ? ptof( dragSource.x ) : clip["position"];
                var cEndPos = clip["position"] + clip["length"] - 1;

luyikei's avatar
luyikei committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
                // Note that in transition mode, they will never collide.
                if ( isTransitionMode === true ) {
                    leastDistance = ptof( magneticMargin );
                    if ( Math.abs( newPos - cPos ) < leastDistance ) {
                        leastDistance = Math.abs( newPos - cPos );
                        newPos = cPos;
                    }
                    if ( Math.abs( newPos + target.length - 1 - cPos ) < leastDistance ) {
                        leastDistance = Math.abs( newPos + target.length - 1 - cPos );
                        newPos = cPos - target.length + 1;
                    }
                    if ( Math.abs( newPos - cEndPos ) < leastDistance ) {
                        leastDistance = Math.abs( newPos - cEndPos );
                        newPos = cEndPos;
                    }
                    if ( Math.abs( newPos + target.length - 1 - cEndPos ) < leastDistance ) {
                        leastDistance = Math.abs( newPos + target.length - 1 - cEndPos );
                        newPos = cEndPos - target.length + 1;
                    }
                }
                else {
                    // In theory, they share the same deltaPos, therefore unable to collide each other.
                    if ( findClipItem( clip.uuid ).selected === true )
                        continue;

                    if ( cEndPos >= newPos && newPos + target.length - 1 >= cPos )
                        isCollided = true;

                    // HACK: If magnetic mode, consider clips bigger
                    var clipMargin = useMagneticMode ? ptof( magneticMargin ) : 0;
                    cPos -= clipMargin;
                    cEndPos += clipMargin;
                    if ( cEndPos >= newPos && newPos + target.length - 1 >= cPos ) {
                        if ( cPos >= newPos ) {
109
                            if ( cPos - target.length + clipMargin >= 0 )
luyikei's avatar
luyikei committed
110 111 112 113 114 115
                                newPos = cPos - target.length + clipMargin;
                            else
                                newPos = target.position;
                        } else {
                            newPos = cEndPos - clipMargin + 1;
                        }
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
                    }
                }

                if ( isCollided )
                    break;
            }
        }

        if ( isCollided ) {
            for ( k = 0; k < clips.count; ++k ) {
                clip = clips.get( k );
                if ( clip.uuid === target.uuid ||
                     ( clip.uuid === dragSource.uuid && target.newTrackId !== dragSource.newTrackId ) )
                    continue;
                cPos = clip.uuid === dragSource.uuid ? ptof( dragSource.x ) : clip["position"];
                cEndPos = clip["position"] + clip["length"] - 1;
                newPos = Math.max( newPos, cEndPos + 1 );
            }
        }

        return newPos;
    }

luyikei's avatar
luyikei committed
139 140
    function clearSelectedClips() {
        while ( selectedClips.length ) {
141
            var clip = findClipItem( selectedClips.pop() );
luyikei's avatar
luyikei committed
142 143
            if ( clip )
                clip.selected = false;
luyikei's avatar
luyikei committed
144 145 146
        }
    }

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    function zerofill( number, width ) {
        var str = "" + number;
        while ( str.length < width ) {
            str = "0" + str;
        }
        return str;
    }

    function timecodeFromFrames( frames ) {
        var seconds = Math.floor( frames / Math.round( fps )  );
        var minutes = Math.floor( seconds / 60 );
        var hours = Math.floor( minutes / 60 );

        return zerofill( hours, 3 ) + ':' + // hours
                zerofill( minutes % 60, 2 ) + ':' + // minutes
                zerofill( seconds % 60, 2 ) + ':' + // seconds
                // The second Math.round prevents the first value from exceeding fps.
                // e.g. 30 % Math.round( 29.97 ) = 0
                zerofill( Math.floor( frames % Math.round( fps ) ), 2 ); // frames in a minute
    }

luyikei's avatar
luyikei committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    // Convert length in frames to pixels
    function ftop( frames )
    {
        return frames / fps * 1000 * ppu / unit;
    }

    // Convert length in pixels to frames
    function ptof( pixels )
    {
        return Math.round( pixels * fps / 1000 / ppu * unit );
    }

    function trackContainer( trackType )
    {
        if ( trackType === "Video" )
            return trackContainers.get( 0 );
        return trackContainers.get( 1 );
    }

    function addTrack( trackType )
    {
luyikei's avatar
luyikei committed
189
        trackContainer( trackType )["tracks"].append( { "clips": [], "transitions": [] } );
luyikei's avatar
luyikei committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    }

    function removeTrack( trackType )
    {
        var tracks = trackContainer( trackType )["tracks"];
        tracks.remove( tracks.count - 1 );
    }

    function addClip( trackType, trackId, clipDict )
    {
        var newDict = {};
        newDict["begin"] = clipDict["begin"];
        newDict["end"] = clipDict["end"];
        newDict["position"] = clipDict["position"];
        newDict["length"] = clipDict["length"];
205
        newDict["libraryUuid"] = clipDict["libraryUuid"];
luyikei's avatar
luyikei committed
206 207
        newDict["uuid"] = clipDict["uuid"];
        newDict["trackId"] = trackId;
luyikei's avatar
luyikei committed
208
        newDict["type"] = trackType;
luyikei's avatar
luyikei committed
209
        newDict["name"] = clipDict["name"];
210
        newDict["selected"] = clipDict["selected"] === false ? false : true ;
luyikei's avatar
luyikei committed
211
        var tracks = trackContainer( trackType )["tracks"];
212 213
        while ( trackId > tracks.count - 1 )
            addTrack( trackType );
luyikei's avatar
luyikei committed
214 215 216 217 218 219 220 221 222
        tracks.get( trackId )["clips"].append( newDict );
        return newDict;
    }

    function removeClipFromTrack( trackType, trackId, uuid )
    {
        var ret = false;
        var tracks = trackContainer( trackType )["tracks"];
        var clips = tracks.get( trackId )["clips"];
luyikei's avatar
luyikei committed
223

luyikei's avatar
luyikei committed
224 225 226 227 228 229 230 231 232 233 234 235 236
        for ( var j = 0; j < clips.count; j++ ) {
            var clip = clips.get( j );
            if ( clip.uuid === uuid ) {
                clips.remove( j );
                ret = true;
                j--;
            }
        }
        return ret;
    }

    function removeClipFromTrackContainer( trackType, uuid )
    {
237
        for ( var i = 0; i < trackContainer( trackType )["tracks"].count; i++  )
luyikei's avatar
luyikei committed
238 239 240
            removeClipFromTrack( trackType, i, uuid );
    }

241 242 243 244 245 246
    function removeClip( uuid )
    {
        removeClipFromTrackContainer( "Audio", uuid );
        removeClipFromTrackContainer( "Video", uuid );
    }

luyikei's avatar
luyikei committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    function findClipFromTrackContainer( trackType, uuid )
    {
        var tracks = trackContainer( trackType )["tracks"];
        for ( var i = 0; i < tracks.count; i++  ) {
            var clip = findClipFromTrack( trackType, i, uuid );
            if( clip )
                return clip;
        }

        return null;
    }

    function findClipFromTrack( trackType, trackId, uuid )
    {
        var clips = trackContainer( trackType )["tracks"].get( trackId )["clips"];
        for ( var j = 0; j < clips.count; j++ ) {
            var clip = clips.get( j );
            if ( clip.uuid === uuid )
                return clip;
        }
        return null;
    }

    function findClip( uuid )
    {
        var v = findClipFromTrackContainer( "Video", uuid );
        if ( !v )
            return findClipFromTrackContainer( "Audio", uuid );
        return v;
    }

luyikei's avatar
luyikei committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    function addTransition( trackType, trackId, transitionDict )
    {
        var newDict = {};
        newDict["begin"] = transitionDict["begin"];
        newDict["end"] = transitionDict["end"];
        newDict["uuid"] = transitionDict["uuid"];
        newDict["trackId"] = trackId;
        newDict["type"] = trackType;
        newDict["identifier"] = transitionDict["identifier"];
        newDict["name"] = transitionDict["name"];
        var tracks = trackContainer( trackType )["tracks"];
        while ( trackId > tracks.count - 1 )
            addTrack( trackType );
        tracks.get( trackId )["transitions"].append( newDict );
        return newDict;
    }

    function removeTransitionFromTrack( trackType, trackId, uuid )
    {
        var ret = false;
        var tracks = trackContainer( trackType )["tracks"];
        var trans = tracks.get( trackId )["transitions"];

        for ( var j = 0; j < trans.count; j++ ) {
            var t = trans.get( j );
            if ( t.uuid === uuid ) {
                trans.remove( j );
                ret = true;
                j--;
            }
        }
        return ret;
    }

    function removeTransitionFromTrackContainer( trackType, uuid )
    {
        for ( var i = 0; i < trackContainer( trackType )["tracks"].count; ++i )
            removeTransitionFromTrack( trackType, i, uuid );
    }

    function removeTransition( uuid )
    {
        removeTransitionFromTrackContainer( "Audio", uuid );
        removeTransitionFromTrackContainer( "Video", uuid );
    }

    function findTransitionFromTrack( trackType, trackId, uuid )
    {
        var trans = trackContainer( trackType )["tracks"].get( trackId )["transitions"];
        for ( var j = 0; j < trans.count; ++j ) {
            var t = trans.get( j );
            if ( t.uuid === uuid )
                return t;
        }
        return null;
    }

    function findTransitionFromTrackContainer( trackType, uuid )
    {
        var tracks = trackContainer( trackType )["tracks"];
        for ( var i = 0; i < tracks.count; ++i  ) {
            var t = findTransitionFromTrack( trackType, i, uuid );
            if( t )
                return t;
        }

        return null;
    }

    function findTransition( uuid )
    {
        var t = findTransitionFromTrackContainer( "Video", uuid );
        if ( !t )
            return findTransitionFromTrackContainer( "Audio", uuid );
        return t;
    }

355
    function findClipItem( uuid ) {
356
        return allClipsDict[uuid];
357 358
    }

luyikei's avatar
luyikei committed
359 360 361 362
    function findTransitionItem( uuid ) {
        return allTransitionsDict[uuid];
    }

luyikei's avatar
luyikei committed
363 364 365 366 367 368 369 370 371
    function adjustTracks( trackType ) {
        var tracks = trackContainer( trackType )["tracks"];

        while ( tracks.count > 1 && tracks.get( tracks.count - 1 )["clips"].count === 0 &&
               tracks.get( tracks.count - 2 )["clips"].count === 0 )
            removeTrack( trackType );

        if ( tracks.get( tracks.count - 1 )["clips"].count > 0 )
            addTrack( trackType );
luyikei's avatar
luyikei committed
372
    }
luyikei's avatar
luyikei committed
373

luyikei's avatar
luyikei committed
374
    function addMarker( pos ) {
luyikei's avatar
luyikei committed
375 376 377
        markers.append( {
                           "position": pos
                       } );
luyikei's avatar
luyikei committed
378 379
    }

luyikei's avatar
luyikei committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    function findMarker( pos ) {
        for ( var i = 0; i < markers.count; ++i ) {
            if ( markers.get( i )["position"] === pos ) {
                return markers.get( i );
            }
        }
        return null;
    }

    function removeMarker( pos ) {
        for ( var i = 0; i < markers.count; ++i ) {
            if ( markers.get( i )["position"] === pos ) {
                markers.remove( i );
                return;
            }
        }
    }

luyikei's avatar
luyikei committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    function addGroup( clips ) {
        groups.push( clips );
    }

    function findGroup( uuid ) {
        for ( var i = 0; i < groups.length; ++i ) {
            var group = groups[i];
            for ( var j = 0; j < group.length; ++j ) {
                if ( group[j] === uuid )
                    return group;
            }
        }
        return null;
    }

    function removeGroup( uuid ) {
        for ( var i = 0; i < groups.length; ++i ) {
            var group = groups[i];
            for ( var j = 0; j < group.length; ++j ) {
                if ( group[j] === uuid ) {
                    groups.splice( i, 1 );
                    return;
                }
            }
        }
    }

425 426 427 428 429 430
    function updateLinkedClips( uuid ) {
        var item = findClipItem( uuid );
        if ( item )
            item.linkedClips = linkedClipsDict[uuid];
    }

431
    function zoomIn( ratio, scrollToCuror ) {
432 433 434
        var newPpu = ppu;
        var newUnit = unit;
        newPpu *= ratio;
435
        var contentXPos = ptof( sView.flickableItem.contentX );
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

        // Don't be too narrow.
        while ( newPpu < 10 ) {
            newPpu *= 2;
            newUnit *= 2;
        }

        // Don't be too distant.
        while ( newPpu > 20 ) {
            newPpu /= 2;
            newUnit /= 2;
        }

        // Can't be more precise than 1000msec / fps.
        var mUnit = 1000 / fps;

        if ( newUnit < mUnit ) {
            newPpu /= ratio; // Restore the original scale.
454
            newPpu *= mUnit / newUnit;
455 456 457
            newUnit = mUnit;
        }

458
        // Make unit a multiple of 1 / fps.
459
        newPpu *= ( newUnit - ( newUnit % mUnit ) ) / newUnit;
460 461 462 463 464 465 466 467 468
        newUnit -= newUnit % mUnit;

        // If "almost" the same value, don't bother redrawing the ruler.
        if ( Math.abs( unit - newUnit ) > 0.01 )
            unit = newUnit;

        if ( Math.abs( ppu - newPpu ) > 0.0001 )
            ppu = newPpu;

469 470 471 472 473 474 475 476 477 478 479
        if ( scrollToCuror === true ) {
            // Let's scroll to the cursor position!
            var newContentX = cursor.x - sView.width / 2;
            // Never show the background behind the timeline
            if ( newContentX >= 0 && sView.flickableItem.contentWidth - newContentX > sView.width  )
                sView.flickableItem.contentX = newContentX;
        }
        else {
            sView.flickableItem.contentX = ftop( contentXPos );
        }

480

481 482 483
        scale = Math.floor( Math.log( newUnit / mUnit ) / Math.log( 2 ) - 1 );
        scale = Math.min( 9, scale );
        scale = Math.max( 0, scale );
484
        mainwindow.setScale( scale );
485 486
    }

487
    // Sort clips in a manner that clips won't overlap each other while they are being moved
488
    function sortSelectedClips( deltaTrackId, deltaPos ) {
489
        // Workaround: We cannot sort selectedClips directly maybe because of a Qt bug
490
        var sorted = selectedClips.concat();
491
        sorted.sort(
492
                    function( clipAUuid, clipBUuid )
493
                    {
494 495
                        var clipA = findClipItem( clipAUuid );
                        var clipB = findClipItem( clipBUuid );
496
                        if ( deltaTrackId > 0 )
497 498 499
                        {
                            return - ( clipA.newTrackId - clipB.newTrackId );
                        }
500
                        else if ( deltaTrackId < 0 )
501 502 503
                        {
                            return clipA.newTrackId - clipB.newTrackId;
                        }
504
                        else if ( deltaPos > 0 )
505 506 507
                        {
                            return - ( clipA.position - clipB.position );
                        }
508
                        else if ( deltaPos < 0 )
509 510
                        {
                            return clipA.position - clipB.position;
511 512
                        };
                        return 0;
513 514
                    }
                    );
515
        selectedClips = sorted;
516
    }
517

518
    function dragFinished( deltaTrackId, deltaPos ) {
519
        dragging = false;
520
        sortSelectedClips( deltaTrackId, deltaPos );
521

luyikei's avatar
luyikei committed
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 548 549 550 551 552 553 554
        var toAdd = [];
        var toMove = [];

        for ( var i = 0; i < allTransitions.length; ++i ) {
            var transitionItem = allTransitions[i];
            if ( transitionItem.inTrack === true ) {
                if ( transitionItem.uuid === "transitionUuid" ) {
                    toAdd.push( [transitionItem.identifier, transitionItem.begin, transitionItem.end,
                                 transitionItem.trackId, transitionItem.type, transitionItem.clips] );
                }
            }
        }

        for ( i = 0; i < allTransitions.length; ++i ) {
            transitionItem = allTransitions[i];
            if ( transitionItem.inTrack === true ) {
                if ( transitionItem.uuid !== "transitionUuid" )
                    toMove.push( [transitionItem.uuid,
                                  transitionItem.begin,
                                  transitionItem.end] );
            }
        }

        removeTransition( "transitionUuid" );

        for ( i = 0; i < toAdd.length; ++i ) {
            var newUuid = workflow.addTransition( toAdd[i][0], toAdd[i][1], toAdd[i][2], toAdd[i][3], toAdd[i][4] );
            findTransitionItem( newUuid ).clips = toAdd[i][5];
        }

        for ( i = 0; i < toMove.length; ++i )
            workflow.moveTransition( toMove[i][0], toMove[i][1], toMove[i][2] );

555 556
        // We don't want to rely on selectedClips while moving since it "will" be changed
        // I'm aware that it's not the best solution but it's the safest solution for sure
luyikei's avatar
luyikei committed
557 558
        toMove = [];
        for ( i = 0; i < selectedClips.length; ++i )
559 560 561 562
        {
            var clip = findClipItem( selectedClips[i] );
            toMove.push( [clip.uuid, clip.newTrackId, clip.position] );
        }
563
        for ( i = 0; i < toMove.length; ++i )
luyikei's avatar
luyikei committed
564
            workflow.moveClip( toMove[i][0], toMove[i][1], toMove[i][2] );
565

luyikei's avatar
luyikei committed
566 567 568 569
        adjustTracks( "Audio" );
        adjustTracks( "Video" );
    }

luyikei's avatar
luyikei committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
    ListModel {
        id: trackContainers

        ListElement {
            name: "Video"
            tracks: []
        }

        ListElement {
            name: "Audio"
            tracks: []
        }

        Component.onCompleted: {
            addTrack( "Video" );
            addTrack( "Audio" );
        }
    }

luyikei's avatar
luyikei committed
589 590 591 592
    ListModel {
        id: markers
    }

luyikei's avatar
luyikei committed
593 594
    MouseArea {
        id: selectionArea
595 596 597 598
        width: parent.width - initPosOfCursor
        height: audioTrackContainer.y + audioTrackContainer.height - videoTrackContainer.y
        y: videoTrackContainer.y
        x: initPosOfCursor
luyikei's avatar
luyikei committed
599 600 601 602

        onPressed: {
            clearSelectedClips();
            selectionRect.visible = true;
603 604
            selectionRect.x = mouseX + x;
            selectionRect.y = mouseY + y;
luyikei's avatar
luyikei committed
605 606
            selectionRect.width = 0;
            selectionRect.height = 0;
607
            selectionRect.initPos = Qt.point( mouseX + x, mouseY + y );
luyikei's avatar
luyikei committed
608 609 610 611
        }

        onPositionChanged: {
            if ( selectionRect.visible === true ) {
612 613 614 615
                selectionRect.x = Math.min( mouseX + x, selectionRect.initPos.x );
                selectionRect.y = Math.min( mouseY + y, selectionRect.initPos.y );
                selectionRect.width = Math.abs( mouseX + x - selectionRect.initPos.x );
                selectionRect.height = Math.abs( mouseY + y - selectionRect.initPos.y );
616
                selectionRect.selectClips();
luyikei's avatar
luyikei committed
617 618 619 620 621 622 623 624
            }
        }

        onReleased: {
            selectionRect.visible = false;
        }
    }

625 626
    ScrollView {
        id: sView
luyikei's avatar
luyikei committed
627 628
        height: page.height
        width: page.width
629 630 631 632

        readonly property int sViewPadding: 50

        flickableItem.contentWidth: Math.max( page.width, ftop( length ) + initPosOfCursor + sViewPadding )
633 634 635
        flickableItem.contentHeight: Math.max( sView.height,
                                              topArea.height + videoTrackContainer.height +
                                              containerMarginItem.height + audioTrackContainer.height )
luyikei's avatar
luyikei committed
636

637
        Flickable {
luyikei's avatar
luyikei committed
638

luyikei's avatar
luyikei committed
639 640
            interactive: false

641 642 643 644 645 646 647 648 649 650 651
            TrackContainer {
                y: topArea.height
                id: videoTrackContainer
                type: "Video"
                isUpward: true
                tracks: trackContainers.get( 0 )["tracks"]
            }

            Rectangle {
                id: containerMarginItem
                anchors.top: videoTrackContainer.bottom
652
                height: 5
653
                width: parent.width
654 655 656 657
                gradient: Gradient {
                    GradientStop {
                        position: 0.00;
                        color: "#797979"
658 659
                    }

660 661 662 663
                    GradientStop {
                        position: 0.748
                        color: "#959697"
                    }
664

665 666 667
                    GradientStop {
                        position: 0.986
                        color: "#858f99"
668
                    }
669
                }
670
            }
671

672 673 674 675 676 677 678 679 680 681 682 683 684 685
            TrackContainer {
                anchors.top: containerMarginItem.bottom
                id: audioTrackContainer
                type: "Audio"
                isUpward: false
                tracks: trackContainers.get( 1 )["tracks"]
            }

            Item {
                id: topArea
                width: parent.width
                height: 52
                x: topLeftArea.width
                y: sView.flickableItem.contentY
luyikei's avatar
luyikei committed
686

687 688 689 690 691 692 693 694
                Ruler {
                    id: ruler

                    Rectangle {
                        id: borderBottomOfRuler
                        width: parent.width
                        height: 1
                        color: "#111111"
luyikei's avatar
luyikei committed
695 696
                    }
                }
697

698 699 700 701 702
                Cursor {
                    id: cursor
                    anchors.top: ruler.bottom
                    z: 2000
                    height: page.height
703
                }
luyikei's avatar
luyikei committed
704

705 706 707 708 709 710 711 712
                Repeater {
                    model: markers
                    anchors.top: topArea.top
                    delegate: Marker {
                        position: model.position
                        markerModel: model
                    }
                }
luyikei's avatar
luyikei committed
713
            }
luyikei's avatar
luyikei committed
714

715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
            Rectangle {
                id: topLeftArea
                x: sView.flickableItem.contentX
                y: sView.flickableItem.contentY
                width: initPosOfCursor
                height: topArea.height
                color: "#333333"
                border.width: 1
                border.color: "#111111"

                Text {
                    id: cursorTimecode
                    x: 5
                    y: 2

                    text: timecodeFromFrames( cursorPosition )
                    color: "#EEEEEE"
                    font.pixelSize: parent.height / 4
                }

                Item {
                    id: properties
                    x: 5
                    y: parent.height / 2
                    width: parent.width - x * 2
                    height: parent.height / 2

luyikei's avatar
luyikei committed
742 743 744 745 746 747 748 749 750
                    Row {
                        spacing: 2

                        PropertyButton {
                            id: magneticModeButton
                            text: "M"
                            selected: true
                        }

luyikei's avatar
luyikei committed
751 752 753 754 755 756
                        PropertyButton {
                            id: transitionModeButton
                            text: "T"
                            selected: false
                        }

757 758 759 760 761 762
                        PropertyButton {
                            id: zoomInButton
                            text: "+"
                            selected: false

                            onPressed: {
763
                                zoomIn( 2.0, true );
764 765 766 767 768 769 770 771 772 773
                                selected = false;
                            }
                        }

                        PropertyButton {
                            id: zoomOutButton
                            text: "-"
                            selected: false

                            onPressed: {
774
                                zoomIn( 0.5, true );
775 776 777
                                selected = false;
                            }
                        }
778 779 780 781 782 783 784 785 786 787 788

                        PropertyButton {
                            id: fxButton
                            text: "Fx"
                            selected: false

                            onPressed: {
                                workflow.showEffectStack();
                                selected = false;
                            }
                        }
789
                    }
luyikei's avatar
luyikei committed
790 791
                }
            }
luyikei's avatar
luyikei committed
792 793 794 795 796 797 798 799
        }
    }

    Rectangle {
        id: selectionRect
        visible: false
        color: "#999999cc"
        property point initPos
800 801 802 803 804 805 806 807 808 809

        function selectClips() {
            for ( var i = 0; i < allClips.length; ++i ) {
                var clip = allClips[i];
                var clipPos = clip.mapToItem( page, 0, 0 );
                if ( ( x - clip.width < clipPos.x && clipPos.x < x + width ) &&
                     ( y - clip.height < clipPos.y && clipPos.y < y + height ) )
                    clip.selected = true;
            }
        }
luyikei's avatar
luyikei committed
810 811
    }

812
    MessageDialog {
luyikei's avatar
luyikei committed
813
        id: removeSelectedClipsDialog
814 815 816 817 818
        title: "VLMC"
        text: qsTr( "Do you really want to remove selected clips?" )
        icon: StandardIcon.Question
        standardButtons: StandardButton.Yes | StandardButton.No
        onYes: {
819 820
            while ( selectedClips.length > 0 )
                workflow.removeClip( selectedClips[0] );
821 822 823 824 825
        }
    }

    Keys.onPressed: {
        if ( event.key === Qt.Key_Delete ) {
luyikei's avatar
luyikei committed
826
            removeSelectedClipsDialog.visible = true;
827
        }
828 829 830
        else if ( event.key === Qt.Key_Plus &&
                 event.modifiers & Qt.ControlModifier
                 && scale > 0 ) {
831
            zoomIn( 2, true );
832
        }
833 834 835
        else if ( event.key === Qt.Key_Minus &&
                 event.modifiers & Qt.ControlModifier &&
                 scale < 9 ) {
836
            zoomIn( 0.5, true );
837 838
        }
        event.accepted = true;
839 840
    }

luyikei's avatar
luyikei committed
841 842
    Connections {
        target: workflow
843 844 845 846 847

        onFpsChanged: {
            page.fps = fps;
        }

luyikei's avatar
luyikei committed
848
        onLengthChanged: {
luyikei's avatar
luyikei committed
849
            page.length = length;
850
        }
851 852 853 854

        onClipAdded: {
            var clipInfo = workflow.clipInfo( uuid );
            var type = clipInfo["audio"] ? "Audio" : "Video";
855
            clipInfo["selected"] = false;
856
            linkedClipsDict[uuid] = clipInfo["linkedClips"];
857
            addClip( type, clipInfo["trackId"], clipInfo );
luyikei's avatar
luyikei committed
858
            adjustTracks( type );
859 860 861 862 863 864
        }

        onClipMoved: {
            var clipInfo = workflow.clipInfo( uuid );
            var type = clipInfo["audio"] ? "Audio" : "Video";
            var oldClip = findClipFromTrackContainer( type, uuid );
865 866
            linkedClipsDict[uuid] = clipInfo["linkedClips"];
            updateLinkedClips( uuid );
867 868 869 870 871

            if ( clipInfo["trackId"] !== oldClip["trackId"] ) {
                addClip( type, clipInfo["trackId"], clipInfo );
                removeClipFromTrack( type, oldClip["trackId"], uuid );
            }
luyikei's avatar
luyikei committed
872 873 874 875 876
            else
            {
                findClipItem( uuid ).position = clipInfo["position"];
                findClipItem( uuid ).lastPosition = clipInfo["position"];
            }
luyikei's avatar
luyikei committed
877
            adjustTracks( type );
878 879 880 881
        }

        onClipRemoved: {
            removeClip( uuid );
luyikei's avatar
luyikei committed
882 883
            adjustTracks( "Audio" );
            adjustTracks( "Video" );
884 885 886 887 888 889
        }

        onClipResized: {
            var clipInfo = workflow.clipInfo( uuid );
            var clip = findClipItem( uuid );
            clip.position = clipInfo["position"];
890
            clip.lastPosition = clipInfo["position"];
891 892
            clip.end = clipInfo["end"];
            clip.begin = clipInfo["begin"];
893
            clip.length = clipInfo["length"];
894
            clip.updateEffects( clipInfo );
895
        }
896 897

        onClipLinked: {
898 899 900 901
            linkedClipsDict[uuidA].push( uuidB );
            linkedClipsDict[uuidB].push( uuidA );
            updateLinkedClips( uuidA );
            updateLinkedClips( uuidB );
902
        }
903

904
        onClipUnlinked: {
905 906 907 908 909 910 911 912 913 914 915 916 917 918
            for ( var i = 0; i < linkedClipsDict[uuidA].length; ++i )
                if ( linkedClipsDict[uuidA][i] === uuidB )
                {
                    linkedClipsDict[uuidA].splice( i, 1 );
                    break;
                }
            for ( i = 0; i < linkedClipsDict[uuidB].length; ++i )
                if ( linkedClipsDict[uuidB][i] === uuidA )
                {
                    linkedClipsDict[uuidB].splice( i, 1 );
                    break;
                }
            updateLinkedClips( uuidA );
            updateLinkedClips( uuidB );
919 920
        }

luyikei's avatar
luyikei committed
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
        onTransitionAdded: {
            var transitionInfo = workflow.transitionInfo( uuid );
            var type = transitionInfo["audio"] ? "Audio" : "Video";
            if ( transitionInfo["isInTrack"] )
                addTransition( type, transitionInfo["trackId"], transitionInfo );
            else
                addTransition( type, transitionInfo["trackBId"], transitionInfo );
        }

        onTransitionMoved: {
            var transitionInfo = workflow.transitionInfo( uuid );
            var transition = findTransition( uuid );
            var type = transitionInfo["audio"] ? "Audio" : "Video";
            if ( transitionInfo["isInTrack"] || transitionInfo["trackBId"] === transition["trackId"] ) {
                transition["begin"] = transitionInfo["begin"];
                transition["end"] = transitionInfo["end"];
            }
            else {
                removeTransition( uuid );
                addTransition( type, transitionInfo["trackBId"], transitionInfo );
            }
        }

        onTransitionRemoved: {
            removeTransition( uuid );
        }

948 949 950 951 952
        onEffectsUpdated: {
            var item = findClipItem( clipUuid );
            if ( item )
                item.updateEffects( workflow.clipInfo( clipUuid ) );
        }
953 954 955 956 957 958
    }

    Connections {
        target: mainwindow
        onScaleChanged: {
            // 10 levels
959
            if ( scale < scaleLevel )
960
                zoomIn( 0.5, false );
961
            else if ( scale > scaleLevel )
962
                zoomIn( 2, false );
963
            scale = scaleLevel;
luyikei's avatar
luyikei committed
964
        }
965 966 967 968 969 970
        onCutToolSelected: {
            isCutMode = true;
        }
        onSelectionToolSelected: {
            isCutMode = false;
        }
luyikei's avatar
luyikei committed
971
    }
972 973 974 975 976 977 978 979 980 981

    Connections {
        target: timeline
        onMarkerAdded: {
            addMarker( pos );
        }
        onMarkerRemoved: {
            removeMarker( pos );
        }
    }
luyikei's avatar
luyikei committed
982 983
}