2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Jussi Laitinen - jussi.laitinen@ixonos.com
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.
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.
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,
22 #include <QVBoxLayout>
23 #include <QPushButton>
26 #include <QPaintEvent>
29 #include <QFormLayout>
30 #include <QSpacerItem>
31 #include <QStylePainter>
34 #include "friendlistitem.h"
35 #include "../user/user.h"
36 #include "imagebutton.h"
37 #include "../common.h"
38 #include "listcommon.h"
40 const int WALK_DISTANCE = 5; ///< Walk distance limit for distance icon
41 const int CAR_DISTANCE = 500; ///< Car distance limit for distance icon
42 const int AEROPLANE_DISTANCE = 5000;///< Aeroplane distance limit for distance icon
44 const int RECTS_MINIMUM_FACTOR_SUM = 3; ///< Rects minimum factor sum
46 FriendListItem::FriendListItem()
50 qDebug() << __PRETTY_FUNCTION__;
52 setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
55 void FriendListItem::calculateTextRects()
57 qDebug() << __PRETTY_FUNCTION__;
59 //Dummy value to get painter font metrics.
60 QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
62 painter.setFont(NOKIA_FONT_SMALL);
63 QFontMetrics smallFontMetrics = painter.fontMetrics();
65 QRect distanceRect = smallFontMetrics.boundingRect(m_distanceText);
66 QRect statusTextRect = smallFontMetrics.boundingRect(m_user->note());
67 QRect updatedRect = smallFontMetrics.boundingRect(m_user->timestamp());
68 QRect locationRect = smallFontMetrics.boundingRect(m_user->address());
70 int statusTextRectFactor = statusTextRect.width() / LABEL_MAX_WIDTH;
71 statusTextRectFactor++;
72 m_statusTextRect = QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT * statusTextRectFactor);
73 int updatedRectFactor = updatedRect.width() / LABEL_MAX_WIDTH;
75 m_updatedRect = QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT * updatedRectFactor);
76 int locationRectFactor = locationRect.width() / LABEL_MAX_WIDTH;
78 m_locationRect = QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT * locationRectFactor);
80 m_expandedHeight = ITEM_MIN_HEIGHT + ((statusTextRectFactor + updatedRectFactor +
81 locationRectFactor - RECTS_MINIMUM_FACTOR_SUM)
84 setData(DISTANCE_SIZE_HINT_INDEX, distanceRect);
87 QPointF FriendListItem::coordinates()
89 qDebug() << __PRETTY_FUNCTION__;
91 return m_user->coordinates();
94 QString FriendListItem::id() const
96 qDebug() << __PRETTY_FUNCTION__;
98 return m_user->userId();
101 void FriendListItem::setUserData(User *user)
103 qDebug() << __PRETTY_FUNCTION__;
107 setData(AVATAR_IMAGE_INDEX, m_user->profileImage());
111 m_user->distance(value, unit);
112 m_distanceText = QString::number(value) + " " + unit;
113 setData(DISTANCE_TEXT_DISPLAY_INDEX, m_distanceText);
114 setDistanceIcon(value, unit);
117 calculateTextRects();
122 void FriendListItem::setAvatarImage(const QPixmap &image)
124 qDebug() << __PRETTY_FUNCTION__;
127 setData(AVATAR_IMAGE_INDEX, m_user->profileImage());
130 void FriendListItem::setDistanceIcon(double value, const QString &unit)
132 qDebug() << __PRETTY_FUNCTION__;
134 QPixmap distanceImage;
136 if ((unit == "m") || (value < WALK_DISTANCE))
137 distanceImage.load(":/res/images/walk_icon_gray.png");
138 else if (value < CAR_DISTANCE)
139 distanceImage.load(":/res/images/car_icon_gray.png");
140 else if (value < AEROPLANE_DISTANCE)
141 distanceImage.load(":/res/images/aeroplane_icon_gray.png");
143 distanceImage.load(":/res/images/rocket_icon_gray.png");
145 setData(DISTANCE_IMAGE_INDEX, distanceImage);
148 void FriendListItem::shortenTexts()
150 qDebug() << __PRETTY_FUNCTION__;
152 //Dummy value to get painter font metrics.
153 QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
154 QPainter painter(&p);
155 painter.setFont(NOKIA_FONT_NORMAL);
156 QFontMetrics nameLabelMetrics = painter.fontMetrics();
157 painter.setFont(NOKIA_FONT_SMALL);
158 QFontMetrics otherLabelsMetrics = painter.fontMetrics();
160 QString name = m_user->name();
161 QString updated = m_user->timestamp();
162 QString statusText = m_user->note();
163 QString location = m_user->address();
165 int nameIndex = name.indexOf('\n');
166 int updatedIndex = updated.indexOf('\n');
167 int statusTextIndex = statusText.indexOf('\n');
168 int locationIndex = location.indexOf('\n');
171 name.truncate(nameIndex);
174 if (updatedIndex > 0) {
175 updated.truncate(updatedIndex);
176 updated.append("...");
178 if (statusTextIndex > 0) {
179 statusText.truncate(statusTextIndex);
180 statusText.append("...");
182 if (locationIndex > 0) {
183 location.truncate(locationIndex);
184 location.append("...");
187 int distanceLabelWidth = otherLabelsMetrics.width(m_distanceText) + MARGIN;
188 m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
189 - distanceLabelWidth);
190 m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
192 m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
193 m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
195 setData(NAME_DISPLAY_INDEX, m_shortenedName);
198 void FriendListItem::setText(bool expanded)
200 qDebug() << __PRETTY_FUNCTION__;
203 setData(STATUS_TEXT_DISPLAY_INDEX, m_user->note());
204 setData(LOCATION_DISPLAY_INDEX, m_user->address());
205 setData(UPDATED_DISPLAY_INDEX, m_user->timestamp());
207 setData(STATUS_TEXT_SIZE_HINT_INDEX, m_statusTextRect);
208 setData(LOCATION_SIZE_HINT_INDEX, m_locationRect);
209 setData(UPDATED_SIZE_HINT_INDEX, m_updatedRect);
211 setData(STATUS_TEXT_DISPLAY_INDEX, m_shortenedStatusText);
212 setData(LOCATION_DISPLAY_INDEX, m_shortenedLocation);
213 setData(UPDATED_DISPLAY_INDEX, m_shortenedUpdated);
215 setData(STATUS_TEXT_SIZE_HINT_INDEX, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
216 setData(LOCATION_SIZE_HINT_INDEX, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
217 setData(UPDATED_SIZE_HINT_INDEX, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
221 bool FriendListItem::toggleSelection()
223 qDebug() << __PRETTY_FUNCTION__;
225 setSelected(!m_selected);
229 void FriendListItem::setSelected(bool selected)
231 qDebug() << __PRETTY_FUNCTION__;
233 m_selected = selected;
237 setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_expandedHeight));
240 setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));