Emit error signal when login fails
[situare] / src / ui / friendlistitem.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <QPainter>
23 #include <QDebug>
24 #include <QPixmap>
25
26 #include "../user/user.h"
27 #include "../common.h"
28 #include "listcommon.h"
29
30 #include "friendlistitem.h"
31
32 FriendListItem::FriendListItem()
33 {
34     qDebug() << __PRETTY_FUNCTION__;
35
36     setSubitemTextWidth(SUBITEM_TEXT_MAX_WIDTH);
37 }
38
39 FriendListItem::~FriendListItem()
40 {
41     qDebug() << __PRETTY_FUNCTION__;
42 }
43
44 GeoCoordinate FriendListItem::coordinates() const
45 {
46     qDebug() << __PRETTY_FUNCTION__;
47
48     return m_coordinates;
49 }
50
51 QString FriendListItem::facebookId() const
52 {
53     return m_facebookId;
54 }
55
56 void FriendListItem::setAvatarImage(const QPixmap &image)
57 {
58     qDebug() << __PRETTY_FUNCTION__;
59
60     if(!image.isNull())
61         setImage(image);
62 }
63
64 void FriendListItem::setCoordinates(const GeoCoordinate &coordinates)
65 {
66     qDebug() << __PRETTY_FUNCTION__;
67
68     m_coordinates = coordinates;
69 }
70
71 void FriendListItem::setDistanceIcon(double value, const QString &unit)
72 {
73     qDebug() << __PRETTY_FUNCTION__;
74
75     const int AEROPLANE_DISTANCE = 5000;///< Aeroplane distance limit for distance icon
76     const int CAR_DISTANCE = 500;       ///< Car distance limit for distance icon
77     const int WALK_DISTANCE = 5;        ///< Walk distance limit for distance icon
78
79     QPixmap distanceImage;
80
81     if ((unit == "m") || (value < WALK_DISTANCE))
82         distanceImage.load(":/res/images/walk_icon_gray.png");
83     else if (value < CAR_DISTANCE)
84         distanceImage.load(":/res/images/car_icon_gray.png");
85     else if (value < AEROPLANE_DISTANCE)
86         distanceImage.load(":/res/images/aeroplane_icon_gray.png");
87     else
88         distanceImage.load(":/res/images/rocket_icon_gray.png");
89
90     setData(DISTANCE_IMAGE_INDEX, distanceImage);
91 }
92
93
94 void FriendListItem::setFacebookId(const QString &facebookId)
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     m_facebookId = facebookId;
99 }
100 void FriendListItem::setUserData(User *user)
101 {
102     qDebug() << __PRETTY_FUNCTION__;
103
104     QString unit;
105     double value;
106     user->distance(value, unit);
107     QString distanceText = QString::number(value) + " " + unit;
108     setData(DISTANCE_TEXT_DISPLAY_INDEX, distanceText);
109     setDistanceIcon(value, unit);
110
111     setFacebookId(user->userId());
112
113     //Dummy value to get painter font metrics.
114     QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
115     QPainter painter(&p);
116     painter.setFont(NOKIA_FONT_SMALL);
117     QFontMetrics distanceTextFontMetrics = painter.fontMetrics();
118     QRect distanceRect = distanceTextFontMetrics.boundingRect(distanceText);
119
120     setData(DISTANCE_SIZE_HINT_INDEX, distanceRect);
121     setTitle(shortenText(user->name(), NAME_TEXT_MAX_WIDTH - distanceRect.width() - MARGIN * 2,
122                         ListItem::TEXT_SIZE_NORMAL));
123     setCoordinates(user->coordinates());
124
125     if (!user->profileImage().isNull())
126         setImage(user->profileImage());
127
128     clearSubItems();
129
130     addSubItem(user->note(), QPixmap(":/res/images/envelope.png"));
131     addSubItem(user->address(), QPixmap(":/res/images/compass.png"));
132     addSubItem(user->timestamp(), QPixmap(":/res/images/clock.png"));
133 }