TransitionItem.qml 5.19 KB
Newer Older
luyikei's avatar
luyikei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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 172 173 174
import QtQuick 2.0

Rectangle {
    id: transition

    x: ftop( begin )
    y: inTrack ? 0 : ( type === "Video" ? trackHeight : 0 ) - height / 2
    z: maxZ + 100
    height: inTrack ? trackHeight - 3 : trackHeight / 2
    width: ftop( end - begin )
    color: inTrack ? "#000000" : "#AAAA88"
    opacity: inTrack ? 0.5 : 1.0
    border.color: "#000000"
    border.width: 1

    property string uuid: "transitionUuid"
    property string identifier
    property string name
    property bool isCrossDissolve: identifier === "dissolve"
    property bool isFadeOut: false
    property bool isFadeIn: false
     // Whether it's in a track or between tracks.
    readonly property bool inTrack: isCrossDissolve || isFadeOut || isFadeIn
    property int begin
    property int end
    property int trackId
    property string type
    property var clips: [] // clips overlapping
    property var transitionInfo

    Drag.keys: ["Transition"]
    Drag.active: mouseArea.drag.active

    onUuidChanged: {
        transitionInfo["uuid"] = uuid;
        allTransitionsDict[uuid] = transition;
    }

    Component.onCompleted: {
        allTransitions.push( transition );
        if ( uuid )
            allTransitionsDict[uuid] = transition;
    }

    Component.onDestruction: {
        Drag.drop();
        for ( var i = 0; i < allTransitions.length; ++i ) {
            if ( allTransitions[i] === transition ) {
                allTransitions.splice( i, 1 );
                return;
            }
        }
    }

    onYChanged: {
        if ( inTrack === false ) {
            if ( ( y + height / 2 ) % trackHeight !== 0 )
                y -= ( y + height / 2 ) % trackHeight;
        }
        // Don't move outside its TrackContainer
        // For Top
        var yToMoveUp = track.mapToItem( container, 0, 0 ).y + y;
        if ( yToMoveUp < 0 )
            y += trackHeight;
        // For Bottom
        if ( yToMoveUp + height > container.height )
            y -= trackHeight;
    }

    Canvas {
        id: tCanvas
        anchors.fill: parent

        onPaint: {
            var ctx = getContext( "2d" );
            ctx.strokeStyle = Qt.rgba( 0.9, 0.8, 0.25, 1 );
            ctx.lineWidth = 1;
            ctx.beginPath();
            if ( isCrossDissolve === true || isFadeOut === true )
            {
                ctx.moveTo( 0, 0 );
                ctx.lineTo( width, height );
            }
            if ( isCrossDissolve === true || isFadeIn === true )
            {
                ctx.moveTo( 0, height );
                ctx.lineTo( width, 0 );
            }
            ctx.closePath();
            ctx.stroke();
        }
    }

    Text {
        id: nameLabel
        visible: !inTrack
        text: identifier
        width: parent.width
        elide: Text.ElideRight
        font.pixelSize: parent.height - 5
        anchors.centerIn: parent
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        visible: !isCrossDissolve
        drag.target: resizing || isCrossDissolve ? null : parent
        drag.minimumX: 0
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        hoverEnabled: true
        cursorShape: Qt.OpenHandCursor

        property bool resizing: false

        onPositionChanged: {
            // If it's too short, don't resize.
            if ( width < 6 ) {
                return;
            }

            if ( mouseArea.pressed === true ) {
                // Handle resizing
                if ( resizing === true ) {
                    var oldBegin = begin;
                    var oldEnd = end;
                    if ( mouseX < width / 2 ) {
                        var newBegin = begin + ptof( mouseX );
                        if ( newBegin < 0 || newBegin >= end )
                            return;
                        begin = newBegin;
                    }
                    else {
                        var newEnd = begin + ptof( mouseX );
                        if ( newEnd <= begin )
                            return;
                        end = newEnd;
                    }
                }
            }
            else {
                if ( mouseX < 3 || ( transition.width - mouseX ) < 3 )
                    resizing = true;
                else
                    resizing = false;
            }
        }

        onReleased: {
            if ( transitionInfo["begin"] !== begin || transitionInfo["end"] !== end )
                workflow.moveTransition( uuid, begin, end );
            if ( transitionInfo["trackBId"] !== trackId )
                workflow.moveTransitionBetweenTracks( uuid, trackId - 1, trackId );
        }

        states: [
            State {
                name: "Move"
                when: !mouseArea.pressed && !mouseArea.resizing
                PropertyChanges { target: mouseArea; cursorShape: Qt.OpenHandCursor }
            },
            State {
                name: "Resizing"
                when: mouseArea.resizing
                PropertyChanges { target: mouseArea; cursorShape: Qt.SizeHorCursor }
            },
            State {
                name: "Dragging"
                when: mouseArea.pressed && !mouseArea.resizing
                PropertyChanges { target: mouseArea; cursorShape: Qt.ClosedHandCursor }
            }
        ]
    }
}