Moved AvatarImage::create from FriendListitem to SituareService.
[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
38 const int BACKGROUND_TOP_HEIGHT = 20;
39 const int BACKGROUND_BOTTOM_HEIGHT = 15;
40 const QColor COLOR_GRAY = QColor(152, 152, 152);    ///< Gray color
41 const QFont NOKIA_FONT_NORMAL = QFont("Nokia Sans", 18, QFont::Normal);    ///< Normal font
42 const QFont NOKIA_FONT_SMALL = QFont("Nokia Sans", 13, QFont::Normal);     ///< Small font
43 const int ICON_HEIGHT = 24;     ///< Icon height
44 const int ICON_WIDTH = 24;       ///< Icon width
45 const int IMAGE_HEIGHT = 60; ///< Friend image height
46 const int IMAGE_WIDTH = 60;  ///< Friend image width
47 const int ITEM_MAX_HEIGHT = 240; ///< Maximum height for item
48 const int ITEM_MAX_WIDTH = 368;  ///< Maximum width for item
49 const int ITEM_MIN_HEIGHT = 141; ///< Minimum height for item
50 const int ITEM_MIN_WIDTH = 368;  ///< Minimum width for item
51 const int MARGIN = 5;   ///< Icon margin
52 const int MOUSE_PRESS_AREA_WIDTH = 20;  ///< Area width for item height toggling
53 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
54 /**
55 * @var NAME_LABEL_MAX_WIDTH
56 *
57 * @brief Name label's maximum width
58 */
59 const int NAME_LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - IMAGE_WIDTH;
60 /**
61 * @var LABEL_MAX_WIDTH
62 *
63 * @brief All label's maximum width
64 */
65 const int LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH;
66
67 FriendListItem::FriendListItem(QWidget *parent)
68     : QWidget(parent)
69     , m_expanded(false)
70     , m_user(0)
71 {
72     qDebug() << __PRETTY_FUNCTION__;
73
74     QVBoxLayout *layout = new QVBoxLayout(this);
75     layout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
76     layout->setSpacing(0);
77     setLayout(layout);
78
79     QHBoxLayout *topLayout = new QHBoxLayout();
80     topLayout->setMargin(0);
81     topLayout->setSpacing(0);
82
83     QHBoxLayout *bottomLayout = new QHBoxLayout();
84     bottomLayout->setMargin(0);
85     bottomLayout->setSpacing(0);
86
87     QFormLayout *infoLayout = new QFormLayout();
88     infoLayout->setMargin(0);
89     infoLayout->setSpacing(0);
90     infoLayout->setLabelAlignment(Qt::AlignTop);
91
92     QLabel *clockLabel = new QLabel();
93     clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
94     clockLabel->setContentsMargins(0, 0, MARGIN, 0);
95     clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
96     QLabel *envelopeLabel = new QLabel();
97     envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
98     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
99     envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
100     QLabel *compassLabel = new QLabel();
101     compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
102     compassLabel->setContentsMargins(0, 0, MARGIN, 0);
103     compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
104
105     m_imageLabel = new QLabel();
106     m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
107
108     m_nameLabel = new QLabel();
109     m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
110
111     m_distanceLabel = new QLabel();
112     m_distanceLabel->setFixedHeight(IMAGE_HEIGHT);
113
114     m_findButton = new ImageButton(this, ":/res/images/show_position.png",
115                                    ":/res/images/show_position_s.png");
116
117     m_updatedLabel = new QLabel();
118     m_updatedLabel->setWordWrap(true);
119     m_statusTextLabel = new QLabel();
120     m_statusTextLabel->setWordWrap(true);
121     m_locationLabel = new QLabel();
122     m_locationLabel->setWordWrap(true);
123
124     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
125     infoLayout->addRow(compassLabel, m_locationLabel);
126     infoLayout->addRow(clockLabel, m_updatedLabel);
127
128     topLayout->addWidget(m_imageLabel);
129     topLayout->addWidget(m_nameLabel, 1);
130     topLayout->addWidget(m_distanceLabel);
131
132     bottomLayout->addWidget(m_findButton, 0, Qt::AlignTop);
133     bottomLayout->addLayout(infoLayout);
134
135     layout->addLayout(topLayout, 0);
136     layout->addLayout(bottomLayout, 1);
137
138     setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
139     setMaximumSize(ITEM_MIN_WIDTH, ITEM_MAX_HEIGHT);
140
141     setFont(NOKIA_FONT_SMALL);
142     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
143     QPalette itemPalette = palette();
144     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
145     setPalette(itemPalette);
146     QPalette namePalette = m_nameLabel->palette();
147     namePalette.setColor(QPalette::Foreground, Qt::white);
148     m_nameLabel->setPalette(namePalette);
149
150     m_backgroundTopImage.load(":/res/images/list_item_top.png");
151     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
152     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
153
154     connect(m_findButton, SIGNAL(clicked()),
155         this, SLOT(findButtonClicked()));
156 }
157
158 void FriendListItem::setData(User *user)
159 {
160     qDebug() << __PRETTY_FUNCTION__;
161
162     m_user = user;
163
164     m_imageLabel->setPixmap(m_user->profileImage());
165
166     QString unit;
167     double value;
168     user->distance(value, unit);
169     m_distanceLabel->setText(QString::number(value) + " " + unit);
170
171     shortenTexts();
172     setText(false);
173 }
174
175 void FriendListItem::shortenTexts()
176 {
177     qDebug() << __PRETTY_FUNCTION__;
178
179     QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
180     QFontMetrics otherLabelsMetrics = m_updatedLabel->fontMetrics();
181
182     QString name = m_user->name();
183     QString updated = m_user->timestamp();
184     QString statusText = m_user->note();
185     QString location = m_user->address();
186
187     int nameIndex = name.indexOf('\n');
188     int updatedIndex = updated.indexOf('\n');
189     int statusTextIndex = statusText.indexOf('\n');
190     int locationIndex = location.indexOf('\n');
191
192     if (nameIndex > 0) {
193         name.truncate(nameIndex);
194         name.append("...");
195     }
196     if (updatedIndex > 0) {
197         updated.truncate(updatedIndex);
198         updated.append("...");
199     }
200     if (statusTextIndex > 0) {
201         statusText.truncate(statusTextIndex);
202         statusText.append("...");
203     }
204     if (locationIndex > 0) {
205         location.truncate(locationIndex);
206         location.append("...");
207     }
208
209     int distanceLabelWidth = otherLabelsMetrics.width(m_distanceLabel->text());
210     m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
211                                                   - distanceLabelWidth);
212     m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
213     m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
214                                                           LABEL_MAX_WIDTH);
215     m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
216 }
217
218 void FriendListItem::setText(bool expanded)
219 {
220     qDebug() << __PRETTY_FUNCTION__;
221
222     if (expanded) {
223         setUpdatesEnabled(false);
224         m_nameLabel->setText(m_shortenedName);
225         m_updatedLabel->setText(m_user->timestamp());
226         m_statusTextLabel->setText(m_user->note());
227         m_locationLabel->setText(m_user->address());
228         setUpdatesEnabled(true);
229     }
230     else {
231         setUpdatesEnabled(false);
232         m_nameLabel->setText(m_shortenedName);
233         m_updatedLabel->setText(m_shortenedUpdated);
234         m_statusTextLabel->setText(m_shortenedStatusText);
235         m_locationLabel->setText(m_shortenedLocation);
236         setUpdatesEnabled(true);
237     }
238 }
239
240 void FriendListItem::mousePressEvent(QMouseEvent *event)
241 {
242     qDebug() << __PRETTY_FUNCTION__;
243
244     m_mousePosition = event->pos();
245 }
246
247 void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
248 {
249     qDebug() << __PRETTY_FUNCTION__;
250
251     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
252         (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
253         if (m_expanded) {
254             setText(false);
255             m_expanded = false;
256         }
257         else {
258             setText(true);
259             m_expanded = true;
260         }
261     }
262 }
263
264 void FriendListItem::paintEvent(QPaintEvent *event)
265 {
266     qDebug() << __PRETTY_FUNCTION__;
267
268     Q_UNUSED(event);
269
270     QPainter painter(this);
271
272     QRect topRect = QRect(0, 0, ITEM_MIN_WIDTH, BACKGROUND_TOP_HEIGHT);
273     QRect middleRect = QRect(0, topRect.bottom(), ITEM_MIN_WIDTH,
274                              height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
275     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), ITEM_MIN_WIDTH,
276                              BACKGROUND_BOTTOM_HEIGHT);
277
278     painter.drawPixmap(topRect, m_backgroundTopImage);
279     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
280     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
281 }
282
283 void FriendListItem::findButtonClicked()
284 {
285     qDebug() << __PRETTY_FUNCTION__;
286
287     emit findFriend(m_user->coordinates());
288 }