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