Added method to engine to set progress indicator enabled/disabled.
[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 "tagsdialog.h"
36 #include "textmodifier.h"
37 #include "user/user.h"
38
39 #include "userinfo.h"
40
41 const int BACKGROUND_BOTTOM_HEIGHT = 15;
42 const int BACKGROUND_TOP_HEIGHT = 20;
43 const int BACKGROUND_WIDTH = 368;
44 const int ICON_HEIGHT = 24;
45 const int ICON_WIDTH = 24;
46 const int MARGIN = 5;
47 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - ICON_WIDTH - 5 * MARGIN;
48
49 UserInfo::UserInfo(QWidget *parent)
50     : QWidget(parent),
51       m_expanded(false),
52       m_tagsDialog(0),
53       m_updateLocation(0)
54 {
55     qDebug() << __PRETTY_FUNCTION__;
56
57     QVBoxLayout *verticalLayout = new QVBoxLayout(this);
58     verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
59     verticalLayout->setSpacing(0);
60     setLayout(verticalLayout);
61
62     QFormLayout *infoLayout = new QFormLayout();
63     infoLayout->setMargin(0);
64     infoLayout->setSpacing(0);
65
66     QLabel *envelopeLabel = new QLabel();
67     envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
68     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
69     envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
70     QLabel *compassLabel = new QLabel();
71     compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
72     compassLabel->setContentsMargins(0, 0, MARGIN, 0);
73     compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
74     QLabel *clockLabel = new QLabel();
75     clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
76     clockLabel->setContentsMargins(0, 0, MARGIN, 0);
77     clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
78     QLabel *tagsLabel = new QLabel();
79     tagsLabel->setPixmap(QPixmap(":/res/images/tag.png"));
80     tagsLabel->setContentsMargins(0, 0, MARGIN, 0);
81     tagsLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
82
83     m_avatar = new ImageButton();
84
85     m_nameLabel = new QLabel();
86
87     m_statusTextLabel = new QLabel();
88     m_statusTextLabel->setWordWrap(true);
89     m_statusTextLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
90     m_locationLabel = new QLabel();
91     m_locationLabel->setWordWrap(true);
92     m_locationLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
93     m_updatedLabel = new QLabel();
94     m_updatedLabel->setWordWrap(true);
95     m_updatedLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
96     m_tagsTextLabel = new QLabel();
97     m_tagsTextLabel->setWordWrap(true);
98     m_tagsTextLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
99
100
101     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
102     infoLayout->addRow(compassLabel, m_locationLabel);
103     infoLayout->addRow(clockLabel, m_updatedLabel);
104     infoLayout->addRow(tagsLabel, m_tagsTextLabel);
105
106     verticalLayout->addWidget(m_avatar, 0, Qt::AlignHCenter);
107     verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
108     verticalLayout->addLayout(infoLayout);
109
110     connect(m_avatar, SIGNAL(clicked()),
111             this, SLOT(findButtonClicked()));
112
113     setFixedWidth(BACKGROUND_WIDTH);
114
115     this->setFont(NOKIA_FONT_SMALL);
116     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
117     QPalette itemPalette = palette();
118     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
119     setPalette(itemPalette);
120     QPalette namePalette = m_nameLabel->palette();
121     namePalette.setColor(QPalette::Foreground, Qt::white);
122     m_nameLabel->setPalette(namePalette);
123
124     m_backgroundTopImage.load(":/res/images/list_item_top.png");
125     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
126     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
127     m_backgroundTopSelectedImage.load(":/res/images/list_item_top_selected.png");
128     m_backgroundMiddleSelectedImage.load(":/res/images/list_item_middle_selected.png");
129     m_backgroundBottomSelectedImage.load(":/res/images/list_item_bottom_selected.png");
130
131     restoreUnsendMessage();
132 }
133
134 UserInfo::~UserInfo()
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     QSettings settings(DIRECTORY_NAME, FILE_NAME);
139
140     if (!m_backupMessage.isEmpty()) {
141         settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
142         settings.setValue(USER_UNSEND_MESSAGE_PUBLISH, m_backupFacebookPublishPolicity);
143     } else {
144         settings.remove(USER_UNSEND_MESSAGE);
145         settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
146     }
147 }
148
149 void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
150 {
151     qDebug() << __PRETTY_FUNCTION__;
152
153     m_backupMessage = status;
154     m_backupFacebookPublishPolicity = publish;
155 }
156
157 void UserInfo::clearUpdateLocationDialogData()
158 {
159     qDebug() << __PRETTY_FUNCTION__;
160
161     m_backupMessage.clear();
162     m_backupFacebookPublishPolicity = false;
163 }
164
165 void UserInfo::collapse()
166 {
167     qDebug() << __PRETTY_FUNCTION__;
168
169     setExpanded(false);
170 }
171
172 void UserInfo::findButtonClicked()
173 {
174     qDebug() << __PRETTY_FUNCTION__;
175
176     emit findUser(m_coordinates);
177 }
178
179 void UserInfo::messageUpdate()
180 {
181     qDebug() << __PRETTY_FUNCTION__;
182
183     delete m_updateLocation;
184     m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
185                                                 this);
186
187     connect(this, SIGNAL(reverseGeoReady(QString)),
188             m_updateLocation, SLOT(setAddress(QString)));
189
190     connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
191             this, SIGNAL(statusUpdate(QString, bool)));
192
193     connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
194             this, SLOT(backupUpdateLocationDialogData(QString, bool)));
195
196     connect(m_updateLocation, SIGNAL(finished(int)),
197             this, SLOT(updateLocationDialogFinished(int)));
198
199     m_updateLocation->show();
200
201     emit requestReverseGeo();
202 }
203
204 void UserInfo::mousePressEvent(QMouseEvent *event)
205 {
206     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
207
208     m_mousePosition = event->pos();
209 }
210
211 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
212 {
213     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
214
215     const int MOUSE_PRESS_AREA_HEIGHT = 20;
216     const int MOUSE_PRESS_AREA_WIDTH = 20;
217
218     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH)
219         && (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
220         if (m_expanded) {
221             setExpanded(false);
222             //m_expanded = false;
223         }
224         else {
225             setExpanded(true);
226             //m_expanded = true;
227         }
228     }
229 }
230
231 void UserInfo::paintEvent(QPaintEvent *event)
232 {
233     qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
234
235     QPainter painter(this);
236
237     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
238
239     QRect middleRect = QRect(topRect.left(), topRect.bottom(), BACKGROUND_WIDTH,
240                              height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
241
242     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
243                              BACKGROUND_BOTTOM_HEIGHT);
244
245     if (m_expanded) {
246         painter.drawPixmap(topRect, m_backgroundTopSelectedImage);
247         painter.drawPixmap(middleRect, m_backgroundMiddleSelectedImage);
248         painter.drawPixmap(bottomRect, m_backgroundBottomSelectedImage);
249     } else {
250         painter.drawPixmap(topRect, m_backgroundTopImage);
251         painter.drawPixmap(middleRect, m_backgroundMiddleImage);
252         painter.drawPixmap(bottomRect, m_backgroundBottomImage);
253     }
254 }
255
256 void UserInfo::restoreUnsendMessage()
257 {
258     qDebug() << __PRETTY_FUNCTION__;
259
260     QSettings settings(DIRECTORY_NAME, FILE_NAME);
261     m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
262     m_backupFacebookPublishPolicity = settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
263 }
264
265 void UserInfo::setAddress(const QString &address)
266 {
267     qDebug() << __PRETTY_FUNCTION__;
268
269     m_locationLabel->setText(address);
270 }
271
272 void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
273 {
274     qDebug() << __PRETTY_FUNCTION__;
275
276     m_coordinates = coordinates;
277 }
278
279 void UserInfo::setMessageText(const QString &text)
280 {
281     qDebug() << __PRETTY_FUNCTION__;
282
283     m_messageText = TextModifier::splitLongWords(m_statusTextLabel->fontMetrics(), text,
284                                                  LABEL_MAX_WIDTH);
285
286     setExpanded(false);
287 }
288
289 void UserInfo::setProfileImage(const QPixmap &image)
290 {
291     qDebug() << __PRETTY_FUNCTION__;
292
293     if(!image.isNull())
294         m_avatar->setButtonIcon(image);
295 }
296
297 void UserInfo::setExpanded(bool expanded)
298 {
299     qDebug() << __PRETTY_FUNCTION__;
300
301     m_expanded = expanded;
302
303     if (expanded) {
304         m_statusTextLabel->setText(m_messageText);
305     } else {
306         m_statusTextLabel->setText(TextModifier::shortenText(m_statusTextLabel->fontMetrics(),
307                                                              m_messageText, LABEL_MAX_WIDTH));
308     }
309     update();
310
311     emit itemSelectionChanged(expanded);
312 }
313
314 void UserInfo::setTags(const QHash<QString, QString> &tags)
315 {
316     qDebug() << __PRETTY_FUNCTION__;
317
318     m_userTags = tags;
319
320     QString tagsText;
321
322     QHashIterator<QString, QString> tagsIterator(tags);
323     while (tagsIterator.hasNext()) {
324         tagsIterator.next();
325         tagsText.append("[" + tagsIterator.value() + "] ");
326     }
327
328     m_tagsTextLabel->setText(tagsText);
329
330     if (m_tagsDialog)
331         m_tagsDialog->populateUserTags(m_userTags);
332 }
333
334 void UserInfo::setPopularTags(const QHash<QString, QString> &tags)
335 {
336     qDebug() << __PRETTY_FUNCTION__;
337
338     if (m_tagsDialog)
339         m_tagsDialog->populatePopularTags(tags);
340 }
341
342 void UserInfo::setTime(const QString &time)
343 {
344     qDebug() << __PRETTY_FUNCTION__;
345
346     m_updatedLabel->setText(time);
347 }
348
349 void UserInfo::setUserName(const QString &name)
350 {
351     qDebug() << __PRETTY_FUNCTION__;
352
353     m_userName = name;
354
355     m_nameLabel->setText(TextModifier::shortenText(m_nameLabel->fontMetrics(), m_userName,
356                                                    LABEL_MAX_WIDTH));
357 }
358
359 void UserInfo::showTagsDialog()
360 {
361     qDebug() << __PRETTY_FUNCTION__;
362
363     m_tagsDialog = new TagsDialog(m_userTags, this);
364     connect(m_tagsDialog, SIGNAL(finished(int)),
365              this, SLOT(tagsDialogFinished(int)));
366     m_tagsDialog->show();
367 }
368
369 void UserInfo::tagsDialogFinished(int reason)
370 {
371     qDebug() << __PRETTY_FUNCTION__;
372
373     if (m_tagsDialog) {
374         if (reason == QDialog::Accepted) {
375             QStringList removedTagsIds;
376
377             foreach (QString removedTag, m_tagsDialog->removedTags())
378                 removedTagsIds.append(m_userTags.key(removedTag));
379
380             if (!removedTagsIds.isEmpty())
381                 emit removeTags(removedTagsIds);
382             if (!m_tagsDialog->newTags().isEmpty())
383                 emit addTags(m_tagsDialog->newTags());
384         }
385
386         m_tagsDialog->deleteLater();
387         m_tagsDialog = 0;
388     }
389 }
390
391 void UserInfo::updateLocationDialogFinished(int reason)
392 {
393     qDebug() << __PRETTY_FUNCTION__;
394
395     Q_UNUSED(reason);
396
397     if (m_updateLocation) {
398         disconnect(this, SIGNAL(reverseGeoReady(QString)),
399                 m_updateLocation, SLOT(setAddress(QString)));
400
401         disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
402                 this, SIGNAL(statusUpdate(QString,bool)));
403
404         disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
405                 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
406
407         disconnect(m_updateLocation, SIGNAL(finished(int)),
408                 this, SLOT(updateLocationDialogFinished(int)));
409
410         m_updateLocation->deleteLater();
411         m_updateLocation = 0;
412     }
413 }