The first "kind of" working version of the new panels
[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 "userinfo.h"
27
28 #include "../user/user.h"
29 #include "common.h"
30 #include "imagebutton.h"
31
32 const int BACKGROUND_WIDTH = 368;           ///< Width for item
33 const int BACKGROUND_TOP_HEIGHT = 20;       ///< Height for item top
34 const int BACKGROUND_BOTTOM_HEIGHT = 15;    ///< Height for item bottom
35 const int ICON_HEIGHT = 24;                 ///< Icon height
36 const int ICON_WIDTH = 24;                  ///< Icon width
37 const int MARGIN = 5;                       ///< Icon margin
38 const int LINE_LENGTH = 27;                 ///< Line length
39 const int MOUSE_PRESS_AREA_WIDTH = 20;      ///< Area width for item height toggling
40 const int MOUSE_PRESS_AREA_HEIGHT = 20;     ///< Area height for item height toggling
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/list_item_top.png");
132     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
133     m_backgroundBottomImage.load(":/res/images/list_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, m_backupFacebookPublishPolicity);
147     } else {
148         settings.remove(USER_UNSEND_MESSAGE);
149         settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
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 GeoCoordinate &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     m_expandedMessageText.clear();
173     QString temp = "";
174     for(int i=0;i < text.length();i++) {
175         if(fontMetrics().width(temp.append(text.at(i))) > 170) {
176             temp.append("\n");
177             if(temp.startsWith(QString(" ")))
178                 temp.remove(0, 1);
179
180             m_expandedMessageText.append(temp);
181             temp.clear();
182         }
183     }
184     m_expandedMessageText.append(temp);
185     setText(false);
186 }
187
188 void UserInfo::setProfileImage(const QPixmap &image)
189 {
190     qDebug() << __PRETTY_FUNCTION__;
191
192     if(!image.isNull())
193         m_findButton->setButtonIcon(image);
194 }
195
196 void UserInfo::setTime(const QString &time)
197 {
198     qDebug() << __PRETTY_FUNCTION__;
199
200     m_updatedLabel->setText(time);
201 }
202
203 void UserInfo::setUserName(const QString &name)
204 {
205     qDebug() << __PRETTY_FUNCTION__;
206
207     m_userName = name;
208     setText(false);
209 }
210
211 void UserInfo::setText(bool expanded)
212 {
213     qDebug() << __PRETTY_FUNCTION__;
214
215     if (expanded) {
216         m_statusTextLabel->setText(m_expandedMessageText);
217     }
218     else {
219         m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
220         m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
221                                                LABEL_MAX_WIDTH));
222     }
223 }
224
225 void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
226 {
227     qDebug() << __PRETTY_FUNCTION__;
228
229     m_backupMessage = status;
230     m_backupFacebookPublishPolicity = publish;
231 }
232
233 void UserInfo::clearUpdateLocationDialogData()
234 {
235     qDebug() << __PRETTY_FUNCTION__;
236
237     m_backupMessage.clear();
238     m_backupFacebookPublishPolicity = false;
239 }
240
241 void UserInfo::findButtonClicked()
242 {
243     qDebug() << __PRETTY_FUNCTION__;
244
245     emit findUser(m_coordinates);
246 }
247
248 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
249 {
250     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
251
252     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
253         (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
254         if (m_expanded) {
255             setText(false);
256             m_expanded = false;
257         }
258         else {
259             setText(true);
260             m_expanded = true;
261         }
262     }
263 }
264
265 void UserInfo::mousePressEvent(QMouseEvent *event)
266 {
267     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
268
269     m_mousePosition = event->pos();
270 }
271
272 void UserInfo::messageUpdate()
273 {
274     qDebug() << __PRETTY_FUNCTION__;
275
276     delete m_updateLocation;
277     m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
278                                                 this);
279
280     connect(this, SIGNAL(reverseGeoReady(QString)),
281             m_updateLocation, SLOT(setAddress(QString)));
282
283     connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
284             this, SIGNAL(statusUpdate(QString,bool)));
285
286     connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
287             this, SLOT(backupUpdateLocationDialogData(QString,bool)));
288
289     connect(m_updateLocation, SIGNAL(finished(int)),
290             this, SLOT(updateLocationDialogFinished(int)));
291
292     m_updateLocation->show();
293
294     emit requestReverseGeo();
295 }
296
297 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
298 {
299     qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
300
301     QPainter painter(this);
302
303     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
304     QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
305                              this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
306     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
307                              BACKGROUND_BOTTOM_HEIGHT);
308
309     painter.drawPixmap(topRect, m_backgroundTopImage);
310     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
311     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
312 }
313
314 void UserInfo::restoreUnsendMessage()
315 {
316     qDebug() << __PRETTY_FUNCTION__;
317
318     QSettings settings(DIRECTORY_NAME, FILE_NAME);
319     m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
320     m_backupFacebookPublishPolicity =
321             settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
322 }
323
324 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
325 {
326     qDebug() << __PRETTY_FUNCTION__;
327
328     QFontMetrics labelMetrics = label->fontMetrics();
329     QString textParam = text;
330     int index = textParam.indexOf('\n');
331     int textWidth = fontMetrics().width(textParam);
332
333         if (index > 0) {
334             textParam.truncate(index);
335             textParam.append("...");
336         }
337             if (textWidth > 250)
338                 textParam.insert(LINE_LENGTH, QString("\n"));
339
340    return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
341 }
342
343 void UserInfo::updateLocationDialogFinished(int reason)
344 {
345     qDebug() << __PRETTY_FUNCTION__;
346
347     Q_UNUSED(reason);
348
349     if (m_updateLocation) {
350         disconnect(this, SIGNAL(reverseGeoReady(QString)),
351                 m_updateLocation, SLOT(setAddress(QString)));
352
353         disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
354                 this, SIGNAL(statusUpdate(QString,bool)));
355
356         disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
357                 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
358
359         disconnect(m_updateLocation, SIGNAL(finished(int)),
360                 this, SLOT(updateLocationDialogFinished(int)));
361
362         m_updateLocation->deleteLater();
363         m_updateLocation = 0;
364     }
365 }