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