Added QSizePolicy to the labels in user info
[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 "textmodifier.h"
36 #include "user/user.h"
37
38 #include "userinfo.h"
39
40 const int BACKGROUND_BOTTOM_HEIGHT = 15;
41 const int BACKGROUND_TOP_HEIGHT = 20;
42 const int BACKGROUND_WIDTH = 368;
43 const int ICON_HEIGHT = 24;
44 const int ICON_WIDTH = 24;
45 const int MARGIN = 5;
46 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - ICON_WIDTH - 5 * MARGIN;
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     QLabel *envelopeLabel = new QLabel();
65     envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
66     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
67     envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
68     QLabel *compassLabel = new QLabel();
69     compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
70     compassLabel->setContentsMargins(0, 0, MARGIN, 0);
71     compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
72     QLabel *clockLabel = new QLabel();
73     clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
74     clockLabel->setContentsMargins(0, 0, MARGIN, 0);
75     clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
76
77     m_avatar = new ImageButton();
78
79     m_nameLabel = new QLabel();
80
81     m_statusTextLabel = new QLabel();
82     m_statusTextLabel->setWordWrap(true);
83     m_statusTextLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
84     m_locationLabel = new QLabel();
85     m_locationLabel->setWordWrap(true);
86     m_locationLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
87     m_updatedLabel = new QLabel();
88     m_updatedLabel->setWordWrap(true);
89     m_updatedLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
90
91     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
92     infoLayout->addRow(compassLabel, m_locationLabel);
93     infoLayout->addRow(clockLabel, m_updatedLabel);
94
95     verticalLayout->addWidget(m_avatar, 0, Qt::AlignHCenter);
96     verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
97     verticalLayout->addLayout(infoLayout);
98
99     connect(m_avatar, SIGNAL(clicked()),
100             this, SLOT(findButtonClicked()));
101
102     setFixedWidth(BACKGROUND_WIDTH);
103
104     this->setFont(NOKIA_FONT_SMALL);
105     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
106     QPalette itemPalette = palette();
107     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
108     setPalette(itemPalette);
109     QPalette namePalette = m_nameLabel->palette();
110     namePalette.setColor(QPalette::Foreground, Qt::white);
111     m_nameLabel->setPalette(namePalette);
112
113     m_backgroundTopImage.load(":/res/images/list_item_top.png");
114     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
115     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
116
117     restoreUnsendMessage();
118 }
119
120 UserInfo::~UserInfo()
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     QSettings settings(DIRECTORY_NAME, FILE_NAME);
125
126     if (!m_backupMessage.isEmpty()) {
127         settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
128         settings.setValue(USER_UNSEND_MESSAGE_PUBLISH, m_backupFacebookPublishPolicity);
129     } else {
130         settings.remove(USER_UNSEND_MESSAGE);
131         settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
132     }
133 }
134
135 void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
136 {
137     qDebug() << __PRETTY_FUNCTION__;
138
139     m_backupMessage = status;
140     m_backupFacebookPublishPolicity = publish;
141 }
142
143 void UserInfo::clearUpdateLocationDialogData()
144 {
145     qDebug() << __PRETTY_FUNCTION__;
146
147     m_backupMessage.clear();
148     m_backupFacebookPublishPolicity = false;
149 }
150
151 void UserInfo::collapse()
152 {
153     qDebug() << __PRETTY_FUNCTION__;
154
155     setExpanded(false);
156 }
157
158 void UserInfo::findButtonClicked()
159 {
160     qDebug() << __PRETTY_FUNCTION__;
161
162     emit findUser(m_coordinates);
163 }
164
165 void UserInfo::messageUpdate()
166 {
167     qDebug() << __PRETTY_FUNCTION__;
168
169     delete m_updateLocation;
170     m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
171                                                 this);
172
173     connect(this, SIGNAL(reverseGeoReady(QString)),
174             m_updateLocation, SLOT(setAddress(QString)));
175
176     connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
177             this, SIGNAL(statusUpdate(QString, bool)));
178
179     connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
180             this, SLOT(backupUpdateLocationDialogData(QString, bool)));
181
182     connect(m_updateLocation, SIGNAL(finished(int)),
183             this, SLOT(updateLocationDialogFinished(int)));
184
185     m_updateLocation->show();
186
187     emit requestReverseGeo();
188 }
189
190 void UserInfo::mousePressEvent(QMouseEvent *event)
191 {
192     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
193
194     m_mousePosition = event->pos();
195 }
196
197 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
198 {
199     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
200
201     const int MOUSE_PRESS_AREA_HEIGHT = 20;
202     const int MOUSE_PRESS_AREA_WIDTH = 20;
203
204     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH)
205         && (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
206         if (m_expanded) {
207             setExpanded(false);
208             m_expanded = false;
209         }
210         else {
211             setExpanded(true);
212             m_expanded = true;
213         }
214     }
215 }
216
217 void UserInfo::paintEvent(QPaintEvent *event)
218 {
219     qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
220
221     QPainter painter(this);
222
223     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
224
225     QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_WIDTH,
226                              this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
227
228     QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_WIDTH,
229                              BACKGROUND_BOTTOM_HEIGHT);
230
231     painter.drawPixmap(topRect, m_backgroundTopImage);
232     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
233     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
234 }
235
236 void UserInfo::restoreUnsendMessage()
237 {
238     qDebug() << __PRETTY_FUNCTION__;
239
240     QSettings settings(DIRECTORY_NAME, FILE_NAME);
241     m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
242     m_backupFacebookPublishPolicity = settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
243 }
244
245 void UserInfo::setAddress(const QString &address)
246 {
247     qDebug() << __PRETTY_FUNCTION__;
248
249     m_locationLabel->setText(address);
250 }
251
252 void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
253 {
254     qDebug() << __PRETTY_FUNCTION__;
255
256     m_coordinates = coordinates;
257 }
258
259 void UserInfo::setMessageText(const QString &text)
260 {
261     qDebug() << __PRETTY_FUNCTION__;
262
263     m_messageText = TextModifier::splitLongWords(m_statusTextLabel->fontMetrics(), text,
264                                                  LABEL_MAX_WIDTH);
265
266     setExpanded(false);
267 }
268
269 void UserInfo::setProfileImage(const QPixmap &image)
270 {
271     qDebug() << __PRETTY_FUNCTION__;
272
273     if(!image.isNull())
274         m_avatar->setButtonIcon(image);
275 }
276
277 void UserInfo::setExpanded(bool expanded)
278 {
279     qDebug() << __PRETTY_FUNCTION__;
280
281     if (expanded) {
282         m_statusTextLabel->setText(m_messageText);
283     } else {
284         m_statusTextLabel->setText(TextModifier::shortenText(m_statusTextLabel->fontMetrics(),
285                                                              m_messageText, LABEL_MAX_WIDTH));
286     }
287 }
288
289 void UserInfo::setTime(const QString &time)
290 {
291     qDebug() << __PRETTY_FUNCTION__;
292
293     m_updatedLabel->setText(time);
294 }
295
296 void UserInfo::setUserName(const QString &name)
297 {
298     qDebug() << __PRETTY_FUNCTION__;
299
300     m_userName = name;
301
302     m_nameLabel->setText(TextModifier::shortenText(m_nameLabel->fontMetrics(), m_userName,
303                                                    LABEL_MAX_WIDTH));
304 }
305
306 void UserInfo::updateLocationDialogFinished(int reason)
307 {
308     qDebug() << __PRETTY_FUNCTION__;
309
310     Q_UNUSED(reason);
311
312     if (m_updateLocation) {
313         disconnect(this, SIGNAL(reverseGeoReady(QString)),
314                 m_updateLocation, SLOT(setAddress(QString)));
315
316         disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
317                 this, SIGNAL(statusUpdate(QString,bool)));
318
319         disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
320                 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
321
322         disconnect(m_updateLocation, SIGNAL(finished(int)),
323                 this, SLOT(updateLocationDialogFinished(int)));
324
325         m_updateLocation->deleteLater();
326         m_updateLocation = 0;
327     }
328 }