Scrolling items while dragging an item

After this Qt Tutorial, I wrote this simple code. There is a horizontal one ListViewwith some simple colored rectangle on it as a delegate for model elements.

import QtQuick 2.5
import QtQuick.Window 2.0
import QtQml.Models 2.2

Window {
    visible: true
    width: 300
    height: 120
    title: qsTr("Hello World")

    Rectangle {
        anchors.fill: parent;

        ListView{
            id: timeline
            anchors.fill: parent
            orientation: ListView.Horizontal
            model: visualModel
            delegate: timelineDelegate

            moveDisplaced: Transition {
                NumberAnimation{
                    properties: "x,y"
                    duration: 200
                }
            }

            DelegateModel {
                id: visualModel
                model: timelineModel
                delegate: timelineDelegate
            }

            Component {
                id: timelineDelegate


                MouseArea {
                    id: dragArea

                    width: 100; height: 100

                    property bool held: false

                    drag.target: held ? content : undefined
                    drag.axis: Drag.XAxis

                    onPressAndHold: held = true
                    onReleased: held = false

                    Rectangle {
                        id: content

                        anchors { horizontalCenter: parent.horizontalCenter; verticalCenter: parent.verticalCenter }
                        width: 100
                        height: 100

                        color: colore
                        opacity: dragArea.held ? 0.8 : 1.0

                        Drag.active: dragArea.held
                        Drag.source: dragArea
                        Drag.hotSpot.x: width / 2
                        Drag.hotSpot.y: height / 2

                        states: State{
                            when: dragArea.held
                            ParentChange { target: content; parent: timeline }
                            AnchorChanges {
                                target: content
                                anchors { horizontalCenter: undefined; verticalCenter: undefined }
                            }
                        }
                    }

                    DropArea {
                        anchors.fill: parent
                        onEntered: {
                            visualModel.items.move( drag.source.DelegateModel.itemsIndex, dragArea.DelegateModel.itemsIndex)
                            timeline.currentIndex = dragArea.DelegateModel.itemsIndex
                        }
                    }
                }
            }

            ListModel {
                id: timelineModel
                // @disable-check M16
                ListElement { colore: "blue" }
                // @disable-check M16
                ListElement { colore: "orange" }
                // @disable-check M16
                ListElement { colore: "red" }
                // @disable-check M16
                ListElement { colore: "yellow" }
                // @disable-check M16
                ListElement { colore: "green" }
                // @disable-check M16
                ListElement { colore: "yellow" }
                // @disable-check M16
                ListElement { colore: "red" }
                // @disable-check M16
                ListElement { colore: "blue" }
                // @disable-check M16
                ListElement { colore: "green" }
            }
        }
    }
}

If I press and hold the button Item, I can change it to another with a good moving effect.
The problem starts when there are a lot of elements in the list, and the target position is outside the visible elements. I can drag an elementโ€™s declaration by moving it next to the right or left border ... here the moving effect is absolutely not nice.

, ?
, , !

Good

Bad

+4
1

ListView ListView.currentIndex. , :

timeline.currentIndex = dragArea.DelegateModel.itemsIndex

, , , . , , , . currentIndex:

timeline.currentIndex = dragArea.DelegateModel.itemsIndex + 1

, . , , :

MouseArea {
    id: dragArea

    property int lastX: 0
    property bool moveRight: false

    onXChanged: {
        moveRight = lastX < x;
        lastX = x;
    }

    //....

    DropArea {
        anchors.fill: parent
        onEntered: {
            visualModel.items.move( drag.source.DelegateModel.itemsIndex, 
                                    dragArea.DelegateModel.itemsIndex)
            if (dragArea.moveRight)
                timeline.currentIndex = dragArea.DelegateModel.itemsIndex + 1
            else
                timeline.currentIndex = dragArea.DelegateModel.itemsIndex - 1
        }
    }
}
+2

Source: https://habr.com/ru/post/1661864/


All Articles