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