Merge branch 'master' into 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
86     m_updatedLabel = new QLabel();
87     m_updatedLabel->setWordWrap(true);
88     m_statusTextLabel = new QLabel();
89     m_statusTextLabel->setWordWrap(true);
90     m_locationLabel = new QLabel();
91     m_locationLabel->setWordWrap(true);
92
93     ImageButton *updateFriendsButton = new ImageButton(this, ":/res/images/refresh.png",
94                                                              ":/res/images/refresh_s.png");
95     ImageButton *updateStatusMessageButton = new ImageButton(this, ":/res/images/send_position.png",
96                                                                    ":/res/images/send_position_s.png");
97
98     buttonLayout->addWidget(updateFriendsButton);
99     buttonLayout->addWidget(updateStatusMessageButton);
100
101
102     infoLayout->addRow(compassLabel, m_locationLabel);
103     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
104     infoLayout->addRow(clockLabel, m_updatedLabel);
105
106     topLayout->addWidget(m_nameLabel);
107
108     gridLayout->addWidget(m_imageLabel);
109
110     verticalLayout->addLayout(gridLayout, 0);
111     verticalLayout->addLayout(topLayout, 1);
112     verticalLayout->addLayout(infoLayout);
113     verticalLayout->addLayout(buttonLayout, 2 );
114
115     connect(updateStatusMessageButton,SIGNAL(clicked()),
116             this,SLOT(messageUpdate()));
117     connect(updateFriendsButton,SIGNAL(clicked()),
118             this,SLOT(updateFriendsStatus()));
119
120     setFont(NOKIA_FONT_SMALL);
121     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
122     QPalette itemPalette = palette();
123     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
124     setPalette(itemPalette);
125     QPalette namePalette = m_nameLabel->palette();
126     namePalette.setColor(QPalette::Foreground, Qt::white);
127     m_nameLabel->setPalette(namePalette);
128
129     m_nameLabel->setText("");
130
131     this->setMaximumHeight(300);
132
133     m_backgroundTopImage.load(":/res/images/list_item_top.png");
134     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
135     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
136 }
137
138
139
140 void UserInfo::setUserName(const QString &name)
141 {
142     qDebug() << __PRETTY_FUNCTION__;
143     qDebug() << name;
144
145     m_nameLabel->setText(name);
146
147 //     shortenTexts();
148 }
149
150 void UserInfo::setAvatar(const QPixmap &image)
151 {
152     m_avatar = image;
153    // m_imageLabel->setPixmap(AvatarImage::create(m_avatar.profileImage()));
154     //m_imageLabel->setPixmap(AvatarImage::create(m_avatar.profileImage()));
155     m_imageLabel->setPixmap(m_avatar);
156 }
157
158
159 void UserInfo::setMessageText(const QString &text)
160 {
161     if(m_messageText == text)
162       return;
163     m_messageText = text;
164     m_statusTextLabel->setText(m_messageText);
165 //    shortenTexts();
166 }
167
168 void UserInfo::setAddress(const QString &addr)
169 {
170     qDebug() << __PRETTY_FUNCTION__;
171
172     m_locationLabel->setText(addr);
173 //    shortenTexts();
174 }
175
176 void UserInfo::setTime(const QString &tim)
177 {
178     if(m_time == tim)
179       return;
180     m_time = tim;
181     m_updatedLabel->setText(m_time);
182     shortenTexts();
183 }
184
185 void UserInfo::shortenTexts()
186 {
187     qDebug() << __PRETTY_FUNCTION__;
188
189     QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
190     QFontMetrics otherLabelsMetrics = m_locationLabel->fontMetrics();
191
192     QString name = m_userName;
193     QString updated = m_time;
194     QString statusText = m_messageText;
195     QString location = m_address;
196
197     int nameIndex = name.indexOf('\n');
198     int updatedIndex = updated.indexOf('\n');
199     int statusTextIndex = statusText.indexOf('\n');
200     int locationIndex = location.indexOf('\n');
201
202     if (nameIndex > 0) {
203         name.truncate(nameIndex);
204         name.append("...");
205     }
206     if (updatedIndex > 0) {
207         updated.truncate(updatedIndex);
208         updated.append("...");
209     }
210     if (statusTextIndex > 0) {
211         statusText.truncate(statusTextIndex);
212         statusText.append("...");
213     }
214     if (locationIndex > 0) {
215         location.truncate(locationIndex);
216         location.append("...");
217     }
218
219     m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, LABEL_MAX_WIDTH + 30);
220     m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
221     m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
222                                                           LABEL_MAX_WIDTH);
223     m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
224 }
225
226 void UserInfo::setText(bool expanded)
227 {
228     qDebug() << __PRETTY_FUNCTION__;
229
230     if (expanded) {
231         m_nameLabel->setText(m_userName);
232         m_updatedLabel->setText(m_time);
233         m_statusTextLabel->setText(m_messageText);
234         m_locationLabel->setText(m_address);
235     }
236     else {
237         m_nameLabel->setText(m_shortenedName);
238         m_updatedLabel->setText(m_shortenedUpdated);
239         m_statusTextLabel->setText(m_shortenedStatusText);
240         m_locationLabel->setText(m_shortenedLocation);
241     }
242 }
243
244 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
245 {
246     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
247
248     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
249         (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
250         if (m_expanded) {
251             setText(false);
252             m_expanded = false;
253         }
254         else {
255             setText(true);
256             m_expanded = true;
257         }
258     }
259 }
260 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
261 {
262 //    Q_UNUSED(aPaintEvent);
263 //
264 //    QStyleOption option;
265 //    option.init(this);
266 //    QStylePainter painter(this);
267 //
268 //   // Temporary border
269 //    painter.setPen(Qt::red);
270 //    painter.drawRect(this->rect().x(), this->rect().y(), this->rect().width() - 1, this->rect().height() - 1);
271 //
272 //    style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
273 //
274     qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
275
276     QPainter painter(this);
277
278     QRect topRect = QRect(0, 0, ITEM_MIN_WIDTH, BACKGROUND_TOP_HEIGHT);
279     QRect middleRect = QRect(0, topRect.bottom(), ITEM_MIN_WIDTH,
280                              height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
281     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), ITEM_MIN_WIDTH,
282                              BACKGROUND_BOTTOM_HEIGHT);
283
284     painter.drawPixmap(topRect, m_backgroundTopImage);
285     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
286     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
287 }
288
289
290 void UserInfo::mousePressEvent(QMouseEvent *event)
291 {
292     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
293
294     m_mousePosition = event->pos();
295 }
296
297 void UserInfo::messageUpdate()
298 {
299     qDebug() << __PRETTY_FUNCTION__;
300     m_locationDialog = new UpdateLocationDialog(this);
301     m_locationDialog->show();
302 }
303
304 void UserInfo::updateFriendsStatus()
305 {
306     qDebug() << __PRETTY_FUNCTION__;
307     //emit launchUpdateFriendsStatus();
308 }