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