Minor changes to userinfo. Remove word wrap from name label and rename button
[situare] / src / ui / userinfo.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jukka Saastamoinen - jukka.saastamoinen@ixonos.com
6        Jussi Laitinen - jussi.laitinen@ixonos.com
7        Katri Kaikkonen - katri.kaikkonen@ixonos.com
8        Henri Lampela - henri.lampela@ixonos.com
9        Ville Tiensuu - ville.tiensuu@ixonos.com
10
11    Situare is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License
13    version 2 as published by the Free Software Foundation.
14
15    Situare is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with Situare; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
23    USA.
24 */
25
26 #include <QFormLayout>
27 #include <QLabel>
28 #include <QMouseEvent>
29 #include <QPainter>
30 #include <QSettings>
31 #include <QVBoxLayout>
32
33 #include "common.h"
34 #include "imagebutton.h"
35 #include "user/user.h"
36
37 #include "userinfo.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;
43 const int ICON_WIDTH = 24;
44 const int LABEL_MAX_WIDTH = 300;
45 const int MARGIN = 5;
46
47 UserInfo::UserInfo(QWidget *parent)
48     : QWidget(parent),
49       m_expanded(false),
50       m_updateLocation(0)
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53
54     QVBoxLayout *verticalLayout = new QVBoxLayout(this);
55     verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
56     verticalLayout->setSpacing(0);
57     setLayout(verticalLayout);
58
59     QFormLayout *infoLayout = new QFormLayout();
60     infoLayout->setMargin(0);
61     infoLayout->setSpacing(0);
62
63     QHBoxLayout *buttonLayout = new QHBoxLayout();
64     buttonLayout->setMargin(0);
65     buttonLayout->setSpacing(0);
66
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);
75     QLabel *clockLabel = new QLabel();
76     clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
77     clockLabel->setContentsMargins(0, 0, MARGIN, 0);
78     clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
79
80     m_avatar = new ImageButton();
81
82     m_nameLabel = new QLabel();
83
84     m_statusTextLabel = new QLabel();
85     m_statusTextLabel->setWordWrap(true);
86     m_locationLabel = new QLabel();
87     m_locationLabel->setWordWrap(true);
88     m_updatedLabel = new QLabel();
89     m_updatedLabel->setWordWrap(true);
90
91     ImageButton *updateFriendsButton = new ImageButton(":/res/images/refresh.png",
92                                                        ":/res/images/refresh_s.png",
93                                                        "", this);
94     ImageButton *updateStatusMessageButton = new ImageButton(":/res/images/send_position.png",
95                                                              ":/res/images/send_position_s.png",
96                                                              "", this);
97
98     buttonLayout->addWidget(updateFriendsButton);
99     buttonLayout->addWidget(updateStatusMessageButton);
100
101     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
102     infoLayout->addRow(compassLabel, m_locationLabel);
103     infoLayout->addRow(clockLabel, m_updatedLabel);
104
105     verticalLayout->addWidget(m_avatar, 0, Qt::AlignHCenter);
106     verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
107     verticalLayout->addLayout(infoLayout);
108     verticalLayout->addLayout(buttonLayout);
109
110     connect(updateStatusMessageButton,SIGNAL(clicked()),
111             this,SLOT(messageUpdate()));
112
113     connect(updateFriendsButton,SIGNAL(clicked()),
114             this, SIGNAL(refreshUserData()));
115
116     connect(m_avatar, SIGNAL(clicked()),
117             this, SLOT(findButtonClicked()));
118
119     setFixedWidth(BACKGROUND_WIDTH);
120
121     this->setFont(NOKIA_FONT_SMALL);
122     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
123     QPalette itemPalette = palette();
124     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
125     setPalette(itemPalette);
126     QPalette namePalette = m_nameLabel->palette();
127     namePalette.setColor(QPalette::Foreground, Qt::white);
128     m_nameLabel->setPalette(namePalette);
129
130     m_backgroundTopImage.load(":/res/images/list_item_top.png");
131     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
132     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
133
134     restoreUnsendMessage();
135 }
136
137 UserInfo::~UserInfo()
138 {
139     qDebug() << __PRETTY_FUNCTION__;
140
141     QSettings settings(DIRECTORY_NAME, FILE_NAME);
142
143     if (!m_backupMessage.isEmpty()) {
144         settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
145         settings.setValue(USER_UNSEND_MESSAGE_PUBLISH, m_backupFacebookPublishPolicity);
146     } else {
147         settings.remove(USER_UNSEND_MESSAGE);
148         settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
149     }
150 }
151
152 void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
153 {
154     qDebug() << __PRETTY_FUNCTION__;
155
156     m_backupMessage = status;
157     m_backupFacebookPublishPolicity = publish;
158 }
159
160 void UserInfo::clearUpdateLocationDialogData()
161 {
162     qDebug() << __PRETTY_FUNCTION__;
163
164     m_backupMessage.clear();
165     m_backupFacebookPublishPolicity = false;
166 }
167
168 void UserInfo::collapse()
169 {
170     qDebug() << __PRETTY_FUNCTION__;
171
172     setExpanded(false);
173 }
174
175 void UserInfo::findButtonClicked()
176 {
177     qDebug() << __PRETTY_FUNCTION__;
178
179     emit findUser(m_coordinates);
180 }
181
182 void UserInfo::messageUpdate()
183 {
184     qDebug() << __PRETTY_FUNCTION__;
185
186     delete m_updateLocation;
187     m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
188                                                 this);
189
190     connect(this, SIGNAL(reverseGeoReady(QString)),
191             m_updateLocation, SLOT(setAddress(QString)));
192
193     connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
194             this, SIGNAL(statusUpdate(QString, bool)));
195
196     connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
197             this, SLOT(backupUpdateLocationDialogData(QString, bool)));
198
199     connect(m_updateLocation, SIGNAL(finished(int)),
200             this, SLOT(updateLocationDialogFinished(int)));
201
202     m_updateLocation->show();
203
204     emit requestReverseGeo();
205 }
206
207 void UserInfo::mousePressEvent(QMouseEvent *event)
208 {
209     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
210
211     m_mousePosition = event->pos();
212 }
213
214 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
215 {
216     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
217
218     const int MOUSE_PRESS_AREA_HEIGHT = 20;
219     const int MOUSE_PRESS_AREA_WIDTH = 20;
220
221     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH)
222         && (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
223         if (m_expanded) {
224             setExpanded(false);
225             m_expanded = false;
226         }
227         else {
228             setExpanded(true);
229             m_expanded = true;
230         }
231     }
232 }
233
234 void UserInfo::paintEvent(QPaintEvent *event)
235 {
236     qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
237
238     QPainter painter(this);
239
240     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
241
242     QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_WIDTH,
243                              this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
244
245     QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_WIDTH,
246                              BACKGROUND_BOTTOM_HEIGHT);
247
248     painter.drawPixmap(topRect, m_backgroundTopImage);
249     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
250     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
251 }
252
253 void UserInfo::restoreUnsendMessage()
254 {
255     qDebug() << __PRETTY_FUNCTION__;
256
257     QSettings settings(DIRECTORY_NAME, FILE_NAME);
258     m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
259     m_backupFacebookPublishPolicity = settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
260 }
261
262 void UserInfo::setAddress(const QString &address)
263 {
264     qDebug() << __PRETTY_FUNCTION__;
265
266     m_locationLabel->setText(address);
267 }
268
269 void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
270 {
271     qDebug() << __PRETTY_FUNCTION__;
272
273     m_coordinates = coordinates;
274 }
275
276 void UserInfo::setMessageText(const QString &text)
277 {
278     qDebug() << __PRETTY_FUNCTION__;
279
280     QStringList list;
281     list = text.split(' ');
282
283     for (int i = 0; i < list.count(); i++) {
284         if (fontMetrics().width(list.at(i)) > LABEL_MAX_WIDTH)
285             list.replace(i, splitWord(list.at(i)));
286     }
287
288     m_messageText = list.join(" ");
289
290     setExpanded(false);
291 }
292
293 void UserInfo::setProfileImage(const QPixmap &image)
294 {
295     qDebug() << __PRETTY_FUNCTION__;
296
297     if(!image.isNull())
298         m_avatar->setButtonIcon(image);
299 }
300
301 void UserInfo::setExpanded(bool expanded)
302 {
303     qDebug() << __PRETTY_FUNCTION__;
304
305     if (expanded)
306         m_statusTextLabel->setText(m_messageText);
307     else
308         m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText, LABEL_MAX_WIDTH));
309 }
310
311 void UserInfo::setTime(const QString &time)
312 {
313     qDebug() << __PRETTY_FUNCTION__;
314
315     m_updatedLabel->setText(time);
316 }
317
318 void UserInfo::setUserName(const QString &name)
319 {
320     qDebug() << __PRETTY_FUNCTION__;
321
322     m_userName = name;
323
324     m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
325 }
326
327 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
328 {
329     qDebug() << __PRETTY_FUNCTION__;
330
331     QFontMetrics labelMetrics = label->fontMetrics();
332     QString copiedText = text;
333     int index = copiedText.indexOf('\n');
334
335     if (index >= 0) {
336         copiedText.truncate(index);
337         copiedText.append("...");
338     }
339
340     return labelMetrics.elidedText(copiedText, Qt::ElideRight, textMaxWidth);
341 }
342
343 QString UserInfo::splitWord(const QString &word) const
344 {
345     qDebug() << __PRETTY_FUNCTION__;
346
347     QString result;
348     QString temp;
349
350     for (int i = 0; i < word.length(); i++) {
351         if (fontMetrics().width(temp.append(word.at(i))) > LABEL_MAX_WIDTH) {
352             result.append(temp.left(temp.length() - 1));
353             result.append(" ");
354             temp.remove(0, temp.length() - 1);
355         }
356     }
357
358     result.append(temp);
359
360     return result;
361 }
362
363 void UserInfo::updateLocationDialogFinished(int reason)
364 {
365     qDebug() << __PRETTY_FUNCTION__;
366
367     Q_UNUSED(reason);
368
369     if (m_updateLocation) {
370         disconnect(this, SIGNAL(reverseGeoReady(QString)),
371                 m_updateLocation, SLOT(setAddress(QString)));
372
373         disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
374                 this, SIGNAL(statusUpdate(QString,bool)));
375
376         disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
377                 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
378
379         disconnect(m_updateLocation, SIGNAL(finished(int)),
380                 this, SLOT(updateLocationDialogFinished(int)));
381
382         m_updateLocation->deleteLater();
383         m_updateLocation = 0;
384     }
385 }