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