Updated some changes to userinfo
[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
9    Situare is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License
11    version 2 as published by the Free Software Foundation.
12
13    Situare is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Situare; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21    USA.
22 */
23
24 #include <QVBoxLayout>
25 #include <QPushButton>
26 #include <QPainter>
27 #include <QDebug>
28 #include <QPaintEvent>
29 #include <QLabel>
30 #include <QPixmap>
31 #include <QFormLayout>
32 #include <QSpacerItem>
33 #include <QStylePainter>
34 #include <math.h>
35
36 #include "imagebutton.h"
37 #include "avatarimage.h"
38 #include "../user/user.h"
39 #include "userinfo.h"
40
41 UserInfo::UserInfo(QWidget *parent)
42     : QWidget(parent)
43         , m_expanded(false)
44 {
45     QVBoxLayout *verticalLayout = new QVBoxLayout(this);
46     verticalLayout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
47     verticalLayout->setSpacing(0);
48     setLayout(verticalLayout);
49
50     QGridLayout *gridLayout = new QGridLayout(this);
51     gridLayout->setMargin(0);
52     gridLayout->setSpacing(0);
53
54     QHBoxLayout *topLayout = new QHBoxLayout(this);
55     topLayout->setMargin(0);
56     topLayout->setSpacing(0);
57
58     QFormLayout *infoLayout = new QFormLayout(this);
59     infoLayout->setMargin(0);
60     infoLayout->setSpacing(0);
61
62     QHBoxLayout *buttonLayout = new QHBoxLayout(this);
63     buttonLayout->setMargin(0);
64     buttonLayout->setSpacing(0);
65
66     QLabel *clockLabel = new QLabel();
67     clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
68     clockLabel->setContentsMargins(0, 0, MARGIN, 0);
69     clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
70     QLabel *envelopeLabel = new QLabel();
71     envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
72     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
73     envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
74     QLabel *compassLabel = new QLabel();
75     compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
76     compassLabel->setContentsMargins(0, 0, MARGIN, 0);
77     compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
78
79     m_imageLabel = new QLabel();
80     m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
81     m_imageLabel->setAlignment(Qt::AlignHCenter);
82
83     m_nameLabel = new QLabel();
84     m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
85     m_nameLabel->setAlignment(Qt::AlignHCenter);
86
87     m_updatedLabel = new QLabel();
88     m_updatedLabel->setWordWrap(true);
89     m_statusTextLabel = new QLabel();
90     m_statusTextLabel->setWordWrap(true);
91     m_locationLabel = new QLabel();
92     m_locationLabel->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
103     infoLayout->addRow(compassLabel, m_locationLabel);
104     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
105     infoLayout->addRow(clockLabel, m_updatedLabel);
106
107     topLayout->addWidget(m_nameLabel);
108
109     gridLayout->addWidget(m_imageLabel);
110
111     verticalLayout->addLayout(gridLayout, 0);
112     verticalLayout->addLayout(topLayout, 1);
113     verticalLayout->addLayout(infoLayout);
114     verticalLayout->addLayout(buttonLayout, 2 );
115
116     connect(updateStatusMessageButton,SIGNAL(clicked()),
117             this,SLOT(messageUpdate()));
118     connect(updateFriendsButton,SIGNAL(clicked()),
119             this, SIGNAL(refreshUserData()));
120
121     this->setFont(NOKIA_FONT_SMALL);
122     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
123     QPalette itemPalette = palette();
124     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
125     setPalette(itemPalette);
126     QPalette namePalette = m_nameLabel->palette();
127     namePalette.setColor(QPalette::Foreground, Qt::white);
128     m_nameLabel->setPalette(namePalette);
129
130     this->setMinimumHeight(300);
131
132     m_backgroundTopImage.load(":/res/images/list_item_top.png");
133     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
134     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
135 }
136
137 void UserInfo::setUserName(const QString &name)
138 {
139     qDebug() << __PRETTY_FUNCTION__;
140     qDebug() << name;
141
142 //    m_nameLabel->setText(name);
143
144 //     shortenTexts();
145 }
146
147 void UserInfo::setAvatar(const QPixmap &image)
148 {
149     m_avatar = image;
150    // m_imageLabel->setPixmap(AvatarImage::create(m_avatar.profileImage()));
151     //m_imageLabel->setPixmap(AvatarImage::create(m_avatar.profileImage()));
152     m_imageLabel->setPixmap(m_avatar);
153 }
154
155
156 void UserInfo::setMessageText(const QString &text)
157 {
158     if(m_messageText == text)
159       return;
160     m_messageText = text;
161 //    m_statusTextLabel->setText(m_messageText);
162 //    shortenTexts();
163 }
164
165 void UserInfo::setAddress(const QString &addr)
166 {
167     qDebug() << __PRETTY_FUNCTION__;
168
169 //    m_locationLabel->setText(addr);
170 //    shortenTexts();
171 }
172
173 void UserInfo::setTime(const QString &tim)
174 {
175     if(m_time == tim)
176       return;
177     m_time = tim;
178 //    m_updatedLabel->setText(m_time);
179     shortenTexts();
180     setText(false);
181 }
182
183 void UserInfo::shortenTexts()
184 {
185     qDebug() << __PRETTY_FUNCTION__;
186
187     QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
188     QFontMetrics otherLabelsMetrics = m_locationLabel->fontMetrics();
189
190     QString name = m_userName;
191     QString updated = m_time;
192     QString statusText = m_messageText;
193     QString location = m_address;
194
195     int nameIndex = name.indexOf('\n');
196     int updatedIndex = updated.indexOf('\n');
197     int statusTextIndex = statusText.indexOf('\n');
198     int locationIndex = location.indexOf('\n');
199
200     if (nameIndex > 0) {
201         name.truncate(nameIndex);
202         name.append("...");
203     }
204     if (updatedIndex > 0) {
205         updated.truncate(updatedIndex);
206         updated.append("...");
207     }
208     if (statusTextIndex > 0) {
209         statusText.truncate(statusTextIndex);
210         statusText.append("...");
211     }
212     if (locationIndex > 0) {
213         location.truncate(locationIndex);
214         location.append("...");
215     }
216
217     m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, LABEL_MAX_WIDTH + 30);
218     m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
219     m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
220                                                           LABEL_MAX_WIDTH);
221     m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
222 }
223
224 void UserInfo::setText(bool expanded)
225 {
226     qDebug() << __PRETTY_FUNCTION__;
227
228     if (expanded) {
229         m_nameLabel->setText(m_userName);
230         m_updatedLabel->setText(m_time);
231         m_statusTextLabel->setText(m_messageText);
232         m_locationLabel->setText(m_address);
233     }
234     else {
235         m_nameLabel->setText(m_shortenedName);
236         m_updatedLabel->setText(m_shortenedUpdated);
237         m_statusTextLabel->setText(m_shortenedStatusText);
238         m_locationLabel->setText(m_shortenedLocation);
239     }
240 }
241
242 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
243 {
244     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
245
246     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
247         (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
248 //        if (m_expanded) {
249 //            setText(false);
250 //            m_expanded = false;
251 //        }
252 //        else {
253 //            setText(true);
254 //            m_expanded = true;
255 //        }
256     }
257 }
258 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
259 {
260 //    Q_UNUSED(aPaintEvent);
261 //
262 //    QStyleOption option;
263 //    option.init(this);
264 //    QStylePainter painter(this);
265 //
266 //   // Temporary border
267 //    painter.setPen(Qt::red);
268 //    painter.drawRect(this->rect().x(), this->rect().y(), this->rect().width() - 1, this->rect().height() - 1);
269 //
270 //    style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
271 //
272     qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
273
274     QPainter painter(this);
275
276     QRect topRect = QRect(0, 0, ITEM_MIN_WIDTH, BACKGROUND_TOP_HEIGHT);
277     QRect middleRect = QRect(0, topRect.bottom(), ITEM_MIN_WIDTH,
278                              height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
279     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), ITEM_MIN_WIDTH,
280                              BACKGROUND_BOTTOM_HEIGHT);
281
282     painter.drawPixmap(topRect, m_backgroundTopImage);
283     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
284     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
285 }
286
287
288 void UserInfo::mousePressEvent(QMouseEvent *event)
289 {
290     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
291
292     m_mousePosition = event->pos();
293 }
294
295 void UserInfo::messageUpdate()
296 {
297     qDebug() << __PRETTY_FUNCTION__;
298
299     m_locationDialog = new UpdateLocationDialog(this);
300     emit requestReverseGeo();
301
302     connect(this, SIGNAL(reverseGeoReady(QString)),
303             m_locationDialog, SLOT(setAddress(QString)));
304     connect(m_locationDialog, SIGNAL(statusUpdate(QString, bool)),
305             this, SIGNAL(statusUpdate(QString, bool)));
306
307     m_locationDialog->show();
308 }
309
310 //void UserInfo::updateFriendsStatus()
311 //{
312 //    qDebug() << __PRETTY_FUNCTION__;
313 //    //emit launchUpdateFriendsStatus();
314 //}