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"
39 const int BACKGROUND_TOP_HEIGHT = 20;
40 const int BACKGROUND_BOTTOM_HEIGHT = 15;
41 const int ICON_HEIGHT = 24; ///< Icon height
42 const int ICON_WIDTH = 24; ///< Icon width
43 const int IMAGE_HEIGHT = 60; ///< Friend image height
44 const int IMAGE_WIDTH = 60; ///< Friend image width
45 const int ITEM_MAX_HEIGHT = 240; ///< Maximum height for item
46 const int ITEM_MAX_WIDTH = 368; ///< Maximum width for item
47 const int ITEM_MIN_HEIGHT = 141; ///< Minimum height for item
48 const int ITEM_MIN_WIDTH = 368; ///< Minimum width for item
49 const int MARGIN = 5; ///< Icon margin
50 const int MOUSE_PRESS_AREA_WIDTH = 20; ///< Area width for item height toggling
51 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
53 * @var NAME_LABEL_MAX_WIDTH
55 * @brief Name label's maximum width
57 const int NAME_LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - IMAGE_WIDTH;
59 * @var LABEL_MAX_WIDTH
61 * @brief All label's maximum width
63 const int LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH;
65 const int WALK_DISTANCE = 5; ///< Walk distance limit for distance icon
66 const int CAR_DISTANCE = 500; ///< Car distance limit for distance icon
67 const int AEROPLANE_DISTANCE = 5000;///< Aeroplane distance limit for distance icon
69 FriendListItem::FriendListItem(QWidget *parent)
74 qDebug() << __PRETTY_FUNCTION__;
76 QVBoxLayout *layout = new QVBoxLayout(this);
77 layout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
78 layout->setSpacing(0);
81 QHBoxLayout *topLayout = new QHBoxLayout();
82 topLayout->setMargin(0);
83 topLayout->setSpacing(0);
85 QVBoxLayout *distanceLayout = new QVBoxLayout();
86 distanceLayout->setMargin(0);
87 distanceLayout->setSpacing(0);
89 QHBoxLayout *bottomLayout = new QHBoxLayout();
90 bottomLayout->setMargin(0);
91 bottomLayout->setSpacing(0);
93 QFormLayout *infoLayout = new QFormLayout();
94 infoLayout->setMargin(0);
95 infoLayout->setSpacing(0);
96 infoLayout->setLabelAlignment(Qt::AlignTop);
98 QLabel *clockLabel = new QLabel();
99 clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
100 clockLabel->setContentsMargins(0, 0, MARGIN, 0);
101 clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
102 QLabel *envelopeLabel = new QLabel();
103 envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
104 envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
105 envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
106 QLabel *compassLabel = new QLabel();
107 compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
108 compassLabel->setContentsMargins(0, 0, MARGIN, 0);
109 compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
111 m_imageLabel = new QLabel();
112 m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
114 m_nameLabel = new QLabel();
115 m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
117 m_distanceTextLabel = new QLabel();
118 m_distanceTextLabel->setFixedHeight(ICON_HEIGHT);
120 m_distanceImageLabel = new QLabel();
121 m_distanceImageLabel->setFixedSize(ICON_WIDTH, ICON_HEIGHT);
123 m_findButton = new ImageButton(this, ":/res/images/show_position.png",
124 ":/res/images/show_position_s.png");
126 m_updatedLabel = new QLabel();
127 m_updatedLabel->setWordWrap(true);
128 m_statusTextLabel = new QLabel();
129 m_statusTextLabel->setWordWrap(true);
130 m_locationLabel = new QLabel();
131 m_locationLabel->setWordWrap(true);
133 distanceLayout->addWidget(m_distanceImageLabel, 0, Qt::AlignRight);
134 distanceLayout->addWidget(m_distanceTextLabel, 0, Qt::AlignRight);
136 infoLayout->addRow(envelopeLabel, m_statusTextLabel);
137 infoLayout->addRow(compassLabel, m_locationLabel);
138 infoLayout->addRow(clockLabel, m_updatedLabel);
140 topLayout->addWidget(m_imageLabel);
141 topLayout->addWidget(m_nameLabel, 1);
142 topLayout->addLayout(distanceLayout);
144 bottomLayout->addWidget(m_findButton, 0, Qt::AlignTop);
145 bottomLayout->addLayout(infoLayout);
147 layout->addLayout(topLayout, 0);
148 layout->addLayout(bottomLayout, 1);
150 setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
151 setMaximumSize(ITEM_MIN_WIDTH, ITEM_MAX_HEIGHT);
153 setFont(NOKIA_FONT_SMALL);
154 m_nameLabel->setFont(NOKIA_FONT_NORMAL);
155 QPalette itemPalette = palette();
156 itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
157 setPalette(itemPalette);
158 QPalette namePalette = m_nameLabel->palette();
159 namePalette.setColor(QPalette::Foreground, Qt::white);
160 m_nameLabel->setPalette(namePalette);
162 m_backgroundTopImage.load(":/res/images/list_item_top.png");
163 m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
164 m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
166 connect(m_findButton, SIGNAL(clicked()),
167 this, SLOT(findButtonClicked()));
170 void FriendListItem::setData(User *user)
172 qDebug() << __PRETTY_FUNCTION__;
176 m_imageLabel->setPixmap(m_user->profileImage());
180 user->distance(value, unit);
181 m_distanceTextLabel->setText(QString::number(value) + " " + unit);
182 setDistanceIcon(value);
188 void FriendListItem::setDistanceIcon(double value)
190 QPixmap distanceImage;
192 if (value < WALK_DISTANCE)
193 distanceImage.load(":/res/images/walk_icon_gray.png");
194 else if (value < CAR_DISTANCE)
195 distanceImage.load(":/res/images/car_icon_gray.png");
196 else if (value < AEROPLANE_DISTANCE)
197 distanceImage.load(":/res/images/aeroplane_icon_gray.png");
199 distanceImage.load(":/res/images/rocket_icon_gray.png");
201 m_distanceImageLabel->setPixmap(distanceImage);
204 void FriendListItem::shortenTexts()
206 qDebug() << __PRETTY_FUNCTION__;
208 QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
209 QFontMetrics otherLabelsMetrics = m_updatedLabel->fontMetrics();
211 QString name = m_user->name();
212 QString updated = m_user->timestamp();
213 QString statusText = m_user->note();
214 QString location = m_user->address();
216 int nameIndex = name.indexOf('\n');
217 int updatedIndex = updated.indexOf('\n');
218 int statusTextIndex = statusText.indexOf('\n');
219 int locationIndex = location.indexOf('\n');
222 name.truncate(nameIndex);
225 if (updatedIndex > 0) {
226 updated.truncate(updatedIndex);
227 updated.append("...");
229 if (statusTextIndex > 0) {
230 statusText.truncate(statusTextIndex);
231 statusText.append("...");
233 if (locationIndex > 0) {
234 location.truncate(locationIndex);
235 location.append("...");
238 int distanceLabelWidth = otherLabelsMetrics.width(m_distanceTextLabel->text());
239 m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
240 - distanceLabelWidth);
241 m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
242 m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
244 m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
247 void FriendListItem::setText(bool expanded)
249 qDebug() << __PRETTY_FUNCTION__;
252 setUpdatesEnabled(false);
253 m_nameLabel->setText(m_shortenedName);
254 m_updatedLabel->setText(m_user->timestamp());
255 m_statusTextLabel->setText(m_user->note());
256 m_locationLabel->setText(m_user->address());
257 setUpdatesEnabled(true);
260 setUpdatesEnabled(false);
261 m_nameLabel->setText(m_shortenedName);
262 m_updatedLabel->setText(m_shortenedUpdated);
263 m_statusTextLabel->setText(m_shortenedStatusText);
264 m_locationLabel->setText(m_shortenedLocation);
265 setUpdatesEnabled(true);
269 void FriendListItem::mousePressEvent(QMouseEvent *event)
271 qDebug() << __PRETTY_FUNCTION__;
273 m_mousePosition = event->pos();
276 void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
278 qDebug() << __PRETTY_FUNCTION__;
280 if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
281 (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
293 void FriendListItem::paintEvent(QPaintEvent *event)
295 qDebug() << __PRETTY_FUNCTION__;
299 QPainter painter(this);
301 QRect topRect = QRect(0, 0, ITEM_MIN_WIDTH, BACKGROUND_TOP_HEIGHT);
302 QRect middleRect = QRect(0, topRect.bottom(), ITEM_MIN_WIDTH,
303 height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
304 QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), ITEM_MIN_WIDTH,
305 BACKGROUND_BOTTOM_HEIGHT);
307 painter.drawPixmap(topRect, m_backgroundTopImage);
308 painter.drawPixmap(middleRect, m_backgroundMiddleImage);
309 painter.drawPixmap(bottomRect, m_backgroundBottomImage);
312 void FriendListItem::findButtonClicked()
314 qDebug() << __PRETTY_FUNCTION__;
316 emit findFriend(m_user->coordinates());