First phase of userdata rewrite
[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 <QVBoxLayout>
23 #include <QPushButton>
24 #include <QPainter>
25 #include <QDebug>
26 #include <QPaintEvent>
27 #include <QLabel>
28 #include <QPixmap>
29 #include <QFormLayout>
30 #include <QSpacerItem>
31 #include <QStylePainter>
32 #include <math.h>
33
34 #include "friendlistitem.h"
35 #include "../user/user.h"
36 #include "imagebutton.h"
37 #include "../common.h"
38
39 const int BACKGROUND_BOTTOM_HEIGHT = 15;
40 const int BACKGROUND_TOP_HEIGHT = 20;
41 const int BACKGROUND_WIDTH = 368;
42 const int ICON_HEIGHT = 24;     ///< Icon height
43 const int ICON_WIDTH = 24;       ///< Icon width
44 const int IMAGE_HEIGHT = 64; ///< Friend image height
45 const int IMAGE_WIDTH = 64;  ///< Friend image width
46 const int ITEM_MIN_HEIGHT = 141; ///< Minimum height for item
47 const int MARGIN = 5;   ///< Icon margin
48 const int MOUSE_PRESS_AREA_WIDTH = 20;  ///< Area width for item height toggling
49 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
50
51 /**
52 * @var NAME_LABEL_MAX_WIDTH
53 *
54 * @brief Name label's maximum width
55 */
56 const int NAME_LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3*MARGIN - IMAGE_WIDTH;
57
58 /**
59 * @var LABEL_MAX_WIDTH
60 *
61 * @brief All label's maximum width
62 */
63 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH;
64
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
68
69 FriendListItem::FriendListItem(QWidget *parent)
70     : QWidget(parent)
71     , m_expanded(false)
72     , m_user(0)
73 {
74     qDebug() << __PRETTY_FUNCTION__;
75
76     QVBoxLayout *layout = new QVBoxLayout(this);
77     layout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
78     layout->setSpacing(0);
79     setLayout(layout);
80
81     QHBoxLayout *topLayout = new QHBoxLayout();
82     topLayout->setMargin(0);
83     topLayout->setSpacing(0);
84
85     QVBoxLayout *distanceLayout = new QVBoxLayout();
86     distanceLayout->setMargin(0);
87     distanceLayout->setSpacing(0);
88
89     QHBoxLayout *bottomLayout = new QHBoxLayout();
90     bottomLayout->setMargin(0);
91     bottomLayout->setSpacing(0);
92
93     QFormLayout *infoLayout = new QFormLayout();
94     infoLayout->setMargin(0);
95     infoLayout->setSpacing(0);
96     infoLayout->setLabelAlignment(Qt::AlignTop);
97
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);
110
111     m_nameLabel = new QLabel();
112     m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
113
114     m_distanceTextLabel = new QLabel();
115     m_distanceTextLabel->setFixedHeight(ICON_HEIGHT);
116
117     m_distanceImageLabel = new QLabel();
118     m_distanceImageLabel->setFixedSize(ICON_WIDTH, ICON_HEIGHT);
119
120     m_findButton = new ImageButton(this);
121
122     m_updatedLabel = new QLabel();
123     m_updatedLabel->setWordWrap(true);
124     m_statusTextLabel = new QLabel();
125     m_statusTextLabel->setWordWrap(true);
126     m_locationLabel = new QLabel();
127     m_locationLabel->setWordWrap(true);
128
129     distanceLayout->addWidget(m_distanceImageLabel, 0, Qt::AlignRight);
130     distanceLayout->addWidget(m_distanceTextLabel, 0, Qt::AlignRight);
131
132     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
133     infoLayout->addRow(compassLabel, m_locationLabel);
134     infoLayout->addRow(clockLabel, m_updatedLabel);
135
136     topLayout->addWidget(m_findButton);
137     topLayout->addWidget(m_nameLabel, 1);
138     topLayout->addLayout(distanceLayout);
139
140     bottomLayout->addSpacing(IMAGE_WIDTH);
141     bottomLayout->addLayout(infoLayout);
142
143     layout->addLayout(topLayout, 0);
144     layout->addLayout(bottomLayout, 1);
145
146     setMinimumSize(BACKGROUND_WIDTH, ITEM_MIN_HEIGHT);
147     setMaximumWidth(BACKGROUND_WIDTH);
148
149     setFont(NOKIA_FONT_SMALL);
150     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
151     QPalette itemPalette = palette();
152     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
153     setPalette(itemPalette);
154     QPalette namePalette = m_nameLabel->palette();
155     namePalette.setColor(QPalette::Foreground, Qt::white);
156     m_nameLabel->setPalette(namePalette);
157
158     m_backgroundTopImage.load(":/res/images/list_item_top.png");
159     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
160     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
161
162     connect(m_findButton, SIGNAL(clicked()),
163         this, SLOT(findButtonClicked()));
164 }
165
166 void FriendListItem::setData(User *user)
167 {
168     qDebug() << __PRETTY_FUNCTION__;
169
170     if(user) {
171         m_user = user;
172
173         if (!m_user->profileImage().isNull())
174             m_findButton->setButtonIcon(m_user->profileImage());
175
176         QString unit;
177         double value;
178         m_user->distance(value, unit);
179         m_distanceTextLabel->setText(QString::number(value) + " " + unit);
180         setDistanceIcon(value, unit);
181
182         shortenTexts();
183         setText(false);
184     }
185 }
186
187 void FriendListItem::setAvatarImage(const QPixmap &image)
188 {
189     qDebug() << __PRETTY_FUNCTION__;
190
191     if(!image.isNull())
192         m_findButton->setButtonIcon(image);
193 }
194
195 void FriendListItem::setDistanceIcon(double value, const QString &unit)
196 {
197     QPixmap distanceImage;
198
199     if ((unit == "m") || (value < WALK_DISTANCE))
200         distanceImage.load(":/res/images/walk_icon_gray.png");
201     else if (value < CAR_DISTANCE)
202         distanceImage.load(":/res/images/car_icon_gray.png");
203     else if (value < AEROPLANE_DISTANCE)
204         distanceImage.load(":/res/images/aeroplane_icon_gray.png");
205     else
206         distanceImage.load(":/res/images/rocket_icon_gray.png");
207
208     m_distanceImageLabel->setPixmap(distanceImage);
209 }
210
211 void FriendListItem::shortenTexts()
212 {
213     qDebug() << __PRETTY_FUNCTION__;
214
215     QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
216     QFontMetrics otherLabelsMetrics = m_updatedLabel->fontMetrics();
217
218     QString name = m_user->name();
219     QString updated = m_user->timestamp();
220     QString statusText = m_user->note();
221     QString location = m_user->address();
222
223     int nameIndex = name.indexOf('\n');
224     int updatedIndex = updated.indexOf('\n');
225     int statusTextIndex = statusText.indexOf('\n');
226     int locationIndex = location.indexOf('\n');
227
228     if (nameIndex > 0) {
229         name.truncate(nameIndex);
230         name.append("...");
231     }
232     if (updatedIndex > 0) {
233         updated.truncate(updatedIndex);
234         updated.append("...");
235     }
236     if (statusTextIndex > 0) {
237         statusText.truncate(statusTextIndex);
238         statusText.append("...");
239     }
240     if (locationIndex > 0) {
241         location.truncate(locationIndex);
242         location.append("...");
243     }
244
245     int distanceLabelWidth = otherLabelsMetrics.width(m_distanceTextLabel->text());
246     m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
247                                                   - distanceLabelWidth);
248     m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
249     m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
250                                                           LABEL_MAX_WIDTH);
251     m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
252 }
253
254 void FriendListItem::setText(bool expanded)
255 {
256     qDebug() << __PRETTY_FUNCTION__;
257
258     if (expanded) {
259         setUpdatesEnabled(false);
260         m_nameLabel->setText(m_shortenedName);
261         m_updatedLabel->setText(m_user->timestamp());
262         m_statusTextLabel->setText(m_user->note());
263         m_locationLabel->setText(m_user->address());
264         setUpdatesEnabled(true);
265     }
266     else {
267         setUpdatesEnabled(false);
268         m_nameLabel->setText(m_shortenedName);
269         m_updatedLabel->setText(m_shortenedUpdated);
270         m_statusTextLabel->setText(m_shortenedStatusText);
271         m_locationLabel->setText(m_shortenedLocation);
272         setUpdatesEnabled(true);
273     }
274 }
275
276 void FriendListItem::mousePressEvent(QMouseEvent *event)
277 {
278     qDebug() << __PRETTY_FUNCTION__;
279
280     m_mousePosition = event->pos();
281 }
282
283 void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
284 {
285     qDebug() << __PRETTY_FUNCTION__;
286
287     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
288         (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
289         if (m_expanded) {
290             setText(false);
291             m_expanded = false;
292         }
293         else {
294             setText(true);
295             m_expanded = true;
296         }
297     }
298 }
299
300 void FriendListItem::paintEvent(QPaintEvent *event)
301 {
302     qDebug() << __PRETTY_FUNCTION__;
303
304     Q_UNUSED(event);
305
306     QPainter painter(this);
307
308     QRect topRect = QRect(0, 0, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
309     QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
310                              height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
311     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
312                              BACKGROUND_BOTTOM_HEIGHT);
313
314     painter.drawPixmap(topRect, m_backgroundTopImage);
315     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
316     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
317 }
318
319 void FriendListItem::findButtonClicked()
320 {
321     qDebug() << __PRETTY_FUNCTION__;
322
323     emit findFriend(m_user->coordinates());
324 }