improved desktop UI
[mardrone] / mardrone / imports / com / nokia / meego / EditBubble.js
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 .pragma library
42
43 Qt.include("Utils.js");
44 Qt.include("UIConstants.js");
45
46 var popup = null;
47
48 function init(item)
49 {
50     if (popup != null)
51         return true;
52
53     var root = findRootItem(item);
54
55     // create root popup
56     var component = Qt.createComponent("EditBubble.qml");
57
58     // due the pragma we cannot access Component.Ready
59     if (component)
60         popup = component.createObject(root);
61
62     return popup != null;
63 }
64
65 /*
66   Open a shared edit bubble for a given input item.
67
68   All operations and changes will be binded to the
69   given item.
70 */
71 function open(input,position)
72 {
73     if (!input)
74         return false;
75
76     if (!init(input))
77         return false;
78
79     // Position when text not selected.
80     popup.position = position;
81
82     // need to set before checking capabilities
83     popup.textInput = input;
84
85     if (popup.valid)
86         popup.state = "opened";
87     else
88         popup.textInput = null;
89
90     return popup.textInput != null;
91 }
92
93 /*
94   Close the shared edit bubble for a given input item.
95 */
96 function close(input)
97 {
98     if (!popup || !input || popup.textInput != input)
99         return false;
100
101     return closePopup(popup);
102 }
103
104 /*
105   Check if the shared edit bubble is opened for the
106   given input item.
107 */
108 function isOpened(input)
109 {
110     return (popup && popup.textInput == input);
111 }
112
113 /*
114   Check if the bubble is in the middle of a text
115   change operation.
116 */
117 function isChangingInput()
118 {
119     return (popup && popup.privateRect.changingText);
120 }
121
122 /*
123   Close a given edit bubble.
124 */
125 function closePopup(bubble)
126 {
127     if (bubble == null || bubble.textInput == null)
128         return false;
129
130     bubble.state = "closed";
131     bubble.textInput = null;
132     return true;
133 }
134
135 /*
136   Adjust EditBubble position to fit in the visible area.
137
138   If no argument is passed, it will adjust the shared
139   bubble position if already initialized.
140 */
141 function adjustPosition(bubble)
142 {
143     if (bubble === undefined)
144         bubble = popup;
145
146     if (bubble == null)
147         return;
148
149     var input = bubble.textInput;
150     var rect = bubble.privateRect;
151     var viewport = rect.parent;
152
153     if (viewport == null || input == null)
154         return;
155
156     var irect = input.positionToRectangle(input.selectionStart);
157     var frect = input.positionToRectangle(input.selectionEnd);
158     var mid = rect.width / 2;
159
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;
165    }
166
167     var ipoint = viewport.mapFromItem(input, irect.x, irect.y);
168     var fpoint = viewport.mapFromItem(input, frect.x, frect.y);
169
170     var px = ipoint.x + (fpoint.x - ipoint.x) / 2 - mid;
171     var py = ipoint.y - rect.height;
172
173     var SHADOW_SIZE = 6
174
175     rect.x = Math.min(Math.max(px, MARGIN_XLARGE - SHADOW_SIZE), viewport.width - rect.width);
176
177     if (py > SHADOW_SIZE) {
178         rect.y = py - SHADOW_SIZE;
179         rect.arrowDown = true;
180     } else {
181         rect.y = Math.min(Math.max(ipoint.y + irect.height, 0),
182                           fpoint.y + frect.height);
183         rect.arrowDown = false;
184     }
185
186     var boundX = mid - rect.arrowBorder;
187     rect.arrowOffset = Math.min(Math.max(-boundX, px - rect.x), boundX);
188 }
189
190 function updateButtons(row)
191 {
192     var children = row.children;
193     var visibleItems = new Array();
194
195     for (var i = 0, j = 0; i < children.length; i++) {
196         var child = children[i];
197
198         if (child.visible)
199             visibleItems[j++] = child;
200     }
201
202     for (var i = 0; i < visibleItems.length; i++) {
203         if (visibleItems.length == 1)
204             visibleItems[i].platformStyle.position = "";
205         else {
206             if (i == 0)
207                 visibleItems[i].platformStyle.position = "horizontal-left";
208             else if (i == visibleItems.length - 1)
209                 visibleItems[i].platformStyle.position = "horizontal-right";
210             else
211                 visibleItems[i].platformStyle.position = "horizontal-center";
212         }
213     }
214 }
215
216 function geometry()
217 {
218     if (popup == null)
219         return;
220
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};
226
227     return rect;
228 }