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