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"
37 #include "imagebutton.h"
39 const int BACKGROUND_TOP_HEIGHT = 20;
40 const int BACKGROUND_BOTTOM_HEIGHT = 15;
41 const QColor COLOR_GRAY = QColor(152, 152, 152); ///< Gray color
42 const QFont NOKIA_FONT_NORMAL = QFont("Nokia Sans", 18, QFont::Normal); ///< Normal font
43 const QFont NOKIA_FONT_SMALL = QFont("Nokia Sans", 13, QFont::Normal); ///< Small font
44 const int ICON_HEIGHT = 24; ///< Icon height
45 const int ICON_WIDTH = 24; ///< Icon width
46 const int IMAGE_HEIGHT = 60; ///< Friend image height
47 const int IMAGE_WIDTH = 60; ///< Friend image width
48 const int ITEM_MAX_HEIGHT = 240; ///< Maximum height for item
49 const int ITEM_MAX_WIDTH = 368; ///< Maximum width for item
50 const int ITEM_MIN_HEIGHT = 141; ///< Minimum height for item
51 const int ITEM_MIN_WIDTH = 368; ///< Minimum width for item
52 const int MARGIN = 5; ///< Icon margin
53 const int MOUSE_PRESS_AREA_WIDTH = 20; ///< Area width for item height toggling
54 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
56 * @var NAME_LABEL_MAX_WIDTH
58 * @brief Name label's maximum width
60 const int NAME_LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - IMAGE_WIDTH;
62 * @var LABEL_MAX_WIDTH
64 * @brief All label's maximum width
66 const int LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH;
68 FriendListItem::FriendListItem(QWidget *parent)
73 qDebug() << __PRETTY_FUNCTION__;
75 QVBoxLayout *layout = new QVBoxLayout(this);
76 layout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
77 layout->setSpacing(0);
80 QHBoxLayout *topLayout = new QHBoxLayout();
81 topLayout->setMargin(0);
82 topLayout->setSpacing(0);
84 QHBoxLayout *bottomLayout = new QHBoxLayout();
85 bottomLayout->setMargin(0);
86 bottomLayout->setSpacing(0);
88 QFormLayout *infoLayout = new QFormLayout();
89 infoLayout->setMargin(0);
90 infoLayout->setSpacing(0);
91 infoLayout->setLabelAlignment(Qt::AlignTop);
93 QLabel *clockLabel = new QLabel();
94 clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
95 clockLabel->setContentsMargins(0, 0, MARGIN, 0);
96 clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
97 QLabel *envelopeLabel = new QLabel();
98 envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
99 envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
100 envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
101 QLabel *compassLabel = new QLabel();
102 compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
103 compassLabel->setContentsMargins(0, 0, MARGIN, 0);
104 compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
106 m_imageLabel = new QLabel();
107 m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
109 m_nameLabel = new QLabel();
110 m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
112 m_distanceLabel = new QLabel();
113 m_distanceLabel->setFixedHeight(IMAGE_HEIGHT);
115 m_findButton = new ImageButton(this, ":/res/images/show_position.png",
116 ":/res/images/show_position_s.png");
118 m_updatedLabel = new QLabel();
119 m_updatedLabel->setWordWrap(true);
120 m_statusTextLabel = new QLabel();
121 m_statusTextLabel->setWordWrap(true);
122 m_locationLabel = new QLabel();
123 m_locationLabel->setWordWrap(true);
125 infoLayout->addRow(envelopeLabel, m_statusTextLabel);
126 infoLayout->addRow(compassLabel, m_locationLabel);
127 infoLayout->addRow(clockLabel, m_updatedLabel);
129 topLayout->addWidget(m_imageLabel);
130 topLayout->addWidget(m_nameLabel, 1);
131 topLayout->addWidget(m_distanceLabel);
133 bottomLayout->addWidget(m_findButton, 0, Qt::AlignTop);
134 bottomLayout->addLayout(infoLayout);
136 layout->addLayout(topLayout, 0);
137 layout->addLayout(bottomLayout, 1);
139 setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
140 setMaximumSize(ITEM_MIN_WIDTH, ITEM_MAX_HEIGHT);
142 setFont(NOKIA_FONT_SMALL);
143 m_nameLabel->setFont(NOKIA_FONT_NORMAL);
144 QPalette itemPalette = palette();
145 itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
146 setPalette(itemPalette);
147 QPalette namePalette = m_nameLabel->palette();
148 namePalette.setColor(QPalette::Foreground, Qt::white);
149 m_nameLabel->setPalette(namePalette);
151 m_backgroundTopImage.load(":/res/images/list_item_top.png");
152 m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
153 m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
155 connect(m_findButton, SIGNAL(clicked()),
156 this, SLOT(findButtonClicked()));
159 void FriendListItem::setData(User *user)
161 qDebug() << __PRETTY_FUNCTION__;
165 m_imageLabel->setPixmap(AvatarImage::create(m_user->profileImage()));
169 user->distance(value, unit);
170 m_distanceLabel->setText(QString::number(value) + " " + unit);
176 void FriendListItem::shortenTexts()
178 qDebug() << __PRETTY_FUNCTION__;
180 QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
181 QFontMetrics otherLabelsMetrics = m_updatedLabel->fontMetrics();
183 QString name = m_user->name();
184 QString updated = m_user->timestamp();
185 QString statusText = m_user->note();
186 QString location = m_user->address();
188 int nameIndex = name.indexOf('\n');
189 int updatedIndex = updated.indexOf('\n');
190 int statusTextIndex = statusText.indexOf('\n');
191 int locationIndex = location.indexOf('\n');
194 name.truncate(nameIndex);
197 if (updatedIndex > 0) {
198 updated.truncate(updatedIndex);
199 updated.append("...");
201 if (statusTextIndex > 0) {
202 statusText.truncate(statusTextIndex);
203 statusText.append("...");
205 if (locationIndex > 0) {
206 location.truncate(locationIndex);
207 location.append("...");
210 int distanceLabelWidth = otherLabelsMetrics.width(m_distanceLabel->text());
211 m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
212 - distanceLabelWidth);
213 m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
214 m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
216 m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
219 void FriendListItem::setText(bool expanded)
221 qDebug() << __PRETTY_FUNCTION__;
224 setUpdatesEnabled(false);
225 m_nameLabel->setText(m_shortenedName);
226 m_updatedLabel->setText(m_user->timestamp());
227 m_statusTextLabel->setText(m_user->note());
228 m_locationLabel->setText(m_user->address());
229 setUpdatesEnabled(true);
232 setUpdatesEnabled(false);
233 m_nameLabel->setText(m_shortenedName);
234 m_updatedLabel->setText(m_shortenedUpdated);
235 m_statusTextLabel->setText(m_shortenedStatusText);
236 m_locationLabel->setText(m_shortenedLocation);
237 setUpdatesEnabled(true);
241 void FriendListItem::mousePressEvent(QMouseEvent *event)
243 qDebug() << __PRETTY_FUNCTION__;
245 m_mousePosition = event->pos();
248 void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
250 qDebug() << __PRETTY_FUNCTION__;
252 if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
253 (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
265 void FriendListItem::paintEvent(QPaintEvent *event)
267 qDebug() << __PRETTY_FUNCTION__;
271 QPainter painter(this);
273 QRect topRect = QRect(0, 0, ITEM_MIN_WIDTH, BACKGROUND_TOP_HEIGHT);
274 QRect middleRect = QRect(0, topRect.bottom(), ITEM_MIN_WIDTH,
275 height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
276 QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), ITEM_MIN_WIDTH,
277 BACKGROUND_BOTTOM_HEIGHT);
279 painter.drawPixmap(topRect, m_backgroundTopImage);
280 painter.drawPixmap(middleRect, m_backgroundMiddleImage);
281 painter.drawPixmap(bottomRect, m_backgroundBottomImage);
284 void FriendListItem::findButtonClicked()
286 emit findFriend(m_user->coordinates());