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