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 "avatarimage.h"
38 FriendListItem::FriendListItem(QWidget *parent)
43 qDebug() << __PRETTY_FUNCTION__;
45 QVBoxLayout *layout = new QVBoxLayout(this);
46 layout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
47 layout->setSpacing(0);
50 QHBoxLayout *topLayout = new QHBoxLayout();
51 topLayout->setMargin(0);
52 topLayout->setSpacing(0);
54 QHBoxLayout *bottomLayout = new QHBoxLayout();
55 bottomLayout->setMargin(0);
56 bottomLayout->setSpacing(0);
58 QFormLayout *infoLayout = new QFormLayout();
59 infoLayout->setMargin(0);
60 infoLayout->setSpacing(0);
61 infoLayout->setLabelAlignment(Qt::AlignTop);
63 QLabel *clockLabel = new QLabel();
64 clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
65 clockLabel->setContentsMargins(0, 0, MARGIN, 0);
66 clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
67 QLabel *envelopeLabel = new QLabel();
68 envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
69 envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
70 envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
71 QLabel *compassLabel = new QLabel();
72 compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
73 compassLabel->setContentsMargins(0, 0, MARGIN, 0);
74 compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
76 m_imageLabel = new QLabel();
77 m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
79 m_nameLabel = new QLabel();
80 m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
82 m_distanceLabel = new QLabel();
83 m_distanceLabel->setFixedHeight(IMAGE_HEIGHT);
85 QLabel *button = new QLabel();
86 button->setPixmap(QPixmap(":/res/images/show_position.png"));
87 button->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
89 m_updatedLabel = new QLabel();
90 m_updatedLabel->setWordWrap(true);
91 m_statusTextLabel = new QLabel();
92 m_statusTextLabel->setWordWrap(true);
93 m_locationLabel = new QLabel();
94 m_locationLabel->setWordWrap(true);
96 infoLayout->addRow(envelopeLabel, m_statusTextLabel);
97 infoLayout->addRow(compassLabel, m_locationLabel);
98 infoLayout->addRow(clockLabel, m_updatedLabel);
100 topLayout->addWidget(m_imageLabel);
101 topLayout->addWidget(m_nameLabel, 1);
102 topLayout->addWidget(m_distanceLabel);
104 bottomLayout->addWidget(button, 0, Qt::AlignTop);
105 bottomLayout->addLayout(infoLayout);
107 layout->addLayout(topLayout, 0);
108 layout->addLayout(bottomLayout, 1);
110 setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
111 setMaximumSize(ITEM_MIN_WIDTH, ITEM_MAX_HEIGHT);
113 setFont(NOKIA_FONT_SMALL);
114 m_nameLabel->setFont(NOKIA_FONT_NORMAL);
115 QPalette itemPalette = palette();
116 itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
117 setPalette(itemPalette);
118 QPalette namePalette = m_nameLabel->palette();
119 namePalette.setColor(QPalette::Foreground, Qt::white);
120 m_nameLabel->setPalette(namePalette);
122 m_backgroundTopImage.load(":/res/images/list_item_top.png");
123 m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
124 m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
127 void FriendListItem::setData(User *user)
129 qDebug() << __PRETTY_FUNCTION__;
133 m_imageLabel->setPixmap(AvatarImage::create(m_user->profileImage()));
137 user->distance(value, unit);
138 m_distanceLabel->setText(QString::number(value) + " " + unit);
144 void FriendListItem::shortenTexts()
146 qDebug() << __PRETTY_FUNCTION__;
148 QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
149 QFontMetrics otherLabelsMetrics = m_updatedLabel->fontMetrics();
151 QString name = m_user->name();
152 QString updated = m_user->timestamp();
153 QString statusText = m_user->note();
154 QString location = m_user->address();
156 int nameIndex = name.indexOf('\n');
157 int updatedIndex = updated.indexOf('\n');
158 int statusTextIndex = statusText.indexOf('\n');
159 int locationIndex = location.indexOf('\n');
162 name.truncate(nameIndex);
165 if (updatedIndex > 0) {
166 updated.truncate(updatedIndex);
167 updated.append("...");
169 if (statusTextIndex > 0) {
170 statusText.truncate(statusTextIndex);
171 statusText.append("...");
173 if (locationIndex > 0) {
174 location.truncate(locationIndex);
175 location.append("...");
178 int distanceLabelWidth = otherLabelsMetrics.width(m_distanceLabel->text());
179 m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
180 - distanceLabelWidth);
181 m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
182 m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
184 m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
187 void FriendListItem::setText(bool expanded)
189 qDebug() << __PRETTY_FUNCTION__;
192 setUpdatesEnabled(false);
193 m_nameLabel->setText(m_shortenedName);
194 m_updatedLabel->setText(m_user->timestamp());
195 m_statusTextLabel->setText(m_user->note());
196 m_locationLabel->setText(m_user->address());
197 setUpdatesEnabled(true);
200 setUpdatesEnabled(false);
201 m_nameLabel->setText(m_shortenedName);
202 m_updatedLabel->setText(m_shortenedUpdated);
203 m_statusTextLabel->setText(m_shortenedStatusText);
204 m_locationLabel->setText(m_shortenedLocation);
205 setUpdatesEnabled(true);
209 void FriendListItem::mousePressEvent(QMouseEvent *event)
211 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
213 m_mousePosition = event->pos();
216 void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
218 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
220 if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
221 (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
233 void FriendListItem::paintEvent(QPaintEvent *event)
235 qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
237 QPainter painter(this);
239 QRect topRect = QRect(0, 0, ITEM_MIN_WIDTH, BACKGROUND_TOP_HEIGHT);
240 QRect middleRect = QRect(0, topRect.bottom(), ITEM_MIN_WIDTH,
241 height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
242 QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), ITEM_MIN_WIDTH,
243 BACKGROUND_BOTTOM_HEIGHT);
245 painter.drawPixmap(topRect, m_backgroundTopImage);
246 painter.drawPixmap(middleRect, m_backgroundMiddleImage);
247 painter.drawPixmap(bottomRect, m_backgroundBottomImage);