libandroidplugin added
[mardrone] / mardrone / imports / com / nokia / android.1.1 / TextSelectionHandle.qml
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 Qt Components project.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
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
20 **     distribution.
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
24 **     permission.
25 **
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."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 1.1
42 import "RectUtils.js" as Utils
43
44 Item {
45     id: root
46
47     property alias imageSource: image.source
48     property int editorPos: 0 //character position of the handle in the editor document
49     property Item editor
50     // the default size of the touch area should come from some style constant
51     property variant touchAreaSize: Qt.size(platformStyle.graphicSizeMedium, platformStyle.graphicSizeMedium)
52     property alias showImage: image.visible
53     property variant viewPortRect: Qt.rect(0, 0, 0, 0) // item's coordinates
54     //parent's coordinate system
55     property variant center: Qt.point(root.x + root.width / 2, root.y + root.height / 2)
56
57     // Point is in Item's coordinate system
58     function containsPoint(point) {
59         var touchAreaRect = Qt.rect(touchArea.x, touchArea.y, touchArea.width, touchArea.height);
60
61         if (!Utils.rectContainsRect(viewPortRect, touchAreaRect)
62             && Utils.rectIntersectsRect(viewPortRect, touchAreaRect)) {
63                 touchAreaRect.x = Math.max(touchAreaRect.x, viewPortRect.x);
64                 touchAreaRect.x = Math.min(touchAreaRect.x, viewPortRect.x + viewPortRect.width - touchAreaRect.width);
65                 touchAreaRect.y = Math.max(touchAreaRect.y, viewPortRect.y);
66                 touchAreaRect.y = Math.min(touchAreaRect.y, viewPortRect.y + viewPortRect.height - touchAreaRect.height);
67         }
68
69         effectiveTouchArea.x = touchAreaRect.x;
70         effectiveTouchArea.y = touchAreaRect.y;
71
72         if (Utils.rectContainsPoint(touchAreaRect, point.x, point.y))
73             return true;
74
75         return false;
76     }
77
78     // Point is in parent's coordinate system
79     function pointDistanceFromCenter(point) {
80         return Utils.manhattan(point, root.center);
81     }
82
83     function updateGeometry() {
84         var rect = root.editor.positionToRectangle(editorPos);
85         var pos = root.editor.mapToItem(root.parent, rect.x, rect.y);
86         root.x = pos.x;
87         root.y = pos.y;
88         root.width = rect.width;
89         root.height = rect.height;
90     }
91
92     onEditorPosChanged: root.updateGeometry()
93
94     Item {
95         id: touchArea
96
97         visible: false
98         anchors.centerIn: root
99         width: root.touchAreaSize.width
100         height: Math.max(root.touchAreaSize.height, root.height)
101     }
102
103     Item {
104         id: effectiveTouchArea
105
106         visible: false
107         width: root.touchAreaSize.width
108         height: Math.max(root.touchAreaSize.height, root.height)
109     }
110
111     BorderImage {
112         id: image
113
114         property real tiny: Math.round(platformStyle.borderSizeMedium / 2)
115
116         anchors {
117             left: root.objectName == "SelectionEnd" ? root.right : undefined
118             right: root.objectName == "SelectionBegin" ? root.left : undefined
119             leftMargin: root.objectName == "SelectionEnd" ? -editor.cursorRectangle.width : 0
120             top: root.top
121             topMargin: -tiny / 2 + 1
122             bottom: root.bottom
123             bottomMargin: -tiny / 2
124         }
125
126         border {
127             left: 0
128             top: tiny
129             right: 0
130             bottom: tiny
131         }
132     }
133 }