aboutsummaryrefslogtreecommitdiffstats
path: root/examples/declarative/dragtarget/text/dragtext.qml
blob: c4a4f24f74ee0303e6725c9c4a447606e6a566a4 (plain)
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
import QtQuick 2.0

Item {
    id: root
    width: 320; height: 480

    Rectangle {
        id: inputRect
        anchors.left: parent.left; anchors.right: parent.right; anchors.top: parent.top
        anchors.margins: 2
        height: input.implicitHeight + 4

        border.width: 1

        TextInput {
            id: input
            anchors.fill: parent; anchors.margins: 2

            text: "the quick brown fox jumped over the lazy dog"

            DragTarget {
                id: inputTarget

                anchors.fill: parent

                Component {
                    id: draggedInputText
                    Text {
                        x: inputTarget.dragX
                        y: inputTarget.dragY
                        text: inputTarget.dragData
                        color: "blue"
                        font: input.font
                    }
                }

                Loader {
                    sourceComponent: parent.containsDrag ? draggedInputText : undefined
                }
            }


            MouseArea {
                id: inputDraggable

                anchors.fill: parent
                enabled: input.selectionStart != input.selectionEnd

                drag.data: input.selectedText
                drag.target: inputDraggable

                drag.onDragged: {
                    var position = input.positionAt(mouse.x);
                    mouse.accepted = position >= input.selectionStart && position < input.selectionEnd
                }

                MouseArea {
                    anchors.fill: parent

                    onPressed: {
                        var position = input.positionAt(mouse.x);
                        if (position < input.selectionStart || position >= input.selectionEnd) {
                            input.cursorPosition = position
                        } else {
                            mouse.accepted = false
                        }
                    }
                    onPositionChanged: input.moveCursorSelection(input.positionAt(mouse.x))
                }
            }
        }
    }

    Rectangle {
        id: editRect
        anchors.left: parent.left; anchors.right: parent.right;
        anchors.top: inputRect.bottom; anchors.bottom: parent.bottom
        anchors.margins: 2

        border.width: 1

        TextEdit {
            id: edit
            anchors.fill: parent; anchors.margins: 2

            text: "the quick brown fox jumped over the lazy dog"
            font.pixelSize: 18
            wrapMode: TextEdit.WordWrap

            DragTarget {
                id: editTarget

                anchors.fill: parent


                Component {
                    id: draggedEditText
                    Text {
                        x: editTarget.dragX
                        y: editTarget.dragY
                        text: editTarget.dragData
                        color: "red"
                        font: edit.font
                    }
                }

                Loader {
                    sourceComponent: parent.containsDrag ? draggedEditText : undefined
                }
            }

            MouseArea {
                id: editDraggable

                anchors.fill: parent
                enabled: edit.selectionStart != edit.selectionEnd

                drag.data: edit.selectedText
                drag.target: editDraggable

                drag.onDragged: {
                    var position = edit.positionAt(mouse.x, mouse.y);
                    mouse.accepted = position >= edit.selectionStart && position < edit.selectionEnd
                }

                MouseArea {
                    anchors.fill: parent

                    onPressed: {
                        var position = edit.positionAt(mouse.x, mouse.y);
                        if (position < edit.selectionStart || position >= edit.selectionEnd) {
                            edit.cursorPosition = position
                        } else {
                            mouse.accepted = false
                        }
                    }
                    onPositionChanged: edit.moveCursorSelection(edit.positionAt(mouse.x, mouse.y))
                }
            }
        }
    }
}