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 Qt Components project.
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 ****************************************************************************/
43 Qt.include("Utils.js");
44 Qt.include("UIConstants.js");
53 var root = findRootItem(item);
56 var component = Qt.createComponent("EditBubble.qml");
58 // due the pragma we cannot access Component.Ready
60 popup = component.createObject(root);
66 Open a shared edit bubble for a given input item.
68 All operations and changes will be binded to the
71 function open(input,position)
79 // Position when text not selected.
80 popup.position = position;
82 // need to set before checking capabilities
83 popup.textInput = input;
86 popup.state = "opened";
88 popup.textInput = null;
90 return popup.textInput != null;
94 Close the shared edit bubble for a given input item.
98 if (!popup || !input || popup.textInput != input)
101 return closePopup(popup);
105 Check if the shared edit bubble is opened for the
108 function isOpened(input)
110 return (popup && popup.textInput == input);
114 Check if the bubble is in the middle of a text
117 function isChangingInput()
119 return (popup && popup.privateRect.changingText);
123 Close a given edit bubble.
125 function closePopup(bubble)
127 if (bubble == null || bubble.textInput == null)
130 bubble.state = "closed";
131 bubble.textInput = null;
136 Adjust EditBubble position to fit in the visible area.
138 If no argument is passed, it will adjust the shared
139 bubble position if already initialized.
141 function adjustPosition(bubble)
143 if (bubble === undefined)
149 var input = bubble.textInput;
150 var rect = bubble.privateRect;
151 var viewport = rect.parent;
153 if (viewport == null || input == null)
156 var irect = input.positionToRectangle(input.selectionStart);
157 var frect = input.positionToRectangle(input.selectionEnd);
158 var mid = rect.width / 2;
160 if (input.selectionStart == input.selectionEnd) {
161 irect.x = popup.position.x;
162 irect.y = popup.position.y;
163 frect.x = popup.position.x;
164 frect.y = popup.position.y;
167 var ipoint = viewport.mapFromItem(input, irect.x, irect.y);
168 var fpoint = viewport.mapFromItem(input, frect.x, frect.y);
170 var px = ipoint.x + (fpoint.x - ipoint.x) / 2 - mid;
171 var py = ipoint.y - rect.height;
175 rect.x = Math.min(Math.max(px, MARGIN_XLARGE - SHADOW_SIZE), viewport.width - rect.width);
177 if (py > SHADOW_SIZE) {
178 rect.y = py - SHADOW_SIZE;
179 rect.arrowDown = true;
181 rect.y = Math.min(Math.max(ipoint.y + irect.height, 0),
182 fpoint.y + frect.height);
183 rect.arrowDown = false;
186 var boundX = mid - rect.arrowBorder;
187 rect.arrowOffset = Math.min(Math.max(-boundX, px - rect.x), boundX);
190 function updateButtons(row)
192 var children = row.children;
193 var visibleItems = new Array();
195 for (var i = 0, j = 0; i < children.length; i++) {
196 var child = children[i];
199 visibleItems[j++] = child;
202 for (var i = 0; i < visibleItems.length; i++) {
203 if (visibleItems.length == 1)
204 visibleItems[i].platformStyle.position = "";
207 visibleItems[i].platformStyle.position = "horizontal-left";
208 else if (i == visibleItems.length - 1)
209 visibleItems[i].platformStyle.position = "horizontal-right";
211 visibleItems[i].platformStyle.position = "horizontal-center";
221 var bubbleContent = popup.privateRect;
222 var rect = {"left": bubbleContent.pos.x,
223 "right": bubbleContent.pos.x + bubbleContent.width,
224 "top": bubbleContent.pos.y,
225 "bottom": bubbleContent.pos.y + bubbleContent.height};