Clip.qml 14.5 KB
Newer Older
luyikei's avatar
luyikei committed
1 2 3 4 5
import QtQuick 2.0

Rectangle {
    id: clip
    // NEVER SET X DIRECTLY. BINDING WILL BE REMOVED.
luyikei's avatar
luyikei committed
6
    x: ftop( position )
7
    width: ftop( end - begin + 1 )
luyikei's avatar
luyikei committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
    gradient: Gradient {
        GradientStop {
            id: gStop1
            position: 0.00;
            color: "#4276a6";
        }
        GradientStop {
            id: gStop2
            position: 1.00;
            color: "#1f546f";
        }
    }
    radius: 2
    border.color: "#1f546f"
    border.width: 1
23
    opacity: page.dragging === true && selectedClips.indexOf( uuid ) !== -1 ? 0.5 : 1.0
luyikei's avatar
luyikei committed
24 25

    property alias name: text.text
26
    property alias thumbnailSource: thumbnailImage.source
luyikei's avatar
luyikei committed
27
    property int trackId
luyikei's avatar
luyikei committed
28
    // Usualy it is trackId, the clip will be moved to the new track immediately.
luyikei's avatar
luyikei committed
29 30
    property int newTrackId
    property int position
31
    property int lastPosition
luyikei's avatar
luyikei committed
32 33
    property int begin
    property int end
34
    property int length
35 36
    property string libraryUuid // Library UUID: For thumbnails
    property string uuid // Instance UUID
37
    property var linkedClips: linkedClipsDict[uuid] ? linkedClipsDict[uuid] : [] // Uuid
luyikei's avatar
luyikei committed
38 39
    property string type
    property bool selected: false
40
    property alias mouseX: dragArea.mouseX
luyikei's avatar
luyikei committed
41 42 43

    property var clipInfo

44
    function forcePosition()
luyikei's avatar
luyikei committed
45
    {
46
        // Reset the binding so that it will reset the x position
luyikei's avatar
luyikei committed
47 48 49 50 51
        x = Qt.binding( function() { return ftop( position ); } );
    }

    function resize() {
        // This function updates Backend
52
        workflow.resizeClip( uuid, begin, end, position );
53 54 55 56 57 58 59 60
        for ( var i = 0; i < linkedClips.length; ++i )
        {
            var linkedClip = linkedClips[i];
            var lc = findClipItem( linkedClip );
            if ( lc === null )
                return;
            workflow.resizeClip( lc.uuid, lc.begin, lc.end, lc.position );
        }
luyikei's avatar
luyikei committed
61 62 63
    }

    function selectLinkedClip() {
64 65 66 67 68
        if ( selected === false )
            return;
        for ( var i = 0; i < linkedClips.length; ++i )
        {
            var linkedClip = linkedClips[i];
luyikei's avatar
luyikei committed
69
            findClipItem( linkedClip ).selected = true;
70 71 72 73 74 75 76 77
        }
    }

    function linked() {
        if ( selectedClips.length < 2 )
            return false;
        for ( var i = 0; i < selectedClips.length; ++i ) {
            for ( var j = i + 1; j < selectedClips.length; ++j ) {
78
                if ( findClipItem( selectedClips[i] ).linkedClips.indexOf( selectedClips[j] ) === -1 )
79 80 81 82
                    return false;
            }
        }
        return true;
luyikei's avatar
luyikei committed
83 84
    }

85 86 87 88
    function updateEffects( clipInfo ) {
        if ( !clipInfo["filters"] )
            return;

89
        var str = "";
90
        for ( var i = 0; i < clipInfo["filters"].length; ++i ) {
91 92 93
            str += clipInfo["filters"][i]["identifier"];
            if ( i < clipInfo["filters"].length - 1 )
                str += ", "
94
        }
95
        effectsItem.text = str;
96 97
    }

98
    // This function only resizes clips in the frontend, not updating the backend
99
    function resizeLinkedClips( oldPos, oldBegin, oldEnd ) {
100 101 102 103 104 105 106 107 108 109 110 111 112
        for ( var i = 0; i < linkedClips.length; ++i )
        {
            var linkedClip = linkedClips[i];
            var lc = findClipItem( linkedClip );
            if ( lc === null )
                return;
            // Don't resize from the begining if the clips didn't shared the same begin position
            if ( lc.position === oldPos ) {
                lc.position = position;
                lc.begin = begin;
            }
            if ( lc.end === oldEnd )
                lc.end = end;
113 114 115
        }
    }

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    function scrollToThis() {
        if ( width > sView.width )
            return;

        var newContentX = sView.flickableItem.contentX;

        if ( sView.flickableItem.contentX + sView.width <
                x + mouseX + initPosOfCursor + sView.sViewPadding )
            newContentX = x + mouseX + initPosOfCursor + sView.sViewPadding - sView.width;
        else if ( sView.flickableItem.contentX + sView.sViewPadding > x + mouseX )
            newContentX = x + mouseX - sView.sViewPadding;

        sView.flickableItem.contentX = Math.max( newContentX, 0 );
    }

luyikei's avatar
luyikei committed
131
    onXChanged: {
132 133 134
        if ( sView.width - initPosOfCursor < width )
            return;

luyikei's avatar
luyikei committed
135 136 137
        if ( sView.flickableItem.contentX + sView.width <
                x + width + initPosOfCursor + sView.sViewPadding )
            Drag.hotSpot.x = 0;
138
        else if ( sView.flickableItem.contentX + sView.sViewPadding > x )
luyikei's avatar
luyikei committed
139 140 141
            Drag.hotSpot.x = width;
    }

luyikei's avatar
luyikei committed
142 143 144 145 146 147 148 149 150 151 152 153
    onYChanged: {
        y -= y % trackHeight;
        // Don't move outside its TrackContainer
        // For Top
        var yToMoveUp = track.mapToItem( container, 0, 0 ).y + y;
        if ( yToMoveUp < 0 )
            y -= yToMoveUp;
        // For Bottom
        if ( yToMoveUp + height > container.height )
            y -= yToMoveUp - container.height + height;
    }

luyikei's avatar
luyikei committed
154 155 156 157 158 159 160
    onClipInfoChanged: {
        if ( !clipInfo )
            return;

        position = clipInfo["position"];
        begin = clipInfo["begin"];
        end = clipInfo["end"];
161
        length = clipInfo["length"];
luyikei's avatar
luyikei committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175
    }

    onPositionChanged: {
        clipInfo["position"] = position;
    }

    onBeginChanged: {
        clipInfo["begin"] = begin;
    }

    onEndChanged: {
        clipInfo["end"] = end;
    }

176 177 178 179
    onLengthChanged: {
        clipInfo["length"] = length;
    }

luyikei's avatar
luyikei committed
180 181
    onSelectedChanged: {
        if ( selected === true ) {
182
            selectedClips.push( uuid );
183

luyikei's avatar
luyikei committed
184
            var group = findGroup( uuid );
185
            for ( var i = 0; i < ( group ? group.length : 0 ); ++i ) {
luyikei's avatar
luyikei committed
186 187 188 189 190 191
                var clipItem = findClipItem( group[i] );
                if ( clipItem )
                    clipItem.selected = true;
            }
            selectLinkedClip();
        }
192 193 194 195 196 197 198
        else
            for ( i = 0; i < selectedClips.length; ++i )
                if ( selectedClips[i] === uuid ) {
                    selectedClips.splice( i, 1 );
                    --i;
                    break;
                }
199 200
    }

luyikei's avatar
luyikei committed
201
    Component.onCompleted: {
202 203 204 205
        for ( var i = 0; i < allClips.length; ++i ) {
            if ( allClips[i].linkedClips.indexOf( uuid ) !== -1 )
                linkedClips.push( allClips[i].uuid );
        }
206 207 208 209
        if ( clipInfo["selected"] === false )
            selected = false;
        else
            selected = true;
luyikei's avatar
luyikei committed
210
        newTrackId = trackId;
luyikei's avatar
luyikei committed
211
        allClips.push( clip );
212
        allClipsDict[uuid] = clip;
213 214

        updateEffects( workflow.clipInfo( uuid ) );
215 216 217

        if ( uuid === "videoUuid" || uuid === "audioUuid" )
            return;
218
        thumbnailSource = "image://thumbnail/" + libraryUuid + "/0";
luyikei's avatar
luyikei committed
219 220 221
    }

    Component.onDestruction: {
luyikei's avatar
luyikei committed
222
        Drag.drop();
luyikei's avatar
luyikei committed
223
        selected = false;
luyikei's avatar
luyikei committed
224

225 226 227
        for ( var i = 0; i < linkedClips.length; ++i )
        {
            var linkedClip = linkedClips[i];
228 229
            var linkedClipItem = findClipItem( linkedClip );
            if ( linkedClipItem )
230 231 232 233 234 235 236 237
                for ( var j = 0; j < linkedClipItem.linkedClips.length; ++j )
                {
                    if ( linkedClipItem.linkedClips[j] === uuid )
                    {
                        linkedClipItem.linkedClips.splice( j, 1 );
                        break;
                    }
                }
238
        }
239

240
        for ( i = 0; i < allClips.length; ++i ) {
luyikei's avatar
luyikei committed
241 242 243 244 245
            if ( allClips[i] === clip ) {
                allClips.splice( i, 1 );
                return;
            }
        }
luyikei's avatar
luyikei committed
246 247
    }

luyikei's avatar
luyikei committed
248 249 250
    Drag.keys: ["Clip"]
    Drag.active: dragArea.drag.active

251 252 253 254
    ListModel {
        id: effects
    }

luyikei's avatar
luyikei committed
255 256 257 258
    Text {
        id: text
        color: "white"
        width: parent.width - 4
259
        height: font.pointSize + 4
luyikei's avatar
luyikei committed
260 261
        x: 4
        y: 4 - font.pointSize / 2
262
        font.pointSize: 7
263 264
        elide: Text.ElideRight
        wrapMode: Text.Wrap
luyikei's avatar
luyikei committed
265 266
    }

267 268 269 270 271 272 273 274 275
    Image {
        id: thumbnailImage
        x: 4
        anchors.top: text.bottom
        anchors.bottom: effectsItem.visible ? effectsItem.top : clip.bottom
        anchors.topMargin: 4
        anchors.bottomMargin: 4
        fillMode: Image.PreserveAspectFit
        visible: width < clip.width
276
        asynchronous: true
277 278
    }

luyikei's avatar
luyikei committed
279 280 281
    MouseArea {
        id: dragArea
        anchors.fill: parent
282
        drag.target: resizing ? null : parent
luyikei's avatar
luyikei committed
283 284
        drag.minimumX: 0
        acceptedButtons: Qt.LeftButton | Qt.RightButton
285 286 287 288 289 290
        hoverEnabled: true
        cursorShape: Qt.OpenHandCursor

        property bool resizing: false

        onPositionChanged: {
luyikei's avatar
luyikei committed
291 292 293
            if ( isCutMode === true )
                return;

luyikei's avatar
luyikei committed
294
            // If it's too short, don't resize.
295 296 297 298 299 300 301 302
            if ( width < 6 ) {
                resizing = false;
                return;
            }

            if ( dragArea.pressed === true ) {
                // Handle resizing
                if ( resizing === true ) {
303 304 305
                    var oldPos = position;
                    var oldBegin = begin;
                    var oldEnd = end;
306
                    if ( mouseX < width / 2 ) {
307 308
                        var newPos = position + ptof( mouseX );
                        var newBegin = begin + ptof( mouseX );
luyikei's avatar
luyikei committed
309
                        if ( newBegin < 0 || newPos < 0 || newBegin >= end )
310
                            return;
luyikei's avatar
luyikei committed
311 312
                        begin = newBegin;
                        position = newPos;
313 314
                    }
                    else {
315
                        var newEnd = begin + ptof( mouseX );
luyikei's avatar
luyikei committed
316
                        if ( newEnd <= begin || newEnd + 1 > clipInfo["length"] )
317
                            return;
luyikei's avatar
luyikei committed
318
                        end = newEnd;
319
                    }
320
                    resizeLinkedClips(oldPos, oldBegin, oldEnd);
321 322 323 324 325 326 327 328 329
                }
            }
            else {
                if ( mouseX < 3 || ( clip.width - mouseX ) < 3 )
                    resizing = true;
                else
                    resizing = false;
            }
        }
luyikei's avatar
luyikei committed
330 331

        onPressed: {
332 333
            clip.Drag.hotSpot = Qt.point( mouseX, clip.height / 2 );

334 335 336 337 338 339 340 341
            if ( mouse.button & Qt.LeftButton ) {
                if ( selected === false ) {
                    if ( !( mouse.modifiers & Qt.ControlModifier ) )
                        clearSelectedClips();
                    selected = true;
                }
                page.dragging = true;
            }
luyikei's avatar
luyikei committed
342 343 344 345
        }

        onClicked: {
            if ( mouse.button & Qt.RightButton ) {
luyikei's avatar
luyikei committed
346
                clipContextMenu.popup();
luyikei's avatar
luyikei committed
347
            }
luyikei's avatar
luyikei committed
348 349 350 351 352 353 354
            else if ( isCutMode === true ) {
                var newClipPos = position + ptof( mouseX );
                var newClipBegin = begin + ptof( mouseX );
                if ( newClipPos - position < 1 || end - newClipBegin < 1 )
                    return;
                workflow.splitClip( uuid, newClipPos, newClipBegin );
            }
355
            page.dragging = false;
luyikei's avatar
luyikei committed
356 357 358
        }

        onReleased: {
359
            // Don't trigger event if the mouse didn't move
luyikei's avatar
luyikei committed
360
            if ( resizing === true && isCutMode === false )
luyikei's avatar
luyikei committed
361
                resize();
362
            else if ( dragArea.drag.active )
luyikei's avatar
luyikei committed
363
                dragFinished();
luyikei's avatar
luyikei committed
364
        }
365 366 367 368

        states: [
            State {
                name: "Normal"
luyikei's avatar
luyikei committed
369 370 371 372 373
                when: isCutMode
                PropertyChanges { target: dragArea; cursorShape: Qt.ArrowCursor }
            },
            State {
                name: "Move"
374 375 376 377 378 379 380 381 382 383 384 385 386 387
                when: !dragArea.pressed && !dragArea.resizing
                PropertyChanges { target: dragArea; cursorShape: Qt.OpenHandCursor }
            },
            State {
                name: "Resizing"
                when: dragArea.resizing
                PropertyChanges { target: dragArea; cursorShape: Qt.SizeHorCursor }
            },
            State {
                name: "Dragging"
                when: dragArea.pressed && !dragArea.resizing
                PropertyChanges { target: dragArea; cursorShape: Qt.ClosedHandCursor }
            }
        ]
luyikei's avatar
luyikei committed
388 389
    }

390 391 392
    DropArea {
        id: effectDropArea
        anchors.fill: parent
393
        keys: ["vlmc/effect_id"]
394 395

        onDropped: {
396
            workflow.addEffect( uuid, drop.getDataAsString( "vlmc/effect_id" ) );
397 398 399
        }
    }

400 401
    Rectangle {
        id: effectsItem
402
        height: 6
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
        width: clip.width
        anchors.bottom: clip.bottom
        visible: effectsTextItem.text ? true : false
        radius: 2
        gradient: Gradient {
            GradientStop {
                id: eGStop1
                position: 0.00;
            }
            GradientStop {
                id: eGStop2
                position: 1.00;
            }
        }

        property alias text: effectsTextItem.text

        Text {
            id: effectsTextItem
            width: clip.width
            color: "white"
            horizontalAlignment: Text.AlignHCenter
            font.pointSize: parent.height - 2
            anchors.centerIn: parent
            elide: Text.ElideRight
        }

        MouseArea {
            id: effectsItemMouseArea
            anchors.fill: parent
            hoverEnabled: true
            onClicked: {
435
                workflow.showEffectStack( uuid );
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
            }
        }

        states: [
            State {
                when: effectsItemMouseArea.pressed
                PropertyChanges { target: eGStop1; color: "#220f3c" }
                PropertyChanges { target: eGStop2; color: "#24245c" }
            },
            State {
                when: effectsItemMouseArea.containsMouse
                PropertyChanges { target: eGStop1; color: "#46469F" }
                PropertyChanges { target: eGStop2; color: "#32215a" }
            },
            State {
                when: !effectsItemMouseArea.containsMouse
                PropertyChanges { target: eGStop1; color: "#24245c" }
                PropertyChanges { target: eGStop2; color: "#200f3c" }
            }
        ]
    }

luyikei's avatar
luyikei committed
458 459
    ClipContextMenu {
        id: clipContextMenu
460
        clip: clip
luyikei's avatar
luyikei committed
461 462
    }

luyikei's avatar
luyikei committed
463
    states: [
464 465 466 467 468 469
        State {
            name: "EffectDrop"
            when: effectDropArea.containsDrag
            PropertyChanges { target: gStop1; color: "#427080" }
            PropertyChanges { target: gStop2; color: "#225060" }
        },
luyikei's avatar
luyikei committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
        State {
            name: "Selected"
            when: selected
            PropertyChanges { target: gStop1; color: "#6498c8" }
            PropertyChanges { target: gStop2; color: "#427080" }
        },
        State {
            name: "Unselected"
            when: !selected
            PropertyChanges { target: gStop1; color: "#4276a6" }
            PropertyChanges { target: gStop2; color: "#1f546f" }
        }
    ]

    transitions: Transition {
        PropertyAnimation {
            properties: "color"
            easing.type: Easing.InOutQuad
            duration: 100
        }
    }
}