1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the examples of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
15 ** * Redistributions of source code must retain the above copyright
16 ** notice, this list of conditions and the following disclaimer.
17 ** * Redistributions in binary form must reproduce the above copyright
18 ** notice, this list of conditions and the following disclaimer in
19 ** the documentation and/or other materials provided with the
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 ** the names of its contributors may be used to endorse or promote
23 ** products derived from this software without specific prior written
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
45 width: 640; height: 480
54 source: "pics/startHandle.sci"
57 x: edit.positionToRectangle(edit.selectionStart).x - flick.contentX-width
58 y: edit.positionToRectangle(edit.selectionStart).y - flick.contentY
59 height: edit.positionToRectangle(edit.selectionStart).height
64 source: "pics/endHandle.sci"
67 x: edit.positionToRectangle(edit.selectionEnd).x - flick.contentX
68 y: edit.positionToRectangle(edit.selectionEnd).y - flick.contentY
69 height: edit.positionToRectangle(edit.selectionEnd).height
76 contentWidth: edit.paintedWidth
77 contentHeight: edit.paintedHeight
81 function ensureVisible(r) {
84 else if (contentX+width <= r.x+r.width)
85 contentX = r.x+r.width-width;
88 else if (contentY+height <= r.y+r.height)
89 contentY = r.y+r.height-height;
97 wrapMode: TextEdit.Wrap
99 onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
101 text: "<h1>Text Selection</h1>"
102 +"<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."
103 +"<p><b>Press-and-hold</b> to select a word, then drag the selection handles."
104 +"<p><b>Drag outside the selection</b> to scroll the text."
105 +"<p><b>Click inside the selection</b> to cut/copy/paste/cancel selection."
106 +"<p>It's too whacky to let you paste if there is no current selection."
109 property string drag: ""
110 property int pressPos
112 x: -startHandle.width
114 width: parent.width+startHandle.width+endHandle.width
115 height: parent.height
118 if (editor.state == "") {
119 edit.cursorPosition = edit.positionAt(mouse.x+x,mouse.y+y);
121 editor.state = "selection"
126 if (editor.state == "") {
127 edit.cursorPosition = edit.positionAt(mouse.x+x,mouse.y+y);
130 edit.openSoftwareInputPanel();
134 function hitHandle(h,x,y) {
135 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
139 if (editor.state == "selection") {
140 if (hitHandle(startHandle,mouse.x+x,mouse.y+y)) {
142 flick.interactive = false
143 } else if (hitHandle(endHandle,mouse.x+x,mouse.y+y)) {
145 flick.interactive = false
147 var pos = edit.positionAt(mouse.x+x,mouse.y+y);
148 if (pos >= edit.selectionStart && pos <= edit.selectionEnd) {
150 flick.interactive = false
153 flick.interactive = true
160 if (editor.state == "selection") {
161 if (drag == "selection") {
162 editor.state = "menu"
166 flick.interactive = true
170 if (editor.state == "selection" && drag != "") {
171 if (drag == "start") {
172 var pos = edit.positionAt(mouse.x+x+startHandle.width/2,mouse.y+y);
173 var e = edit.selectionEnd;
177 } else if (drag == "end") {
178 var pos = edit.positionAt(mouse.x+x-endHandle.width/2,mouse.y+y);
179 var s = edit.selectionStart;
195 anchors.centerIn: parent
199 border.color: "darkBlue"
206 anchors.centerIn: parent
211 border.color: "darkBlue"
216 Text { anchors.centerIn: parent; text: "Cut" }
220 onClicked: { edit.cut(); editor.state = "" }
226 border.color: "darkBlue"
231 Text { anchors.centerIn: parent; text: "Copy" }
235 onClicked: { edit.copy(); editor.state = "selection" }
241 border.color: "darkBlue"
246 Text { anchors.centerIn: parent; text: "Paste" }
250 onClicked: { edit.paste(); edit.cursorPosition = edit.selectionEnd; editor.state = "" }
256 border.color: "darkBlue"
261 Text { anchors.centerIn: parent; text: "Deselect" }
266 edit.cursorPosition = edit.selectionEnd;
279 PropertyChanges { target: startHandle; opacity: 1.0 }
280 PropertyChanges { target: endHandle; opacity: 1.0 }
284 PropertyChanges { target: startHandle; opacity: 0.5 }
285 PropertyChanges { target: endHandle; opacity: 0.5 }
286 PropertyChanges { target: menu; opacity: 1.0 }