Sorted includes alphabetically.
[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 const int AEROPLANE_DISTANCE = 5000;///< Aeroplane distance limit for distance icon
33 const int CAR_DISTANCE = 500;       ///< Car distance limit for distance icon
34 const int WALK_DISTANCE = 5;        ///< Walk distance limit for distance icon
35
36 FriendListItem::FriendListItem()
37 {
38     qDebug() << __PRETTY_FUNCTION__;
39
40     setSubitemTextWidth(SUBITEM_TEXT_MAX_WIDTH);
41 }
42
43 GeoCoordinate FriendListItem::coordinates()
44 {
45     qDebug() << __PRETTY_FUNCTION__;
46
47     return m_coordinates;
48 }
49
50 void FriendListItem::setUserData(User *user)
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53
54     if(user) {
55
56         QString unit;
57         double value;
58         user->distance(value, unit);
59         QString distanceText = QString::number(value) + " " + unit;
60         setData(DISTANCE_TEXT_DISPLAY_INDEX, distanceText);
61         setDistanceIcon(value, unit);
62
63         //Dummy value to get painter font metrics.
64         QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
65         QPainter painter(&p);
66         painter.setFont(NOKIA_FONT_SMALL);
67         QFontMetrics distanceTextFontMetrics = painter.fontMetrics();
68         QRect distanceRect = distanceTextFontMetrics.boundingRect(distanceText);
69
70         setData(DISTANCE_SIZE_HINT_INDEX, distanceRect);
71         setName(shortenText(user->name(), NAME_TEXT_MAX_WIDTH - distanceRect.width() + MARGIN,
72                             ListItem::TEXT_SIZE_NORMAL));
73         setCoordinates(user->coordinates());
74
75         if (!user->profileImage().isNull())
76             setImage(user->profileImage());
77
78         clearSubItems();
79
80         addSubItem(user->note(), QPixmap(":/res/images/envelope.png"));
81         addSubItem(user->address(), QPixmap(":/res/images/compass.png"));
82         addSubItem(user->timestamp(), QPixmap(":/res/images/clock.png"));
83     }
84 }
85
86 void FriendListItem::setAvatarImage(const QPixmap &image)
87 {
88     qDebug() << __PRETTY_FUNCTION__;
89
90     if(!image.isNull())
91         setImage(image);
92 }
93
94 void FriendListItem::setCoordinates(const GeoCoordinate &coordinates)
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     m_coordinates = coordinates;
99 }
100
101 void FriendListItem::setDistanceIcon(double value, const QString &unit)
102 {
103     qDebug() << __PRETTY_FUNCTION__;
104
105     QPixmap distanceImage;
106
107     if ((unit == "m") || (value < WALK_DISTANCE))
108         distanceImage.load(":/res/images/walk_icon_gray.png");
109     else if (value < CAR_DISTANCE)
110         distanceImage.load(":/res/images/car_icon_gray.png");
111     else if (value < AEROPLANE_DISTANCE)
112         distanceImage.load(":/res/images/aeroplane_icon_gray.png");
113     else
114         distanceImage.load(":/res/images/rocket_icon_gray.png");
115
116     setData(DISTANCE_IMAGE_INDEX, distanceImage);
117 }
118
119 FriendListItem::~FriendListItem()
120 {
121     qDebug() << __PRETTY_FUNCTION__;
122 }