1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 | import QtQuick 1.0
|
---|
42 |
|
---|
43 | Rectangle {
|
---|
44 | id: editor
|
---|
45 | color: "lightGrey"
|
---|
46 | width: 640; height: 480
|
---|
47 |
|
---|
48 | Rectangle {
|
---|
49 | color: "white"
|
---|
50 | anchors.fill: parent
|
---|
51 | anchors.margins: 20
|
---|
52 |
|
---|
53 | BorderImage {
|
---|
54 | id: startHandle
|
---|
55 | source: "pics/startHandle.sci"
|
---|
56 | opacity: 0.0
|
---|
57 | width: 10
|
---|
58 | x: edit.positionToRectangle(edit.selectionStart).x - flick.contentX-width
|
---|
59 | y: edit.positionToRectangle(edit.selectionStart).y - flick.contentY
|
---|
60 | height: edit.positionToRectangle(edit.selectionStart).height
|
---|
61 | }
|
---|
62 |
|
---|
63 | BorderImage {
|
---|
64 | id: endHandle
|
---|
65 | source: "pics/endHandle.sci"
|
---|
66 | opacity: 0.0
|
---|
67 | width: 10
|
---|
68 | x: edit.positionToRectangle(edit.selectionEnd).x - flick.contentX
|
---|
69 | y: edit.positionToRectangle(edit.selectionEnd).y - flick.contentY
|
---|
70 | height: edit.positionToRectangle(edit.selectionEnd).height
|
---|
71 | }
|
---|
72 |
|
---|
73 | Flickable {
|
---|
74 | id: flick
|
---|
75 |
|
---|
76 | anchors.fill: parent
|
---|
77 | contentWidth: edit.paintedWidth
|
---|
78 | contentHeight: edit.paintedHeight
|
---|
79 | interactive: true
|
---|
80 | clip: true
|
---|
81 |
|
---|
82 | function ensureVisible(r) {
|
---|
83 | if (contentX >= r.x)
|
---|
84 | contentX = r.x;
|
---|
85 | else if (contentX+width <= r.x+r.width)
|
---|
86 | contentX = r.x+r.width-width;
|
---|
87 | if (contentY >= r.y)
|
---|
88 | contentY = r.y;
|
---|
89 | else if (contentY+height <= r.y+r.height)
|
---|
90 | contentY = r.y+r.height-height;
|
---|
91 | }
|
---|
92 |
|
---|
93 | TextEdit {
|
---|
94 | id: edit
|
---|
95 | width: flick.width
|
---|
96 | height: flick.height
|
---|
97 | focus: true
|
---|
98 | wrapMode: TextEdit.Wrap
|
---|
99 |
|
---|
100 | onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
|
---|
101 |
|
---|
102 | text: "<h1>Text Selection</h1>"
|
---|
103 | +"<p>This example is a whacky text selection mechanisms, showing how these can be implemented in the TextEdit element, to cater for whatever style is appropriate for the target platform."
|
---|
104 | +"<p><b>Press-and-hold</b> to select a word, then drag the selection handles."
|
---|
105 | +"<p><b>Drag outside the selection</b> to scroll the text."
|
---|
106 | +"<p><b>Click inside the selection</b> to cut/copy/paste/cancel selection."
|
---|
107 | +"<p>It's too whacky to let you paste if there is no current selection."
|
---|
108 |
|
---|
109 | MouseArea {
|
---|
110 | property string drag: ""
|
---|
111 | property int pressPos
|
---|
112 |
|
---|
113 | x: -startHandle.width
|
---|
114 | y: 0
|
---|
115 | width: parent.width+startHandle.width+endHandle.width
|
---|
116 | height: parent.height
|
---|
117 |
|
---|
118 | onPressAndHold: {
|
---|
119 | if (editor.state == "") {
|
---|
120 | edit.cursorPosition = edit.positionAt(mouse.x+x,mouse.y+y);
|
---|
121 | edit.selectWord();
|
---|
122 | editor.state = "selection"
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | onClicked: {
|
---|
127 | if (editor.state == "") {
|
---|
128 | edit.cursorPosition = edit.positionAt(mouse.x+x,mouse.y+y);
|
---|
129 | if (!edit.focus)
|
---|
130 | edit.focus = true;
|
---|
131 | edit.openSoftwareInputPanel();
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | function hitHandle(h,x,y) {
|
---|
136 | return x>=h.x+flick.contentX && x<h.x+flick.contentX+h.width && y>=h.y+flick.contentY && y<h.y+flick.contentY+h.height
|
---|
137 | }
|
---|
138 |
|
---|
139 | onPressed: {
|
---|
140 | if (editor.state == "selection") {
|
---|
141 | if (hitHandle(startHandle,mouse.x+x,mouse.y+y)) {
|
---|
142 | drag = "start"
|
---|
143 | flick.interactive = false
|
---|
144 | } else if (hitHandle(endHandle,mouse.x+x,mouse.y+y)) {
|
---|
145 | drag = "end"
|
---|
146 | flick.interactive = false
|
---|
147 | } else {
|
---|
148 | var pos = edit.positionAt(mouse.x+x,mouse.y+y);
|
---|
149 | if (pos >= edit.selectionStart && pos <= edit.selectionEnd) {
|
---|
150 | drag = "selection"
|
---|
151 | flick.interactive = false
|
---|
152 | } else {
|
---|
153 | drag = ""
|
---|
154 | flick.interactive = true
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | onReleased: {
|
---|
161 | if (editor.state == "selection") {
|
---|
162 | if (drag == "selection") {
|
---|
163 | editor.state = "menu"
|
---|
164 | }
|
---|
165 | drag = ""
|
---|
166 | }
|
---|
167 | flick.interactive = true
|
---|
168 | }
|
---|
169 |
|
---|
170 | onPositionChanged: {
|
---|
171 | if (editor.state == "selection" && drag != "") {
|
---|
172 | if (drag == "start") {
|
---|
173 | var pos = edit.positionAt(mouse.x+x+startHandle.width/2,mouse.y+y);
|
---|
174 | var e = edit.selectionEnd;
|
---|
175 | if (e < pos)
|
---|
176 | e = pos;
|
---|
177 | edit.select(pos,e);
|
---|
178 | } else if (drag == "end") {
|
---|
179 | var pos = edit.positionAt(mouse.x+x-endHandle.width/2,mouse.y+y);
|
---|
180 | var s = edit.selectionStart;
|
---|
181 | if (s > pos)
|
---|
182 | s = pos;
|
---|
183 | edit.select(s,pos);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | Item {
|
---|
192 | id: menu
|
---|
193 | opacity: 0.0
|
---|
194 | width: 100
|
---|
195 | height: 120
|
---|
196 | anchors.centerIn: parent
|
---|
197 |
|
---|
198 | Rectangle {
|
---|
199 | border.width: 1
|
---|
200 | border.color: "darkBlue"
|
---|
201 | radius: 15
|
---|
202 | color: "#806080FF"
|
---|
203 | anchors.fill: parent
|
---|
204 | }
|
---|
205 |
|
---|
206 | Column {
|
---|
207 | anchors.centerIn: parent
|
---|
208 | spacing: 8
|
---|
209 |
|
---|
210 | Rectangle {
|
---|
211 | border.width: 1
|
---|
212 | border.color: "darkBlue"
|
---|
213 | color: "#ff7090FF"
|
---|
214 | width: 60
|
---|
215 | height: 16
|
---|
216 |
|
---|
217 | Text { anchors.centerIn: parent; text: "Cut" }
|
---|
218 |
|
---|
219 | MouseArea {
|
---|
220 | anchors.fill: parent
|
---|
221 | onClicked: { edit.cut(); editor.state = "" }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | Rectangle {
|
---|
226 | border.width: 1
|
---|
227 | border.color: "darkBlue"
|
---|
228 | color: "#ff7090FF"
|
---|
229 | width: 60
|
---|
230 | height: 16
|
---|
231 |
|
---|
232 | Text { anchors.centerIn: parent; text: "Copy" }
|
---|
233 |
|
---|
234 | MouseArea {
|
---|
235 | anchors.fill: parent
|
---|
236 | onClicked: { edit.copy(); editor.state = "selection" }
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | Rectangle {
|
---|
241 | border.width: 1
|
---|
242 | border.color: "darkBlue"
|
---|
243 | color: "#ff7090FF"
|
---|
244 | width: 60
|
---|
245 | height: 16
|
---|
246 |
|
---|
247 | Text { anchors.centerIn: parent; text: "Paste" }
|
---|
248 |
|
---|
249 | MouseArea {
|
---|
250 | anchors.fill: parent
|
---|
251 | onClicked: { edit.paste(); edit.cursorPosition = edit.selectionEnd; editor.state = "" }
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | Rectangle {
|
---|
256 | border.width: 1
|
---|
257 | border.color: "darkBlue"
|
---|
258 | color: "#ff7090FF"
|
---|
259 | width: 60
|
---|
260 | height: 16
|
---|
261 |
|
---|
262 | Text { anchors.centerIn: parent; text: "Deselect" }
|
---|
263 |
|
---|
264 | MouseArea {
|
---|
265 | anchors.fill: parent
|
---|
266 | onClicked: {
|
---|
267 | edit.cursorPosition = edit.selectionEnd;
|
---|
268 | edit.select(edit.cursorPosition, edit.cursorPosition);
|
---|
269 | editor.state = ""
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | states: [
|
---|
278 | State {
|
---|
279 | name: "selection"
|
---|
280 | PropertyChanges { target: startHandle; opacity: 1.0 }
|
---|
281 | PropertyChanges { target: endHandle; opacity: 1.0 }
|
---|
282 | },
|
---|
283 | State {
|
---|
284 | name: "menu"
|
---|
285 | PropertyChanges { target: startHandle; opacity: 0.5 }
|
---|
286 | PropertyChanges { target: endHandle; opacity: 0.5 }
|
---|
287 | PropertyChanges { target: menu; opacity: 1.0 }
|
---|
288 | }
|
---|
289 | ]
|
---|
290 | }
|
---|