main.qml 16.4 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
luyikei's avatar
luyikei committed
14
    property int initPosOfCursor: 100
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 21 22
    property var allClips: [] // Actual clip item objects
    property var selectedClips: [] // Actual clip item objects
    property var groups: [] // list of lists of clip uuids
23
    property alias isMagneticMode: magneticModeButton.selected
luyikei's avatar
luyikei committed
24 25 26 27 28

    property int trackHeight: 30

    function clearSelectedClips() {
        while ( selectedClips.length ) {
luyikei's avatar
luyikei committed
29 30 31
            var clip = selectedClips.pop();
            if ( clip )
                clip.selected = false;
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
    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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    // 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 )
    {
        trackContainer( trackType )["tracks"].append( { "clips": [] } );
    }

    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"];
        newDict["uuid"] = clipDict["uuid"];
        newDict["trackId"] = trackId;
        newDict["name"] = clipDict["name"];
96
        newDict["selected"] = clipDict["selected"] === false ? false : true ;
97
        newDict["linkedClip"] = clipDict["linkedClip"] ? clipDict["linkedClip"] : "";
luyikei's avatar
luyikei committed
98
        var tracks = trackContainer( trackType )["tracks"];
99 100
        while ( trackId > tracks.count - 1 )
            addTrack( trackType );
luyikei's avatar
luyikei committed
101 102 103 104 105 106 107 108 109 110 111 112 113
        tracks.get( trackId )["clips"].append( newDict );

        if ( clipDict["uuid"] === "tempUuid" )
            return 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
114

luyikei's avatar
luyikei committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        for ( var j = 0; j < clips.count; j++ ) {
            var clip = clips.get( j );
            if ( clip.uuid === uuid ) {
                clips.remove( j );
                ret = true;
                j--;
            }
        }
        if ( uuid === "tempUuid" )
            return ret;

        return ret;
    }

    function removeClipFromTrackContainer( trackType, uuid )
    {
131
        for ( var i = 0; i < trackContainer( trackType )["tracks"].count; i++  )
luyikei's avatar
luyikei committed
132 133 134
            removeClipFromTrack( trackType, i, uuid );
    }

135 136 137 138 139 140
    function removeClip( uuid )
    {
        removeClipFromTrackContainer( "Audio", uuid );
        removeClipFromTrackContainer( "Video", uuid );
    }

luyikei's avatar
luyikei committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    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;
    }

172 173 174 175 176 177 178 179
    function findClipItem( uuid ) {
        for ( var i = 0; i < allClips.length; ++i ) {
            if ( uuid === allClips[i].uuid )
                return allClips[i];
        }
        return null;
    }

180
    function moveClipTo( trackType, uuid, trackId, position )
luyikei's avatar
luyikei committed
181
    {
luyikei's avatar
luyikei committed
182
        var clip = findClipItem( uuid );
luyikei's avatar
luyikei committed
183 184
        if ( !clip )
            return;
luyikei's avatar
luyikei committed
185
        clip.position = position;
186
        workflow.moveClip( trackId, uuid, position );
luyikei's avatar
luyikei committed
187 188
    }

luyikei's avatar
luyikei committed
189 190 191 192 193 194 195 196 197
    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
198
    }
luyikei's avatar
luyikei committed
199

luyikei's avatar
luyikei committed
200
    function addMarker( pos ) {
luyikei's avatar
luyikei committed
201 202 203
        markers.append( {
                           "position": pos
                       } );
luyikei's avatar
luyikei committed
204 205
    }

luyikei's avatar
luyikei committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    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
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    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;
                }
            }
        }
    }

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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    function zoomIn( ratio ) {
        var newPpu = ppu;
        var newUnit = unit;
        newPpu *= ratio;

        // 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.
            newPpu *= mUnit / newPpu;
            newUnit = mUnit;
        }

        // Make unit a multiple of fps. This can change the scale but let's ignore it.
        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;

        // 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;
    }

luyikei's avatar
luyikei committed
294 295 296 297 298 299 300 301 302 303 304
    function dragFinished() {
        var length = selectedClips.length;
        for ( var i = length - 1; i >= 0; --i ) {
            if ( selectedClips[i] ) {
                selectedClips[i].move();
            }
        }
        adjustTracks( "Audio" );
        adjustTracks( "Video" );
    }

luyikei's avatar
luyikei committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    ListModel {
        id: trackContainers

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

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

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

luyikei's avatar
luyikei committed
324 325 326 327
    ListModel {
        id: markers
    }

luyikei's avatar
luyikei committed
328 329
    MouseArea {
        id: selectionArea
330 331 332 333
        width: parent.width - initPosOfCursor
        height: audioTrackContainer.y + audioTrackContainer.height - videoTrackContainer.y
        y: videoTrackContainer.y
        x: initPosOfCursor
luyikei's avatar
luyikei committed
334 335 336 337

        onPressed: {
            clearSelectedClips();
            selectionRect.visible = true;
338 339
            selectionRect.x = mouseX + x;
            selectionRect.y = mouseY + y;
luyikei's avatar
luyikei committed
340 341
            selectionRect.width = 0;
            selectionRect.height = 0;
342
            selectionRect.initPos = Qt.point( mouseX + x, mouseY + y );
luyikei's avatar
luyikei committed
343 344 345 346
        }

        onPositionChanged: {
            if ( selectionRect.visible === true ) {
347 348 349 350
                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 );
luyikei's avatar
luyikei committed
351 352 353 354 355 356 357 358
            }
        }

        onReleased: {
            selectionRect.visible = false;
        }
    }

359 360
    ScrollView {
        id: sView
luyikei's avatar
luyikei committed
361 362
        height: page.height
        width: page.width
363
        flickableItem.contentWidth: Math.max( page.width, ftop( length ) + initPosOfCursor + 100 )
luyikei's avatar
luyikei committed
364

365
        Flickable {
luyikei's avatar
luyikei committed
366

367 368
            Column {
                width: parent.width
luyikei's avatar
luyikei committed
369

370
                Row {
371
                    width: parent.width
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
                    Rectangle {
                        id: topLeftArea
                        width: initPosOfCursor
                        height: 52
                        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

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

                    Ruler {
                        id: ruler
                        z: 1000

                        Rectangle {
                            id: borderBottomOfRuler
                            width: parent.width
                            height: 1
                            color: "#111111"
                        }
                    }
416 417 418 419 420 421 422 423
                }

                TrackContainer {
                    id: videoTrackContainer
                    type: "Video"
                    isUpward: true
                    tracks: trackContainers.get( 0 )["tracks"]
                }
luyikei's avatar
luyikei committed
424

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
                Rectangle {
                    height: 20
                    width: parent.width
                    gradient: Gradient {
                        GradientStop {
                            position: 0.00;
                            color: "#797979"
                        }

                        GradientStop {
                            position: 0.748
                            color: "#959697"
                        }

                        GradientStop {
                            position: 0.986
                            color: "#858f99"
                        }
luyikei's avatar
luyikei committed
443 444
                    }
                }
445 446 447 448 449 450 451

                TrackContainer {
                    id: audioTrackContainer
                    type: "Audio"
                    isUpward: false
                    tracks: trackContainers.get( 1 )["tracks"]
                }
luyikei's avatar
luyikei committed
452 453
            }

454 455
            Cursor {
                id: cursor
456
                y: ruler.height
457 458 459
                z: 2000
                height: page.height
                x: initPosOfCursor
luyikei's avatar
luyikei committed
460
            }
luyikei's avatar
luyikei committed
461 462 463 464 465 466 467 468

            Repeater {
                model: markers
                delegate: Marker {
                    position: model.position
                    markerModel: model
                }
            }
luyikei's avatar
luyikei committed
469 470 471 472 473 474 475 476 477 478
        }
    }

    Rectangle {
        id: selectionRect
        visible: false
        color: "#999999cc"
        property point initPos
    }

479
    MessageDialog {
luyikei's avatar
luyikei committed
480
        id: removeSelectedClipsDialog
481 482 483 484 485
        title: "VLMC"
        text: qsTr( "Do you really want to remove selected clips?" )
        icon: StandardIcon.Question
        standardButtons: StandardButton.Yes | StandardButton.No
        onYes: {
486 487
            while ( selectedClips.length )
                workflow.removeClip( selectedClips[0].uuid );
488 489 490 491 492
        }
    }

    Keys.onPressed: {
        if ( event.key === Qt.Key_Delete ) {
luyikei's avatar
luyikei committed
493
            removeSelectedClipsDialog.visible = true;
494
        }
495 496 497 498 499 500 501 502 503
        else if ( event.key === Qt.Key_Plus && event.modifiers & Qt.ControlModifier )
        {
            zoomIn( 2 );
        }
        else if ( event.key === Qt.Key_Minus && event.modifiers & Qt.ControlModifier )
        {
            zoomIn( 0.5 );
        }
        event.accepted = true;
504 505
    }

luyikei's avatar
luyikei committed
506 507 508
    Connections {
        target: workflow
        onLengthChanged: {
509 510 511 512
            if ( page.length < length ) {
                page.length = length;
                zoomIn( sView.width / ( ftop( length ) + initPosOfCursor + 100 ) );
            }
513
        }
514 515 516 517

        onClipAdded: {
            var clipInfo = workflow.clipInfo( uuid );
            var type = clipInfo["audio"] ? "Audio" : "Video";
518
            clipInfo["selected"] = false;
519
            addClip( type, clipInfo["trackId"], clipInfo );
luyikei's avatar
luyikei committed
520
            adjustTracks( type );
521 522 523 524 525 526 527 528 529 530 531 532 533 534
        }

        onClipMoved: {
            var clipInfo = workflow.clipInfo( uuid );
            var type = clipInfo["audio"] ? "Audio" : "Video";
            var oldClip = findClipFromTrackContainer( type, uuid );

            if ( clipInfo["trackId"] !== oldClip["trackId"] ) {
                addClip( type, clipInfo["trackId"], clipInfo );
                removeClipFromTrack( type, oldClip["trackId"], uuid );
            }
            else if ( oldClip["position"] !== clipInfo["position"] ) {
                findClipItem( uuid ).position = clipInfo["position"];
            }
luyikei's avatar
luyikei committed
535
            adjustTracks( type );
536 537 538 539
        }

        onClipRemoved: {
            removeClip( uuid );
luyikei's avatar
luyikei committed
540 541
            adjustTracks( "Audio" );
            adjustTracks( "Video" );
542 543 544 545 546 547 548 549 550
        }

        onClipResized: {
            var clipInfo = workflow.clipInfo( uuid );
            var clip = findClipItem( uuid );
            clip.position = clipInfo["position"];
            clip.end = clipInfo["end"];
            clip.begin = clipInfo["begin"];
        }
551 552 553 554 555

        onClipLinked: {
            findClipItem( uuidA ).linkedClip = uuidB;
            findClipItem( uuidB ).linkedClip = uuidA;
        }
556 557 558 559 560 561 562 563 564 565 566
    }

    Connections {
        target: mainwindow
        onScaleChanged: {
            // 10 levels
            if ( scale < scaleLevel * 10 )
                zoomIn( 0.5 );
            else if ( scale > scaleLevel * 10 )
                zoomIn( 2 );
            scale = scaleLevel * 10;
luyikei's avatar
luyikei committed
567 568 569 570
        }
    }
}