1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
45 property variant friends : [];
46 property string name : "";
47 property string twitterId : "";
48 property string image : "";
49 property string url : "";
50 property string desc : "";
55 property bool hasFocus : false;
56 property variant canvas;
57 property variant manager;
58 property variant linkColor;
59 property bool selected : false;
68 border.color:"steelblue"
72 Text {color:"steelblue"; font.pointSize:15; width:250; height:30; text:twitterUser.name; wrapMode: Text.WrapAnywhere}
73 Text {color:"steelblue"; font.pointSize:8; width:250; height:30; text:twitterUser.url; wrapMode: Text.WrapAnywhere}
78 twitterUser.posX = twitterUser.x;
79 twitterUser.posY = twitterUser.y;
80 twitterUser.canvas.requestPaint();
88 drag.target : twitterUser
89 drag.axis : Drag.XandYAxis
92 if (!twitterUser.selected) {
93 twitterUser.selected = true;
94 twitterStatus.opacity = 1;
95 twitterStatus.visible = true;
97 twitterUser.selected = false;
98 twitterStatus.opacity = 0;
103 twitterStatus.opacity = 0;
104 twitterUser.selected = false;
105 twitterUser.hasFocus = true;
106 twitterUser.canvas.twitterName = twitterUser.name;
107 twitterUser.canvas.twitterId = twitterUser.twitterId;
108 twitterUser.canvas.loading = true;
109 twitterUser.createFriends();
113 function show(ctx, layoutChanged) {
114 var w = canvas.width;
115 var h = canvas.height;
116 if (twitterUser.hasFocus) {
117 twitterUser.width = 60
118 twitterUser.height = 60
119 twitterUser.posX = w/2;
120 twitterUser.posY = h/2;
122 twitterUser.width = 40
123 twitterUser.height = 40
127 if (twitterUser.hasFocus) {
129 twitterUser.layoutFriends();
130 twitterUser.linkFriends(ctx);
131 twitterUser.showFriends(ctx);
132 ctx.shadowOffsetX = 5;
133 ctx.shadowOffsetY = 5;
135 ctx.shadowColor = "blue";
138 ctx.shadowOffsetX = 5;
139 ctx.shadowOffsetY = 5;
141 ctx.shadowColor = twitterUser.linkColor;
142 ctx.globalAlpha = 0.6;
145 if (twitterUser.canvas.isImageLoaded(twitterUser.image)) {
146 ctx.drawImage(twitterUser.image, twitterUser.posX, twitterUser.posY, twitterUser.width, twitterUser.height);
148 // ctx.font = "15px";
149 // var nameSize = ctx.measureText(twitterUser.name).width;
150 // ctx.fillText(twitterUser.name, twitterUser.posX + nameSize/2 - twitterUser.width/2, twitterUser.posY + twitterUser.height/2 + 10);
153 console.log("name:" + twitterUser.name
154 + " x:" + twitterUser.posX
155 + " y:" + twitterUser.posY
156 + " width:" + twitterUser.width
157 + " height:" + twitterUser.height
158 + " id:" + twitterUser.twitterId
159 + " image:" + twitterUser.image
160 + " url:" + twitterUser.url + "\n" + twitterUser.desc);
163 function layoutFriends() {
164 var w = canvas.width;
165 var h = canvas.height;
166 for (var i=0; i < twitterUser.friends.length; i++) {
167 var friend = manager.getById(twitterUser.friends[i]);
169 friend.x = Math.random() *w;
170 friend.y = Math.random() *h;
175 function showFriends(ctx) {
176 var w = canvas.width;
177 var h = canvas.height;
178 for (var i=0; i < twitterUser.friends.length && i < 15; i++) {
179 var friend = manager.getById(twitterUser.friends[i]);
180 if (friend && twitterUser.canvas.isImageLoaded(friend.image)) {
181 friend.hasFocus = false;
182 friend.show(ctx, false);
187 function linkFriends(ctx) {
188 var w = canvas.width;
189 var h = canvas.height;
190 for (var i=0; i < twitterUser.friends.length && i < 15; i++) {
191 var friend = manager.getById(twitterUser.friends[i]);
192 if (friend && twitterUser.canvas.isImageLoaded(friend.image)) {
193 if (!friend.linkColor)
194 friend.linkColor = Qt.rgba( ((Math.random() * 200) +55)/255
195 , ((Math.random() * 200) +55)/255
196 , ((Math.random() * 200) +55)/255, 0.8);
197 ctx.strokeStyle = friend.linkColor;
200 ctx.moveTo(twitterUser.posX + twitterUser.width/2, twitterUser.posY + twitterUser.height/2);
201 ctx.lineTo(friend.x + friend.width/2, friend.y + friend.height/2);
208 function create(url) {
209 var x = new XMLHttpRequest;
212 x.onreadystatechange = function() {
213 if (x.readyState == XMLHttpRequest.DONE) {
214 var user = eval('(' + x.responseText +')')[0];
215 twitterUser.name = user.name;
216 twitterUser.twitterId = user.id;
217 twitterUser.image = user.profile_image_url;
218 twitterUser.canvas.loadImage(twitterUser.image);
219 twitterUser.url = user.url;
220 twitterUser.desc = user.description;
221 twitterUser.createFriends();
227 function createByName(name) {
228 if (twitterUser.name === "" && twitterUser.twitterId === "") {
229 twitterUser.name = name;
230 var userUrl = "http://api.twitter.com/1/users/lookup.json?stringify_ids=true&screen_name=" + name;
231 twitterUser.create(userUrl);
235 function createById(id) {
236 if (twitterUser.name === "" && twitterUser.twitterId === "") {
237 twitterUser.twitterId = id;
238 var userUrl = "http://api.twitter.com/1/users/lookup.json?stringify_ids=true&user_id=" + id;
239 twitterUser.create(userUrl);
243 function createFriends() {
244 if (twitterUser.friends.length === 0) {
245 var x = new XMLHttpRequest;
246 var friendsUrl = "https://api.twitter.com/1/friends/ids.json?cursor=-1&stringify_ids=true&user_id=" + twitterUser.twitterId;
247 x.open("GET", friendsUrl);
249 x.onreadystatechange = function() {
250 if (x.readyState == XMLHttpRequest.DONE) {
251 twitterUser.friends = eval('(' + x.responseText +')').ids;
252 var doRequest = false;
253 var userUrl = "http://api.twitter.com/1/users/lookup.json?stringify_ids=true&user_id=";
255 for (var i=0; i<twitterUser.friends.length && i < 100; i++) {
256 var friend = manager.getById(twitterUser.friends[i]);
258 userUrl += "," + twitterUser.friends[i];
263 if (!doRequest) return;
265 var xx = new XMLHttpRequest;
266 xx.open("GET", userUrl);
267 xx.onreadystatechange = function() {
268 if (xx.readyState == XMLHttpRequest.DONE) {
269 var friendUsers = eval('(' + xx.responseText +')');
270 for(var i=0; i<friendUsers.length; i++) {
271 var friend = manager.createTwitterUser(twitterUser.canvas);
272 friend.name = friendUsers[i].name;
273 friend.twitterId = friendUsers[i].id;
274 friend.image = friendUsers[i].profile_image_url ? friendUsers[i].profile_image_url : "";
275 friend.url = friendUsers[i].url ? friendUsers[i].url : "";
276 friend.desc = friendUsers[i].description ? friendUsers[i].description : "";
277 friend.manager = twitterUser.manager;
278 twitterUser.canvas.loadImage(friend.image);
281 if (twitterUser.hasFocus && twitterUser.canvas) {
282 twitterUser.canvas.layoutChanged = true;
283 twitterUser.canvas.loading = false;
284 twitterUser.canvas.requestPaint();