backup before review
[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 {
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     m_messageDialog = new UpdateLocationDialog(this);
94     m_messageDialog->hide();
95     m_messageDialog->setWindowModality(Qt::NonModal);
96
97     ImageButton *updateFriendsButton = new ImageButton(this, ":/res/images/refresh.png",
98                                                              ":/res/images/refresh_s.png");
99     ImageButton *updateStatusMessageButton = new ImageButton(this, ":/res/images/send_position.png",
100                                                                    ":/res/images/send_position_s.png");
101
102     buttonLayout->addWidget(updateFriendsButton);
103     buttonLayout->addWidget(updateStatusMessageButton);
104
105     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
106     infoLayout->addRow(compassLabel, m_locationLabel);
107     infoLayout->addRow(clockLabel, m_updatedLabel);
108
109     verticalLayout->addWidget(m_findButton, 0, Qt::AlignHCenter);
110     verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
111     verticalLayout->addLayout(infoLayout);
112     verticalLayout->addLayout(buttonLayout);
113
114     connect(updateStatusMessageButton,SIGNAL(clicked()),
115             this,SLOT(messageUpdate()));
116
117     connect(updateFriendsButton,SIGNAL(clicked()),
118             this, SIGNAL(refreshUserData()));
119
120     connect(m_findButton, SIGNAL(clicked()),
121         this, SLOT(findButtonClicked()));
122
123     connect(this, SIGNAL(reverseGeoReady(QString)),
124             m_messageDialog, SLOT(setAddress(QString)));
125
126     connect(m_messageDialog, SIGNAL(statusUpdate(QString,bool)),
127             this, SIGNAL(statusUpdate(QString,bool)));
128
129     connect(m_messageDialog, SIGNAL(statusUpdate(QString,bool)),
130             this, SLOT(backupUpdateLocationDialogData(QString,bool)));
131
132     setFixedWidth(BACKGROUND_WIDTH);
133
134     this->setFont(NOKIA_FONT_SMALL);
135     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
136     QPalette itemPalette = palette();
137     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
138     setPalette(itemPalette);
139     QPalette namePalette = m_nameLabel->palette();
140     namePalette.setColor(QPalette::Foreground, Qt::white);
141     m_nameLabel->setPalette(namePalette);
142
143     m_backgroundTopImage.load(":/res/images/user_info_item_top.png");
144     m_backgroundMiddleImage.load(":/res/images/user_info_item_middle.png");
145     m_backgroundBottomImage.load(":/res/images/user_info_item_bottom.png");
146
147     restoreUnsendMessage();
148 }
149
150 UserInfo::~UserInfo()
151 {
152     qDebug() << __PRETTY_FUNCTION__;
153
154     if (!m_backupMessage.isEmpty()) {
155         QSettings settings(DIRECTORY_NAME, FILE_NAME);
156         settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
157         settings.setValue(USER_UNSEND_MESSAGE_PUBLISH_POLICITY, m_backupFacebookPublishPolicity);
158     }
159
160 }
161
162 void UserInfo::backupUpdateLocationDialogData(const QString &status, const bool &publish)
163 {
164     qDebug() << __PRETTY_FUNCTION__;
165
166     m_backupMessage = status;
167     m_backupFacebookPublishPolicity = publish;
168
169     m_messageUpdateVerified = false;
170 }
171
172 void UserInfo::clearUpdateLocationDialogData()
173 {
174     qDebug() << __PRETTY_FUNCTION__;
175
176     qDebug() << "Message Before Clearing: " << m_backupMessage;
177     m_backupMessage.clear();
178     qDebug() << "Message After Clearing: " << m_backupMessage;
179     m_backupFacebookPublishPolicity = false;
180 }
181
182 void UserInfo::restoreUnsendMessage()
183 {
184     qDebug() << __PRETTY_FUNCTION__;
185
186     QSettings settings(DIRECTORY_NAME, FILE_NAME);
187     m_backupMessage = settings.value(USER_UNSEND_MESSAGE, "").toString();
188     m_backupFacebookPublishPolicity =
189             settings.value(USER_UNSEND_MESSAGE_PUBLISH_POLICITY, false).toBool();
190 }
191
192 void UserInfo::setAddress(const QString &address)
193 {
194     qDebug() << __PRETTY_FUNCTION__;
195
196     m_locationLabel->setText(address);
197 }
198
199 void UserInfo::setCoordinates(const QPointF &coordinates)
200 {
201     qDebug() << __PRETTY_FUNCTION__;
202
203     m_coordinates = coordinates;
204 }
205
206 void UserInfo::setMessageText(const QString &text)
207 {
208     qDebug() << __PRETTY_FUNCTION__;
209
210     m_messageText = text;
211     setText(false);
212 }
213
214 void UserInfo::setProfileImage(const QPixmap &image)
215 {
216     qDebug() << __PRETTY_FUNCTION__;
217
218     m_findButton->setButtonIcon(image);
219 }
220
221 void UserInfo::setTime(const QString &time)
222 {
223     qDebug() << __PRETTY_FUNCTION__;
224
225     m_updatedLabel->setText(time);
226 }
227
228 void UserInfo::setUserName(const QString &name)
229 {
230     qDebug() << __PRETTY_FUNCTION__;
231
232     m_userName = name;
233     setText(false);
234 }
235
236 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
237 {
238     qDebug() << __PRETTY_FUNCTION__;
239
240     QFontMetrics labelMetrics = label->fontMetrics();
241
242     QString textParam = text;
243     int index = textParam.indexOf('\n');
244
245     if (index > 0) {
246         textParam.truncate(index);
247         textParam.append("...");
248     }
249
250    return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
251 }
252
253 void UserInfo::setText(bool expanded)
254 {
255     qDebug() << __PRETTY_FUNCTION__;
256
257     if (expanded) {
258         m_nameLabel->setText(m_userName);
259         m_statusTextLabel->setText(m_messageText);
260     }
261     else {
262         m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
263         m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
264                                                LABEL_MAX_WIDTH));
265     }
266 }
267
268 void UserInfo::verifyMessageUpdateToServer(const QString &errorMessage)
269 {
270     qDebug() << __PRETTY_FUNCTION__;
271
272     Q_UNUSED(errorMessage);
273
274     if (!m_messageUpdateVerified) {
275         QString message = QObject::tr("Status update failed\n\nYour message is saved to textbox "
276                                       "until sending will succeed");
277
278         qDebug() << "m_messageText: " << m_messageText;
279         qDebug() << "m_backupMessage: " << m_backupMessage;
280
281         if (m_messageText != m_backupMessage && !m_backupMessage.isEmpty())
282             emit notificateUpdateFailing(message);
283     }
284
285     m_messageUpdateVerified = true;
286 }
287
288 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
289 {
290     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
291
292     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
293         (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
294         if (m_expanded) {
295             setText(false);
296             m_expanded = false;
297         }
298         else {
299             setText(true);
300             m_expanded = true;
301         }
302     }
303 }
304
305 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
306 {
307     qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
308
309     QPainter painter(this);
310
311     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
312     QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
313                              height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
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::mousePressEvent(QMouseEvent *event)
323 {
324     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
325
326     m_mousePosition = event->pos();
327 }
328
329 void UserInfo::findButtonClicked()
330 {
331     qDebug() << __PRETTY_FUNCTION__;
332
333     emit findUser(m_coordinates);
334 }
335
336 void UserInfo::messageUpdate()
337 {
338     qDebug() << __PRETTY_FUNCTION__;
339
340     emit requestReverseGeo();
341
342     m_messageDialog->show();
343     m_messageDialog->m_textEdit->setPlainText(m_backupMessage);
344     m_messageDialog->m_checkBox->setCheckState(Qt::Unchecked);
345
346     if (m_backupFacebookPublishPolicity == true)
347         m_messageDialog->m_checkBox->setCheckState(Qt::Checked);
348 }