Review and fixes
[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_WIDTH = 368;
40 const int BACKGROUND_TOP_HEIGHT = 20;
41 const int BACKGROUND_BOTTOM_HEIGHT = 15;
42 const int ICON_HEIGHT = 24;
43 const int ICON_WIDTH = 24;
44 const int MARGIN = 5;
45 const int LABEL_MAX_WIDTH = 300;
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_findButton = new ImageButton();
81
82     m_nameLabel = new QLabel();
83     m_nameLabel->setWordWrap(true);
84
85     m_statusTextLabel = new QLabel();
86     m_statusTextLabel->setWordWrap(true);
87     m_locationLabel = new QLabel();
88     m_locationLabel->setWordWrap(true);
89     m_updatedLabel = new QLabel();
90     m_updatedLabel->setWordWrap(true);
91
92     ImageButton *updateFriendsButton = new ImageButton(":/res/images/refresh.png",
93                                                        ":/res/images/refresh_s.png",
94                                                        "", this);
95     ImageButton *updateStatusMessageButton = new ImageButton(":/res/images/send_position.png",
96                                                              ":/res/images/send_position_s.png",
97                                                              "", this);
98
99     buttonLayout->addWidget(updateFriendsButton);
100     buttonLayout->addWidget(updateStatusMessageButton);
101
102     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
103     infoLayout->addRow(compassLabel, m_locationLabel);
104     infoLayout->addRow(clockLabel, m_updatedLabel);
105
106     verticalLayout->addWidget(m_findButton, 0, Qt::AlignHCenter);
107     verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
108     verticalLayout->addLayout(infoLayout);
109     verticalLayout->addLayout(buttonLayout);
110
111     connect(updateStatusMessageButton,SIGNAL(clicked()),
112             this,SLOT(messageUpdate()));
113
114     connect(updateFriendsButton,SIGNAL(clicked()),
115             this, SIGNAL(refreshUserData()));
116
117     connect(m_findButton, SIGNAL(clicked()),
118             this, SLOT(findButtonClicked()));
119
120     setFixedWidth(BACKGROUND_WIDTH);
121
122     this->setFont(NOKIA_FONT_SMALL);
123     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
124     QPalette itemPalette = palette();
125     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
126     setPalette(itemPalette);
127     QPalette namePalette = m_nameLabel->palette();
128     namePalette.setColor(QPalette::Foreground, Qt::white);
129     m_nameLabel->setPalette(namePalette);
130
131     m_backgroundTopImage.load(":/res/images/list_item_top.png");
132     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
133     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
134
135     restoreUnsendMessage();
136 }
137
138 UserInfo::~UserInfo()
139 {
140     qDebug() << __PRETTY_FUNCTION__;
141
142     QSettings settings(DIRECTORY_NAME, FILE_NAME);
143
144     if (!m_backupMessage.isEmpty()) {
145         settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
146         settings.setValue(USER_UNSEND_MESSAGE_PUBLISH, m_backupFacebookPublishPolicity);
147     } else {
148         settings.remove(USER_UNSEND_MESSAGE);
149         settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
150     }
151 }
152
153 void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
154 {
155     qDebug() << __PRETTY_FUNCTION__;
156
157     m_backupMessage = status;
158     m_backupFacebookPublishPolicity = publish;
159 }
160
161 void UserInfo::clearUpdateLocationDialogData()
162 {
163     qDebug() << __PRETTY_FUNCTION__;
164
165     m_backupMessage.clear();
166     m_backupFacebookPublishPolicity = false;
167 }
168
169 void UserInfo::findButtonClicked()
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     emit findUser(m_coordinates);
174 }
175
176 void UserInfo::messageUpdate()
177 {
178     qDebug() << __PRETTY_FUNCTION__;
179
180     delete m_updateLocation;
181     m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
182                                                 this);
183
184     connect(this, SIGNAL(reverseGeoReady(QString)),
185             m_updateLocation, SLOT(setAddress(QString)));
186
187     connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
188             this, SIGNAL(statusUpdate(QString, bool)));
189
190     connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
191             this, SLOT(backupUpdateLocationDialogData(QString, bool)));
192
193     connect(m_updateLocation, SIGNAL(finished(int)),
194             this, SLOT(updateLocationDialogFinished(int)));
195
196     m_updateLocation->show();
197
198     emit requestReverseGeo();
199 }
200
201 void UserInfo::mousePressEvent(QMouseEvent *event)
202 {
203     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
204
205     m_mousePosition = event->pos();
206 }
207
208 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
209 {
210     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
211
212     const int MOUSE_PRESS_AREA_HEIGHT = 20;
213     const int MOUSE_PRESS_AREA_WIDTH = 20;
214
215     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH)
216         && (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
217         if (m_expanded) {
218             setText(false);
219             m_expanded = false;
220         }
221         else {
222             setText(true);
223             m_expanded = true;
224         }
225     }
226 }
227
228 void UserInfo::paintEvent(QPaintEvent *event)
229 {
230     qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
231
232     QPainter painter(this);
233
234     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
235
236     QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_WIDTH,
237                              this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
238
239     QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_WIDTH,
240                              BACKGROUND_BOTTOM_HEIGHT);
241
242     painter.drawPixmap(topRect, m_backgroundTopImage);
243     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
244     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
245 }
246
247 void UserInfo::restoreUnsendMessage()
248 {
249     qDebug() << __PRETTY_FUNCTION__;
250
251     QSettings settings(DIRECTORY_NAME, FILE_NAME);
252     m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
253     m_backupFacebookPublishPolicity = settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
254 }
255
256 void UserInfo::setAddress(const QString &address)
257 {
258     qDebug() << __PRETTY_FUNCTION__;
259
260     m_locationLabel->setText(address);
261 }
262
263 void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
264 {
265     qDebug() << __PRETTY_FUNCTION__;
266
267     m_coordinates = coordinates;
268 }
269
270 void UserInfo::setMessageText(const QString &text)
271 {
272     qDebug() << __PRETTY_FUNCTION__;
273
274     QStringList list;
275     list = text.split(' ');
276
277     for (int i = 0; i < list.count(); i++) {
278         if (fontMetrics().width(list.at(i)) > LABEL_MAX_WIDTH)
279             list.replace(i, splitWord(list.at(i)));
280     }
281
282     m_messageText = list.join(" ");
283     setText(false);
284 }
285
286 void UserInfo::setProfileImage(const QPixmap &image)
287 {
288     qDebug() << __PRETTY_FUNCTION__;
289
290     if(!image.isNull())
291         m_findButton->setButtonIcon(image);
292 }
293
294 void UserInfo::setText(bool expanded)
295 {
296     qDebug() << __PRETTY_FUNCTION__;
297
298     if (expanded)
299         m_statusTextLabel->setText(m_messageText);
300     else
301         m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText, LABEL_MAX_WIDTH));
302 }
303
304 void UserInfo::setTime(const QString &time)
305 {
306     qDebug() << __PRETTY_FUNCTION__;
307
308     m_updatedLabel->setText(time);
309 }
310
311 void UserInfo::setUserName(const QString &name)
312 {
313     qDebug() << __PRETTY_FUNCTION__;
314
315     m_userName = name;
316     m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
317 }
318
319 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
320 {
321     qDebug() << __PRETTY_FUNCTION__;
322
323     QFontMetrics labelMetrics = label->fontMetrics();
324     QString copiedText = text;
325     int index = copiedText.indexOf('\n');
326
327     if (index >= 0) {
328         copiedText.truncate(index);
329         copiedText.append("...");
330     }
331
332     return labelMetrics.elidedText(copiedText, Qt::ElideRight, textMaxWidth);
333 }
334
335 QString UserInfo::splitWord(const QString &word) const
336 {
337     qDebug() << __PRETTY_FUNCTION__;
338
339     QString result;
340     QString temp;
341
342     for (int i = 0; i < word.length(); i++) {
343         if (fontMetrics().width(temp.append(word.at(i))) > LABEL_MAX_WIDTH) {
344             result.append(temp.left(temp.length() - 1));
345             result.append(" ");
346             temp.remove(0, temp.length() - 1);
347         }
348     }
349
350     result.append(temp);
351
352     return result;
353 }
354
355 void UserInfo::updateLocationDialogFinished(int reason)
356 {
357     qDebug() << __PRETTY_FUNCTION__;
358
359     Q_UNUSED(reason);
360
361     if (m_updateLocation) {
362         disconnect(this, SIGNAL(reverseGeoReady(QString)),
363                 m_updateLocation, SLOT(setAddress(QString)));
364
365         disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
366                 this, SIGNAL(statusUpdate(QString,bool)));
367
368         disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
369                 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
370
371         disconnect(m_updateLocation, SIGNAL(finished(int)),
372                 this, SLOT(updateLocationDialogFinished(int)));
373
374         m_updateLocation->deleteLater();
375         m_updateLocation = 0;
376     }
377 }