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